-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathova-to-qcow2
executable file
·91 lines (77 loc) · 2.08 KB
/
ova-to-qcow2
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
#!/bin/bash
# SPDX-FileCopyrightText: 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command tar qemu-img && exit 3
function print_usage() {
show_header "Usage: ova-to-qcow2"
cat << EOF
-h|--help print (this) help message
EOF
}
OPTIONS=h
LONGOPTIONS=help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR"
exit 3
;;
esac
done
TMP="$(mktemp -d)"
trap 'rm -rf "${TMP}"' INT TERM EXIT ERR
if ! [ $# -eq 1 ]; then
show_error "ERROR: Incorrect # of inputs. Exiting."
exit 3
fi
if ! [ -f "${1}" ]; then
show_error "ERROR: Input ${1@Q} does not exist. Exiting."
exit 3
fi
IN="${1}"
NAME="${IN%.*}"
EXT="${IN##*.}"
if ! [[ "${EXT,,}" = ova ]]; then
show_error "ERROR: Not an OVA file. Exiting."
exit 3
fi
tar -xvf "${IN}" -C "${TMP}"
pushd "${TMP}" > /dev/null
if [ -f "${NAME}.vmdk" ]; then
qemu-img convert -f vmdk -O qcow2 "${NAME}.vmdk" "${NAME}.qcow2"
elif [ -f "${NAME}.vdi" ]; then
qemu-img convert -f vdi -O qcow2 "${NAME}.vdi" "${NAME}.qcow2"
else
show_error "ERROR: No compatible image extracted. Exiting."
fi
popd > /dev/null
if [ -f "${TMP}/${NAME}.qcow2" ]; then
mv -v "${TMP}/${NAME}.qcow2" .
fi