]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_compactvdi
Towards a new VM setup
[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 # user data
41 rm -f vbmnt/root/.*history
42 # everything for the default user
43 find vbmnt/home/brain -mindepth 1 -delete
44
45 # unmount filesystem
46 umount vbmnt
47 # zero out empty space
48 zerofree -v vbdev/Partition1
49 # close whole VDI
50 umount vbdev
51
52 # compact VDI
53 # THIS NEEDS TO BE DONE ON A VDI THAT IS REGISTERED WITH VIRTUALBOX
54 sudo -u "$SUDO_USER" VBoxManage modifyhd "$vdifile" --compact
55
56 # cleanup
57 rm -rf $wdir