How to use QEMU to run a VM client, assuming that the kernel have kvm enabled and running?
a. create rootfs image as your OS file image, with all the general GNU/Linux utilities:
This is how I create the rootfs for Xenial (I copied and modified from Syzkaller project), using the debootstrap command mainly, but for CentOS rootfs, perhaps you can try:
https://linuxconfig.org/how-to-debootstrap-on-centos-linux
or:
https://github.com/dozzie/yumbootstrap
And here is the script for creating Xenial-based rootfs using debootstrap:
#!/bin/bash
# Copyright 2016 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.# create-image.sh creates a minimal Debian-xenial Linux image suitable for syzkaller.
set -eux
# Create a minimal Debian-xenial distributive as a directory.
sudo rm -rf xenial
mkdir -p xenial
sudo debootstrap –include=openssh-server xenial xenial# Set some defaults and enable promtless ssh to the machine for root.
sudo sed -i ‘/^root/ { s/:x:/::/ }’ xenial/etc/passwd
echo ‘V0:23:respawn:/sbin/getty 115200 hvc0’ | sudo tee -a xenial/etc/inittab
printf ‘\nauto eth0\niface eth0 inet dhcp\n’ | sudo tee -a xenial/etc/network/interfaces
echo ‘debugfs /sys/kernel/debug debugfs defaults 0 0’ | sudo tee -a xenial/etc/fstab
echo ‘debug.exception-trace = 0’ | sudo tee -a xenial/etc/sysctl.conf
sudo mkdir xenial/root/.ssh/
mkdir -p ssh
ssh-keygen -f ssh/id_rsa -t rsa -N ”
cat ssh/id_rsa.pub | sudo tee xenial/root/.ssh/authorized_keys# Install some misc packages.
sudo chroot xenial /bin/bash -c “export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; \
apt-get update; apt-get install –yes curl tar time strace”# Build a disk image
dd if=/dev/zero of=xenial.img bs=5M seek=2047 count=1
mkfs.ext4 -F xenial.img
sudo mkdir -p /mnt/xenial
sudo mount -o loop xenial.img /mnt/xenial
sudo cp -a xenial/. /mnt/xenial/.
sudo mkdir -p /mnt/xenial/lib/modules/xxx/
sudo cp -a /lib/modules/xxx/. /mnt/xenial/lib/modules/xxx/.
sudo umount /mnt/xenial
b. compile the linux kernel, and this will generate a few files: vmlinux, initrd, and bzImage.
When compiling the kernel:
make will generate the vmlinux + bzImage file.
make install will generate the the initramfs.img file.
make modules_install will generate the kernel modules located in /lib/modules/xxx directory, which is used above.
c. boot it up with the correct option:
qemu-system-x86_64 -hda xenial.img -snapshot -m 2048 -net nic -net user,host=10.0.2.10,hostfwd=tcp::53167-:22 -nographic -enable-kvm -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 -smp sockets=2,cores=2,threads=1 -usb -usbdevice mouse -usbdevice tablet -soundhw all -kernel /linux/arch/x86/boot/bzImage -append “console=ttyS0 root=/dev/sda debug earlyprintk=serial slub_debug=UZ” -initrd /boot/initramfs.img
From above we can see that the options choices are very great, which is why virt-manager is highly recommended to use, as it provides an interface for automatic generation of the different option easily:
https://tthtlc.wordpress.com/2016/03/13/setting-up-virtual-machine-via-virshvirt-managervirt-viewer/
Notice that vmlinux is not used above, but it is needed when kgdb debugging is needed:
https://tthtlc.wordpress.com/2012/06/16/virtualbox-kgdb-analysis-of-linux-kernel-v3-4-0-rc3/
https://tthtlc.wordpress.com/2014/05/21/how-to-kgdb-qemu-freebsd-10-kernel-debugging/