3

I created a multi-boot USB stick to load various ISO files, which can either be live systems or used to install different Linux distributions.

sda -sda1 1M | BIOS Boot partition to store MBR -sda2 64M | ESP for UEFI boot -sda3 100G | EXT4 for ISO & IMAGE 

I can easily start ISO files such as Debian, Ubuntu, Kali or other distributions from it, using the corresponding menuentry in the

/efi/boot/grub/grub.cfg

Now I have an disk-image created with dd from a whole USB stick where Debian is installed.

However, it's not a true ISO or image but rather a disk-image (snapshot/backup) of the USB stick with all its partitions, with the named example debian1.iso or debian2.img

I've now copied this file to the partition sda3 with the other correct ISO's.

I created the following menuentry with chainloader, but on the first attempt, GRUB simply restarts the boot process, and on the second attempt, I get an 'invalid signature' error.

menuentry "Chainload GRUB from USB Image debian1.iso" { loopback loop (hd0,3)/debian1.iso set root=(loop,1) chainloader +1 } menuentry "Boot Debian from ISO on USB (UEFI)" { set root=(hd0,3) loopback loop /debian2.img set root=(loop,1) chainloader /EFI/debian/grubx64.efi } 

How can I start this disk-image from GRUB and what does the menu entry look like?

It might be possible if the disk-image is written to a separate partition, but I don’t want that.

Here are entries I found, but they mostly deal with ISO files.

At first, I found that it wasn’t possible, but then I discovered that it might be possible with memdisk or GRUB2 using loopback and overlayfs.

So, it is indeed possible.

Memdisk

Memdisk is primarily intended for temporary boot environments, such as starting installation programs or diagnostic tools where permanent storage is not required or desired.

It is very useful if you just want to test the disk image.

loopback and overlayfs

You can boot a disk image with GRUB’s loopback function and make the filesystem writable by combining it with overlayfs. An overlay layer is placed on top of the read-only image, writing changes to a separate partition or file. This way, the original image remains unchanged, and you can save modifications.

I need to figure out how to handle the changes if it works.

My GRUB is installed on a USB stick without an operating system, so I can't use grub-mkconfig.

So I have to make all the changes manually.

Is it possible to boot a disk-image via GRUB that is writable, make changes, and have those changes written directly to the image and how?

Is there another way besides memdisk to boot where I can make changes?

If it works with loopback and overlayfs, what do I need to consider, how do I set it up, and what does the menuentry looks like?

Is there some kind of small hack, or should I load additional files onto the GRUB stick to make this work and how?

The disk image itself also has GRUB, and the operating system on it is encrypted with LUKS.

Is it maybe possible to unpack the image into memory with GRUB, call GRUB from the disk image, unlock LUKS, boot the OS, make the changes, and save the whole thing as a new disk image to some partition?

4
  • 2
    You have to keep in mind, that any shenanigans you do in GRUB, you have to replicate when you actually boot kernel + initramfs. Even for Linux Live ISOs, it only works for those that do the loop-mounting in their initrds. And you can do that with any bootloader, if you extract the kernel+initrd beforehand (if they can't grab it from within the ISO like GRUB does). Commented Sep 4, 2024 at 9:29
  • 2
    Basically your disk image is the one that needs to provide the functionality, more so than GRUB. If you want it to work out of the box, then create partitions (one for every partition in your disk image, since partitions inside partitions also don't work - another alternative might be LVM). That usually works since "mount by uuid" is possible then, regardless of device name or partition number; it is the most direct solution. Commented Sep 4, 2024 at 9:36
  • 2
    Otherwise it's more or less not possible, since GRUB does not emulate block devices for the kernel or anything. Commented Sep 4, 2024 at 9:38
  • I know a way to boot a a disk image in read/write mode, but it requires to add a specific and modified initrd in the disk image. I don't know if it can work with LUKS encrypted filesystems, I have no knowledge of disk encryption. If you are interested, I can post the method in an answer. Commented Sep 8, 2024 at 6:20

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.