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/cache/apt/archives/ -name '*.deb' -delete
31 rm -f vbmnt/var/cache/apt/*.bin
32 # device files -- udev restores them
37 find vbmnt/var/log -type f -delete
39 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete
41 rm -f vbmnt/root/.*history
42 # everything for the default user
43 find vbmnt/home/brain -mindepth 1 -delete
47 # zero out empty space
48 zerofree -v vbdev/Partition1
53 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
54 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact