Virtualizing my old desktop with QEMU

In the process I made a disk image of my OS, in case I needed some data I forgot to back up. Now I could have restored the disk image, booted into it, and then back it up. But then I got an idea, what if I just boot into it with QEMU.

I recently decided it was time to move away from Ubuntu (18.04) and go to a more lean distro: Void Linux. In the process I made a disk image of my OS, in case I needed some data I forgot to back up.

And as it happens, I was missing some profile metadata for Firefox. Now I could have restored the disk image, booted into it, and then back it up. But then I got an idea, what if I just boot into it with QEMU.

QEMU is a virtualization framework similar to VMWare/VirtualBox but with more advanced options (such as multi-arch, hardware spoofing, and direct linux kernel booting).

And this is where the rabbit hole started...

I first tried to use the image directly with -device file=<path>,format=raw but sadly this made it read-only in my setup which broke the boot process. So I had to mount it in a loopback device first.

After a bit of finagling with the QEMU parameters I got it to boot into the console, however the gnome login seemed unresponsive, none of my inputs were going through (even after switching out different virtual input devices). Still I knew it wasn't frozen since after a few minutes it started going into sleep mode.

I thought it would be a good idea to setup VNC (since I could still access the tty) and log in that way, how hard could it be.
Well the default network device (-nic user) doesn't provide a simple way to access the guest OS; sure you could forward ports, but that's very inconvient if you don't yet know what you need.

So I created a dummy bridge (brctl addbr br0), added the bridge to the Ubuntu VM; setup a OpenWRT VM with the same bridge as well as a user-mode NIC (eth0 being wan, br0 being lan). And finally I could access my old PC with a good old IPv4 address.

I got a blank screen but with some guidance from the internet I found that I needed to change my xstartup config to start a GUI manually.

With all that I was able to export the required data and import it into the new OS. And while it was a hassle to setup, this does seem like a promising way to switch OS without having to dual boot or worry about what you might have lost.

Appendices

Qemu command for Ubuntu Image

Note that in the virt-manager version it has the extra br0 nic.
$ sudo qemu-system-x86_64 -initrd /boot/initramfs-5.10.17_1.img -kernel /boot/vmlinuz-5.10.17_1 -serial mon:stdio -append "console=ttyS0 root=/dev/vda5" -m 8G -blockdev node-name=q1,driver=raw,file.driver=host_device,file.filename=/dev/loop2 -device virtio-blk,drive=q1 -net user

VNC xstartup

#!/bin/sh

export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &

gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &