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 vdifile=$(readlink -f $1)
13 if [ -z "$vdifile" ]; then
14 echo "You need to provide a VDI file."
18 wdir=$(mktemp -d -t compactvdi.XXXXXX)
23 # get access to disks inside the VDIs
24 vdfuse -f "$vdifile" vbdev
25 # mount partitition (for now only support one)
26 mount -o loop vbdev/Partition1 vbmnt
30 find vbmnt/var/ -wholename '*xapian*.DB' -delete
32 find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete
33 rm -f vbmnt/var/cache/apt/*.bin
34 # device files -- udev restores them
39 find vbmnt/var/log -type f -delete
41 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete
43 # cannot clean all root stuff, because it also contains useful thing (e.g. git setup)
44 rm -f vbmnt/root/.*history
45 rm -f vbmnt/home/brain/.*history vbmnt/home/brain/nd*
49 # zero out empty space
50 zerofree -v vbdev/Partition1
55 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
56 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact