-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqemu.sh
72 lines (65 loc) · 1.91 KB
/
qemu.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
create_qemu_img(){
local filename=$1
local size=${2:-"100M"}
local format=${3:-"qcow2"}
local options=${4:+"-o ${4}"}
if [ -f ${filename} ]; then
die "${filename} already exists"
fi
if ! spawn "qemu-img create ${options} -f ${format} ${filename} ${size}"; then
die "pre_partition can't create gentoo image file ${filename}"
fi
}
nbd_device=
mount_qemu_img(){
local filename=$1
local device=${2:-"/dev/nbd0"}
local opts=$3
if [ ! -b /dev/nbd0 ]; then
#max_part=8 to enable partitions
modprobe nbd max_part=8
fi
if [ ! -b /dev/nbd0 ]; then
die "qemu_mount_img can't load nbd kernel module"
fi
#make sure we are not already mounted
if mount | grep ${device} > /dev/null 2>&1; then
die "${device} is already mounted"
fi
if get_device_size_in_mb ${device} > /dev/null 2>&1; then
#if echo q | fdisk ${device} > /dev/null 2>&1; then
ps aux | grep qemu-nbd | grep ${device}
die "${device} is already connected."
fi
if ! qemu-nbd ${opts} -c "${device}" "${filename}"; then
die "qmeu-nbd failed to load ${filename} and connect it to ${device}"
fi
nbd_device="${device}"
local timeout=10
local count=0
while ! get_device_size_in_mb ${device} > /dev/null 2>&1; do
if [ $count -gt $timeout ]; then
die "Timeout while waiting for nbd to come online!"
break
fi
sleep 1
count=$(expr $count + 1)
done
}
cleanup_nbd() {
if [ ! -z "${nbd_device}" ]; then
debug cleanup_nbd "disconnect nbd device ${nbd_device}"
if mount | grep ${nbd_device} > /dev/null 2>&1; then
warn "${nbd_device} is still mounted! not disconnect nbd device."
elif ! spawn "qemu-nbd -d \"${nbd_device}\""; then
warn "cleanup_nbd: Failed to disconnect nbd device ${nbd_device}"
fi
fi
}
#run cleanup whenever quickstart exits, no matter whether it succeeds or not
post_finishing_cleanup() {
cleanup_nbd
}
post_failure_cleanup(){
cleanup_nbd
}