From af2946358849ac01ee3a40b79f74c3cc989d6640 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 22 Aug 2022 16:56:44 +0200 Subject: [PATCH 1/3] use /dev/disk/by-path for better nvme support --- lib/default.nix | 56 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 66e667a..62aed38 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -9,6 +9,31 @@ let { body.mount = mount-f {}; + helper.find-device = device: let + environment = helper.device-id device; + in '' + if echo '${device}' | grep -q '^/dev/disk'; then + # DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path + ${environment}='${device}' + else + ${environment}=$(for x in /dev/disk/by-path/*; do + dev=$x + if [ "$(readlink -f $x)" = '${device}' ]; then + target=$dev + break + fi + done + if test -z $target; then + echo 'unable to find path of disk: ${device}, bailing out' >&2 + exit 1 + else + echo $target + fi) + fi + ''; + + helper.device-id = device: "DEVICE${builtins.substring 0 5 (builtins.hashString "sha1" device)}"; + config-f = q: x: config.${x.type} q x; config.filesystem = q: x: { @@ -70,19 +95,24 @@ let { create.noop = q: x: ""; - create.partition = q: x: '' - parted -s ${q.device} mkpart ${x.part-type} ${x.fs-type or ""} ${x.start} ${x.end} + create.partition = q: x: let + env = helper.device-id q.device; + in '' + parted -s "''${${env}}" mkpart ${x.part-type} ${x.fs-type or ""} ${x.start} ${x.end} + # ensure /dev/disk/by-path/..-partN exists before continuing + udevadm trigger --subsystem-match=block; udevadm settle ${optionalString (x.bootable or false) '' - parted -s ${q.device} set ${toString q.index} boot on + parted -s "''${${env}}" set ${toString q.index} boot on ''} ${concatMapStringsSep "" (flag: '' - parted -s ${q.device} set ${toString q.index} ${flag} on + parted -s "''${${env}}" set ${toString q.index} ${flag} on '') (x.flags or [])} - ${create-f { device = q.device + toString q.index; } x.content} + ${create-f { device = "\"\${${env}}\"-part" + toString q.index; } x.content} ''; create.table = q: x: '' - parted -s ${q.device} mklabel ${x.format} + ${helper.find-device q.device} + parted -s "''${${helper.device-id q.device}}" mklabel ${x.format} ${concatStrings (imap (index: create-f (q // { inherit index; })) x.partitions)} ''; @@ -91,8 +121,8 @@ let { mount.filesystem = q: x: { fs.${x.mountpoint} = '' - if ! findmnt "${q.device}" "/mnt${x.mountpoint}" > /dev/null 2>&1; then - mount "${q.device}" "/mnt${x.mountpoint}" -o X-mount.mkdir + if ! findmnt ${q.device} "/mnt${x.mountpoint}" > /dev/null 2>&1; then + mount ${q.device} "/mnt${x.mountpoint}" -o X-mount.mkdir fi ''; }; @@ -102,6 +132,7 @@ let { # attrValues returns values sorted by name. This is important, because it # ensures that "/" is processed before "/foo" etc. in '' + ${optionalString (hasAttr "table" z) (concatStringsSep "\n" (attrValues z.table))} ${optionalString (hasAttr "luks" z) (concatStringsSep "\n" (attrValues z.luks))} ${optionalString (hasAttr "lvm" z) (concatStringsSep "\n" (attrValues z.lvm))} ${optionalString (hasAttr "fs" z) (concatStringsSep "\n" (attrValues z.fs))} @@ -129,8 +160,11 @@ let { mount.noop = q: x: {}; mount.partition = q: x: - mount-f { device = q.device + toString q.index; } x.content; + mount-f { device = "\"\${${helper.device-id q.device}}\"-part" + toString q.index; } x.content; - mount.table = q: x: - foldl' recursiveUpdate {} (imap (index: mount-f (q // { inherit index; })) x.partitions); + mount.table = q: x: ( + recursiveUpdate + (foldl' recursiveUpdate {} (imap (index: mount-f (q // { inherit index; device = helper.device-id q.device; })) x.partitions)) + {table.${q.device} = helper.find-device q.device;} + ); } From e9d1f151a2582d5a3084311414eceb768edcbd2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 23 Aug 2022 16:22:36 +0200 Subject: [PATCH 2/3] use nix at eval time rather grep at runtime --- lib/default.nix | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 62aed38..9f62428 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -11,11 +11,11 @@ let { helper.find-device = device: let environment = helper.device-id device; - in '' - if echo '${device}' | grep -q '^/dev/disk'; then - # DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path - ${environment}='${device}' - else + in + # DEVICE points already to /dev/disk, so we don't handle it via /dev/disk/by-path + if hasPrefix "/dev/disk" device then + "${environment}='${device}'" + else '' ${environment}=$(for x in /dev/disk/by-path/*; do dev=$x if [ "$(readlink -f $x)" = '${device}' ]; then @@ -29,8 +29,7 @@ let { else echo $target fi) - fi - ''; + ''; helper.device-id = device: "DEVICE${builtins.substring 0 5 (builtins.hashString "sha1" device)}"; From 5643130fbbb79075ed152f89cbb01ff03080d136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 23 Aug 2022 20:08:56 +0200 Subject: [PATCH 3/3] fix mount.partition by non rehashing already hashed device-id --- lib/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 9f62428..f2908f5 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -159,7 +159,7 @@ let { mount.noop = q: x: {}; mount.partition = q: x: - mount-f { device = "\"\${${helper.device-id q.device}}\"-part" + toString q.index; } x.content; + mount-f { device = "\"\${${q.device}}\"-part" + toString q.index; } x.content; mount.table = q: x: ( recursiveUpdate