Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Last active January 8, 2016 02:29
Show Gist options
  • Save betweenbrain/dc372b03375023afc125 to your computer and use it in GitHub Desktop.
Save betweenbrain/dc372b03375023afc125 to your computer and use it in GitHub Desktop.
Shrink VirtualBox Shell Script
# From http://vmassuchetto.github.io/2013/08/14/reducing-a-vagrant-box-size/
# Added here as the published version transform text to uppercase and breaks commands
#!/bin/sh
# Install guest additions - https://andrewelkins.com/2014/03/08/install-virtualbox-guest-additions-command-line/
## set the version number for easier updating
vbVersion=5.0.4
## Get Guest Additions ISO
wget http://download.virtualbox.org/virtualbox/${vbVersion}/VBoxGuestAdditions_${vbVersion}.iso
## Install dependencies
sudo apt-get install -y gcc build-essential linux-headers-server
## Mount ISO
sudo mount -o loop VBoxGuestAdditions_${vbVersion}.iso /mnt
cd /mnt
## Install Guest Additions
sudo ./VBoxLinuxAdditions.run
# Update OS
sudo apt-get update -y
sudo apt-get upgrade -y
# Add Vagrant user to Sudoers
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
# Fetch Vagrant SSH keys
mkdir -p /home/vagrant/.ssh
sudo chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
sudo chmod 0600 /home/vagrant/.ssh/authorized_keys
sudo chown -R vagrant /home/vagrant/.ssh
# Install and configure openssh server
sudo apt-get install -y openssh-server
sudo sed -i "s|#AuthorizedKeysFile|AuthorizedKeysFile|g" /etc/ssh/sshd_config
# Restart SSH
sudo service ssh restart
# Passwordless login
echo "[SeatDefaults]
user-session=xubuntu
autologin-user=vagrant" > /etc/lightdm/lightdm.conf.d/50-myconfig.conf
# Remove unused dependencies
sudo apt-get autoremove -y
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
# - https://gist.github.com/adrienbrault/3775253
# Remove APT cache
sudo apt-get clean -y
sudo apt-get autoclean -y
# Zero free space to aid VM compression
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
# Remove APT files
find /var/lib/apt -type f | xargs rm -f
# Remove documentation files
find /var/lib/doc -type f | xargs rm -f
# Remove Virtualbox specific files
sudo rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*
# Remove Linux headers
sudo rm -rf /usr/src/linux-headers*
# Remove Unused locales (edit for your needs, this keeps only en* and pt_BR)
sudo find /usr/share/locale/{af,am,ar,as,ast,az,bal,be,bg,bn,bn_IN,br,bs,byn,ca,cr,cs,csb,cy,da,de,de_AT,dz,el,en_AU,en_CA,eo,es,et,et_EE,eu,fa,fi,fo,fr,fur,ga,gez,gl,gu,haw,he,hi,hr,hu,hy,id,is,it,ja,ka,kk,km,kn,ko,kok,ku,ky,lg,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,nb,ne,nl,nn,no,nso,oc,or,pa,pl,ps,qu,ro,ru,rw,si,sk,sl,so,sq,sr,sr*latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,tt,ur,urd,ve,vi,wa,wal,wo,xh,zh,zh_HK,zh_CN,zh_TW,zu} -type d -delete
# Remove bash history
unset HISTFILE
sudo rm -f /root/.bash_history
sudo rm -f /home/vagrant/.bash_history
# Cleanup log files
sudo 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}'`;
count=$((count -= 1))
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=$((count -= 1))
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace;
# Whiteout swap
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`
swapoff $swappart;
dd if=/dev/zero of=$swappart;
mkswap $swappart;
swapon $swappart;
# Clear bash history
history -c
@betweenbrain
Copy link
Author

Shrink VirtualBox disk

vboxmanage modifyhd --compact <disk file>.vdi

If not VDI format, copy disk as VDI

vboxmanage clonehd <disk file>.vmdk <disk file>.vdi --format VDI

Package box

vagrant package --base <machine name> --output <machine name>.box --vagrantfile <filename> --include <directory or filename>

  • output, vagrantfile and include are optional

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