]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_compactvdi
434311f2b043e1b83147bc33f2726205828eb245
[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 # package cache
30 find vbmnt/var/cache/apt/archives/ -name '*.deb' -delete
31 rm -f vbmnt/var/cache/apt/*.bin
32 # device files -- udev restores them
33 rm -rf vbmnt/dev/*
34 # tmp
35 rm -rf vbmnt/tmp/*
36 # log files
37 find vbmnt/var/log -type f -delete
38 # apt lists
39 find vbmnt/var/lib/apt -type f -name '*debian*' -o -type f -name '*list*' -delete
40 # history
41 rm -f vbmnt/root/.bash_history
42 rm -f vbmnt/home/*/.bash_history
43
44 # unmount filesystem
45 umount vbmnt
46 # zero out empty space
47 zerofree -v vbdev/Partition1
48 # close whole VDI
49 umount vbdev
50
51 # compact VDI
52 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
53 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact
54
55 # cleanup
56 rm -rf $wdir