Skip to content

Instantly share code, notes, and snippets.

@pajtai
Last active September 13, 2017 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pajtai/d50f80a8af55da89f170 to your computer and use it in GitHub Desktop.
Save pajtai/d50f80a8af55da89f170 to your computer and use it in GitHub Desktop.
Cleaning up a Vagrant box before packaging

I tested the following script on a vagrant box.

The box was 850MB without the script, and 450 MB with the script.

The script was taken from chef/bento with just one modification.

screen shot 2015-02-08 at 3 27 20 pm

#!/bin/bash -eux
# source is: https://github.com/chef/bento/blob/master/packer/scripts/ubuntu/cleanup.sh
# only modification is the "try catch" (air quotes) for dd - since that snipped was taken from another context
# delete all linux headers
dpkg --list | awk '{ print $2 }' | grep linux-headers | xargs apt-get -y purge
# this removes specific linux kernels, such as
# linux-image-3.11.0-15-generic but
# * keeps the current kernel
# * does not touch the virtual packages, e.g.'linux-image-generic', etc.
#
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 X11 libraries
apt-get -y purge libx11-data xauth libxmuu1 libxcb1 libx11-6 libxext6
# delete obsolete networking
apt-get -y purge ppp pppconfig pppoeconf
# delete oddities
apt-get -y purge popularity-contest
apt-get -y autoremove
apt-get -y clean
rm -rf VBoxGuestAdditions_*.iso VBoxGuestAdditions_*.iso.?
rm -f /tmp/chef*deb
# with the e flag set, we have to "catch" the error dd is gonna trigger when it fills up the disk
{
dd if=/dev/zero of=/EMPTY bs=1M
} || {
rm -f /EMPTY
}
# Block until the empty file has been removed, otherwise, Packer
# will try to kill the box while the disk is still full and that's bad
sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment