Repository navigation
Expand file tree
/
Copy pathstarwinds.sh
More file actions
executable file
·439 lines (414 loc) · 10.6 KB
/
Copy pathstarwinds.sh
File metadata and controls
executable file
·439 lines (414 loc) · 10.6 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
root() {
# root or GTFO
if [ "${EUID}" -ne 0 ]
then echo "Please run as root"
exit 1
fi
}
# Variables
BKUP=/root/vm2hv-BAK.tar
IRAMFS=initramfs-$(uname -r).img
SYSC=/etc/sysconfig
NMCFG=/etc/NetworkManager/system-connections
UBMODS=/etc/initramfs-tools/modules
NWS=${SYSC}/network-scripts
NETP=/etc/netplan
NPUB=99-netcfg-vmware.yaml
NPHV=99-netcfg-hyperv.yaml
UDEV=/etc/udev/rules.d/70-persistent-net.rules
get_osdist() {
if [ -f /etc/redhat-release ]; then
OSDIST=redhat
OSVER=$(cat /etc/redhat-release | cut -d" " -f 6 | grep -oP "[0-9]+" | head -1)
elif [ -f /etc/lsb-release ]; then
source /etc/lsb-release
OSDIST=${DISTRIB_ID}
OSVER=${DISTRIB_RELEASE}
else
echo "Incompatible OS. Exiting"
exit 1
fi
case ${OSDIST} in
*[Uu]buntu*)
OSDIST=ubuntu
;;
*[hH]at*)
OSDIST=redhat
;;
esac
}
# Help text variables
NORM=`tput sgr0`
BOLD=`tput bold`
REV=`tput smso`
help() {
# Help info
echo "Basic usage: ${BOLD}$0 ${NORM}"
echo -e "This will prep a RHEL 7 or 8 for V2V conversion"
echo -e "to HyperV. By default it will convert and shutdown."
echo -e ""
echo -e "This script will ${BOLD}NOT${NORM} execute conversion without the -y,--yes option"
echo -e ""
echo -e "\t${BOLD}-y, --yes${NORM}:\t\tExecute the conversion."
echo -e "\t${BOLD}-n, --noshutdown${NORM}:\tDo ${BOLD}NOT${NORM} shutdown at the end"
echo -e "\t${BOLD}-r, --restore${NORM}:\t\tRestore system files"
echo -e "\t${BOLD}-m, --mkinit${NORM}:\t\tDo ${BOLD}NOT${NORM} run mkinitrd command"
echo -e "\t${BOLD}-t, --test${NORM}:\t\tShow info about conversion"
echo -e ""
echo ""
}
# Code funtions
ifaces() {
IFACES=$(ls -1 /sys/class/net/ | grep -v lo | grep -E '^ens[0-9]{3}\b|^eth[0-9]{1}\b')
IFA=($IFACES)
}
vmtools() {
case ${OSDIST} in
ubuntu)
# vmtools function
if [ "${_test}" -eq 1 ]; then
echo "apt-get -yq remove open-vm-tools && apt-get -yq install linux-virtual linux-cloud-tools-virtual linux-tools-virtual"
echo "=========="
else
apt-get -yq remove open-vm-tools && apt-get -yq install linux-virtual linux-cloud-tools-virtual linux-tools-virtual
if [ $? -ne 0 ]; then
echo "APT command failed. exiting"
exit 1
fi
fi
;;
redhat)
# vmtools function
if [ "${_test}" -eq 1 ]; then
echo "yum -yq -e 0 remove open-vm-tools && yum -yq -e 0 install hyperv-daemons hyperv-tools"
echo "=========="
else
yum -yq -e 0 remove open-vm-tools && yum -yq -e 0 install hyperv-daemons hyperv-tools
if [ $? -ne 0 ]; then
echo "YUM command failed. exiting"
exit 1
fi
fi
;;
esac
}
backup() {
echo "Saving network files"
case ${OSDIST} in
redhat)
case ${OSVER} in
8|7|6)
if [ "${_test}" -eq 1 ]; then
echo "tar cvf ${BKUP} ${SYSC}/network ${NWS}/ifcfg-e* ${IRAMFS} ${UDEV}"
echo "============"
else
tar cvf ${BKUP} ${SYSC}/network ${NWS}/ifcfg-e* /boot/${IRAMFS} ${UDEV}
fi
;;
9)
if [ "${_test}" -eq 1 ]; then
echo "tar cvf ${BKUP} ${NMCFG} ${IRAMFS}"
echo "============"
else
tar cvf ${BKUP} ${NMCFG} /boot/${IRAMFS}
fi
;;
esac
;;
ubuntu)
if [ "${_test}" -eq 1 ]; then
echo "tar cvf ${BKUP} ${NETP}/"
echo "============"
else
tar cvf ${BKUP} ${NETP}/
fi
;;
esac
}
rhel76() {
# rhel 7 function
if [ "${_test}" -eq 1 ]; then
echo "OS Version: RHEL ${OSVER}"
echo "Interfaces found: ${IFA[@]}"
echo "=========="
echo "rm ${UDEV}"
else
if [[ "${IFACES}" != *eth* ]]; then
# fix the ifaces
for I in "${!IFA[@]}"; do
sed "s/${IFA[$I]}/eth${I}/g; /HWADDR/d" ${NWS}/ifcfg-${IFA[$I]} > ${NWS}/ifcfg-eth${I}
done
# Swap gateway in network file
sed -i "s/${IFA[0]}/eth0/" ${SYSC}/network
else
# Still remove HWADDR from ethX
for I in "${!IFA[@]}"; do
sed -i "/HWADDR/d" ${NWS}/ifcfg-${IFA[$I]}
done
fi
rm ${UDEV}
fi
if [ "${_mkinit}" -eq 0 ]; then
if [ "${_test}" -eq 1 ]; then
echo "mkinitrd -f -v --with=hid-hyperv --with=hv_utils --with=hv_vmbus --with=hv_storvsc --with=hv_netvsc /boot/initramfs-$(uname -r).img $(uname -r)"
else
# run mkinitrd
mkinitrd -f -v --with=hid-hyperv --with=hv_utils --with=hv_vmbus --with=hv_storvsc --with=hv_netvsc /boot/initramfs-$(uname -r).img $(uname -r)
fi
fi
}
rhel8() {
# rhel 8 function
if [ "${_test}" -eq 1 ]; then
echo "OS Version: RHEL ${OSVER}"
echo "Interfaces found: ${IFA[@]}"
echo "=========="
echo "sed -i '/GATEWAY/d; /GATEWAYDEV/d' ${SYSC}/network"
else
if [[ "${IFACES}" != *eth* ]]; then
# fix the ifaces
for I in "${!IFA[@]}"; do
sed "s/${IFA[$I]}/eth${I}/g; /HWADDR/d" ${NWS}/ifcfg-${IFA[$I]} > ${NWS}/ifcfg-eth${I}
done
else
# Still remove HWADDR from ethX
for I in "${!IFA[@]}"; do
sed -i "/HWADDR/d" ${NWS}/ifcfg-${IFA[$I]}
done
fi
# Remove GATEWAY* from network file
sed -i '/GATEWAY/d; /GATEWAYDEV/d' ${SYSC}/network
fi
if [ "${_mkinit}" -eq 0 ]; then
if [ "${_test}" -eq 1 ]; then
echo "mkinitrd -f -v --with=hid-hyperv --with=hv_utils --with=hv_vmbus --with=hv_storvsc --with=hv_netvsc /boot/initramfs-$(uname -r).img $(uname -r)"
else
# run mkinitrd
mkinitrd -f -v --with=hid-hyperv --with=hv_utils --with=hv_vmbus --with=hv_storvsc --with=hv_netvsc /boot/initramfs-$(uname -r).img $(uname -r)
fi
fi
}
rhel9() {
#RHEL 9 function
if [ "${_test}" -eq 1 ]; then
echo "OS Version: RHEL ${OSVER}"
echo "Interfaces found: ${IFA[@]}"
echo "=========="
else
if [[ "${IFACES}" != *eth* ]]; then
# fix the ifaces
for I in "${!IFA[@]}"; do
nmcli con clone ${IFA[$I]} eth${I}
nmcli con modify eth${I} connection.interface-name eth${I}
rm -f ${NMCFG}/${IFA[$I]}.nmconnection
done
fi
fi
}
ubuntu() {
# ubuntu function
## Check test
if [ "${_test}" -eq 1 ]; then
echo "OS Version: Ubuntu ${OSVER}"
echo "Interfaces found: ${IFA[@]}"
echo "=========="
else
if [[ "${IFACES}" != *eth* ]]; then
# Sed out interfaces
mv ${NETP}/${NPHV} ${NETP}/${NPUB}
sed 's/ens160/eth0/g ; s/ens192/eth1/g ; s/ens224/eth2/g' ${NETP}/${NPUB} > ${NETP}/${NPHV}
if [ -f ${NETP}/${NPHV} ] ; then
rm ${NETP}/${NPUB}
else
echo "NETWORK ISSUE, STOPPING"
exit 1
fi
else
echo "No IFACES to change."
fi
fi
}
poweroff() {
if [[ "${_test}" -eq 1 ]] || [[ "${_noshutdown}" -eq 1 ]]; then
echo "No shutdown performed"
echo "=========="
## shutdown
else
echo "Shutting down in 5 sec. CTRL-C to stop" && sleep 5
shutdown -h now
fi
}
# reverse it all
restore() {
get_osdist
case ${OSDIST} in
redhat)
if [ "${_test}" -eq 1 ]; then
echo "TAR location: ${BKUP}"
echo "Packages: "
echo -e "\tInstall: open-vm-tools"
echo -e "\tRemove: hyperv-daemons hyperv-tools"
echo "=========="
exit $?
else
# Get rid of old files
rm ${NWS}/ifcfg-e*
# Restore tar file, uninstall hyperV tools, install open-vm-tools
cd / ; tar xvf ${BKUP} && ( yum -yq -e 0 install open-vm-tools && yum -yq -e 0 remove hyperv-daemons hyperv-tools )
EXIT=$?
if [ $EXIT -gt 0 ]; then
echo "Restore failed. Please check ${BKUP}"
echo "=========="
exit $EXIT
else
echo "Restore complete"
echo "=========="
exit $EXIT
fi
fi
;;
ubuntu)
if [ "${_test}" -eq 1 ]; then
echo "TAR location: ${BKUP}"
echo "Packages: "
echo -e "\tInstall: open-vm-tools"
echo -e "\tRemove: "
echo "=========="
exit $?
else
# Get rid of old files
rm ${NETP}/${NPHV}
# Restore tar file, uninstall hyperV tools, install open-vm-tools
cd / ; tar xvf ${BKUP} && ( apt-get -yq install open-vm-tools && apt-get -yq remove linux-virtual linux-cloud-tools-virtual linux-tools-virtual )
EXIT=$?
if [ $EXIT -gt 0 ]; then
echo "Restore failed. Please check ${BKUP}"
echo "=========="
exit $EXIT
else
echo "Restore complete"
echo "=========="
exit $EXIT
fi
fi
;;
esac
}
# CLInt GENERATED_CODE: start
# Default values
_noshutdown=0
_test=0
_restore=0
_mkinit=0
# No-arguments is not allowed
[ $# -eq 0 ] && help && exit 1
# Converting long-options into short ones
for arg in "$@"; do
shift
case "$arg" in
"--noshutdown") set -- "$@" "-n";;
"--test") set -- "$@" "-t";;
"--restore") set -- "$@" "-r";;
"--yes") set -- "$@" "-y";;
"--mkinit") set -- "$@" "-m";;
*) set -- "$@" "$arg"
esac
done
function print_illegal() {
echo Unexpected flag in command line \"$@\"
}
# Parsing flags and arguments
while getopts 'hntrvym' OPT; do
case $OPT in
h) help
exit 1 ;;
n) _noshutdown=1 ;;
t) _test=1 ;;
r) _restore=1 ;;
y) _yes=1 ;;
m) _mkinit=1 ;;
\?) print_illegal $@ >&2;
echo "---"
help
exit 1
;;
esac
done
# CLInt GENERATED_CODE: end
# Execute
if [ "${_test}" -eq 1 ]; then
echo "=====TESTING ONLY====="
echo "====NO CHANGES MADE==="
echo ""
else
root
fi
if [ "${_restore}" -eq 1 ]; then
restore
exit $?
fi
# OS test: RHEL or Ubuntu
get_osdist
case ${OSDIST} in
ubuntu)
case ${OSVER} in
18)
OSDIST=ubuntu
ifaces
backup
vmtools
ubuntu
poweroff
;;
20.04|22.04)
OSDIST=ubuntu
ifaces
backup
vmtools
ubuntu
poweroff
;;
*)
echo "OS Version failure"
echo ${OSVER}
exit 1
esac
;;
redhat)
# OS test: RHEL 7 or RHEL 8
case ${OSVER} in
7|6)
OSDIST=redhat
ifaces
backup
vmtools
rhel76
poweroff
;;
8)
OSDIST=redhat
ifaces
backup
vmtools
rhel8
poweroff
;;
9)
OSDIST=redhat
ifaces
backup
vmtools
rhel9
poweroff
;;
*)
echo "OS version failure"
exit 1
esac
;;
*)
echo "No valid OS found:"
echo ${OSDIST}
exit 1
esac