feat(vms): migrate the idol desktops from KubeVirt to libvirt (#287)

* wip(kubevirt-shoryu): enable libvirt/qemu

* feat(kubevirt-shoryu): enable libvirt/qemu with br0 + no NAT network

* chore: lock the microvm flake input

* feat(kubevirt-shoryu): define the libvirt domains declaratively

* fix(kubevirt-shoryu): pin the kana domain UUID

* feat(kubevirt-shoryu): migrate idols-ruby to libvirt
This commit is contained in:
ryan4yin | 二花 authored and GitHub committed 2026-09-21 15:12:37 +08:00
1 parent 3753396a01
commit 787b51df25
4 files changed
+185

No files matched your search

Generated
+38
View File
@@ -462,6 +462,27 @@
"type": "github"
}
},
"microvm": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"spectrum": "spectrum"
},
"locked": {
"lastModified": 1789506833,
"narHash": "sha256-4L8SOCF84de1irB72Ka9VHcu9wrAJ9Amuey4lU9oh4Y=",
"owner": "microvm-nix",
"repo": "microvm.nix",
"rev": "187b0a390ee054028106a674e7b01b1cb940cbba",
"type": "github"
},
"original": {
"owner": "microvm-nix",
"repo": "microvm.nix",
"type": "github"
}
},
"my-asahi-firmware": {
"flake": false,
"locked": {
@@ -873,6 +894,7 @@
"home-manager": "home-manager_2",
"lanzaboote": "lanzaboote",
"llm-agents": "llm-agents",
"microvm": "microvm",
"my-asahi-firmware": "my-asahi-firmware",
"mysecrets": "mysecrets",
"nix-darwin": "nix-darwin",
@@ -916,6 +938,22 @@
"type": "github"
}
},
"spectrum": {
"flake": false,
"locked": {
"lastModified": 1786032750,
"narHash": "sha256-+Ba/kIn5BjvV7PN8bWOo7RoqgTY/Yv2M7qeMf3wS2Ag=",
"ref": "refs/heads/main",
"rev": "77d2389874bb092b8dbf05ecc5b1030372e80e98",
"revCount": 1417,
"type": "git",
"url": "https://spectrum-os.org/git/spectrum"
},
"original": {
"type": "git",
"url": "https://spectrum-os.org/git/spectrum"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
+6
View File
@@ -50,6 +50,12 @@
# get some latest packages from the master branch
nixpkgs-master.url = "github:nixos/nixpkgs/master";
# NixOS MicroVMs -- run the VMs directly on the hosts, replacing KubeVirt
microvm = {
url = "github:microvm-nix/microvm.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
# for macos
# nixpkgs-darwin.url = "github:nixos/nixpkgs/nixpkgs-26.05-darwin";
nixpkgs-darwin.url = "github:nixos/nixpkgs/nixpkgs-unstable";
+82
View File
@@ -0,0 +1,82 @@
{
pkgs,
lib,
mylib,
...
}:
let
genXml = import (mylib.relativeToRoot "lib/genLibvirtDomainXml.nix") { inherit lib; };
# Declarative libvirt domains. The disk image is a one-time copy (reflink) of
# the VM's former KubeVirt disk, e.g.:
# cp --reflink=auto /var/lib/rancher/k3s/storage/vms/<vm>-disk/disk.img \
# /var/lib/libvirt/images/<vm>.img
domains = {
idols-kana = {
uuid = "e744f563-7c7d-45e6-bc71-61980790208d";
vcpu = 4;
memoryGiB = 8;
disk = "/var/lib/libvirt/images/idols-kana.img";
mac = "52:54:00:5a:51:02";
vncPort = 5902;
};
idols-ruby = {
uuid = "3a959d02-11de-498d-bce7-85f78713ca63";
vcpu = 4;
memoryGiB = 8;
disk = "/var/lib/libvirt/images/idols-ruby.img";
mac = "52:54:00:5a:51:03";
vncPort = 5903;
};
};
in
{
# libvirt/QEMU -- runs the VMs directly instead of on KubeVirt. Used for the
# VMs that need to feel like a real machine (the agent desktops kana/ruby) and
# for Windows.
virtualisation.libvirtd.enable = true;
# The VMs attach to our existing bridge (br0), so allow it explicitly.
virtualisation.libvirtd.allowedBridges = [ "br0" ];
# We don't want libvirt's NAT network (virbr0): every VM uses br0.
systemd.services.libvirt-disable-default-net = {
description = "Disable libvirt's default NAT network";
after = [ "libvirtd.service" ];
requires = [ "libvirtd.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = ''
${pkgs.libvirt}/bin/virsh net-destroy default || true
${pkgs.libvirt}/bin/virsh net-autostart --disable default || true
'';
};
# Define (and autostart) the declarative domains on every activation.
systemd.services.libvirt-define-domains = {
description = "Define the declarative libvirt domains";
after = [ "libvirtd.service" ];
requires = [ "libvirtd.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = lib.concatStringsSep "\n" (
lib.mapAttrsToList (name: args: ''
${pkgs.libvirt}/bin/virsh define ${
pkgs.writeText "libvirt-domain-${name}.xml" (genXml (args // { inherit name; }))
}
${pkgs.libvirt}/bin/virsh autostart ${name} || true
'') domains
);
};
# VM disk images (the declarative domains reference files here).
systemd.tmpfiles.rules = [
"d /var/lib/libvirt/images 0755 root root -"
];
}
+59
View File
@@ -0,0 +1,59 @@
# Generate a libvirt domain XML for a VM that behaves like a real machine
# (BIOS/q35, virtio disk, bridged NIC, VNC display).
#
# The NIC is pinned to q35 PCI bus 0x02 and the disk to bus 0x04 so the guest
# sees the same interface name it did under KubeVirt (enp2s0) and the same
# device ordering.
{ lib }:
{
name,
uuid,
vcpu,
memoryGiB,
disk,
mac,
bridge ? "br0",
vncPort,
}:
''
<domain type='kvm'>
<name>${name}</name>
<uuid>${uuid}</uuid>
<memory unit='GiB'>${toString memoryGiB}</memory>
<vcpu>${toString vcpu}</vcpu>
<os>
<type arch='x86_64' machine='q35'>hvm</type>
</os>
<features>
<acpi/>
</features>
<cpu mode='host-passthrough'/>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<devices>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='${disk}'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
</disk>
<interface type='bridge'>
<mac address='${mac}'/>
<source bridge='${bridge}'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
</interface>
<controller type='usb' model='qemu-xhci'/>
<graphics type='vnc' listen='0.0.0.0' port='${toString vncPort}' autoport='no'/>
<video>
<model type='virtio'/>
</video>
<console type='pty'/>
<serial type='pty'/>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
</rng>
</devices>
</domain>
''