nixos/qemu-vm: Don't assume boot drive is always vdb

This commit is contained in:
Chuck 2020-01-28 15:08:56 -08:00 committed by Niklas Hambüchen
parent a5e211dd7f
commit e74755c422

View File

@ -46,6 +46,13 @@ let
description = "Extra options passed to device flag."; description = "Extra options passed to device flag.";
}; };
name = mkOption {
type = types.nullOr types.str;
default = null;
description =
"A name for the drive. Must be unique in the drives list. Not passed to qemu.";
};
}; };
}; };
@ -84,6 +91,14 @@ let
else else
"/dev/vd${letter}"; "/dev/vd${letter}";
lookupDriveDeviceName = driveName: driveList:
(findSingle (drive: drive.name == driveName)
(throw "Drive ${driveName} not found")
(throw "Multiple drives named ${driveName}") driveList).device;
addDeviceNames =
imap1 (idx: drive: drive // { device = driveDeviceName idx; });
# Shell script to start the VM. # Shell script to start the VM.
startVM = startVM =
'' ''
@ -406,6 +421,7 @@ in
mkOption { mkOption {
type = types.listOf (types.submodule driveOpts); type = types.listOf (types.submodule driveOpts);
description = "Drives passed to qemu."; description = "Drives passed to qemu.";
apply = addDeviceNames;
}; };
diskInterface = diskInterface =
@ -552,12 +568,14 @@ in
virtualisation.qemu.drives = mkMerge [ virtualisation.qemu.drives = mkMerge [
[{ [{
name = "root";
file = "$NIX_DISK_IMAGE"; file = "$NIX_DISK_IMAGE";
driveExtraOpts.cache = "writeback"; driveExtraOpts.cache = "writeback";
driveExtraOpts.werror = "report"; driveExtraOpts.werror = "report";
}] }]
(mkIf cfg.useBootLoader [ (mkIf cfg.useBootLoader [
{ {
name = "boot";
file = "$TMPDIR/disk.img"; file = "$TMPDIR/disk.img";
driveExtraOpts.media = "disk"; driveExtraOpts.media = "disk";
deviceExtraOpts.bootindex = "1"; deviceExtraOpts.bootindex = "1";
@ -610,7 +628,7 @@ in
}; };
} // optionalAttrs cfg.useBootLoader } // optionalAttrs cfg.useBootLoader
{ "/boot" = { "/boot" =
{ device = "${driveDeviceName 1}2"; { device = "${lookupDriveDeviceName "boot" cfg.qemu.drives}2";
fsType = "vfat"; fsType = "vfat";
options = [ "ro" ]; options = [ "ro" ];
noCheck = true; # fsck fails on a r/o filesystem noCheck = true; # fsck fails on a r/o filesystem