From c312692abe6441fc8e9ac21f76a607e9f2cfb3f7 Mon Sep 17 00:00:00 2001 From: Sk7Str1pe <159684143+Sk7Str1pe@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:56:32 +0300 Subject: [PATCH 1/3] Create luks-btrfs-raid.nix --- example/luks-btrfs-raid.nix | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 example/luks-btrfs-raid.nix diff --git a/example/luks-btrfs-raid.nix b/example/luks-btrfs-raid.nix new file mode 100644 index 0000000..cbcce8e --- /dev/null +++ b/example/luks-btrfs-raid.nix @@ -0,0 +1,52 @@ +{ + disko.devices = { + disk = { + disk0 = { + type = "disk"; + device = "/dev/sdb"; + content = { + type = "gpt"; + partitions = { + crypted2 = { + name = "crypt_raidp2"; + size = "100%"; + content = { + type = "luks"; + name = "raidp2"; # this is DM name + }; + }; + }; + }; + }; + disk1 = { + type = "disk"; + device = "/dev/sda"; + content = { + type = "gpt"; + partitions = { + crypted1 = { + size = "100%"; + name = "crypt_raidp1"; + content = { + type = "luks"; + name = "raidp1"; + content = { + type = "btrfs"; + extraArgs = [ "-f" "-m raid1 -d raid1" "/dev/mapper/raidp2"]; # raidp2 - DM name of 2nd disk + subvolumes = { + "/" = { + mountpoint = "/mnt/SoftWare"; + mountOptions = [ + "rw" "relatime" "ssd" "discard=async" "space_cache=v2" "subvolid=5" "subvol=/" + ]; + }; + }; + }; + }; + }; + }; + }; + }; + }; + }; +} From ba436edc9dafb10733d00b1da5fcb0415c4d9f46 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Tue, 1 Oct 2024 17:12:32 +0200 Subject: [PATCH 2/3] Test luks-encrypted btrfs raid --- tests/luks-btrfs-raid.nix | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/luks-btrfs-raid.nix diff --git a/tests/luks-btrfs-raid.nix b/tests/luks-btrfs-raid.nix new file mode 100644 index 0000000..6e12749 --- /dev/null +++ b/tests/luks-btrfs-raid.nix @@ -0,0 +1,14 @@ +{ + pkgs ? import { }, + diskoLib ? pkgs.callPackage ../lib { }, +}: +diskoLib.testLib.makeDiskoTest { + inherit pkgs; + name = "luks-btrfs-raid"; + disko-config = ../example/luks-btrfs-raid.nix; + extraTestScript = '' + machine.succeed("cryptsetup isLuks /dev/vda2"); + machine.succeed("cryptsetup isLuks /dev/vdb1"); + machine.succeed("btrfs subvolume list /"); + ''; +} From b9c99485aa0c6fb4021cbb4a7556b832ac6d8c65 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Tue, 1 Oct 2024 17:12:57 +0200 Subject: [PATCH 3/3] Fix and comment luks-encrypted btrfs raid example --- example/luks-btrfs-raid.nix | 92 ++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 33 deletions(-) diff --git a/example/luks-btrfs-raid.nix b/example/luks-btrfs-raid.nix index cbcce8e..13ac197 100644 --- a/example/luks-btrfs-raid.nix +++ b/example/luks-btrfs-raid.nix @@ -1,49 +1,75 @@ { disko.devices = { disk = { - disk0 = { - type = "disk"; - device = "/dev/sdb"; - content = { - type = "gpt"; - partitions = { - crypted2 = { - name = "crypt_raidp2"; - size = "100%"; - content = { - type = "luks"; - name = "raidp2"; # this is DM name - }; - }; - }; - }; - }; + # Devices will be mounted and formatted in alphabetical order, and btrfs can only mount raids + # when all devices are present. So we define an "empty" luks device on the first disk, + # and the actual btrfs raid on the second disk, and the name of these entries matters! disk1 = { type = "disk"; device = "/dev/sda"; content = { type = "gpt"; partitions = { - crypted1 = { + ESP = { + size = "512M"; + type = "EF00"; + content = { + type = "filesystem"; + format = "vfat"; + mountpoint = "/boot"; + mountOptions = [ "umask=0077" ]; + }; + }; + crypt_p1 = { size = "100%"; - name = "crypt_raidp1"; content = { type = "luks"; - name = "raidp1"; - content = { - type = "btrfs"; - extraArgs = [ "-f" "-m raid1 -d raid1" "/dev/mapper/raidp2"]; # raidp2 - DM name of 2nd disk + name = "p1"; # device-mapper name when decrypted + # Remove settings.keyFile if you want to use interactive password entry + settings = { + allowDiscards = true; + keyFile = "/tmp/secret.key"; + }; + }; + }; + }; + }; + }; + disk2 = { + type = "disk"; + device = "/dev/sdb"; + content = { + type = "gpt"; + partitions = { + crypt_p2 = { + size = "100%"; + content = { + type = "luks"; + name = "p2"; + # Remove settings.keyFile if you want to use interactive password entry + settings = { + allowDiscards = true; + keyFile = "/tmp/secret.key"; # Same key for both devices + }; + content = { + type = "btrfs"; + extraArgs = [ + "-d raid1" + "/dev/mapper/p1" # Use decrypted mapped device, same name as defined in disk1 + ]; subvolumes = { - "/" = { - mountpoint = "/mnt/SoftWare"; - mountOptions = [ - "rw" "relatime" "ssd" "discard=async" "space_cache=v2" "subvolid=5" "subvol=/" - ]; - }; - }; - }; - }; - }; + "/root" = { + mountpoint = "/"; + mountOptions = [ + "rw" + "relatime" + "ssd" + ]; + }; + }; + }; + }; + }; }; }; };