forked from marvel-nccr/quantum-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact_hd.sh
executable file
·48 lines (36 loc) · 1.54 KB
/
compact_hd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
set -e
echo "### Parsing globalconfig.yml"
source other_stuff/yaml_parser.sh
eval $(parse_yaml globalconfig.yml)
vm_id="$vm_name $vm_version"
echo "### Getting size and uuid of vdisk"
vdisk_path_grep=`vboxmanage showvminfo --machinereadable "$vm_id" | grep "SATA Controller-0-0"`
[[ $vdisk_path_grep =~ ^.*=\"(.*)\"$ ]]
vdisk_path=${BASH_REMATCH[1]}
vdisk_uuid_grep=`vboxmanage showvminfo --machinereadable "$vm_id" | grep "SATA Controller-ImageUUID-0-0"`
[[ $vdisk_uuid_grep =~ ^.*=\"(.*)\"$ ]]
vdisk_uuid=${BASH_REMATCH[1]}
vm_vdisk_size=`du -sh "$vdisk_path" | awk '{print $1}' `
echo "### Initial vdisk size: $vm_vdisk_size"
echo "### Filling free space with zeros (this can take several minutes)"
ssh -F vagrant-ssh default "cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill"
echo "### Halting any running machines"
vagrant halt
echo "### Converting vdisk to vdi format"
tmp_vdisk_vdi=tmp.vdi
rm -f $tmp_vdisk_vdi
vboxmanage clonehd "$vdisk_path" $tmp_vdisk_vdi --format vdi
echo "### Compacting vdisk"
vboxmanage modifyhd $tmp_vdisk_vdi --compact
echo "### Converting vdisk back to vmdk"
tmp_vdisk_vmdk=tmp.vmdk
rm -f $tmp_vdisk_vmdk
vboxmanage clonehd $tmp_vdisk_vdi $tmp_vdisk_vmdk --format vmdk
vboxmanage closemedium $tmp_vdisk_vdi --delete
echo "### Re-connecting vdisk to VM"
cp $tmp_vdisk_vmdk "$vdisk_path"
vboxmanage closemedium $tmp_vdisk_vmdk --delete
vboxmanage internalcommands sethduuid "$vdisk_path" $vdisk_uuid
vm_vdisk_size=`du -sh "$vdisk_path" | awk '{print $1}' `
echo "### Final vdisk size: $vm_vdisk_size"