Kjetil's Information Center: A Blog About My Projects

Buildroot for KVM Guest

Here is a set of configuration files to quickly build a minimal KVM-based hypervisor guest with Buildroot. It attempts to use mostly VirtIO paravirtualization to maximize performance. This includes virtual console, virtual network and virtualized filesystem with the 9P protocol.

These files are based on version 2016.11 of Buildroot, gotten from https://buildroot.org/downloads/buildroot-2016.11.tar.bz2

Main Buildroot configuration file, should be stored as "configs/kvm_guest_defconfig" in the Buildroot structure:

BR2_x86_64=y
BR2_x86_core2=y
BR2_TOOLCHAIN_BUILDROOT_GLIBC=y
BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_4_4=y
BR2_TOOLCHAIN_BUILDROOT_CXX=y
BR2_TARGET_GENERIC_HOSTNAME="kvm-guest"
BR2_TARGET_GENERIC_ISSUE="Welcome to Virtualized KVM Guest"
BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV=y
BR2_TARGET_GENERIC_GETTY_PORT="hvc0"
BR2_TARGET_GENERIC_GETTY_BAUDRATE_115200=y
BR2_LINUX_KERNEL=y
BR2_LINUX_KERNEL_CUSTOM_VERSION=y
BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="4.4.38"
BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/kvm_guest/linux-4.4.config"
BR2_LINUX_KERNEL_INSTALL_TARGET=y
BR2_TARGET_ROOTFS_EXT2=y
BR2_TARGET_ROOTFS_EXT2_4=y
# BR2_TARGET_ROOTFS_TAR is not set
          


Linux kernel configuration, should be stored as "board/kvm_guest/linux-4.4.config":

CONFIG_SYSVIPC=y
CONFIG_NO_HZ_IDLE=y
CONFIG_HIGH_RES_TIMERS=y
CONFIG_VIRT_CPU_ACCOUNTING_GEN=y
CONFIG_NAMESPACES=y
CONFIG_KALLSYMS_ALL=y
CONFIG_EMBEDDED=y
CONFIG_MODULES=y
CONFIG_MODULE_UNLOAD=y
CONFIG_SMP=y
CONFIG_HYPERVISOR_GUEST=y
CONFIG_SCHED_SMT=y
CONFIG_NUMA=y
CONFIG_IA32_EMULATION=y
CONFIG_NET=y
CONFIG_PACKET=y
CONFIG_UNIX=y
CONFIG_INET=y
CONFIG_NET_9P=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_DEVTMPFS=y
CONFIG_DEVTMPFS_MOUNT=y
CONFIG_VIRTIO_BLK=y
CONFIG_NETDEVICES=y
CONFIG_VIRTIO_NET=y
CONFIG_VT_HW_CONSOLE_BINDING=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_HW_RANDOM=y
CONFIG_HW_RANDOM_VIRTIO=y
CONFIG_VIRT_DRIVERS=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_BALLOON=y
CONFIG_VIRTIO_INPUT=y
CONFIG_VIRTIO_MMIO=y
CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y
CONFIG_EXT2_FS=y
CONFIG_EXT3_FS=y
CONFIG_TMPFS=y
CONFIG_TMPFS_POSIX_ACL=y
CONFIG_9P_FS=y
CONFIG_9P_FS_POSIX_ACL=y
CONFIG_DEBUG_FS=y
CONFIG_STACKTRACE=y
          


Once the files are in place, the build can be performed with:

make kvm_guest_defconfig
make
          


Here is a helper script to unpack the root filesystem and launch the whole thing under QEMU:

#!/bin/sh

if [ `id -u` != "0" ]; then
  echo "Must be root!"
  exit 1
fi

cd `dirname "$0"`

if [ ! -d /tmp/kvm_guest ]; then
  mkdir -p /tmp/kvm_guest
  mkdir -p /tmp/kvm_guest_loop
  mount -o loop output/images/rootfs.ext4 /tmp/kvm_guest_loop
  cp -a /tmp/kvm_guest_loop/* /tmp/kvm_guest
  umount /tmp/kvm_guest_loop
  rmdir /tmp/kvm_guest_loop
fi

qemu-system-x86_64 \
  -enable-kvm \
  -cpu host \
  -m 128 \
  -kernel output/images/bzImage \
  -serial none \
  -display none \
  -monitor none \
  -chardev stdio,id=char0 \
  -device virtio-serial \
  -device virtconsole,chardev=char0 \
  -fsdev local,id=fs0,path=/tmp/kvm_guest,security_model=none \
  -device virtio-9p-pci,fsdev=fs0,mount_tag=/dev/root \
  -net nic,macaddr=00:00:de:ad:be:ef,model=virtio,vlan=1 \
  -net tap,ifname=tap0,vlan=1,script=no,downscript=no \
  -append "root=/dev/root rw rootfstype=9p rootflags=trans=virtio console=hvc0"
          


Topic: Configuration, by Kjetil @ 14/01-2017, Article Link