Skip to content

Instantly share code, notes, and snippets.

@jerome-breton
Forked from vmassuchetto/purge.sh
Last active September 20, 2019 16:36
Show Gist options
  • Select an option

  • Save jerome-breton/d041dd70cc61fee6e70a1da0db9fad39 to your computer and use it in GitHub Desktop.

Select an option

Save jerome-breton/d041dd70cc61fee6e70a1da0db9fad39 to your computer and use it in GitHub Desktop.
Script to clean a Virtualbox VM before packaging it.
#!/bin/bash
# Run with:
# curl https://gist.githubusercontent.com/jerome-breton/d041dd70cc61fee6e70a1da0db9fad39/raw/purge.sh | sudo bash
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
# - https://gist.github.com/adrienbrault/3775253
# - https://gist.github.com/vmassuchetto/6225959
# - https://superuser.com/a/1116213/449019
function lbl { echo ""; echo -e "\e[1m\e[37;1;42m\t$1\t\e[0m"; }
# Check if run with sudo
if [ "$EUID" -ne 0 ]
then lbl "Please run as root"
exit
fi
lbl "Shutdown services"
service apache2 stop
service nginx stop
service mysql stop
service mysqld stop
service elasticsearch stop
service redis stop
service redis-server stop
service php-fpm stop
service php7.0-fpm stop
service maildev stop
lbl "Remove unused dependancies and APT cache"
apt-get autoremove --purge
apt-get clean -y
apt-get autoclean -y
lbl "Remove APT files"
find /var/lib/apt -type f | xargs rm -f
lbl "Remove documentation files"
find /var/lib/doc -type f | xargs rm -f
lbl "Remove Virtualbox specific files"
rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*
lbl "Remove Linux headers"
rm -rf /usr/src/linux-headers*
lbl "Remove Unused locales" # edit for your needs, this keeps only en* and fr_FR
find /usr/share/locale/{af,am,ar,as,ast,az,bal,be,bg,bn,bn_IN,br,bs,byn,ca,cr,cs,csb,cy,da,de,de_AT,dz,el,en_AU,en_CA,eo,es,et,et_EE,eu,fa,fi,fo,fur,ga,gez,gl,gu,haw,he,hi,hr,hu,hy,id,is,it,ja,ka,kk,km,kn,ko,kok,ku,ky,lg,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,nb,ne,nl,nn,no,nso,oc,or,pa,pl,ps,pt,pt_BR,qu,ro,ru,rw,si,sk,sl,so,sq,sr,sr*latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,tt,ur,urd,ve,vi,wa,wal,wo,xh,zh,zh_HK,zh_CN,zh_TW,zu} -type d -delete
lbl "Remove bash history"
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/*/.bash_history
lbl "Cleanup log files"
find /var/log -type f | while read f; do echo -ne '' > $f; done;
find /var/www -type f -iname \*.log | while read f; do echo -ne '' > $f; done;
lbl "Clear other caches"
npm cache clean --force
lbl "Defrag"
e4defrag /
lbl "Whiteout root"
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count;
rm /tmp/whitespace;
lbl "Whiteout /boot"
count=`df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace;
lbl "Whiteout swap" # changes UUID, check if your /etc/fstab is using it
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`
swapoff $swappart;
dd if=/dev/zero of=$swappart;
mkswap $swappart;
swapon $swappart;
lbl "Shut down"
halt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment