Subject: [PATCH] bootloader: grub: Add support for root on lvm.
From: Michael Rohleder <[email-masked]>
To: [email-masked]
Cc: Michael Rohleder <[email-masked]>
Date: Sat, 31 Dec 2022 02:25:11 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

This fixes <https://issues.guix.gnu.org/44877>.

* gnu/bootloader/grub.scm (install-grub)
(install-grub-efi)
(install-grub-efi-removable)
(install-grub-efi32): Add setting GRUB_PRELOAD_MODULES to lvm.
* gnu/tests/install.scm (test-lvm-root-os): New variable.
---
 gnu/bootloader/grub.scm |  9 +++++
 gnu/tests/install.scm   | 81 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/gnu/bootloader/grub.scm b/gnu/bootloader/grub.scm
index ecd44e7f3c..1e2d142452 100644
--- a/gnu/bootloader/grub.scm
+++ b/gnu/bootloader/grub.scm
@@ -9,6 +9,7 @@
 ;;; Copyright © 2020 Stefan <[email-masked]>
 ;;; Copyright © 2022 Karl Hallsby <[email-masked]>
 ;;; Copyright © 2022 Denis 'GNUtoo' Carikli <[email-masked]>
+;;; Copyright © 2022 Michael Rohleder <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -557,6 +558,8 @@ (define install-grub
               ;; Tell 'grub-install' that there might be a LUKS-encrypted
               ;; /boot or root partition.
               (setenv "GRUB_ENABLE_CRYPTODISK" "y")
+              ;; Let us boot from lvm
+              (setenv "GRUB_PRELOAD_MODULES" "lvm")
 
               ;; Hide potentially confusing messages from the user, such as
               ;; "Installing for i386-pc platform."
@@ -629,6 +632,8 @@ (define install-grub-efi
           ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
           ;; root partition.
           (setenv "GRUB_ENABLE_CRYPTODISK" "y")
+          ;; Let us boot from lvm
+          (setenv "GRUB_PRELOAD_MODULES" "lvm")
           (invoke/quiet grub-install "--boot-directory" install-dir
                         "--bootloader-id=Guix"
                         "--efi-directory" target-esp)))))
@@ -652,6 +657,8 @@ (define install-grub-efi-removable
           ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
           ;; root partition.
           (setenv "GRUB_ENABLE_CRYPTODISK" "y")
+          ;; Let us boot from lvm
+          (setenv "GRUB_PRELOAD_MODULES" "lvm")
           (invoke/quiet grub-install "--boot-directory" install-dir
                         "--removable"
                         ;; "--no-nvram"
@@ -675,6 +682,8 @@ (define install-grub-efi32
           ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
           ;; root partition.
           (setenv "GRUB_ENABLE_CRYPTODISK" "y")
+          ;; Let us boot from lvm
+          (setenv "GRUB_PRELOAD_MODULES" "lvm")
           (invoke/quiet grub-install "--boot-directory" install-dir
                         "--bootloader-id=Guix"
 			(cond ((target-x86?) "--target=i386-efi")
diff --git a/gnu/tests/install.scm b/gnu/tests/install.scm
index 4e0e274e66..f9d34c1e28 100644
--- a/gnu/tests/install.scm
+++ b/gnu/tests/install.scm
@@ -5,6 +5,7 @@
 ;;; Copyright © 2020 Danny Milosavljevic <[email-masked]>
 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <[email-masked]>
 ;;; Copyright © 2020, 2021, 2022 Maxim Cournoyer <[email-masked]>
+;;; Copyright © 2022 Michael Rohleder <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -76,6 +77,7 @@ (define-module (gnu tests install)
             %test-jfs-root-os
             %test-f2fs-root-os
             %test-xfs-root-os
+            %test-lvm-root-os
             %test-lvm-separate-home-os
 
             %test-gui-installed-os
@@ -831,7 +833,6 @@ (define %test-encrypted-root-os
 ;;; Separate /home on LVM
 ;;;
 
-;; Since LVM support in guix currently doesn't allow root-on-LVM we use /home on LVM
 (define-os-with-source (%lvm-separate-home-os %lvm-separate-home-os-source)
   (use-modules (gnu) (gnu tests))
 
@@ -913,6 +914,84 @@ (define %test-lvm-separate-home-os
                       `(,@command) "lvm-separate-home-os")))))
 
 
+
+;;;
+;;; LVM root device.
+;;;
+
+(define-os-with-source (%lvm-root-os %lvm-root-os-source)
+  (use-modules (gnu) (gnu tests))
+
+  (operating-system
+    (host-name "root-os-on-lvm")
+    (timezone "Europe/Paris")
+    (locale "en_US.utf8")
+
+    (bootloader (bootloader-configuration
+                 (bootloader grub-bootloader)
+                 (targets (list "/dev/vdb"))))
+    (kernel-arguments '("console=ttyS0"))
+
+    (mapped-devices (list (mapped-device
+                           (source "vg0")
+                           (target "vg0-root")
+                           (type lvm-device-mapping))))
+    (file-systems (cons* (file-system
+                           (device "/dev/mapper/vg0-root")
+                           (mount-point "/")
+                           (type "ext4")
+                           (dependencies mapped-devices))
+                         %base-file-systems))
+    (users %base-user-accounts)
+    (services (cons (service marionette-service-type
+                             (marionette-configuration
+                              (imported-modules '((gnu services herd)
+                                                  (guix combinators)))))
+                    %base-services))))
+
+(define %lvm-root-os-installation-script
+  "\
+. /etc/profile
+set -e -x
+guix --version
+
+export GUIX_BUILD_OPTIONS=--no-grafts
+parted --script /dev/vdb mklabel gpt \\
+  mkpart primary ext2 1M 3M \\
+  mkpart primary 3M 1.7G \\
+  set 1 boot on \\
+  set 1 bios_grub on
+pvcreate /dev/vdb2
+vgcreate vg0 /dev/vdb2
+lvcreate -l 100%FREE -n root vg0
+vgchange -ay
+mkfs.ext4 -L root-fs /dev/mapper/vg0-root
+mount /dev/mapper/vg0-root /mnt
+herd start cow-store /mnt
+mkdir /mnt/etc
+cp /etc/target-config.scm /mnt/etc/config.scm
+guix system init /mnt/etc/config.scm /mnt --no-substitutes
+sync
+reboot\n")
+
+(define %test-lvm-root-os
+  (system-test
+   (name "lvm-root-os")
+   (description
+    "Test functionality of an OS installed with a LVM / partition")
+   (value
+    (mlet* %store-monad ((image   (run-install %lvm-root-os
+                                               %lvm-root-os-source
+                                               #:script
+                                               %lvm-root-os-installation-script
+                                               #:packages (list lvm2-static)))
+                         (command (qemu-command* image
+                                                 #:memory-size 512)))
+      (run-basic-test %lvm-root-os
+                      `(,@command) "lvm-root-os")))))
+
+
+
 ;;;
 ;;; LUKS-encrypted /home, unencrypted root.
 ;;;
-- 
2.38.1