0

I wish to boot my system from a LVM partition hosted by a loopback file, where the boot is on a USB drive.

Using a non LVM setup, with an ext4 loopback file, it boot with not problems (the are serveral guides on the net), but I need more... I need LVM!

My partitions layout is:

  • USB drive (MBR): Grub 2
  • USB drive (hd0,msdos1): ext4 512Mb /boot
  • Notebook internal disk: hosts .linux-loops/system0.lvm file where I have installed the OS and mapped it as /dev/mapper/vg_system-lv_root.

Here is my Grub 2 menu entry:

set BOOT_PART=(hd0,msdos1)
set HOST_PART=(hd1,gpt5)
set LOOP_FILE=.linux-loops/system0.lvm
set LOOP_DEV=loop0
set LVM_VG=vg_system
set LVM_LV=lv_root
set ROOT_DEV=/dev/mapper/${LVM_VG}-${LVM_LV}

menuentry 'Loopback / 4.10.0-38-generic' --class ubuntu --class gnu-linux --class gnu --class os {
    echo "Initializing environment..."

    set KERN_VER=4.10.0-38-generic

    recordfail  load_video  gfxmode $linux_gfx_mode

    echo "Loading partition drivers..."

    insmod ext2
    insmod gzio
    insmod part_msdos
    insmod part_gpt

    echo Loopback(s) setup...

    loopback ${LOOP_DEV} ${HOST_PART}/${LOOP_FILE}
    insmod lvm

    echo Debug LVM...
    ls
    ls (lvm/${LVM_VG}/${LVM_LV)/

    echo "Loading kernel..."

    set root=${BOOT_PART}

    linux   /vmlinuz-${KERN_VER} root=${ROOT_DEV} rw verbose nosplash debug
    initrd /initrd.img-${KERN_VER}

}

The two ls debug commands show me that the LVM partition has been seen by grub, but during initrd running, the system shows me that the group vg_system is not found and lvmetad is not yet running.

I think that the loopback setup inside the grub script cannot be propagated to the init script, so it cannot initialize again the LVM mapper.

In the non LVM scenario (mentioned above) the vmlinuz ... loop=... solves the problem.

The question is: how to setup the loopback files in order to get them available to LVM mapper by grub?

Thank you so much!

1

Your thinking is correct: GRUB cannot do the set-up on behalf of the kernel.

GRUB relies on system firmware (either BIOS or UEFI) for disk access, so anything it does is based on those firmware routines. And as soon as the kernel takes over, those firmware routines aren't used any more... and anything that would build on top of them also becomes unusable, until the OS provides its own drivers for the same job.

(In the case of BIOS, the 16-bit BIOS disk access routines become unusable as the kernel transitions the processor to full 32/64-bit mode. In the case of UEFI, I think disk access services are one of the UEFI features that become unusable as the standard "terminate boot services" UEFI call is executed, completing the UEFI side of the hand-over of hardware control to the OS.)

Instead, you'll need to set up a script to be executed within initrd in a very early phase: it should activate the necessary loopback device before LVM starts up, or alternatively perform a vgscan --mknodes and/or vgchange -ay <name of the VG containing the root filesystem> after setting up the loop device if LVM is already initialized.

Without knowing the name and version of the Linux distribution you're using, it's hard to give any more specific advice.

  • Thak you for your support. I am using Linux Mint 18.3 (aka Ubuntu 16.04 LTS). How can I update INITRD scripts if I cannot yet boot the system? – Antonio Petricca Mar 9 '18 at 15:46
  • Boot to recovery mode from installation media, or use a Live-CD/DVD to gain access to the system. – telcoM Mar 9 '18 at 15:52
  • Sure, I know how to do this, but the flow is a bit different. On a running system I add/edit scripts inside /etc/initramfs-tools. Then I run update update-initramfs to generate the new initrd. By a live CD I can mount then LVM disk and the USB key. In this scenario, how could I update the initrd image? – Antonio Petricca Mar 9 '18 at 16:00
  • I think a solution to update the initramfs is by the chroot command. I will give it a try asap. ;) – Antonio Petricca Mar 9 '18 at 16:12
0

I have posted my solution at GitHub:

This guide will give you detailed instructions on how to install linux on a LVM loopback disk booting from a USB drive (grub and boot partition) without have to change your PC internal disk boot sector.

The relevant part have been implemented by the https://github.com/antonio-petricca/buddy-linux/blob/master/assets/initramfs/lvm-loops-setup script:

#!/bin/sh -e

# Antonio Petricca <antonio.petricca@gmail.com> - 17/03/2018

PREREQS=""

# Output pre-requisites

prereqs() {
    echo "$PREREQ" }

case "$1" in
    prereqs)
        prereqs
        exit 0
    ;; esac

. /scripts/functions

MNT=/host ROOT_MNT=${rootmnt}${MNT}

_log_msg "Moving LVM loops host device mount point from \"${MNT}\" to {ROOT_MNT}\"...\n"

mount -o remount,rw ${rootmnt} || panic "Cannot remount \"${rootmnt}\" R/W."

[ -d ${ROOT_MNT} ] || mkdir -p ${ROOT_MNT} || panic "Cannot create \"${ROOT_MNT}\" mount point."

mount -n -o move ${MNT} ${ROOT_MNT} || panic "Cannot move \"${MNT}\" to \"${ROOT_MNT}\"."

log_end_msg "Done"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.