-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrink-vm
executable file
·176 lines (149 loc) · 4.55 KB
/
shrink-vm
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
function pause(){
echo "--"
read -p "Press any key to continue..."
}
Usage="--
-- Usage: shrink-vm [-l] [-h]
-- -l: create a copy of the vm disk in local directory
-- -d: delete the backup of the original disk at the end
-- -h: print help
--"
while getopts ":hld" opt; do
case $opt in
l)
LOCALDISKCOPY=yes
;;
d)
DELETEBACKUP=yes
;;
h)
#echo "-h was triggered!" >&2
echo "$Usage"
exit 0
;;
\?)
echo "-- ERROR: invalid option -$OPTARG" >&2
;;
:)
echo "-- ERROR: option -$OPTARG requires an argument" >&2
echo "$Usage"
exit 1
;;
esac
done
LOCALDIR=$( pwd )
#echo "LOCALDIR=$LOCALDIR"
VMFOLDER=$( VBoxManage list systemproperties | grep '^Default machine folder' | awk -F ':' '{ print $2 }' | xargs )
#echo "VMFOLDER='$VMFOLDER'"
VMNAME=$( vboxmanage showvminfo `cat .vagrant/machines/default/virtualbox/id` --machinereadable | grep "^name=" | awk -F '=' '{ print $2 }' | xargs )
#echo "VMNAME=$VMNAME"
VMDIR="$VMFOLDER/$VMNAME"
#echo "VMDIR=$VMDIR"
cd "$VMDIR"
VMDISK=$( ls *.vmdk )
#echo "VMDISK=$VMDISK"
VMDISKNAME="${VMDISK%.*}"
#echo "VMDISKNAME=$VMDISKNAME"
VMDISKUUID=$( vboxmanage showhdinfo "$VMDISK" | grep "^UUID:" | awk '{print $2}' )
#echo "VMDISKUUID=$VMDISKUUID"
echo "--"
echo "-- Compacting disk:"
echo "-- VM: $VMNAME"
echo "-- Disk UUID: $VMDISKUUID"
echo "-- Disk file: '$VMDIR/$VMDISK'"
if [ -e ${VMDISKNAME}.vdi ]; then
echo "--"
echo "-- ERROR: cannot convert disk to VDI format."
echo "-- File ${VMDIR}${VMDISKNAME}.vdi already exists"
echo "-- Delete it manually using the following command:"
echo "-- vboxmanage closemedium disk \"${VMDIR}/${VMDISKNAME}.vdi\" --delete"
echo "-- and execute the script again."
echo "--"
exit 1
fi
echo "--"
echo "-- Making a backup of vmdk disk..."
cp -v "${VMDIR}/${VMDISKNAME}.vmdk" "${VMDIR}/${VMDISKNAME}.vmdk.bak"
#pause
echo "--"
echo "-- Clone disk to vdi format..."
echo "-- vboxmanage clonehd $VMDISK ${VMDISKNAME}.vdi --format vdi"
vboxmanage clonehd $VMDISK ${VMDISKNAME}.vdi --format vdi
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot clone disk to vdi format."
echo "--"
exit 1
fi
#pause
echo "--"
echo "-- Compacting vdi disk..."
echo "-- vboxmanage modifyhd ${VMDISKNAME}.vdi --compact"
vboxmanage modifyhd ${VMDISKNAME}.vdi --compact
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot compact vdi disk."
echo "--"
exit 1
fi
#pause
echo "--"
echo "-- Deleting the original disk and converting the vdi disk to vmdk..."
#rm -v "${VMDIR}/${VMDISKNAME}.vmdk"
echo "-- vboxmanage clonehd ${VMDISKNAME}.vdi ${VMDISKNAME}-new.vmdk --format vmdk"
vboxmanage clonehd ${VMDISKNAME}.vdi ${VMDISKNAME}-new.vmdk --format vmdk
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot clone disk to vmdk format."
echo "--"
exit 1
fi
echo "-- vboxmanage closemedium disk \"${VMDIR}/${VMDISKNAME}-new.vmdk\""
vboxmanage closemedium disk "${VMDIR}/${VMDISKNAME}-new.vmdk"
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot close cloned disk."
echo "--"
exit 1
fi
#pause
echo "--"
echo "-- Setting the original UUID in the new disk..."
echo "-- vboxmanage internalcommands sethduuid \"${VMDIR}/${VMDISKNAME}-new.vmdk\" $VMDISKUUID"
vboxmanage internalcommands sethduuid "${VMDIR}/${VMDISKNAME}-new.vmdk" $VMDISKUUID
if [ $? -ne 0 ]; then
echo "--"
echo "---- ERROR: Cannot set the original disk UUID to the cloned disk."
echo "--"
exit 1
fi
mv -v ${VMDISKNAME}-new.vmdk ${VMDISKNAME}.vmdk
#pause
if [ "$LOCALDISKCOPY" == "yes" ]; then
echo "--"
echo "-- Copying the vdi disk to localdir and deleting it from Virtualbox..."
mv -v "${VMDIR}/${VMDISKNAME}.vdi" $LOCALDIR
echo "-- vboxmanage closemedium disk \"${VMDIR}/${VMDISKNAME}.vdi\" --delete"
vboxmanage closemedium disk "${VMDIR}/${VMDISKNAME}.vdi" --delete
echo "--"
echo "-- A local copy of the VM disk is available in: "
ls -lh $LOCALDIR/${VMDISKNAME}.vdi
echo "-- Delete it if not needed."
else
echo "--"
echo "-- Deleting vdi disk copy..."
rm "${VMDIR}/${VMDISKNAME}.vdi"
fi
if [ "$DELETEBACKUP" == "yes" ]; then
echo "--"
echo "-- Deleting the disk backup made at the beginning of the process..."
rm -v "${VMDIR}/${VMDISKNAME}.vmdk.bak"
else
echo "--"
echo "-- The backup of the original disk is available at:"
echo "-- ${VMDIR}/${VMDISKNAME}.vmdk.bak"
echo "-- Delete it if not needed."
fi
echo "--"
echo "-- ...done"