A client asked me to find a way to keep running an ancient system (from 10+ years ago) in a new server. The old binary packages didn’t work. I couldn’t even compile those packages for our linux in the new server. Then I though in create a Virtual Machine. So this is how to run a VM headless in QEMU-KVM-libvirt-etc.
Virtual Machine creation
The idea was to install the minimal amount of extra software on the server. I also have some time working with gnome-boxes, sometimes alternating with virt-manager.
My idea is do not install those frontends on the server, and launch the virtual machine by command line. On the other hand, I don’t have much time to learn how to do everything from the command line¹.
The solution? I’ve created – with gnome-boxes of course – a Debian 6.0.10 (from 2014) in my own PC, then uploaded the disk image to the server and launch there the virtual machine. By the way, if for some reason you need to deploy some old software you can find Debian CD/DVD images here https://cdimage.debian.org/cdimage/archive/.
Launching the VM headless
Software needed
In order to run the virtual machine with qemu we need to install the virtualization packages. My server happens to work with Debian 11, so I’m installing qemu, kvm and all the stuff with apt-get:
apt-get install qemu-kvm libvirt-clients virtinst bridge-utils cpu-checker
After the installation make sure libvirtd is running
systemctl status libvirtd
If not, you can start it with systemctl start libvirtd.
Creating a new VM with the disk image
With the virt-install command you can import a disk image using this command line:
virt-install --import --name <some name for your vm> --memory <desired amount of RAM> --vcpus <#cores> --disk </path/to/your/disk/image.qcow>,bus=sata --os-variant <which os are you running> --network default
You can add the switch –-noautoconsole to run on background. The get the options for the --os-variant switch run:
virt-install --osinfo list
For example, I’m running an ancient Debian distribution:
This is it.
Connecting to your brand new VM
So, I’ve got my VM running… how to connect to do something with that? First I need to know the IP² for this VM. I’m pretty sure there are better ways to do it, but I did it with arp:
Now is just mater of connect to the virtual machine via ssh:
Now you know how to run a VM headless in qemu-kvm-libvirtd.
VNC access
Alternatively you can enable a VNC console by adding the following to /etc/libvirt/qemu/vm-name.xml, before the end of </devices> section:
<graphics type='vnc' port='-1' autoport='yes' listen='ip' passwd='some password'>
Then restart your VM with
virsh vmname stop virsh vmname start virsh vncdisplay vmname
The last line will tell you where to connect, for example if the output is:
127.0.0.1:1
You should connect with a VNC client to 127.0.0.1 and port 5901 (the port is always 5900+display number)
Notes
¹ as soon as this job ends I definitely I should learn how to do everything from the command line, to save time later
² the screenshots for this article where taken on different computers and on different days, just ignore the different IPs