Linux live OS determine current boot media(CD/USB) which is used to boot the kernel and rootfs residing on the same media

1

Is there a way to detect the current boot media used to boot a Linux Live OS? In a live OS system, I would like to uniquely determine the current boot media(CD/USB) that has been used to boot the disk. I would like to determine this at the OS level not at the bootloader level.

Current boot media in my case means the media where the boot loader that was used to start the OS resides.

I have inspected the following but none seem to have the information of the dev path of current boot media (like for instance /dev/sr0 indicating current boot path is cd)

  • Checking systemd journalctl logs.
  • Checking dmesg logs.
  • Possible interfaces in udev and udevadm utility.
  • Busybox rdev like suggested in http://free-electrons.com/blog/find-root-device/. The procedure to compare stat / with that of major:minor device number of /dev/sr0 doesn't seem to match.

I also checked in GRUB2, http://git.savannah.gnu.org/cgit/grub.git/tree/grub-core/kern/i386/pc/init.c where the GRUB environment variables root and prefix are set. For instance, on a GRUB2 shell that was booted from a CD, if I type echo $root, I get hd31. Now how do I do a mapping of this to a Linux block device path like /dev/sr0 for CD or some /dev/sdb1 for a pen drive? I also checked Linux: get the current boot disk's device name and https://stackoverflow.com/questions/6787819/how-to-determine-the-booting-device but both of it don't seem to help.

Ideally I would want to avoid mounting every CD/USB (/dev/sr0 or /dev/sdb1 and the like) to manually check for the presence of initrd and kernel files. This method isn't foolproof. Is there any straightforward way to do this ?

1337ninja

Posted 2015-09-30T07:34:22.527

Reputation: 29

Please [edit] to clarify "boot media" in the context of this question. Is it the media on which the boot manager that was used to bootstrap the system is installed? Is it the media that the running kernel binary is stored on? (In principle, then, what if the running kernel's binary has been deleted? That's valid.) Is it the media on which the originally mounted root file system resides? Is it the media on which the currently mounted root file system resides? Is it the media on which the /boot directory resides? Or what? All of these could be valid answers to "which boot media was used?". – a CVn – 2015-09-30T08:15:25.303

For the moment, I have voted to put this question on hold as unclear, simply because "current boot media" can have so many different meanings and the answers are likely to be different for each variant. If you edit your question to clarify exactly which portion of the boot process you are interested in, this should be quite answerable and (if put on hold in the interim) get reopened fairly quickly. – a CVn – 2015-09-30T08:16:58.553

@MichaelKjörling: I have the bootloader, initrd and kernel on the media, CD or USB. After I select the boot media at BIOS, GRUB kicks in and boots the kernel and the initrd is extracted to the Ramdisk. I simply want to mount the block device path of the CD/USB that corresponds to the one I selected at the BIOS. – 1337ninja – 2015-09-30T08:27:01.977

"I simply want to mount the block device path of the CD/USB that corresponds to the one I selected at the BIOS." So you want the device that corresponds to the boot loader's location? (GRUB, in your case, but not guaranteed in the general case even for Linux.) – a CVn – 2015-09-30T08:29:10.910

@MichaelKjörling: Yes. In my particular case all happens to be on the media, GRUB, initrd and the kernel. – 1337ninja – 2015-09-30T08:59:30.047

Not possible, which is why most Live Media have a magic file (of sorts) to help find it. – Daniel B – 2015-12-10T21:05:49.763

Answers

0

I found the following but am not sure what would set it. Maybe the kernel/grub, in which case you should be set.

"It could also be the device node reference for the mount point of another LiveOS filesystem, including the currently-running one (such as a booted Live CD/DVD/USB, where /run/initramfs/livedev links to the booted LiveOS device)."

From https://www.mankier.com/8/livecd-iso-to-disk

Harrison

Posted 2015-09-30T07:34:22.527

Reputation: 1

0

The Linux kernel itself doesn't care where it's loaded from - and it considers that the bootloader's job. Ideally the bootloader would record this information somewhere where the kernel can get it later - which is trivial with the U-Boot bootloader, but not so much x86 BIOS/UEFI.

As a result, I don't really think you can get this info from Linux itself, though where the root filesystem lives is probably where Linux was loaded from most of the time. It is 100% possible to load the kernel from one place and have the root filesystem come from another place, which is common on embedded platforms.

So some things you can try.

  • cat /proc/cmdline will probably tell you where the root file system was loaded - the root= kernel command line parameter tells you where the kernel was told to find the root filesystem when it was loaded.

  • You can also issue a mount command and find the / filesystem, and look at its device.

LawrenceC

Posted 2015-09-30T07:34:22.527

Reputation: 63 487