Skip to content

Commit

Permalink
Add optional label to volumes
Browse files Browse the repository at this point in the history
This makes volumes trivial to identify on the guest by using
`/dev/disks/by-label`.
  • Loading branch information
ereslibre authored and astro committed Jan 14, 2024
1 parent 8a8b8c6 commit f554d2f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ let
microvm.writableStoreOverlay = "/nix/.rw-store";
microvm.volumes = [ {
image = "nix-store-overlay.img";
label = "nix-store";
mountPoint = config.microvm.writableStoreOverlay;
size = 128;
} ];
Expand Down
1 change: 1 addition & 0 deletions checks/startup-shutdown.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let
};
microvm = {
volumes = [ {
label = "var";
mountPoint = "/var";
image = "var.img";
size = 32;
Expand Down
22 changes: 18 additions & 4 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,35 @@ rec {

createVolumesScript = pkgs: pkgs.lib.concatMapStringsSep "\n" (
{ image
, label
, size ? throw "Specify a size for volume ${image} or use autoCreate = false"
, fsType ? defaultFsType
, autoCreate ? true
, ...
}: nixpkgs-lib.optionalString autoCreate ''
PATH=$PATH:${with pkgs; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
}: pkgs.lib.warnIf
(label != null && !autoCreate) "Volume is not automatically labeled unless autoCreate is true. Volume has to be labeled manually, otherwise it will not be identified"
(let labelOption =
if autoCreate then
(if builtins.elem fsType ["ext2" "ext3" "ext4" "xfs"] then "-L"
else if fsType == "vfat" then "-n"
else (pkgs.lib.warnIf (label != null)
"Will not label volume ${label} with filesystem type ${fsType}. Open an issue on the microvm.nix project to request a fix."
null))
else null;
labelArgument =
if (labelOption != null && label != null) then "${labelOption} '${label}'"
else "";
in (nixpkgs-lib.optionalString autoCreate ''
PATH=$PATH:${with pkgs.buildPackages; lib.makeBinPath [ coreutils util-linux e2fsprogs ]}
if [ ! -e '${image}' ]; then
touch '${image}'
# Mark NOCOW
chattr +C '${image}' || true
fallocate -l${toString size}MiB '${image}'
mkfs.${fsType} '${image}'
mkfs.${fsType} ${pkgs.lib.optionalString (label != null) "-L '${label}'"} '${image}'
fi
'');
''));

buildRunner = import ./runner.nix;

Expand Down
9 changes: 7 additions & 2 deletions nixos-modules/microvm/mounts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ lib.mkIf config.microvm.guest.enable {
};
} (
# Volumes
builtins.foldl' (result: { mountPoint, letter, fsType ? defaultFsType, ... }:
builtins.foldl' (result: { label, mountPoint, letter, fsType ? defaultFsType, ... }:
result // lib.optionalAttrs (mountPoint != null) {
"${mountPoint}" = {
inherit fsType;
device = "/dev/vd${letter}";
# Prioritize identifying a device by label if provided. This
# minimizes the risk of misidentifying a device.
device = if label != null then
"/dev/disk/by-label/${label}"
else
"/dev/vd${letter}";
} // lib.optionalAttrs (mountPoint == config.microvm.writableStoreOverlay) {
neededForBoot = true;
};
Expand Down
5 changes: 5 additions & 0 deletions nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ in
type = str;
description = "Path to disk image on the host";
};
label = mkOption {
type = nullOr str;
default = null;
description = "Label of the volume, if any. Only applicable if autoCreate is true; otherwise labeling of the volume must be done manually";
};
mountPoint = mkOption {
type = nullOr path;
description = "If and where to mount the volume inside the container";
Expand Down

0 comments on commit f554d2f

Please sign in to comment.