Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to reduce VM size before packaging for vagrant
#!/bin/sh
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
aptitude -y purge ri
aptitude -y purge installation-report landscape-common wireless-tools wpasupplicant ubuntu-serverguide
aptitude -y purge python-dbus libnl1 python-smartpm python-twisted-core libiw30
aptitude -y purge python-twisted-bin libdbus-glib-1-2 python-pexpect python-pycurl python-serial python-gobject python-pam python-openssl libffi5
apt-get purge -y linux-image-3.0.0-12-generic-pae
# Remove APT cache
apt-get clean -y
apt-get autoclean -y
# Zero free space to aid VM compression
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
# 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;
# Whiteout root
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`;
let 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}'`;
let count--
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace;
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`
swapoff $swappart;
dd if=/dev/zero of=$swappart;
mkswap $swappart;
swapon $swappart;
@shuhaowu

This comment has been minimized.

shuhaowu commented Mar 17, 2013

I'm on debian... let: not found?

Also it seems like that this removes ruby..?

@lfbittencourt

This comment has been minimized.

lfbittencourt commented Jun 2, 2013

What about replace let count-- by : $((count -= 1))?

@jonathanglima

This comment has been minimized.

jonathanglima commented Dec 22, 2013

nah... just replace #!/bin/sh to #!/bin/bash

works for me =]

thx a lot

@evgeny-goldin

This comment has been minimized.

evgeny-goldin commented May 8, 2014

Had to change the last block to

swappart=$(cat /proc/swaps | grep -v Filename | tail -n1 | awk -F ' ' '{print $1}')
if [ "$swappart" != "" ]; then
  swapoff $swappart;
  dd if=/dev/zero of=$swappart;
  mkswap $swappart;
  swapon $swappart;
fi

Otherwise a "Filename" file is created occupying the entire disk if the script is run twice

@mkno

This comment has been minimized.

mkno commented Jun 4, 2016

well this did nothing for me. I actually removed a bunch of large files but after running this script the resulting box size was GREATER! how in the hell does deleting file end up increasing the box size????

update: doing @evgeny-goldin's suggestion seems to have worked.

1.8gb went to 750mb so I'm not complaining.

@erickrmz

This comment has been minimized.

erickrmz commented Mar 20, 2017

I went from having 8GB image to 2.6GB, thanks!

@utdrmac

This comment has been minimized.

utdrmac commented Aug 7, 2017

How come the dd for swap has no bs nor count?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment