Loopback.cfg

From Super Grub Disk Wiki
Jump to navigationJump to search

What is a loopback.cfg?

A loopback.cfg is basically just a grub.cfg that's designed to be used to boot a live distribution from an iso file on a filesystem rather than an actual physical CD.

What problem is it trying to solve?

First some background:

Most GNU/Linux distributions' LiveCDs support being booted with the iso file on a hard drive, without needing to actually burn the image to a CD. This works not by "chainloading" the iso (which is impossible) but by extracting the kernel from the iso, either before hand or at boot time with grub2's "loopback" command. The kernel is then passed a parameter that tells it that it needs to look for its root not in /dev/cdrom, but by loop mounting an iso file. This parameter also tells the kernel (more precisely the initrd scripts) what path the iso can be found at.

The problem is that to actually boot such a distribution you need to know the path to the kernel / initrd within the iso, the particular kernel parameter that distribution uses to specify the path to the iso, and any other kernel parameters relevant to the distribution. Even knowing that, when you boot the iso you don't get the menu and options you'd get when booting the CDROM. With a loopback.cfg you get the full menu of options at boot and don't have to worry about differences between different distributions.

How do you use a loopback.cfg to boot an iso?

To boot an iso using its loopback.cfg you need to be using grub2. Below is a template for a menu entry that should work with any iso that has a loopback.cfg. Replace TITLE with the title you want for the menu entry and PATH with the path to the iso file (not including device name) for example: "/boot-isos/foo.iso". This entry should be added to your /boot/grub/grub.cfg or if your grub.cfg is being automatically generated using grub-mkconfig you should add it to /boot/grub/custom.cfg (creating the file if it does not already exist).

menuentry "TITLE" {
  iso_path=PATH
  export iso_path
  search --set=root --file $iso_path
  loopback loop $iso_path
  root=(loop)
  configfile /boot/grub/loopback.cfg
  loopback --delete loop
}

But the great thing about having a loopback.cfg is that it makes automating this process easy. With the isodetect.lua script that is part of Super GRUB2 Disk any isos in /boot-isos or /boot/boot-isos (Jordan_U agreed that the next version will also search in /bootisos and /boot/bootisos for ISO 9660 8.3 (8 without dot for directories) and FAT file name compatibility) that contain a loopback.cfg (and some that don't) will be detected at boot and a menu generated to choose between them. All you have to do to add another loop bootable iso is to put it in /boot-isos/. isodetect.lua can be used outside super grub disk, as can all of the super grub disk scripts, though it requires grub to be compiled with lua support from grub-extras.

How do you create a loopback.cfg?

As far as grub is concerned a loopback.cfg is a regular grub.cfg. What makes it useful is that a grub.cfg can use variables, like $iso_path, and can run other grub.cfg files with "configfile". Whatever grub.cfg script uses configfile to load the loopback.cfg will first set $root to the loopback device then set and export the variable $iso_path, so that it can be used in menu entries to pass that path to the kernel however the distribution's initrd scripts expect it. For example, this entry could be used in a loopback.cfg to boot Ubuntu 10.04, notice that since $root is already set to the loop device before the loopback.cfg is started paths are relative to the root of the iso filesystem:

menuentry "Ubuntu 10.04 loopback" {
  linux  /casper/vmlinuz boot=casper iso-scan/filename=$iso_path quiet splash --
  initrd /casper/initrd.lz 
}

The loopback.cfg file must be found at the path /boot/grub/loopback.cfg.

Grml was the first distribution to include a loopback.cfg. The grml developers already shipped a regular grub.cfg on their isos and didn't want to maintain two files. Instead they modified their grub.cfg so it could work for both purposes, regular and loop booting. When booting normally, $iso_path is not set and no extra kernel parameter is added. When loop booting, $iso_path is set (by the calling script) and findiso=${iso_path} is added to the list of kernel parameters. You can see the relatively simple commit/diff that made their normal grub.cfg usable as a loopback.cfg here.

So that scripts like isodetect.lua, which look for a loopback.cfg, would work they added a /boot/grub/loopback.cfg with only this for the contents:

source /boot/grub/grub.cfg

What distributions are shipping with one?

  • Grml
  • MirBSD, but this can only boot the installer (and some extras like memtest86+, FreeDOS, gPXE and grml-mir which are on some ISOs), not the MirBSD live system
  • Ubuntu