Skip to content

Commit f554d2f

Browse files
ereslibreastro
authored andcommitted
Add optional label to volumes
This makes volumes trivial to identify on the guest by using `/dev/disks/by-label`.
1 parent 8a8b8c6 commit f554d2f

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

checks/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ let
6262
microvm.writableStoreOverlay = "/nix/.rw-store";
6363
microvm.volumes = [ {
6464
image = "nix-store-overlay.img";
65+
label = "nix-store";
6566
mountPoint = config.microvm.writableStoreOverlay;
6667
size = 128;
6768
} ];

checks/startup-shutdown.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let
1515
};
1616
microvm = {
1717
volumes = [ {
18+
label = "var";
1819
mountPoint = "/var";
1920
image = "var.img";
2021
size = 32;

lib/default.nix

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,35 @@ rec {
3030

3131
createVolumesScript = pkgs: pkgs.lib.concatMapStringsSep "\n" (
3232
{ image
33+
, label
3334
, size ? throw "Specify a size for volume ${image} or use autoCreate = false"
3435
, fsType ? defaultFsType
3536
, autoCreate ? true
3637
, ...
37-
}: nixpkgs-lib.optionalString autoCreate ''
38-
PATH=$PATH:${with pkgs; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
38+
}: pkgs.lib.warnIf
39+
(label != null && !autoCreate) "Volume is not automatically labeled unless autoCreate is true. Volume has to be labeled manually, otherwise it will not be identified"
40+
(let labelOption =
41+
if autoCreate then
42+
(if builtins.elem fsType ["ext2" "ext3" "ext4" "xfs"] then "-L"
43+
else if fsType == "vfat" then "-n"
44+
else (pkgs.lib.warnIf (label != null)
45+
"Will not label volume ${label} with filesystem type ${fsType}. Open an issue on the microvm.nix project to request a fix."
46+
null))
47+
else null;
48+
labelArgument =
49+
if (labelOption != null && label != null) then "${labelOption} '${label}'"
50+
else "";
51+
in (nixpkgs-lib.optionalString autoCreate ''
52+
PATH=$PATH:${with pkgs.buildPackages; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
3953
4054
if [ ! -e '${image}' ]; then
4155
touch '${image}'
4256
# Mark NOCOW
4357
chattr +C '${image}' || true
4458
fallocate -l${toString size}MiB '${image}'
45-
mkfs.${fsType} '${image}'
59+
mkfs.${fsType} ${pkgs.lib.optionalString (label != null) "-L '${label}'"} '${image}'
4660
fi
47-
'');
61+
''));
4862

4963
buildRunner = import ./runner.nix;
5064

nixos-modules/microvm/mounts.nix

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@ lib.mkIf config.microvm.guest.enable {
9494
};
9595
} (
9696
# Volumes
97-
builtins.foldl' (result: { mountPoint, letter, fsType ? defaultFsType, ... }:
97+
builtins.foldl' (result: { label, mountPoint, letter, fsType ? defaultFsType, ... }:
9898
result // lib.optionalAttrs (mountPoint != null) {
9999
"${mountPoint}" = {
100100
inherit fsType;
101-
device = "/dev/vd${letter}";
101+
# Prioritize identifying a device by label if provided. This
102+
# minimizes the risk of misidentifying a device.
103+
device = if label != null then
104+
"/dev/disk/by-label/${label}"
105+
else
106+
"/dev/vd${letter}";
102107
} // lib.optionalAttrs (mountPoint == config.microvm.writableStoreOverlay) {
103108
neededForBoot = true;
104109
};

nixos-modules/microvm/options.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ in
180180
type = str;
181181
description = "Path to disk image on the host";
182182
};
183+
label = mkOption {
184+
type = nullOr str;
185+
default = null;
186+
description = "Label of the volume, if any. Only applicable if autoCreate is true; otherwise labeling of the volume must be done manually";
187+
};
183188
mountPoint = mkOption {
184189
type = nullOr path;
185190
description = "If and where to mount the volume inside the container";

0 commit comments

Comments
 (0)