summaryrefslogtreecommitdiff
path: root/build-vm-kvm
diff options
context:
space:
mode:
Diffstat (limited to 'build-vm-kvm')
-rw-r--r--build-vm-kvm108
1 files changed, 98 insertions, 10 deletions
diff --git a/build-vm-kvm b/build-vm-kvm
index 455ecc1..641bc4d 100644
--- a/build-vm-kvm
+++ b/build-vm-kvm
@@ -26,8 +26,17 @@ kvm_console=ttyS0
# assume virtio support by default
kvm_device=virtio-blk-pci
+kvm_serial_device=
+kvm_rng_device=virtio-rng-pci
kvm_options=
+function complain()
+{
+ local ex=$1; shift
+ printf "Error: %s\n" "$@" >&2
+ cleanup_and_exit $ex
+}
+
kvm_check_ppc970() {
if ! grep -q -E '(kvm_rma_count.*kvm_hpt_count)|(kvm_hpt_count.*kvm_rma_count)' /proc/cmdline ; then
echo "put kvm_rma_count=<VM number> or kvm_hpt_count=<> to your boot options"
@@ -64,23 +73,40 @@ vm_verify_options_kvm() {
armv7l)
kvm_bin="/usr/bin/qemu-system-arm"
kvm_console=ttyAMA0
- kvm_options="-enable-kvm -M vexpress-a15 -dtb /boot/a15-guest.dtb -cpu cortex-a15"
+ kvm_options="-enable-kvm -M virt -cpu host"
vm_kernel=/boot/zImage
vm_initrd=/boot/initrd
# prefer the guest kernel/initrd
test -e /boot/zImage.guest && vm_kernel=/boot/zImage.guest
test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
kvm_device=virtio-blk-device
+ kvm_rng_device=virtio-rng-device
;;
- aarch64)
+ armv8l|aarch64)
kvm_bin="/usr/bin/qemu-system-aarch64"
kvm_console=ttyAMA0
- kvm_options="-enable-kvm -M virt -cpu host"
vm_kernel=/boot/Image
vm_initrd=/boot/initrd
- # prefer the guest kernel/initrd
- test -e /boot/Image.guest && vm_kernel=/boot/Image.guest
- test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
+ if test "${BUILD_ARCH#aarch}" != "$BUILD_ARCH" -o "${BUILD_ARCH#armv8}" != "$BUILD_ARCH"; then
+ kvm_options="-enable-kvm -cpu host "
+ test -e /boot/Image.guest && vm_kernel=/boot/Image.guest
+ test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
+ else
+ # Running an armv7 kernel on aarch64
+ kvm_options="-enable-kvm -cpu host,aarch64=off "
+ # prefer the guest kernel/initrd
+ test -e /boot/Image.guest32 && vm_kernel=/boot/Image.guest32
+ test -e /boot/initrd.guest32 && vm_initrd=/boot/initrd.guest32
+ fi
+ # This option only exists with QEMU 2.5 or newer
+ if $kvm_bin -machine 'virt,?' 2>&1 | grep -q gic-version ; then
+ # We want to use the host gic version in order to make use
+ # of all available features (e.g. more than 8 CPUs) and avoid
+ # the emulation overhead of vGICv2 on a GICv3 host.
+ kvm_options+="-M virt,gic-version=host"
+ else
+ kvm_options+="-M virt"
+ fi
kvm_device=virtio-blk-device
;;
ppc|ppcle|ppc64|ppc64le)
@@ -104,6 +130,8 @@ vm_verify_options_kvm() {
vm_kernel=/boot/image
vm_initrd=/boot/initrd
kvm_device=virtio-blk-ccw
+ kvm_serial_device=virtio-serial-ccw
+ kvm_rng_device=virtio-rng-ccw
;;
esac
@@ -152,13 +180,59 @@ vm_verify_options_kvm() {
VM_SWAPDEV=/dev/sdb
;;
esac
+
+ if test -n "$VM_NETOPT" -o -n "$VM_NETDEVOPT" ; then
+ if test -n "$VM_NETOPT" ; then
+ for item in "${VM_NETOPT[@]}" ; do
+ kvm_options="$kvm_options -net $item"
+ done
+ fi
+ if test -n "$VM_NETDEVOPT" ; then
+ for item in "${VM_NETDEVOPT[@]}" ; do
+ kvm_options="$kvm_options -netdev $item"
+ done
+ fi
+ fi
+
+ if test -n "$VM_DEVICEOPT" ; then
+ for item in "${VM_DEVICEOPT[@]}" ; do
+ kvm_options="$kvm_options -device $item"
+ done
+ fi
+
+ if test -n "$kvm_rng_device" ; then
+ if test -c /dev/hwrng &&
+ test -f /sys/class/misc/hw_random/rng_current &&
+ test "$(cat /sys/class/misc/hw_random/rng_current)" != none; then
+ rng_dev="/dev/hwrng"
+ else
+ rng_dev="/dev/random"
+ fi
+ kvm_options="$kvm_options -object rng-random,filename=$rng_dev,id=rng0 -device $kvm_rng_device,rng=rng0"
+ fi
}
vm_startup_kvm() {
qemu_bin="$kvm_bin"
- qemu_args=(-drive file="$VM_IMAGE",if=none,id=disk,serial=0,cache=unsafe -device "$kvm_device",drive=disk)
+ qemu_args=(-drive file="$VM_IMAGE",format=raw,if=none,id=disk,serial=0,cache=unsafe -device "$kvm_device",drive=disk)
+ if [ -n "$VM_USER" ] ; then
+ getent passwd "$VM_USER" > /dev/null || complain 22 "cannot find KVM user '$VM_USER'"
+ else
+ # use qemu user by default if available
+ getent passwd qemu >/dev/null && VM_USER=qemu
+ fi
+ [ -n "$VM_USER" ] && kvm_options="$kvm_options -runas $VM_USER"
if test -n "$VM_SWAP" ; then
- qemu_args=("${qemu_args[@]}" -drive file="$VM_SWAP",if=none,id=swap,serial=1,cache=unsafe -device "$kvm_device",drive=swap)
+ qemu_args=("${qemu_args[@]}" -drive file="$VM_SWAP",format=raw,if=none,id=swap,serial=1,cache=unsafe -device "$kvm_device",drive=swap)
+ fi
+ # the serial console device needs to be compiled into the target kernel
+ # which is why we can not use virtio-serial on other platforms
+ if test -n "$kvm_serial_device" ; then
+ qemu_args=("${qemu_args[@]}" -device "$kvm_serial_device" -device virtconsole,chardev=virtiocon0 -chardev stdio,id=virtiocon0)
+ elif test -n "$VM_CONSOLE_INPUT" ; then
+ qemu_args=("${qemu_args[@]}" -serial mon:stdio)
+ else
+ qemu_args=("${qemu_args[@]}" -serial stdio)
fi
if test -n "$BUILD_JOBS" -a "$icecream" = 0 -a -z "$BUILD_THREADS" ; then
@@ -170,10 +244,24 @@ vm_startup_kvm() {
test "$kvm_console" != ttyAMA0 && kvm_options="$kvm_options -cpu host"
test -n "$HUGETLBFSPATH" && kvm_options="$kvm_options -mem-prealloc -mem-path $HUGETLBFSPATH"
fi
- set -- $qemu_bin -no-reboot -nographic -vga none -net none $kvm_options \
+ qemu_rootfstype=""
+ if test -n "$VMDISK_FILESYSTEM" ; then
+ qemu_rootfstype="rootfstype=$VMDISK_FILESYSTEM"
+ fi
+ qemu_rootflags=""
+ if test -n "$VMDISK_MOUNT_OPTIONS" ; then
+ qemu_rootflags="rootflags=${VMDISK_MOUNT_OPTIONS#-o }"
+ fi
+ if test -z "$VM_NETOPT" -a -z "$VM_NETDEVOPT"; then
+ kvm_options="$kvm_options -net none"
+ fi
+ if test -n "$VM_TELNET"; then
+ kvm_options="$kvm_options -netdev user,id=telnet,hostfwd=tcp:127.0.0.1:$VM_TELNET-:23 -device e1000,netdev=telnet"
+ fi
+ set -- $qemu_bin -nodefaults -no-reboot -nographic -vga none $kvm_options \
-kernel $vm_kernel \
-initrd $vm_initrd \
- -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" \
+ -append "root=$qemu_rootdev $qemu_rootfstype $qemu_rootflags panic=1 quiet no-kvmclock nmi_watchdog=0 rw rd.driver.pre=binfmt_misc elevator=noop console=$kvm_console init=$vm_init_script" \
${VM_MEMSIZE:+-m $VM_MEMSIZE} \
"${qemu_args[@]}"