Skip to content

Instantly share code, notes, and snippets.

@aagontuk
Last active March 16, 2023 02:38
Show Gist options
  • Save aagontuk/f7a7986894e07c6eb9b08484b2f33981 to your computer and use it in GitHub Desktop.
Save aagontuk/f7a7986894e07c6eb9b08484b2f33981 to your computer and use it in GitHub Desktop.
Installing/Preparing a KVM guest for Linux kernel development in Debian.

Graphical Installation

Bellow instructions are for creating a kvm guest OS(Debian) on Debian. Though the examples are based on Debian it should be same for all other distro.

  • Check if your CPU support virtualization: grep -E --color=auto 'vmx|svm|0xc0f' /proc/cpuinfo

  • Check if the host kernel support KVM: grep CONFIG_KVM /boot/config-$(uname -r)

  • Install necessary packages for QEMU/KVM: sudo apt install qemu-system

  • Create a qcow2 image for the guest: qemu-img create -f qcow2 virtualdebian.img 50G

  • Boot ISO to install the guest OS: (You need to run this as root for networking to work)

sudo qemu-system-x86_64 -enable-kvm -m RAM_IN_MB -smp NUM_CPU -drive file=virtualdebian.img -cdrom debian.iso -boot d
  • Install guest OS.

  • Now run the guest OS with qemu whenever you want:

sudo qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp 2 -drive file=virtualdebian.img
  • If you want to share a folder from host to guest:

    1. Run the guest with this command:
    sudo qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp 2 -drive file=virtualdebian.img,if=virtio -fsdev local,security_model=passthrough,id=fsdev0,path=/path/to/host/folder -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare
    
    1. In guest mount the host folder: mount -t 9p -o trans=virtio,version=9p2000.L hostshare /path/to/mount/point
  • A better alternative for sharing file would be virtiofs: Needs testing.

CLI installation (-nographic)

Debian netinst doesn't show any output if qemu is run with -nographic option while installing from ISO. You have to use -kernel /path/to/kernel with -append "console=ttyS0". Other Solutions:

Once the installation is done you can start the VM normally.

ARM

Running Ubuntu on Qemu

sudo apt-get install qemu-efi
cp /usr/share/qemu-efi-aarch64/QEMU_EFI.fd flash0.img
truncate -s 64M flash0.img
truncate -s 64M flash1.img
  • Run Ubuntu cloud image with QEMU:
qemu-system-aarch64 -m 8G -cpu cortex-a57 -M virt,gic-version=max -smp 16 -nographic -drive if=pflash,format=raw,file=flash0.img,readonly -drive if=pflash,format=raw,file=flash1.img -drive if=none,file=focal-server-cloudimg-arm64.img,id=hd0 -device virtio-blk-device,drive=hd0

Miscellaneous

Compiling the kernel

Quick Configure, build and install

make olddefconfig
make localmodconfig
make -j$(nproc)
make -j$(nproc) bzImage # If you want to create kernel bzImage
make modules
make modules_install
make install

Running the compiled kernel and Debugging

Automating QEMU

General References

  1. https://wiki.debian.org/KVM
  2. https://wiki.debian.org/QEMU
  3. https://wiki.archlinux.org/index.php/KVM
  4. https://wiki.archlinux.org/index.php/QEMU
  5. https://wiki.gentoo.org/wiki/QEMU/Options
  6. https://www.tauceti.blog/posts/qemu-kvm-share-host-directory-with-vm-with-virtio/
  7. QEMU Disk Images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment