Skip to content

a simple guide to qemu wizardry and "secret" os partitions

commandline_be edited this page Nov 24, 2019 · 17 revisions

Goals

  • Making use of Qemu to do more than just boot virtual machines from image files.
  • How to boot an OS on a multi-boot system with Qemu.
  • How to install an OS from a running system using Qemu.
  • How to boot the booted OS (again)

Exploring Qemu wizardry

As in 'what wizardry is this !?'

You'll find qemu allows for the most amazing variations, nothing up the sleeve, no virtual disk volumes, just partitions on your computer systems. This for local (hdd, ssd, usb) as well as remote (pxe,nfs, http, ssh ...)

Qemu considerations

Qemu offers a lot of possiblities and does so well.

Qemu-kvm works with Linux, MacOS, Microsoft Windows. This document is written for a Linux system host.

What varies most is how to define devices like cdrom, harddisk, file, directory, usb etc. Make sure to read the relevant documentation here

You must consider if you want to use the physical disk for OS and/or data partition or if you want to use a pure VM. If you seek to use a pure VM consider installation and use of virt-manager or virtualbox

The part about multi-boot and dual-boot systems could be a bit risky due to pebkac, remain vigilant.

Gearing up

  • Install QEMU and/or gparted or the partition manager you prefer (keywords: qemu, kvm, x86, x86_64, arm, mips ...)

  • If you are using UEFI images to boot or have a system which has UEFI to boot from, also install OVMF

Usually these packages are available straight from your favorite distro.

Preparation

  • Use gparted if you like a Gui or lsblk -f to learn about partitioning on your machine.

  • Determine free disk space or allocate free disk space by resizing existing partitions. This may require quite a few of manual actions so consider to prepare and document first. This documenation may be a good starting point.

  • Check where your distro left OVMF, typical name is OVMF.fd, it should be there in the package repository.

a list of commands you may care for

Typically these commands tell you what the installed version of qemu is capable of.

sudo qemu-system-x86_64 -device help

qemu-system-x86_64 -accel help

qemu-system-x86_64 -machine help

Note to put all of this in effect you may require to install more software (like xen)

Installing while running an OS

Yes, this is possible, i have done it. If you have no free diskspace on your system, use gparted or similar tool to resize a partition and you are ready to go. If you have free disk space (>20GB) you are good to go.

You can use multiple ways here since Qemu is very flexible and offers at times unexpected functionality.

I document a few possiblities in simple ways below to illustrate Qemu can work for you. This implies you have sudo privileges for qemu and/or can run commands as root.

The commands are to be executed from the commandline since this is simply much more easy than mucking around in some Gui. Because it is not all commandline blues is it not ?

a few pointers

  • for qemu to work well (fast) kvm is mandatory hence it is included in the commands below

  • it is assumed you're using a 64bit machine, for compatiblity qemu64 is chosen

  • the number of cores assigned is 4 but you can increase or decrease in line with your machine corecount-2

  • 4G (4 GB) of ram is assigned but you can obviously adjust with M or G as you see fit

  • if=virtio is used because it works well with what i tested

  • -boot d implies booting from cdrom, check the documentation here (scroll one line up)

  • using -vga qxl works well for most OS i tried, it may not work well for you

  • when installing on your computer while running qemu on your booted machine -bios should be used with care, take time to prepare before going ahead with this option.

  • Have a rescue USB stick at hand, just in case (grin).

  • replace /dev/sdX with something relevant to your system.
    Use gparted or lsblk -f to identify partition identifiers and/or freespace.

what were the goals again ?

  • Making use of Qemu to do more than just boot virtual machines from image files.
  • How to boot an OS on a multi-boot system with Qemu.
  • How to install an OS from a running system using Qemu.
  • How to boot the booted OS (again)

How to boot an OS on a multi-boot system with Qemu.

sudo qemu-system-x86_64 -machine accel=kvm -cpu qemu64 -smp cores=4 -drive if=virtio,file=/dev/sdX,media=disk -m 4G -name qemuboot -vga qxl

doing the same on an UEFI system.

consider running whereis OVMF.fd to find it on your system.
The .fd filename may vary for the distro you are running, specific ones can be found online.

sudo qemu-system-x86_64 -machine accel=kvm -cpu qemu64 -smp cores=4 -drive if=virtio,file=/dev/sdX -m 4G -name qemuboot -boot menu=on -vga qxl --bios /usr/share/ovmf/OVMF.fd

The above also covers : How to boot the booted OS (again)

How to install an OS from a running system using Qemu.

  • Booting from an USB stick, install to Harddisk

    sudo qemu-system-x86_64 -machine accel=kvm -cpu qemu64 -smp cores=4 -hda /dev/sdY1 -drive if=virtio,file=/dev/sdX4,media=disk -m 4G -name qemuboot -vga qxl

  • Booting from an ISO file, install to Harddisk

    sudo qemu-system-x86_64 -machine accel=kvm -cpu qemu64 -smp cores=4 -drive file=isos/mybootableosinstallationmedium.iso,media=cdrom -boot d -drive if=virtio,file=/dev/sdX -m 4G -name qemuboot -vga qxl

  • Booting the Microsoft OS installed as multi-boot on the same harddisk the Linux OS booted from (with an NVME disk)

    qemu-system-x86_64 -machine accel=kvm --enable-kvm -cpu host -smp cores=2 -drive file=/dev/nvme0n1,index=n,index=m,media=disk -m 4G -name qemuboot -boot menu=on -vga qxl --bios /usr/share/ovmf/OVMF.fd --bios /usr/share/OVMF/OVMF_VARS.fd --bios /usr/share/OVMF/OVMF_CODE.fd

Prepare your system to work with nvme

explaining a bit

  • The above string works on my system but is far from ideal. A number of changes happened before this worked. I hope i make no omission or add extraneous actions below.
  • the index=n and index=m values in the above string are to indicate partition numbers for the UEFI and MS OS partitions (ntfs), if you have additional drives list them in a similar fashion
  • you can use if=virtio only with the virtio drivers installed
  • you can use -drive if=virtio,file=/dev/sdaN,media=disk for SATA drive optimal performance

prepare kvm

create: /etc/modprobe.d/kvm.conf containing options kvm ignore_msrs=1

prepare virtio-vfio

With the command lspci -nn look up the value near or a the end of the line like [1021:10e6] for the controllers you want to expose to qemu and will be available through vfio-virtio. Whereid:id in the configuration below is a placeholder for the value like for example [1021:10e6] shown in the output for lspci -nn. Choose the value for at least the NVME controller or other controllers you consider.

create: /etc/modprobe.d/vfio.conf containing options vfio-pci ids=<id:id>,<id:id>

prepare booting using virtio

prepare the Linux kernel to work well with virtio and vfio.

  • Enable iommu for the cpu you work with (intel_iommu=on, amd_iommu=on) must be a kernel boot parameter
  • for AMD amd_iommu=on iommu=ptkvm_amd.npt=1 appended to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub works really well
Clone this wiki locally