Skip to content

Instantly share code, notes, and snippets.

@vmassuchetto
Created May 30, 2017 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmassuchetto/db90f9f1e7112e52c71be28c902a5c1c to your computer and use it in GitHub Desktop.
Save vmassuchetto/db90f9f1e7112e52c71be28c902a5c1c to your computer and use it in GitHub Desktop.
Script to clean a CentOS 5 virtual machine for pre-packaging on Vagrant
#!/bin/sh
usage() {
echo ""
echo "USAGE:"
echo ""
echo " Run as root on virtual environment:"
echo " $0"
echo ""
echo "RESULTS:"
echo ""
echo " - cleans transitional files"
echo " - removes white spaces on filesystem"
echo ""
exit 1
}
if [ `whoami` != "root" ]; then usage; fi
if [ $1 == "-h" ] ; then usage; fi
echo "Stopping services..."
/etc/init.d/httpd stop
/etc/init.d/mysqld stop
echo "Unmounting project /vagrant..."
umount /vagrant 2>&1
if grep -qs "/vagrant" /proc/mounts; then
echo "Error: project is mounted. Aborting."
exit
fi
echo "Removing yum cache..."
yum clean packages
yum clean headers
yum clean metadata
yum clean all
echo "Removing zero free space to aid VM compression..."
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
echo "Remove documentation files..."
find /usr/share/doc -type f | xargs rm -f
echo "Remove VirtualBox specific files..."
rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*
# Remove Linux headers
rm -rf /usr/src/linux-headers*
echo "Removing unused locales..."
# Edit for your needs, this keeps only en*
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,fr,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_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 -exec rm -rf {} \; > /dev/null 2>&1
echo "Removing bash history..."
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/vagrant/.bash_history
echo "Clean log files..."
find /var/log -type f | while read f; do echo -ne '' > $f; done;
echo "Whiteouting root filesystem..."
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/EMPTY bs=1024 count=$count
rm -f /EMPTY
echo "Whiteouting /boot filesystem..."
count=`df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/EMPTY bs=1024 count=$count
rm -f /EMPTY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment