]> git.donarmstrong.com Git - neurodebian.git/blob - vm/tools/nd_compactvdi
Also for stats report which repo and which job number use our setup
[neurodebian.git] / vm / tools / nd_compactvdi
1 #!/bin/bash
2
3 set -e
4 set -u
5
6 # Alternative:
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
10
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
15 vdifile="$1"
16
17 if [ -z "$vdifile" ] || [ ! -e "$vdifile" ] || [ "${vdifile:0:1}" != "/" ] ; then
18   echo "You need to provide full pathname to an existing VDI file."
19   exit 1
20 fi
21
22 wdir=$(mktemp -d -t compactvdi.XXXXXX)
23
24 cd $wdir
25 # make mount points
26 mkdir -p vbdev vbmnt
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
31
32 # remove cruft
33 #xapian indices
34 find vbmnt/var/ -wholename '*xapian*.DB' -delete
35 # package cache
36 find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete
37 rm -f vbmnt/var/cache/apt/*.bin
38 # device files -- udev restores them
39 rm -rf vbmnt/dev/*
40 # tmp
41 rm -rf vbmnt/tmp/*
42 # log files
43 find vbmnt/var/log -type f -delete
44 # apt lists
45 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' | xargs -r rm
46 # user data
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*
50
51 # unmount filesystem
52 umount vbmnt
53 # zero out empty space
54 zerofree -v vbdev/Partition1
55 # close whole VDI
56 umount vbdev
57
58 # compact VDI
59 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
60 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact
61
62 # cleanup
63 echo "I: Cleaning up after VDI compact operation"
64 rm -rf $wdir