Debian Codegraph @

build-vm-qemu

raw
1 #
2 # qemu specific functions
3 #
4 ################################################################
5 #
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 or 3 as
10 # published by the Free Software Foundation.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program (see the file COPYING); if not, write to the
19 # Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22 ################################################################
23
24 # This used to be just an alias for kvm.
25 # Now it is used to emulate foreign architectures via qemu system emulator.
26 # Note: this is not for the faster qemu user land emulator, this is is still
27 # handled in chroot or kvm mode
28
29 vm_verify_options_qemu() {
30 if test -n "$KILL" -o -n "$DO_WIPE" ; then
31 return
32 fi
33
34 vm_kernel=/.build.kernel.kvm
35 vm_initrd=/.build.initrd.kvm
36
37 # overwrite some options for specific build architectures
38 case $BUILD_ARCH in
39 armv6l|armv7l)
40 qemu_bin="/usr/bin/qemu-system-arm"
41 qemu_console=ttyAMA0
42 qemu_options="-M virt"
43 qemu_device=virtio-blk-device
44 qemu_rng_device=virtio-rng-device
45 qemu_cpu="-cpu cortex-a15"
46 ;;
47 armv8l|aarch32|aarch64|aarch64_ilp32)
48 qemu_bin="/usr/bin/qemu-system-aarch64"
49 qemu_console=ttyAMA0
50 qemu_cpu="-cpu cortex-a72"
51 qemu_options="$qemu_options -M virt"
52 qemu_device=virtio-blk-device
53 qemu_rng_device=virtio-rng-device
54 ;;
55 ppc|ppcle|ppc64|ppc64le)
56 qemu_bin="/usr/bin/qemu-system-ppc64"
57 qemu_console=hvc0
58 qemu_options="-M pseries"
59 qemu_cpu=""
60 grep -q PPC970MP /proc/cpuinfo && qemu_check_ppc970
61 qemu_device=virtio-blk
62 qemu_rng_device=virtio-rng
63 ;;
64 riscv64)
65 qemu_bin="/usr/bin/qemu-system-riscv64"
66 qemu_console=ttyS0
67 qemu_cpu="-cpu rv64"
68 qemu_options="$qemu_options -M virt -bios /usr/share/qemu/opensbi-riscv64-virt-fw_jump.bin"
69 qemu_device=virtio-blk-device
70 qemu_rng_device=virtio-rng-device
71 ;;
72 s390|s390x)
73 qemu_bin="/usr/bin/qemu-system-s390x"
74 qemu_cpu="-cpu qemu"
75 qemu_console=hvc0
76 qemu_device=virtio-blk-ccw
77 qemu_serial_device=virtio-serial-ccw
78 qemu_rng_device=virtio-rng-ccw
79 ;;
80 x86_64)
81 qemu_bin="/usr/bin/qemu-system-x86_64"
82 qemu_cpu="-cpu qemu64"
83 # Use defaults and fallbacks for other values
84 ;;
85 #
86 # untested architectures
87 #
88 hppa)
89 qemu_bin="/usr/bin/qemu-system-hppa"
90 qemu_console=ttyAMA0
91 qemu_cpu=""
92 qemu_options="$qemu_options -bios /usr/share/qemu/hppa-firmware.img"
93 ;;
94 m68k)
95 qemu_bin="/usr/bin/qemu-system-m68k"
96 qemu_console=ttyAMA0
97 qemu_cpu="-cpu m68000"
98 qemu_options="$qemu_options -M none"
99 ;;
100 riscv32)
101 qemu_bin="/usr/bin/qemu-system-riscv32"
102 qemu_console=ttyS0
103 qemu_cpu="-cpu rv32"
104 qemu_options="$qemu_options -M virt -bios /usr/share/qemu/opensbi-riscv32-virt-fw_jump.bin"
105 qemu_device=virtio-blk-device
106 qemu_rng_device=virtio-rng-device
107 ;;
108 esac
109
110 case $qemu_device in
111 virtio*)
112 VM_ROOTDEV=/dev/disk/by-id/virtio-0
113 VM_SWAPDEV=/dev/disk/by-id/virtio-1
114 ;;
115 *)
116 VM_ROOTDEV=/dev/sda
117 VM_SWAPDEV=/dev/sdb
118 ;;
119 esac
120
121 if test -n "$VM_NETOPT" -o -n "$VM_NETDEVOPT" ; then
122 if test -n "$VM_NETOPT" ; then
123 for item in "${VM_NETOPT[@]}" ; do
124 qemu_options="$qemu_options -net $item"
125 done
126 fi
127 if test -n "$VM_NETDEVOPT" ; then
128 for item in "${VM_NETDEVOPT[@]}" ; do
129 qemu_options="$qemu_options -netdev $item"
130 done
131 fi
132 fi
133 if test -n "$VM_DEVICEOPT" ; then
134 for item in "${VM_DEVICEOPT[@]}" ; do
135 qemu_options="$qemu_options -device $item"
136 done
137 fi
138
139 if test -n "$qemu_rng_device" ; then
140 if test -c /dev/hwrng &&
141 test -f /sys/class/misc/hw_random/rng_current &&
142 test "$(cat /sys/class/misc/hw_random/rng_current)" != none; then
143 rng_dev="/dev/hwrng"
144 else
145 rng_dev="/dev/random"
146 fi
147 qemu_options="$qemu_options -object rng-random,filename=$rng_dev,id=rng0 -device $qemu_rng_device,rng=rng0"
148 fi
149 }
150
151 vm_startup_qemu() {
152 qemu_args=(-drive file="$VM_ROOT",format=raw,if=none,id=disk,cache=unsafe -device "$qemu_device",drive=disk,serial=0)
153 if [ -n "$VM_USER" ] ; then
154 getent passwd "$VM_USER" > /dev/null || cleanup_and_exit 3 "cannot find KVM user '$VM_USER'"
155 else
156 # use qemu user by default if available
157 getent passwd qemu >/dev/null && VM_USER=qemu
158 fi
159 [ -n "$VM_USER" ] && qemu_options="$qemu_options -runas $VM_USER"
160 if test -n "$VM_SWAP" ; then
161 qemu_args=("${qemu_args[@]}" -drive file="$VM_SWAP",format=raw,if=none,id=swap,cache=unsafe -device "$qemu_device",drive=swap,serial=1)
162 fi
163
164 kvm_add_console_args "$qemu_serial_device"
165
166 if test -n "$BUILD_JOBS" -a "$icecream" = 0 -a -z "$BUILD_THREADS" ; then
167 qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS")
168 elif test -n "$BUILD_JOBS" -a -n "$BUILD_THREADS" ; then
169 qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS,threads=$BUILD_THREADS")
170 fi
171 qemu_append="root=$VM_ROOTDEV"
172 if test -n "$VMDISK_FILESYSTEM" ; then
173 qemu_append="$qemu_append rootfstype=$VMDISK_FILESYSTEM"
174 fi
175 if test -n "$VMDISK_MOUNT_OPTIONS" ; then
176 qemu_append="$qemu_append rootflags=${VMDISK_MOUNT_OPTIONS#-o }"
177 fi
178 qemu_append="$qemu_append $vm_linux_kernel_parameter"
179 qemu_append="$qemu_append panic=1 quiet no-kvmclock elevator=noop"
180 qemu_append="$qemu_append nmi_watchdog=0 rw rd.driver.pre=binfmt_misc"
181 qemu_append="$qemu_append console=$qemu_console init=$vm_init_script"
182 if test -z "$VM_NETOPT" -a -z "$VM_NETDEVOPT"; then
183 qemu_options="$qemu_options -net none"
184 fi
185 if test -n "$VM_TELNET"; then
186 qemu_options="$qemu_options -netdev user,id=telnet,hostfwd=tcp:127.0.0.1:$VM_TELNET-:23 -device e1000,netdev=telnet"
187 fi
188 if test -n "$VM_CUSTOMOPT"; then
189 qemu_options="$qemu_options $VM_CUSTOMOPT"
190 fi
191 set -- $qemu_bin -nodefaults -no-reboot -nographic -vga none $qemu_cpu $qemu_options \
192 -kernel $vm_kernel \
193 -initrd $vm_initrd \
194 -append "$qemu_append" \
195 ${VM_MEMSIZE:+-m $VM_MEMSIZE} \
196 "${qemu_args[@]}"
197
198 if test "$PERSONALITY" != 0 ; then
199 # have to switch back to PER_LINUX to make qemu work
200 set -- linux64 "$@"
201 fi
202 export QEMU_AUDIO_DRV=none # we do not want to have sound inside the VMs
203 echo "$@"
204 "$@"
205 qemu_ret=$?
206 test "$qemu_ret" = "137" && cleanup_and_exit 3 "qemu got SIGKILL"
207 }
208
209 vm_kill_qemu() {
210 vm_kill_kvm
211 }
212
213 vm_fixup_qemu() {
214 vm_fixup_kvm
215
216 if test -z "$qemu_serial_device" ; then
217 if test -e "$BUILD_ROOT/.build.console.kvm" -a ! -L "$BUILD_ROOT/.build.console.kvm" && grep -q '^virtio$' "$BUILD_ROOT/.build.console.kvm" ; then
218 echo "Detected virtio-serial support"
219 qemu_serial_device=virtio-serial,max_ports=2
220 qemu_console=hvc0
221 fi
222 fi
223
224 }
225
226 vm_attach_root_qemu() {
227 vm_attach_root_kvm
228 }
229
230 vm_attach_swap_qemu() {
231 vm_attach_swap_kvm
232 }
233
234 vm_detach_root_qemu() {
235 vm_detach_root_kvm
236 }
237
238 vm_detach_swap_qemu() {
239 vm_detach_swap_kvm
240 }
241
242 vm_cleanup_qemu() {
243 vm_cleanup_kvm
244 }
245
246 vm_sysrq_qemu() {
247 vm_sysrq_kvm
248 }
249
250 vm_wipe_qemu() {
251 vm_wipe_kvm
252 }
253