]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_compactvdi
initial version for blends-inject
[neurodebian.git] / 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 vdifile=$(readlink -f $1)
12
13 if [ -z "$vdifile" ]; then
14   echo "You need to provide a VDI file."
15   exit 1
16 fi
17
18 wdir=$(mktemp -d -t compactvdi.XXXXXX)
19
20 cd $wdir
21 # make mount points
22 mkdir -p vbdev vbmnt
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
27
28 # remove cruft
29 #xapian indices
30 find vbmnt/var/ -wholename '*xapian*.DB' -delete
31 # package cache
32 find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete
33 rm -f vbmnt/var/cache/apt/*.bin
34 # device files -- udev restores them
35 rm -rf vbmnt/dev/*
36 # tmp
37 rm -rf vbmnt/tmp/*
38 # log files
39 find vbmnt/var/log -type f -delete
40 # apt lists
41 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete
42 # user data
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*
46
47 # unmount filesystem
48 umount vbmnt
49 # zero out empty space
50 zerofree -v vbdev/Partition1
51 # close whole VDI
52 umount vbdev
53
54 # compact VDI
55 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
56 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact
57
58 # cleanup
59 rm -rf $wdir