Reducing vagrant box size

Here are some tricks I use to make my vagrant boxes as small as possible:

Tips:

Booting in single user mode:

I boot in single user mode since it will prevent running services that could output logs. I do this because I zero out all my logs before packaging the box.

Updating:

After updating any package, run yum clean (or the apt equivalent).

When booted in single user mode, don’t forget to start-up your network before updating.

When updating kernels, install the kernel packages, reboot and remove the old kernel packages that are no longer in use. Remember to re-install the VirtualBox add-ons too after a kernel update.

Cleanup:

After doing whatever you need to do with the box, I do some rather nasty stuff to make sure the box uses as little as possible place. If you are using a RAW hard-disks, these might be a bad idea (stuff gets BIG).

  • Zero out all remaining unused disk space
  • Zero out the swap
  • Clear out all log files (I just make them empty, I do NOT delete them)
Script:

(You can find this script – or an older version in /root/tools/cleanup_diskspace.sh on my newer vagrant boxes.)

cat - << EOWARNING
WARNING: This script will fil up your left over disk space.
 
DO NOT RUN THIS WHEN YOUR VIRTUAL HD IS RAW!!!!!!
 
You should NOT do this on a running system.
This is purely for making vagrant boxes damn small.
 
Press Ctrl+C within the next 10 seconds if you want to abort!!
 
EOWARNING
sleep 10;
 
echo 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/vagrant/.bash_history ] && rm /home/vagrant/.bash_history
 
echo 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done;
 
echo '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;
 
echo '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;

Furthermore – about this script – USE IT AT YOUR OWN RISK

7 comments on “Reducing vagrant box size

    • I’ve updated the script with the latest version you can find on my base boxes.
      I also added a small tweak to not completely fill up the partitions, but leave 1 mb as it is since I had some out of diskspace issues due to the kernel logging a event once in a while.

  1. Another typo. “[-f /home/vagrant/…” is missing a space between the bracket and the -f

    Excellent help, though. This makes a huge difference in the final .box size.

    • Thanks, nice catch. Must have gotten removed after struggling with wordpress editor.

      Also, on a side note: You can also run `yum clean all` or `apt-get clean` somewhere in the beginning of the script. I did not add it here since its distro specific.

  2. Does dd if=/dev/zero of=/whitespace bs=1024 really have an impact? (note that I removed /tmp because you never know whether /tmp is no tmpfs)

    I’m asking because when issuing “dd if=/dev/zero of=/whitespace bs=1024” the virtual maching backing .vdi’s size didn’t change at all while “du -h” reports I just filled up the root partition.

    Any idea?

    • I use dd to fill up all left over disk space with zeros. This overwrites whatever garbage is left on the disk. This helps when compressing the box disk image.
      You should remove the zeros-file after filling up the disk completely of course.

  3. You might also want to discard unused files in boot before zero-ing it.

    sudo apt-get purge $(dpkg -l linux-{image,headers}-“[0-9]*” | awk ‘/ii/{print $2}’ | grep -ve “$(uname -r | sed -r ‘s/-[a-z]+//’)”)

Leave a Reply to Chris Cancel reply

Your email address will not be published. Required fields are marked *