From 800639f287b2e51834db898c2d6a46406f9e5c39 Mon Sep 17 00:00:00 2001 From: Chuck Date: Tue, 28 Jan 2020 13:54:46 -0800 Subject: [PATCH 1/3] nixos/qemu-vm: Refactor: Combine duplicate disk definitions --- nixos/modules/virtualisation/qemu-vm.nix | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index be06d6feb11..a9f8e327e42 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -542,25 +542,18 @@ in ]; virtualisation.qemu.drives = mkMerge [ + [{ + file = "$NIX_DISK_IMAGE"; + driveExtraOpts.cache = "writeback"; + driveExtraOpts.werror = "report"; + }] (mkIf cfg.useBootLoader [ - { - file = "$NIX_DISK_IMAGE"; - driveExtraOpts.cache = "writeback"; - driveExtraOpts.werror = "report"; - } { file = "$TMPDIR/disk.img"; driveExtraOpts.media = "disk"; deviceExtraOpts.bootindex = "1"; } ]) - (mkIf (!cfg.useBootLoader) [ - { - file = "$NIX_DISK_IMAGE"; - driveExtraOpts.cache = "writeback"; - driveExtraOpts.werror = "report"; - } - ]) (imap0 (idx: _: { file = "$(pwd)/empty${toString idx}.qcow2"; driveExtraOpts.werror = "report"; From a5e211dd7f95167ab42066e82bfaa5a65971e67c Mon Sep 17 00:00:00 2001 From: Chuck Date: Tue, 28 Jan 2020 14:30:41 -0800 Subject: [PATCH 2/3] nixos/qemu-vm: Generalize drive naming --- nixos/modules/virtualisation/qemu-vm.nix | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index a9f8e327e42..7507db40578 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -74,6 +74,16 @@ let drivesCmdLine = drives: concatStringsSep " " (imap1 driveCmdline drives); + # Creates a device name from a 1-based a numerical index, e.g. + # * `driveDeviceName 1` -> `/dev/vda` + # * `driveDeviceName 2` -> `/dev/vdb` + driveDeviceName = idx: + let letter = elemAt lowerChars (idx - 1); + in if cfg.qemu.diskInterface == "scsi" then + "/dev/sd${letter}" + else + "/dev/vd${letter}"; + # Shell script to start the VM. startVM = '' @@ -512,8 +522,7 @@ in optional cfg.writableStore "overlay" ++ optional (cfg.qemu.diskInterface == "scsi") "sym53c8xx"; - virtualisation.bootDevice = - mkDefault (if cfg.qemu.diskInterface == "scsi" then "/dev/sda" else "/dev/vda"); + virtualisation.bootDevice = mkDefault (driveDeviceName 1); virtualisation.pathsInNixDB = [ config.system.build.toplevel ]; @@ -601,7 +610,7 @@ in }; } // optionalAttrs cfg.useBootLoader { "/boot" = - { device = "/dev/vdb2"; + { device = "${driveDeviceName 1}2"; fsType = "vfat"; options = [ "ro" ]; noCheck = true; # fsck fails on a r/o filesystem From e74755c422180bfa0ca2f74eb11f32e20d4030ee Mon Sep 17 00:00:00 2001 From: Chuck Date: Tue, 28 Jan 2020 15:08:56 -0800 Subject: [PATCH 3/3] nixos/qemu-vm: Don't assume boot drive is always vdb --- nixos/modules/virtualisation/qemu-vm.nix | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index 7507db40578..5a502c36180 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -46,6 +46,13 @@ let 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 "/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. startVM = '' @@ -406,6 +421,7 @@ in mkOption { type = types.listOf (types.submodule driveOpts); description = "Drives passed to qemu."; + apply = addDeviceNames; }; diskInterface = @@ -552,12 +568,14 @@ in virtualisation.qemu.drives = mkMerge [ [{ + name = "root"; file = "$NIX_DISK_IMAGE"; driveExtraOpts.cache = "writeback"; driveExtraOpts.werror = "report"; }] (mkIf cfg.useBootLoader [ { + name = "boot"; file = "$TMPDIR/disk.img"; driveExtraOpts.media = "disk"; deviceExtraOpts.bootindex = "1"; @@ -610,7 +628,7 @@ in }; } // optionalAttrs cfg.useBootLoader { "/boot" = - { device = "${driveDeviceName 1}2"; + { device = "${lookupDriveDeviceName "boot" cfg.qemu.drives}2"; fsType = "vfat"; options = [ "ro" ]; noCheck = true; # fsck fails on a r/o filesystem