2 # kvm/qemu specific functions
4 ################################################################
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
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.
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.
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
22 ################################################################
24 kvm_bin=/usr/bin/qemu-kvm
27 # assume virtio support by default
28 kvm_device=virtio-blk-pci
32 if ! grep -q -E '(kvm_rma_count.*kvm_hpt_count)|(kvm_hpt_count.*kvm_rma_count)' /proc/cmdline ; then
33 echo "put kvm_rma_count=<VM number> or kvm_hpt_count=<> to your boot options"
39 if ! grep -q "$HUGETLBFSPATH" /proc/mounts ; then
40 echo "hugetlbfs is not mounted to $HUGETLBFSPATH"
43 local HUGETLBBLKSIZE=$(stat -f -c "%S" "$HUGETLBFSPATH")
44 HUGETLBBLKSIZE=$(( ${HUGETLBBLKSIZE:-0} / 1024 ))
45 if test "$HUGETLBBLKSIZE" -lt 1 -o ! -e "/sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB" ; then
46 echo "could not determine hugetlbfs block size"
49 local PAGES_FREE=$(cat /sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB/free_hugepages)
50 local PAGES_REQ=$(( ${VM_MEMSIZE:-64} * 1024 / $HUGETLBBLKSIZE ))
51 if test "$PAGES_FREE" -lt "$PAGES_REQ" ; then
52 echo "expected $PAGES_REQ to be available (have $PAGES_FREE)"
53 echo "please adjust nr_hugepages"
58 vm_verify_options_kvm() {
62 # overwrite some options for specific host architectures
65 kvm_bin="/usr/bin/qemu-system-arm"
67 kvm_options="-enable-kvm -M vexpress-a15 -dtb /boot/a15-guest.dtb -cpu cortex-a15"
68 vm_kernel=/boot/zImage
69 vm_initrd=/boot/initrd
70 # prefer the guest kernel/initrd
71 test -e /boot/zImage.guest && vm_kernel=/boot/zImage.guest
72 test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
73 kvm_device=virtio-blk-device
76 kvm_bin="/usr/bin/qemu-system-aarch64"
78 kvm_options="-enable-kvm -M virt -cpu host"
80 vm_initrd=/boot/initrd
81 # prefer the guest kernel/initrd
82 test -e /boot/Image.guest && vm_kernel=/boot/Image.guest
83 test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
84 kvm_device=virtio-blk-device
86 ppc|ppcle|ppc64|ppc64le)
87 kvm_bin="/usr/bin/qemu-system-ppc64"
89 kvm_options="-enable-kvm -M pseries"
90 grep -q PPC970MP /proc/cpuinfo && kvm_check_ppc970
91 vm_kernel=/boot/vmlinux
92 vm_initrd=/boot/initrd
93 if test "$BUILD_ARCH" = ppc64le -a -e /boot/vmlinuxle ; then
94 vm_kernel=/boot/vmlinuxle
95 vm_initrd=/boot/initrdle
97 grep -q "pSeries" /proc/cpuinfo && kvm_device=scsi-hd # no virtio on pSeries
98 grep -q "PowerNV" /proc/cpuinfo || kvm_device=scsi-hd # no virtio on ppc != power7 yet
101 kvm_bin="/usr/bin/qemu-system-s390x"
102 kvm_options="-enable-kvm"
104 vm_kernel=/boot/image
105 vm_initrd=/boot/initrd
106 kvm_device=virtio-blk-ccw
110 # check if we can run kvm
111 if ! test -r /dev/kvm -a -x "$kvm_bin" ; then
112 echo "host does not support kvm"
113 echo "either the kvm kernel-module is not loaded or kvm is not installed or hardware virtualization is deactivated in the BIOS."
118 test -n "$HUGETLBFSPATH" -a "$VM_TYPE" = kvm && kvm_check_hugetlb
121 test -n "$VM_KERNEL" && vm_kernel="$VM_KERNEL"
122 test -z "$vm_kernel" && vm_kernel=/boot/vmlinuz
125 test -n "$VM_INITRD" && vm_initrd="$VM_INITRD"
126 if test -z "$vm_initrd" ; then
127 # find a nice default
128 if test -e "/boot/initrd-build" ; then
129 vm_initrd="/boot/initrd-build"
130 elif test -e "/boot/initrd-virtio" ; then
131 vm_initrd="/boot/initrd-virtio"
133 vm_initrd="/boot/initrd"
135 # use /etc/sysconfig/kernel as indication if we have virtio
136 if test -e /etc/sysconfig/kernel ; then
137 local im=$(INITRD_MODULES=; . /etc/sysconfig/kernel; echo "$INITRD_MODULES")
138 if test "$im" != "${im/virtio/}" ; then
139 kvm_device=virtio-blk-pci
147 qemu_rootdev=/dev/disk/by-id/virtio-0
148 VM_SWAPDEV=/dev/disk/by-id/virtio-1
151 qemu_rootdev=/dev/sda
159 qemu_args=(-drive file="$VM_IMAGE",if=none,id=disk,serial=0,cache=unsafe -device "$kvm_device",drive=disk)
160 if test -n "$VM_SWAP" ; then
161 qemu_args=("${qemu_args[@]}" -drive file="$VM_SWAP",if=none,id=swap,serial=1,cache=unsafe -device "$kvm_device",drive=swap)
164 if test -n "$BUILD_JOBS" -a "$icecream" = 0 -a -z "$BUILD_THREADS" ; then
165 qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS")
166 elif test -n "$BUILD_JOBS" -a -n "$BUILD_THREADS" ; then
167 qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS,threads=$BUILD_THREADS")
169 if test "$VM_TYPE" = kvm ; then
170 test "$kvm_console" != ttyAMA0 && kvm_options="$kvm_options -cpu host"
171 test -n "$HUGETLBFSPATH" && kvm_options="$kvm_options -mem-prealloc -mem-path $HUGETLBFSPATH"
173 set -- $qemu_bin -no-reboot -nographic -vga none -net none $kvm_options \
176 -append "root=$qemu_rootdev panic=1 quiet no-kvmclock nmi_watchdog=0 rw rd.driver.pre=binfmt_misc elevator=noop console=$kvm_console init=$vm_init_script" \
177 ${VM_MEMSIZE:+-m $VM_MEMSIZE} \
180 if test "$PERSONALITY" != 0 ; then
181 # have to switch back to PER_LINUX to make qemu work
184 export QEMU_AUDIO_DRV=none # we do not want to have sound inside the VMs
190 if ! fuser -k -TERM "$VM_IMAGE" ; then
191 echo "could not kill build in $VM_IMAGE"
197 # check if we will use a kernel from the build root, in this case
198 # we assume the kernel does virtio
199 if test -z "$VM_KERNEL" -a -e "$BUILD_ROOT/.build.kernel.$VM_TYPE" ; then
200 # ide-hd is the non-virtio default
201 if test "$kvm_device" = ide-hd ; then
202 kvm_device=virtio-blk-pci
203 qemu_rootdev=/dev/disk/by-id/virtio-0
204 VM_SWAPDEV=/dev/disk/by-id/virtio-1
209 vm_attach_root_kvm() {
213 vm_attach_swap_kvm() {
217 vm_detach_root_kvm() {
221 vm_detach_swap_kvm() {