From 79eab0e82cb126bf4ac170f44af82479f0895ab5 Mon Sep 17 00:00:00 2001 From: Kira Bruneau Date: Sun, 7 Apr 2024 16:49:23 -0400 Subject: [PATCH] types btrfs: fix format without swap or subvolumes --- example/btrfs-only-root-subvolume.nix | 37 +++++++++++++++++++++ lib/types/btrfs.nix | 48 ++++++++++++++------------- tests/btrfs-only-root-subvolume.nix | 11 ++++++ 3 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 example/btrfs-only-root-subvolume.nix create mode 100644 tests/btrfs-only-root-subvolume.nix diff --git a/example/btrfs-only-root-subvolume.nix b/example/btrfs-only-root-subvolume.nix new file mode 100644 index 0000000..6a91656 --- /dev/null +++ b/example/btrfs-only-root-subvolume.nix @@ -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" ]; + }; + }; + }; + }; + }; + }; + }; +} + diff --git a/lib/types/btrfs.nix b/lib/types/btrfs.nix index 0b32993..4b9fb6a 100644 --- a/lib/types/btrfs.nix +++ b/lib/types/btrfs.nix @@ -120,29 +120,31 @@ in if ! (blkid '${config.device}' -o export | grep -q '^TYPE='); then mkfs.btrfs "${config.device}" ${toString config.extraArgs} fi - if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then - ${lib.optionalString (config.swap != {}) '' - ( - MNTPOINT=$(mktemp -d) - mount ${device} "$MNTPOINT" -o subvol=/ - trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT - ${swapCreate "$MNTPOINT" config.swap} - ) - ''} - ${lib.concatMapStrings (subvol: '' - ( - MNTPOINT=$(mktemp -d) - mount ${config.device} "$MNTPOINT" -o subvol=/ - trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT - SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}" - mkdir -p "$(dirname "$SUBVOL_ABS_PATH")" - if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then - btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs} - fi - ${swapCreate "$SUBVOL_ABS_PATH" subvol.swap} - ) - '') (lib.attrValues config.subvolumes)} - fi + ${lib.optionalString (config.swap != {} || config.subvolumes != {}) '' + if (blkid "${config.device}" -o export | grep -q '^TYPE=btrfs$'); then + ${lib.optionalString (config.swap != {}) '' + ( + MNTPOINT=$(mktemp -d) + mount ${device} "$MNTPOINT" -o subvol=/ + trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT + ${swapCreate "$MNTPOINT" config.swap} + ) + ''} + ${lib.concatMapStrings (subvol: '' + ( + MNTPOINT=$(mktemp -d) + mount ${config.device} "$MNTPOINT" -o subvol=/ + trap 'umount $MNTPOINT; rm -rf $MNTPOINT' EXIT + SUBVOL_ABS_PATH="$MNTPOINT/${subvol.name}" + mkdir -p "$(dirname "$SUBVOL_ABS_PATH")" + if ! btrfs subvolume show "$SUBVOL_ABS_PATH" > /dev/null 2>&1; then + btrfs subvolume create "$SUBVOL_ABS_PATH" ${toString subvol.extraArgs} + fi + ${swapCreate "$SUBVOL_ABS_PATH" subvol.swap} + ) + '') (lib.attrValues config.subvolumes)} + fi + ''} ''; }; _mount = diskoLib.mkMountOption { diff --git a/tests/btrfs-only-root-subvolume.nix b/tests/btrfs-only-root-subvolume.nix new file mode 100644 index 0000000..818aec7 --- /dev/null +++ b/tests/btrfs-only-root-subvolume.nix @@ -0,0 +1,11 @@ +{ pkgs ? import { } +, 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 /"); + ''; +}