Skip to content

Instantly share code, notes, and snippets.

@d78ui98
Last active July 19, 2018 18:43
Show Gist options
  • Save d78ui98/cb2cca74c52de667172fbaf77348b3c6 to your computer and use it in GitHub Desktop.
Save d78ui98/cb2cca74c52de667172fbaf77348b3c6 to your computer and use it in GitHub Desktop.
#!/bin/bash -x
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
# - https://github.com/chef/bento/blob/82752ec/packer/scripts/ubuntu/cleanup.sh
DU_BEFORE=$(df -h)
# delete all linux headers
dpkg --list | awk '{ print $2 }' | grep linux-headers | xargs apt-get -y purge
# removes old kernel packages
dpkg --list | awk '{ print $2 }' | grep 'linux-image-3.*-generic' | grep -v `uname -r` | xargs apt-get -y purge
# delete linux source
dpkg --list | awk '{ print $2 }' | grep linux-source | xargs apt-get -y purge
# delete development packages
dpkg --list | awk '{ print $2 }' | grep -- '-dev$' | xargs apt-get -y purge
# delete compilers and other development tools
apt-get -y purge cpp gcc g++
# delete obsolete networking, oddities
apt-get -y purge ppp pppconfig pppoeconf popularity-contest
# removing packages, libs, apt cache
dpkg -l | grep -- '-dev' | xargs apt-get purge -y
apt-get -y autoremove --purge
apt-get -y clean
apt-get -y autoclean
apt-get purge -y locate
# removing index list. Note apt update will be required
rm -rf /var/lib/apt/lists/*
# Whiteout root
count=$(df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}')
(( count-- ))
dd if=/dev/zero of=/tmp/whitespace bs=1024 count="${count}"
rm /tmp/whitespace
# Whiteout /boot
count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}')
(( count-- ))
dd if=/dev/zero of=/boot/whitespace bs=1024 count="${count}"
rm /boot/whitespace
# remove Bash history
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/vagrant/.bash_history
# removing logs
find /var/log -type f | while read f; do echo -ne '' > $f; done;
# compressing effectively. fixing fragmentation issue
sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
sync
echo '### Disk usage before cleanup ###'
echo "${DU_BEFORE}"
echo '### Disk usage after cleanup ###'
df -h
history -c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment