-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize_ova.sh
executable file
·117 lines (100 loc) · 3.23 KB
/
resize_ova.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
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
#!/bin/bash
ME=`basename $0`
##########################################################################
# Resize disk in an ova
#
# Background:
# ova files released usually have minimal disk requirements.
# This script uses VBoxManage to resize it internally, before
# a VM is created. After VM starts, re-partitioning may still
# be needed in order to make full use of the disk size.
#
# cloud images may not have a swap space, so partitioning may
# not be required.
##########################################################################
usage() {
cat <<EOF
$ME /path/to/ova [ new-disk-size ]
1st arg location to ova image
2nd arg optional disk size in MB (default is 51200, for 50G)
Note: making the size smaller than the size in the ova will be nothing but trouble
EOF
}
DEBUG=$$
if [ "$1" = "-d" ]; then
DEBUG=debug
shift
fi
if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" -o ! -s "$1" ]; then
usage
exit 1
fi
declare -i DISKSIZE=${2:-51200}
OVADIR="$( cd "$( dirname "$1" )" && pwd )"
OVA=$(basename $1)
pushd $OVADIR > /dev/null
if [ -s ${OVA}.bak ]; then
printf "\nWARNING! ${OVA}.bak already present, so leaving it\n"
rm -f $OVA
else
mv $OVA ${OVA}.bak
fi
TMPDIR=${OVADIR}/${OVA}${DEBUG}
VMDK=x
CLONE=cloned.vdi
cleanup() {
trap - 0 1 2 3 15 21 22
if [ ! -s "${OVADIR}/$OVA" ]; then
printf "\nno results, restoring original\n"
mv ${OVADIR}/${OVA}.bak ${OVADIR}/${OVA}
if [ -s ${TMPDIR}/${VMDK} ]; then
VBoxManage closemedium disk $VMDK --delete
fi
if [ -s ${TMPDIR}/${CLONE} ]; then
VBoxManage closemedium disk $CLONE --delete
fi
else
printf "\n${OVADIR}/${OVA} re-sized\n"
if [ "$DEBUG" != "debug" ]; then
rm -f ${OVADIR}/${OVA}.bak
else
printf "\nDEBUG: leaving original ${OVADIR}/${OVA}.bak intact\n"
fi
fi
if [ "$DEBUG" != "debug" ]; then
rm -rf $TMPDIR
else
printf "\nDEBUG: leaving working directory $TMPDIR intact\n"
fi
}
trap cleanup 0 1 2 3 15 21 22
rm -rf $TMPDIR
mkdir $TMPDIR
pushd $TMPDIR > /dev/null
printf "\nExtracting ova content\n"
tar xf $OVADIR/${OVA}.bak 2>/dev/null || { printf "\nova file does not appear to be valid\n"; exit 1; }
VMDK=$(ls *vmdk 2>/dev/null) || { printf "\nova file does not appear to be valid\n"; exit 1; }
printf "\nConverting $VMDK to vdi format\n"
ls -hl $VMDK
VBoxManage clonemedium $VMDK $CLONE --format vdi || exit
printf "\nRe-size vdi to $DISKSIZE\n"
VBoxManage modifymedium $CLONE --compact --resize $DISKSIZE || exit
ls -hl $CLONE
VBoxManage closemedium disk $VMDK --delete || exit
printf "\nConvert to vmdk format needed for ova\n"
VBoxManage clonemedium $CLONE $VMDK --format vmdk || exit
ls -hl $VMDK
VBoxManage closemedium disk $CLONE --delete || exit
printf "\nRe-create ova\n"
set $(sha256sum $VMDK) x x
SUM=$1
sed -i "/vmdk/s/= .*/= $SUM/" *mf
tar cf $OVADIR/${OVA} * || exit
VBoxManage closemedium disk $VMDK --delete || exit
set $(sha256sum $OVADIR/${OVA}) x x
cat <<EOF
##########################################################################
source_path=$OVADIR/${OVA}
checksum=$1
##########################################################################
EOF