forked from piggyvenus/cloud_suite_fastrax_public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv2v.sh
executable file
·246 lines (216 loc) · 5.8 KB
/
v2v.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
#
# v2v.sh
#
# USAGE: v2v.sh [-h] -i inputfile -o outputdir -l logfile -t tempdir [-L sysprep_log]
#
# DESCRIPTION:
# virtual-to-virtual script for American Express to migrate an OVA file from disk,
# (which is generated by VMWare's 'ovftool') to a raw image suitable for OpenStack.
#
# LIMITATIONS:
# Below is an inclusive list of guest Operating Systems which are supported:
# - Windows 2008 R2
# - RHEL6
# - RHEL7
#
# REQUIREMENTS: rpm virt-v2v v1.28+
# Runs under RHEL7
# Available from http://people.redhat.com/~rjones/libguestfs-RHEL-7.1-preview/
#
#----------------------------------------------------------------------------------------------------
ulimit -c unlimited
v2v_help() {
cat << EOF
Usage: ${0##*/} [-h] -i inputfile -o outputdir -l logfile -t tempdir -n #nics [-d] [-I IPADDR]
Convert an OVA file to a raw image file
-h show this help screen
-i full path to input OVA file (mandatory)
-o existing directory to which output file will be written (mandatory)
-l full path to logging file that will be made (mandatory)
-t full path to temporary directory (used to untar the ova) (mandatory)
-L full path to logging file for virt-sysprep
-n number of nics that will be converted to dhcp. defaults to 1 to change eth0. 0 will not replace eth0, using existing eth0.
-I IP Address for vm
EOF
}
stop_time() {
echo "Stop time: `date`"
}
V2V_INPUT=""
V2V_OUTPUT=""
V2V_TMPDIR=""
V2V_LOG=""
# This will break virt-v2v
V2V_DEBUG_ARGS="-x -v"
SDA_PATH=""
SYSPREP_LOG=""
NUM_NICS=1
if [[ $1 == "" ]]; then
echo -e "No options specified\n"
v2v_help >&2
exit 1
fi
##
##
##
while getopts "hi:l:o:t:d:L:n:I:" opt; do
case "$opt" in
h)
v2v_help
exit 0
;;
i)
V2V_INPUT=$OPTARG
;;
l)
V2V_LOG=$OPTARG
;;
o)
V2V_OUTPUT=$OPTARG
;;
t)
V2V_TMPDIR=$OPTARG
;;
d)
V2V_DEBUG_ARGS="-x -v"
;;
L)
echo "got $OPTARG"
SYSPREP_LOG=$OPTARG
;;
n)
NUM_NICS=$OPTARG
;;
I)
IPADDR=$OPTARG
;;
\?)
echo -e "Invalid option specified\n"
v2v_help >&2
stop_time
exit 1
;;
':')
echo "Option -$OPTARG requires an argument" >&2
esac
done
##
## Input File (-i) is mandatory; verify it was specified with -i and that it exists.
##
if [ x$V2V_INPUT == "x" ]; then
echo "ERROR: Input ova file (-i) is mandatory but was not specified. "
v2v_help
stop_time
exit 1
fi
if [ ! -f ${V2V_INPUT} ]; then
echo "Input file ${V2V_INPUT} does not exist. Exiting...."
stop_time
exit 1
fi
##
## Output Directory (-o) is mandatory; verify it was specified with -o and that it exists.
##
if [ x$V2V_OUTPUT == "x" ]; then
echo "ERROR: Output directory (-o) is mandatory but was not specified. "
v2v_help
stop_time
exit 1
fi
if [ ! -d ${V2V_OUTPUT} ]; then
echo "Ouput directory ${V2V_OUTPUT} does not exist. Creating..."
mkdir -p ${V2V_OUTPUT}
#stop_time
#exit 1
fi
##
## Output Directory must not be the same as the input directory.
##
if [[ $(dirname ${V2V_INPUT}) == ${V2V_OUTPUT} ]]; then
echo "ERROR: Attempting to overwrite input file"
stop_time
exit 1
fi
##
## Log file (-l) is mandatory; verify it was specified by -l argument and that its directory exists.
##
if [ x$V2V_LOG == "x" ]; then
echo "ERROR: Log file is mandatory but was not specified. "
v2v_help
stop_time
exit 1
fi
if [ ! -d $(dirname ${V2V_LOG}) ]; then
echo "Directory for Log file ${V2V_LOG} does not exist. Exiting..."
stop_time
exit 1
fi
##
## Temp dir (-t) is mandatory; verify it was specified by -t argument and that it exists.
##
if [ x$V2V_TMPDIR == "x" ]; then
echo "ERROR: TMP directory is mandatory but was not specified. "
v2v_help
stop_time
exit 1
fi
if [ ! -d ${V2V_TMPDIR} ]; then
echo "Temp directory ${V2V_TMPDIR} does not exist. Exiting..."
stop_time
exit 1
fi
export TMPDIR=${V2V_TMPDIR}
export LIBGUESTFS_BACKEND=direct
##
## RPM virtio-win is required to convert Windows images (but not RHEL images). I cannot tell if the
## image being converted here is a Windows or RHEL image, so I'll complain either way.
## Once it's installed, we won't need to deal with the decision again.
##
rpm -q virtio-win &>/dev/null
if [ "$?" -ne "0" ]; then
echo "Package virtio-win is not installed. Please install the virtio-win rpm on this machine and try again. Exiting..."
stop_time
exit 1
fi
##
## Convert the image to a raw image, place it in $V2V_OUTPUT, and log the work
##
echo "Converting ova file ${V2V_INPUT} to raw file in ${V2V_OUTPUT}, logging to ${V2V_LOG} "
echo "Start time: `date`"
if [[ ${V2V_INPUT} == *.ova ]]
then
/usr/bin/virt-v2v ${V2V_DEBUG_ARGS} -i ova ${V2V_INPUT} -o local -of raw -os ${V2V_OUTPUT} > ${V2V_LOG} 2>&1
elif [[ ${V2V_INPUT} == *.ovf ]]
then
XMLFILE=/tmp/.xml.virt.$$
/mnt/migrate/tools/parse_rhev_ovf.py -f ${V2V_INPUT} > $XMLFILE
/usr/bin/virt-v2v ${V2V_DEBUG_ARGS} -i libvirtxml ${XMLFILE} -o local -of raw -os ${V2V_OUTPUT} > ${V2V_LOG} 2>&1
rm -f $XMLFILE
else
echo "Error, unsupported input file!"
exit 1
fi
##
## Run virt-sysprep if the log file has been provided. Build out the command by getting any converted disks a through z in the v2v output
##
if [ ! -z "$SYSPREP_LOG" ]; then
echo "Running virt-sysprep against disks in ${V2V_OUTPUT} and logging to ${SYSPREP_LOG}"
call_later="/usr/bin/virt-sysprep --enable net-hwaddr,udev-persistent-net,customize "
if [ -n "$IPADDR" ]
then
BOOTPROTO=none
NETCFG="IPADDR=$IPADDR\nPREFIX=24\nGATEWAY=172.16.0.1\nDNS1=172.16.0.25\n"
else
BOOTPROTO=dhcp
NETCFG=""
fi
call_later+=" --run-command 'if [ ! -f /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then echo -e \"NAME=eth0\nDEVICE=eth0\nBOOTPROTO=$BOOTPROTO\n$NETCFG\nONBOOT=yes\" > /etc/sysconfig/network-scripts/ifcfg-eth0; fi' "
for f in "$V2V_OUTPUT/"*-sd[a-z]; do
call_later+=" -a $f"
done
call_later+=" > ${SYSPREP_LOG} 2>&1"
echo $call_later
eval $call_later
fi
stop_time