From: Michael Hanke Date: Tue, 5 Oct 2010 01:52:55 +0000 (-0400) Subject: NF: script to auto-compact a VDI image. X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=6993fdedd1edfe57cafbeef3616113f7b6892253;p=neurodebian.git NF: script to auto-compact a VDI image. --- diff --git a/debian/control b/debian/control index 04fdf46..c97419d 100644 --- a/debian/control +++ b/debian/control @@ -22,6 +22,7 @@ Description: neuroscience research environment Package: neurodebian-dev Architecture: all Depends: ${misc:Depends}, devscripts, cowbuilder, python +Recommends: virtualbox-ose, virtualbox-ose-fuse, zerofree Suggests: Description: NeuroDebian development tools Pacifier diff --git a/tools/nd_compactvdi b/tools/nd_compactvdi new file mode 100755 index 0000000..434311f --- /dev/null +++ b/tools/nd_compactvdi @@ -0,0 +1,56 @@ +#!/bin/bash + +set -e +set -u + +# Alternative: +# shrink VDI image by writting to a new (unfragmented) image +# target VDI needs to have proper partition table and MBR +# simplest solution: clonezilla + +vdifile=$(readlink -f $1) + +if [ -z "$vdifile" ]; then + echo "You need to provide a VDI file." + exit 1 +fi + +wdir=$(mktemp -d -t compactvdi.XXXXXX) + +cd $wdir +# make mount points +mkdir -p vbdev vbmnt +# get access to disks inside the VDIs +vdfuse -f "$vdifile" vbdev +# mount partitition (for now only support one) +mount -o loop vbdev/Partition1 vbmnt + +# remove cruft +# package cache +find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete +rm -f vbmnt/var/cache/apt/*.bin +# device files -- udev restores them +rm -rf vbmnt/dev/* +# tmp +rm -rf vbmnt/tmp/* +# log files +find vbmnt/var/log -type f -delete +# apt lists +find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete +# history +rm -f vbmnt/root/.bash_history +rm -f vbmnt/home/*/.bash_history + +# unmount filesystem +umount vbmnt +# zero out empty space +zerofree -v vbdev/Partition1 +# close whole VDI +umount vbdev + +# compact VDI +# THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX +sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact + +# cleanup +rm -rf $wdir