Renaming a VM, and getting a desktop out of it
Two days ago I built a pair of plain Arch VMs on svchost2 for remote desktops, and named them vm-rhino and vm-arch. The names came from an older plan where one would run a different operating system than the other, and by the time the VMs existed that plan was gone. They’re really my wife’s desktop and mine, so this go around they became virtbri and virttom, and mine got an actual desktop on it. Both renames ran at the same time, in parallel agent sessions, which turned out to be most of the fun.
A rename sounds like a one-liner, and in libvirt it nearly is: shut the VM down, virsh domrename vm-arch virttom, done. Except it isn’t done. domrename renames the domain, its config and the autostart link, but the disk image, the UEFI variables file and the serial log path in the XML all keep the old name. So the disk was still vm-arch.qcow2, sitting under a VM called virttom. The fix is unexciting (move the files, edit the paths in virsh dumpxml --inactive, define it again), but it’s the kind of thing you only find out by looking.
Inside the VM the hostname was the easy part. cloud-init was the slightly less easy part, because it has its own idea of what the hostname should be (it remembers the seed it booted from), so I told it preserve_hostname: true and left the seed’s instance ID alone. Changing that would make cloud-init think it’s a brand new machine and redo first boot, which is not what you want on a VM you’re trying to keep. Then the rest of the list: the SSH alias and pinned host key on both of my machines (same key, new name), the GitLab project path (GitLab keeps a redirect from the old one, which is nice of it), the local directory on both seats, the DNS reservation, the uptime check, and a list of hosts in the script I use to hand out temporary root. Every one of those files was also being edited by the virtbri session, so each change went in as a quick locked edit straight onto main, never left sitting on a branch for the other session to trip over.
Two small surprises came out of it. Shutting the VM down for the rename also ended the agent’s temporary sudo on it, because that window doesn’t survive a reboot (obviously, in hindsight). And the weekly update heartbeat reports under a key built from the hostname, so renaming the host silently moves which check it reports to; the monitor had to be renamed in step, or the first Sunday after would have been an alert for a machine that was perfectly fine.
Then the desktop itself. I asked for something modest: browsing, and somewhere to test scripts and commands, not a place to run the home network from. From my own setup it gets my terminal tools and nothing else. XFCE, because it’s light and it behaves over remote protocols, and VNC, because that’s what I asked for. My first instinct was VNC from the hypervisor, since libvirt already has a VNC console for every VM. That turned out to be a bad idea, and the reasons were good enough that I didn’t argue. The connection will eventually come from Guacamole on www, which faces the Internet, and pointing that at the hypervisor means a public-facing box reaching into the machine that runs five VMs, rather than just the one desktop. libvirt’s VNC passwords top out at 8 characters, the traffic isn’t encrypted, and the console is the VM’s actual screen, boot loader included, so anyone who gets to it can boot the thing into a root shell. So VNC lives inside the VM instead: TigerVNC running an XFCE session for me, TLS plus my real password, and a firewall on the VM that only lets www (and SSH tunnels) near the port.
The first run of the setup script fell over, which was my fault (sort of). It used plain pacman -S, the package database was a few days stale, and a mirror returned a 404. Switching to pacman -Syu fixed that and also upgraded the whole VM, including the kernel, at which point the running kernel’s modules were gone and nft refused to load the firewall with “Operation not supported”. The script stopped right there, which is the correct place to stop: no firewall, no VNC. (The virtbri session hit the same thing and was less lucky with the ordering, so both scripts now make the VNC service require the firewall service.)
That left a reboot outside the usual window, and a reboot ends sudo, and I was travelling and couldn’t renew it. So the agent left a one-shot unit that would run the setup once at the next boot and then delete itself, and rebooted. It worked, mostly. The unit used $rc in its command line, and systemd helpfully expanded that itself (to nothing) before the shell ever saw it, so the cleanup step failed and the unit reported an error while everything it was supposed to do had in fact been done. Write $$ in unit files. Also, Arch’s nftables.service loads the ruleset and then reports itself as inactive, which is a great way to make a status check lie to you.
So what I have now is virttom: an XFCE desktop running for me in TigerVNC, encrypted, offering only the one login method, and dropped from everywhere except the places it should be, plus a setup script that rebuilds it. Guacamole doesn’t exist yet, so for now it’s an SSH tunnel and Remmina. One VM, two names, about twenty files.