]> git.donarmstrong.com Git - neurodebian.git/commitdiff
NF: script to auto-compact a VDI image.
authorMichael Hanke <michael.hanke@gmail.com>
Tue, 5 Oct 2010 01:52:55 +0000 (21:52 -0400)
committerMichael Hanke <michael.hanke@gmail.com>
Tue, 5 Oct 2010 01:52:55 +0000 (21:52 -0400)
debian/control
tools/nd_compactvdi [new file with mode: 0755]

index 04fdf4621e92be5c11baf3867221cf3a86b3302e..c97419d9e643aa6d5b8de1529dd9041be6f707e3 100644 (file)
@@ -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 (executable)
index 0000000..434311f
--- /dev/null
@@ -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