Ubuntu – How to boot an ISO file from the drive using grub2 on UEFI machines

bootgrub2isolive-usbuefi

I have a bunch of live ISO files like GParted, CloneZilla, Boot-Repair, Ubuntu 14.04 32 bit, … downloaded in /opt and on my old BIOS machine, I used to be able to just add them to the grub menu and boot them. 🙂

I now have an UEFI machine and all the information I can find are for BIOS machines.

What are the steps to boot these ISO files from grub?

I'm just I'm sick and tired of "burning" these to a USB stick all the time as I can never find the stick I need whereas my computer itself if much harder to loose in the total chaos around me here…) 🙁

Best Answer

  • There is a bug in grub 2.04 so ensure you have a prior or later version.

    Well, the basics for adding an ISO file to grub are the same for an UEFI as for a BIOS machine: edit /etc/grub.d/40_custom and add a menuentry item (GParted is used in this example) to the bottom of the file:

    menuentry "GParted Live ISO" {
    }
    

    Now we're going to add a variable containing the directory where we stored the ISO (so far, so good: no differences with BIOS machines):

    menuentry "GParted Live ISO" {
      set GPartedISOFile="/opt/Live-ISOs/gparted-live-0.31.0-1-amd64.iso"
    }
    

    I'm using /opt to store these as I don't like creating directories in the root of my machine and according to the Linux File System Hierarchy that's where optional software should reside anyway.

    Before we add the loopback variable, we need to find out on which hard disk the file is stored, so we do a: df --output=source /opt/Live-ISOs/gparted-live-0.31.0-1-amd64.iso | tail -1 and the output on my machine is: /dev/sdb2.

    However grub uses (hdX,Y) notation and this is where the difference between UEFI and BIOS machines comes in! So now reboot your machine, go into the grub menu and press C: This will bring you to the grub command prompt with different commands than you're used to but the only one that you need is: ls.

    On my machine the output is:

    (hd0) (hd1) (hd1,gpt3) (hd1,gpt2) (hd1,gpt1) (hd2) ... (hd3) ...
    

    Huh? 4 drives? I only have 3! And it's not (hd1,4) line on a BIOS but (hd1,gpt3) in UEFI and (hd0) has no partitions at all!

    Well, apparently when part of the NVRAM is used as storage and shows up as (hd0) you need to start numbering your drives at 1!  Whereas all the information you find on booting ISO files says you have to start numbering from 0 (on BIOS machines this is always true, this is not necessarily the case on some UEFI machines ! )

    So the value for loopback becomes (hd2,gpt2)$GPartedISOFile as the ISO file on my machine was /dev/sdb2 (second drive hd2, second partition gpt2):

    menuentry "GParted Live ISO" {
      set GPartedISOFile="/opt/Live-ISOs/gparted-live-0.31.0-1-amd64.iso"
      loopback loop (hd2,gpt2)$GPartedISOFile
    }
    

    Another difference is that the linux and initrd on BIOS machines are called linuxefi and initrdefi on UEFI machines, which gives us our final result:

    #!/bin/sh
    exec tail -n +3 $0
    # This file provides an easy way to add custom menu entries.  Simply type the
    # menu entries you want to add after this comment.  Be careful not to change
    # the 'exec tail' line above.
    
    menuentry "GParted Live ISO" {
      set GPartedISOFile="/opt/Live-ISOs/gparted-live-0.31.0-1-amd64.iso"
      loopback loop (hd2,gpt2)$GPartedISOFile
      linuxefi (loop)/live/vmlinuz boot=live components config findiso=$GPartedISOFile ip=frommedia toram=filesystem.squashfs union=overlay username=user
      initrdefi (loop)/live/initrd.img
    }
    

    So now save that file, and update grub with:

    update-grub
    

    After all of the above, reboot, go into the grub menu, choose GParted Live ISO and you can now easily boot your ISO without having to hunt for a USB stick ever again!

    :-)

    CloneZilla Live example (for this question )

    menuentry "CloneZilla ISO" {
      set ISOFile="/opt/Live-ISOs/clonezilla-live-20170905-zesty-amd64.iso"
      loopback loop (hd2,gpt2)$ISOFile
      linuxefi (loop)/live/vmlinuz boot=live components config findiso=$ISOFile ip=frommedia toram=filesystem.squashfs union=overlay
      initrdefi (loop)/live/initrd.img
    }
    
  • Related Question