Skip to content

Instantly share code, notes, and snippets.

@flyinprogrammer
Created December 22, 2014 04:15
Show Gist options
  • Save flyinprogrammer/f70d11f6392f1d137e0c to your computer and use it in GitHub Desktop.
Save flyinprogrammer/f70d11f6392f1d137e0c to your computer and use it in GitHub Desktop.
Ubuntu vagrant cleanup script
#!/bin/bash
# Unmount project
umount /vagrant
# Tell installer to keep en_US
echo en_US > /etc/locale.gen
# Install localepurge - NO dpkg
apt-get install -y localepurge
localepurge
apt-get remove -y pollinate overlayroot fonts-ubuntu-font-family-console cloud-init python-apport landscape-client juju chef open-vm-tools localepurge
# Remove APT cache
apt-get clean -y
apt-get autoclean -y
# Remove APT files
find /var/lib/apt -type f | xargs rm -f
# Clear cache
find /var/cache -type f -exec rm -rf {} \;
# Clear docs
# Leave virtualbox* - otherwise /vagrant won’t mount
shopt -s extglob
rm -rf /usr/share/doc/!(virtual*)
rm -rf /usr/share/doc-base/*
# Remove Virtualbox specific files
rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*
# Remove Linux headers
rm -rf /usr/src/linux-headers*
# Remove bash history
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/vagrant/.bash_history
# Cleanup log files
find /var/log -type f | while read f; do echo -ne '' > $f; done;
# Zero free space to aid VM compression
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
dd if=/dev/zero of=/run/EMPTY bs=1M
rm -f /run/EMPTY
dd if=/dev/zero of=/run/lock/EMPTY bs=1M
rm -f /run/lock/EMPTY
dd if=/dev/zero of=/run/shm/EMPTY bs=1M
rm -f /run/shm/EMPTY
dd if=/dev/zero of=/run/user/EMPTY bs=1M
rm -f /run/user/EMPTY
dd if=/dev/zero of=/dev/EMPTY bs=1M
rm -f /dev/EMPTY
dd if=/dev/zero of=/sys/fs/cgroup/EMPTY bs=1M
rm -f /sys/fs/cgroup/EMPTY
echo finished running clean script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment