Subject: [bug#41143] [PATCH 2/2] mapped-devices: Add 'lvm-device-mapping'

From: tsmish <[email-masked]>
Date: Sat, 9 May 2020 01:22:07 +0000

"vgscan --mknodes" is a bit of a hack. Everyone else relies on udev to
create files in /dev/mapper, but since initrd doesn't have working
udevd, they need to be created this way.
Also, while this code is able to boot from root on LVM, grub in
current configuration can't find required files, This can be fixed by
placing (format port "insmod lvm") in grub configuration builder, but
this is somewhat hacky.

---
 gnu/system/mapped-devices.scm | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/gnu/system/mapped-devices.scm b/gnu/system/mapped-devices.scm
index 3339e509e0..03bc7c782d 100644
--- a/gnu/system/mapped-devices.scm
+++ b/gnu/system/mapped-devices.scm
@@ -34,7 +34,7 @@
   #:autoload   (gnu build linux-modules)
                  (missing-modules)
   #:autoload   (gnu packages cryptsetup) (cryptsetup-static)
-  #:autoload   (gnu packages linux) (mdadm-static)
+  #:autoload   (gnu packages linux) (mdadm-static lvm2-static)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-34)
@@ -59,7 +59,8 @@
             check-device-initrd-modules           ;XXX: needs a better place

             luks-device-mapping
-            raid-device-mapping))
+            raid-device-mapping
+            lvm-device-mapping))

 ;;; Commentary:
 ;;;
@@ -269,4 +270,28 @@ TARGET (e.g., \"/dev/md0\"), using 'mdadm'."
    (open open-raid-device)
    (close close-raid-device)))

+(define (open-lvm-device source target)
+  #~(begin
+       (use-modules
+        (srfi srfi-1)
+        (srfi srfi-26))
+       (and
+        (zero? (system* #$(file-append lvm2-static "/sbin/lvm")
+                        "vgchange" "--activate" "y" #$source))
+        (zero? (system* #$(file-append lvm2-static "/sbin/lvm")
+                         "vgscan" "--mknodes")) ; make /dev/mapper
nodes when in initrd
+        (every file-exists? (map (cut string-append "/dev/mapper/" <>)
+                                 (let ((t '#$target))
+                                   (if (list? t) t (list t))))))))
+
+
+(define (close-lvm-device source target)
+  #~(zero? (system* #$(file-append lvm2-static "/sbin/lvm")
+                    "vgchange" "--activate" "n" #$source)))
+
+(define lvm-device-mapping
+  (mapped-device-kind
+   (open open-lvm-device)
+   (close close-lvm-device)))
+
 ;;; mapped-devices.scm ends here
-- 
2.26.2