forked from piggyvenus/cloud_suite_fastrax_public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rhev_v2v.sh
executable file
·261 lines (227 loc) · 6.31 KB
/
rhev_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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
#
# rhev_v2v.sh
#
# USAGE: rhev_v2v.sh [-h] -i inputfile -o outputdir -l logfile -t tempdir [-L sysprep_log]
#
# DESCRIPTION:
# virtual-to-virtual script to migrate an OVA file from disk,
# to a rhev export domain suitable for RHEV.
#
# 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
#
#----------------------------------------------------------------------------------------------------
ulimit -c unlimited
v2v_help() {
cat << EOF
Usage: ${0##*/} [-h] -i inputfile -o rhev_export_path -l logfile -t tempdir -n #nics [-d] [-I IP_Address]
Convert an OVA file to a raw image file
-h show this help screen
-i full path to input OVA file (mandatory)
-o RHEV export domain NFS mount path, i.e. 10.0.0.1:/exports/rhev/export
-l full path to logging file that will be made (mandatory)
-t full path to temporary directory (used to untar the ova) (mandatory)
-I IP address to assign host for migration
-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.
EOF
}
stop_time() {
echo "Stop time: `date`"
}
V2V_INPUT=""
V2V_OUTPUT=""
V2V_TMPDIR=""
V2V_LOG=""
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
##
## RHEV Export Domain (-o) is mandatory; verify it was specified with -o and that it exists.
##
if [ x$V2V_OUTPUT == "x" ]; then
echo "ERROR: RHEV export domain (-o) is mandatory but was not specified. "
v2v_help
stop_time
exit 1
fi
# This won't work for RHEV export domain.
#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.
##
# Can't happen with RHEV.
#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 qcow image, output it to RHEV export datastore, and log the work
##
echo "Converting ova file ${V2V_INPUT} to qcow image and loading to ${V2V_OUTPUT}, logging to ${V2V_LOG} "
echo "Start time: `date`"
#/usr/bin/virt-v2v ${V2V_DEBUG_ARGS} -i ova ${V2V_INPUT} -o local -of raw -os ${V2V_OUTPUT} > ${V2V_LOG} 2>&1
/usr/bin/virt-v2v ${V2V_DEBUG_ARGS} -i ova ${V2V_INPUT} -o rhev -of qcow2 -os ${V2V_OUTPUT} --network rhevm > ${V2V_LOG} 2>&1
# Determine the location of the image(s) on RHEV Export from the log.
QCOW_FILES=""
for i in $(/usr/bin/awk '/^qemu-img convert/ {print $NF}' $V2V_LOG | /usr/bin/sed -e "s/'//g" -e "s|^$TMPDIR||" | /usr/bin/perl -pe 's|^/.*?/|/|')
do
j="/mnt/${i}"
if [ ! -f "$j" ]
then
echo "QCOW file <$j> not found!"
stop_time
exit 1
fi
QCOW_FILES+=" ${j}"
done
##
## 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
if [ -z "$QCOW_FILES" ]
then
echo "No image file was found in $V2V_LOG, unable to run sysprep."
stop_time
exit 1
fi
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 "
# This section will switch NICs to DHCP. Commenting out for now as we don't have DHCP in RHEV.
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
# Fallback to at least one eth0 if host had non ethX interfaces -prutledg
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 $QCOW_FILES
do
call_later+=" -a $f"
done
call_later+=" > ${SYSPREP_LOG} 2>&1"
echo $call_later
eval $call_later
fi
stop_time