add btrfs subvolumes

This commit is contained in:
lassulus 2022-08-25 21:46:17 +02:00
parent dadc491330
commit 81e704b638
3 changed files with 80 additions and 0 deletions

View file

@ -69,6 +69,18 @@ let
create-f = q: x: create.${x.type} q x;
create.btrfs = q: x: ''
mkfs.btrfs ${q.device}
${lib.optionalString (!isNull x.subvolumes or null) ''
MNTPOINT=$(mktemp -d)
(
mount ${q.device} "$MNTPOINT"
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
${concatMapStringsSep "\n" (subvolume: "btrfs subvolume create \"$MNTPOINT\"/${subvolume}") x.subvolumes}
)
''}
'';
create.filesystem = q: x: ''
mkfs.${x.format} ${q.device}
'';
@ -178,6 +190,8 @@ let
'';
};
mount.btrfs = mount.filesystem;
mount.devices = q: x: let
z = foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { device = "/dev/${name}"; inherit name; }) x.content);
# attrValues returns values sorted by name. This is important, because it

View file

@ -0,0 +1,26 @@
{
type = "devices";
content = {
vdb = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
part-type = "primary";
start = "0%";
end = "100%";
content = {
type = "btrfs";
mountpoint = "/";
subvolumes = [
"/home"
"/test"
];
};
}
];
};
};
}

View file

@ -0,0 +1,40 @@
{ makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, pkgs ? (import <nixpkgs> {})
}:
let
makeTest' = args:
makeTest args {
inherit pkgs;
inherit (pkgs) system;
};
disko-config = import ../example/btrfs-subvolumes.nix;
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config);
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config);
in makeTest' {
name = "disko";
nodes.machine =
{ config, pkgs, modulesPath, ... }:
{
imports = [
(modulesPath + "/profiles/installation-device.nix")
(modulesPath + "/profiles/base.nix")
];
# speed-up eval
documentation.enable = false;
virtualisation.emptyDiskImages = [ 512 ];
};
testScript = ''
machine.succeed("echo 'secret' > /tmp/secret.key");
machine.succeed("${tsp-create}");
machine.succeed("${tsp-mount}");
machine.succeed("${tsp-mount}"); # verify that the command is idempotent
machine.succeed("test -e /mnt/test");
machine.succeed("btrfs subvolume list /mnt | grep -qs 'path test$'");
'';
}