From 6993fdedd1edfe57cafbeef3616113f7b6892253 Mon Sep 17 00:00:00 2001 From: Michael Hanke Date: Mon, 4 Oct 2010 21:52:55 -0400 Subject: [PATCH] NF: script to auto-compact a VDI image. --- debian/control | 1 + tools/nd_compactvdi | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 tools/nd_compactvdi 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 -- 2.39.5