-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqemu.sh
executable file
·203 lines (175 loc) · 4.34 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
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
#!/usr/bin/env bash
function find_kernel()
{
local paths=(
/src/linux/arch/x86/boot/bzImage
)
for path in "${paths[@]}"; do
[ -f $path ] && {
echo $path
return
}
done
}
function qemu()
{
local kernel_opts=(
console=ttyS0
root=/dev/vda
raid=noautodetect
# panic_on_warn=1
panic=-1
intel_iommu=on
# breaks debugger
nokaslr
mitigations=off
audit=0
"${kernel_args[@]}"
)
local cpus=$(getconf _NPROCESSORS_ONLN)
local qemu_opts=(
-enable-kvm
-machine q35,accel=kvm
-device intel-iommu
-name linux-dev
-no-reboot
# shorthand for -gdb tcp::1234
-s
-nographic
-serial mon:stdio
# snapshot by default, <Ctrl-T s> to manually flush
-drive if=virtio,file="$rootfs_image",$drive_opts
-append "${kernel_opts[*]}"
# <Ctrl-T> as an escape character
# (since default <Ctrl-A> is tmux control-key)
-echr 0x14
-smp $(( cpus/2 ))
-m 1G
)
qemu-system-x86_64 "${qemu_opts[@]}" "$@"
}
function make_spare_drive()
{
local i="$1" size="$2"
shift 2
local path="$i.img"
[ -f "$path" ] || fallocate -l "$size" "$path"
echo "$path"
}
function make_spare_drive_in_mem()
{
local i="$1" size="$2"
shift 2
local path="/dev/shm/$i.img"
fallocate -l "$size" "$path"
echo "$path"
}
function make_drive()
{
local path="$1"
shift
local base="$(basname "$path")"
fallocate -l1G "$base.img"
mke2fs -F -t ext4 "$base.img"
mkdir -p "$mnt.mnt"
sudo mount -o loop "$base.img" "$mnt.mnt"
rsync -a "$path" "$mnt.mnt"
sudo umount "$mnt.mnt"
echo "$base.img"
}
function parse_drives()
{
local i
for i in "$@"; do
# it is likely some qemu option
if [ "${i:0:1}" == "-" ]; then
qemu_args+=( "$i" )
continue
fi
if [ -f "$i" ]; then
qemu_args+=( "-drive" "if=ide,file=$i" )
continue
fi
if [ -d "$i" ]; then
local drive="$(make_drive "$i")"
qemu_args+=( "-drive" "if=ide,file=$drive" )
continue
fi
echo "Unknown option: $i" >&2
return 2
done
}
declare -A opts
function usage()
{
cat <<EOL
$0 [ OPTS ] -- [ DRIVE/PATH/... ] [ QEMU OPTS ]
Options:
-p preserve spare drives
-S <size of spare drive>
-M <size of spare drive in memory>
-K <kernel arg, cmdline>
-Q <qemu args> (i.e. -S)
-k <kernel image>
-i <rootfs image>
EOL
}
function parse_opts()
{
local OPTARG OPTIND c
qemu_args=()
kernel_image=$(find_kernel)
rootfs_image=rootfs.img
drive_opts=()
kernel_args=()
qemu_args=()
spare_preserve=0
spare_drives=()
spare_drives_in_mem=()
cleanup_images=()
while getopts "hsS:M:K:Q:k:i:p" c; do
case "$c" in
s)
echo "snapshot mode is used, <Ctrl-T s> to manually flush"
drive_opts="snapshot=on"
;;
S) spare_drives+=("$OPTARG");;
M) spare_drives_in_mem+=("$OPTARG");;
p) spare_preserve=1;;
K) kernel_args+=("$OPTARG");;
Q) qemu_args+=("$OPTARG");;
k) kernel_image="$OPTARG";;
i) rootfs_image="$OPTARG";;
h) usage && exit 0;;
*) usage >&2 && exit 1;;
esac
done
for ((i = 0; i < ${#spare_drives[@]}; ++i)); do
local size="${spare_drives[$i]}"
local img="$(make_spare_drive $i $size)"
[ $spare_preserve -eq 1 ] || cleanup_images+=( "$img" )
qemu_args+=(
"-drive"
"if=ide,file=$img,$drive_opts"
)
done
for ((i = 0; i < ${#spare_drives_in_mem[@]}; ++i)); do
local size="${spare_drives_in_mem[$i]}"
local img="$(make_spare_drive_in_mem $i $size)"
[ $spare_preserve -eq 1 ] || cleanup_images+=( "$img" )
qemu_args+=(
"-drive"
"if=ide,file=$img,$drive_opts"
)
done
qemu_args+=( -kernel "$kernel_image" )
shift $(( OPTIND-1 ))
parse_drives "$@" || return
}
function main()
{
parse_opts "$@" || return
qemu "${qemu_args[@]}"
[[ -z "${cleanup_images[@]}" ]] || rm -fr "${cleanup_images[@]?}"
}
main "$@"