types btrfs: fix format without swap or subvolumes

This commit is contained in:
Kira Bruneau 2024-04-07 16:49:23 -04:00 committed by mergify[bot]
parent 9e0bf2fe7f
commit 79eab0e82c
3 changed files with 73 additions and 23 deletions

View file

@ -0,0 +1,37 @@
{
disko.devices = {
disk = {
vdb = {
type = "disk";
device = "/dev/disk/by-diskseq/1";
content = {
type = "gpt";
partitions = {
ESP = {
priority = 1;
name = "ESP";
start = "1M";
end = "128M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
size = "100%";
content = {
type = "btrfs";
extraArgs = [ "-f" ]; # Override existing partition
mountpoint = "/";
mountOptions = [ "compress=zstd" "noatime" ];
};
};
};
};
};
};
};
}

View file

@ -120,29 +120,31 @@ in
if ! (blkid '${config.device}' -o export | grep -q '^TYPE='); then if ! (blkid '${config.device}' -o export | grep -q '^TYPE='); then
mkfs.btrfs "${config.device}" ${toString config.extraArgs} mkfs.btrfs "${config.device}" ${toString config.extraArgs}
fi fi
if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then ${lib.optionalString (config.swap != {} || config.subvolumes != {}) ''
${lib.optionalString (config.swap != {}) '' if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then
( ${lib.optionalString (config.swap != {}) ''
MNTPOINT=$(mktemp -d) (
mount ${device} "$MNTPOINT" -o subvol=/ MNTPOINT=$(mktemp -d)
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT mount ${device} "$MNTPOINT" -o subvol=/
${swapCreate "$MNTPOINT" config.swap} trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
) ${swapCreate "$MNTPOINT" config.swap}
''} )
${lib.concatMapStrings (subvol: '' ''}
( ${lib.concatMapStrings (subvol: ''
MNTPOINT=$(mktemp -d) (
mount ${config.device} "$MNTPOINT" -o subvol=/ MNTPOINT=$(mktemp -d)
trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT mount ${config.device} "$MNTPOINT" -o subvol=/
SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}" trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT
mkdir -p "$(dirname "$SUBVOL_ABS_PATH")" SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}"
if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then mkdir -p "$(dirname "$SUBVOL_ABS_PATH")"
btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs} if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then
fi btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs}
${swapCreate "$SUBVOL_ABS_PATH" subvol.swap} fi
) ${swapCreate "$SUBVOL_ABS_PATH" subvol.swap}
'') (lib.attrValues config.subvolumes)} )
fi '') (lib.attrValues config.subvolumes)}
fi
''}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {

View file

@ -0,0 +1,11 @@
{ pkgs ? import <nixpkgs> { }
, diskoLib ? pkgs.callPackage ../lib { }
}:
diskoLib.testLib.makeDiskoTest {
inherit pkgs;
name = "btrfs-only-root-subvolume";
disko-config = ../example/btrfs-only-root-subvolume.nix;
extraTestScript = ''
machine.succeed("btrfs subvolume list /");
'';
}