7 # shrink VDI image by writting to a new (unfragmented) image
8 # target VDI needs to have proper partition table and MBR
9 # simplest solution: clonezilla
11 # No deferencing of symbolic links should be done
12 # since otherwise might not be found by VirtualBox
13 #vdifile=$(readlink -f $1)
14 # Just asssume that we are providing full pathname
17 if [ -z "$vdifile" ] || [ ! -e "$vdifile" ] || [ "${vdifile:0:1}" != "/" ] ; then
18 echo "You need to provide full pathname to an existing VDI file."
22 wdir=$(mktemp -d -t compactvdi.XXXXXX)
27 # get access to disks inside the VDIs
28 vdfuse -f "$vdifile" vbdev
29 # mount partitition (for now only support one)
30 mount -o loop vbdev/Partition1 vbmnt
34 find vbmnt/var/ -wholename '*xapian*.DB' -delete
36 find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete
37 rm -f vbmnt/var/cache/apt/*.bin
38 # device files -- udev restores them
43 find vbmnt/var/log -type f -delete
45 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete
47 # cannot clean all root stuff, because it also contains useful thing (e.g. git setup)
48 rm -f vbmnt/root/.*history
49 rm -f vbmnt/home/brain/.*history vbmnt/home/brain/nd*
53 # zero out empty space
54 zerofree -v vbdev/Partition1
59 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
60 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact
63 echo "I: Cleaning up after VDI compact operation"