-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvbox2kvm.sh
More file actions
51 lines (42 loc) · 1.41 KB
/
vbox2kvm.sh
File metadata and controls
51 lines (42 loc) · 1.41 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
#!/bin/bash
# Looking for the commande "vboxmanage", used later in the script to dump the data
if ! command -v vboxmanage >/dev/null 2>&1; then
echo "vboxmanage could not be found"
exit 1
fi
# Looking for the commande "qemu-img", used later in the script to insert the data
if ! command -v qemu-img >/dev/null 2>&1; then
echo "qemu-img could not be found"
exit 1
fi
# Looking for number of arguments passed in the script
if [[ "$#" -ne 1 ]]; then
echo "Wrong arguments passed to the script, check the documentation."
exit 1
fi
# Checking if the argument (aka. virtualbox diskFile) exists
if [[ -f $1 ]]; then
if [[ "$1" =~ \.vdi$ ]] || [[ "$1" =~ \.vmdk$ ]] || [[ "$1" =~ \.VHD$ ]] || [[ "$1" =~ \.HDD$ ]]; then
baseDisk="$1"
strippedPath="${baseDisk%.*}"
echo "$strippedPath"
else
echo "File not ending in a format of a known virtualbox disk, exiting..."
exit 1
fi
else
echo "File not found, does the script have the rights to run ?"
exit 1
fi
# Creating variables to make the script clearer
tempDisk="$strippedPath".img
qcowDisk="$strippedPath".qcow2
echo "Exporting $baseDisk to $tempDisk as raw image file..."
vboxmanage clonehd "$baseDisk" "$tempDisk" -format raw
echo "Done!"
echo "Converting $tempDisk into $qcowDisk"
qemu-img convert -f raw -O qcow2 "$tempDisk" "$qcowDisk"
echo "Done!"
echo "Removing $tempDisk..."
rm $tempDisk
echo "Done!"