nixos-generators
nixos-generators copied to clipboard
Adding virtio kernel modules to initrd for qcow format
TLDR; might be good to add some extra initrd kernel headers to the qcow format to support their use as virtio disks (sample config below).
I'm playing with terraform-provider-libvirt, and building a config based on their ubuntu-example.tf, which maps a qcow image to a libvirt volume, and adds that volume to the VM config as a virtio disk. This was giving me trouble (VM wasn't able to find the disk /dev/disk/by-label/nixos during boot), but I didn't have any issues creating a VM w/ Virt Manager, whose wizard maps the disk to an IDE port instead.
After searching around, I saw a virtualization page on the NixOS wiki with a section on building a base image that mentioned adding some headers to support virtio. After copying those headers into a config and regenerating the image I was finally able to boot with it mapped as a virtio disk. Throwing my config below in case it helps anyone else, or if you'd want to add these options into the qcow format by default (they didn't seem to hurt booting w/ the qcow attached as IDE).
To get this working, drop the following into configuration.nix, and then run nixos-generate --format qcow --configuration ./configuration.nix:
{ config, lib, pkgs, ... }:
{
# Add these modules
boot.initrd.availableKernelModules = [
"xhci_pci"
"ehci_pci"
"ahci"
"usbhid"
"usb_storage"
"sd_mod"
"virtio_balloon"
"virtio_blk"
"virtio_pci"
"virtio_ring"
];
# Convenience things
services.sshd.enable = true;
# Enable cloud-init (not necessary to get volume to boot, but see mention of terraform-provider-libvirt above that uses this).
services.cloud-init = {
enable = true;
ext4.enable = true;
};
}
I'm just starting to kick the tires on NixOS and realized there's also a qemu-guest profile, which overlaps with that module list above. E.g. you could also just use this in your configuration.nix:
{ config, lib, pkgs, ... }:
{
# In lieu of boot.initrd.availableKernelModules in comment above.
imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
services.sshd.enable = true;
# Enable cloud-init (if needed)
services.cloud-init = {
enable = true;
ext4.enable = true;
};
}
I'm having what seems to be a similar issue with the virtualbox format. Trying to load a virtualbox ova built with nixos-generators into XCP-ng but it hangs waiting for /dev/disk/by-label/nixos to appear. Adding the virtio kernel modules hasn't seemed to help so far.
imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
seems like the way to go.
maybe we shoula add that to the qcow format or add a new format qemu ?