Understanding the GRUB 2 bootloader

Booting from an ISO File

To illustrate how to use GRUB 2 configuration files, I'll show how to use GRUB 2 to boot from an ISO file. This example also has practical utility: It lets you install from a distribution without needing to burn the image to a USB stick or a DVD. You just use the ISO file you downloaded.

To boot from an ISO file, you'll use a loop device [7], which is a virtual block device that treats a file like a virtual disk. When you use this virtual disk at boot time, you can boot directly from this file, which is where GRUB 2 comes into play. GRUB 2 lets you inspect the ISO file before booting. This file provides the correct parameters for booting.

I put all of my ISO files in a separate partition (Figure 6). First, I mount this partition, and then I mount the ISO file as a partition on my filesystem. Going into the partition, you will see the names of the two files needed for booting: the kernel (vmlinuz) and the RAM filesystem (initrd.gz), which are located in the root of the ISO file.

Figure 6: Mounting and reading the ISO partition.

With this knowledge, you are now ready to boot using GRUB 2. From the command line, type c in the GRUB 2 menu to enter the GRUB 2 shell. Once the shell opens, use ls to look at the disks and partitions.

As shown in Figure 6, the ISO files are located in partition 4, but you can verify this with

ls(hd0,gpt4)/

to confirm that the files are really there. Next, you need to communicate this knowledge to the loopback. Instead of typing the ISO file name, you can press the tab key to complete the file name for you. You also need to give GRUB 2 the kernel name, tell GRUB 2 to load it into RAM, and inform GRUB 2 of initrd's location. That's it.

Looking at Figure 7, you'll see I entered initrd instead of initrd.gz in the first trial, which caused the GRUB 2 shell to throw an error. If you don't get an error message, you know you entered the correct file names.

Figure 7: The GRUB 2 shell disks and partitions.

Now tell GRUB 2 to boot. Voila, it boots your ISO file.

Next you can try to integrate the ISO file boot into your bootloader so that you have a choice at boot time. You will do nearly the same thing as you did for the GRUB 2 shell. The only difference is that you put the information you entered using the shell into the custom file found at /etc/grub.d. As root, use the file 40_custom (without removing the exec tail line) and attach the code (see Figure 8). I recommend getting elevated rights with

sudo su -
Figure 8: The 40_custom file.

Save the file and reconfigure GRUB 2 with update-grub. Once you reboot, you will see the new boot entry (Figure 9), which will allow you to boot from the Puppy Linux ISO.

Figure 9: You'll find a new boot entry for Puppy Linux ISO in the GRUB 2 menu.

The exact location and names for linuz and imageram may differ between ISO files. You should always inspect the files and adapt your configuration accordingly. In Figure 10, I added the Ubuntu ISO file to show how the parameters can differ between ISO files. For instance, the linuz file is in another location and the initrd file's location and name have changed. With the Ubuntu ISO, you will also notice that linuz and initrd are in the casper directory; casper is a tool designed for booting Live Linux systems.

Figure 10: Configuration parameters differ depending on the ISO file as shown here with Puppy Linux and Ubuntu.

You will find examples online that demonstrate how to configure the custom GRUB 2 file. Be aware that these instructions, found in user forums, may contain errors. When doing a custom configuration, make sure you know what you are doing based on your ISO distribution. I found LinuxBabe [8] useful; it provides a long list of ISO distributions (including Microsoft Windows), along with comments on the parameters used.

Remember that any customizations should be added to the custom file in /etc/config/grub instead of grub.cfg, which is overwritten when the kernel is updated.

GRUB 2 for Windows

If you want to use GRUB 2 on Windows, you will need Grub2Win [9]. You can download Grub2Win as a ZIP file from SourceForge. The ZIP file contains the installer, which downloads the software required and performs the installation. Once installed, Grub2Win will assist you in the configuration, which is a straightforward process.

Grub2Win, with its GUI interface (Figure 11), shares similar concepts to the Linux version, so I will not repeat the instructions on how to use GRUB 2 with Windows. Once installed, Grub2Win will even survive a Windows software update. An outstanding user manual completes this useful software.

Figure 11: The Grub2Win GUI.

Conclusion

GRUB 2 is a sophisticated, highly-configurable software system, which may initially seem complex. When using GRUB 2, it is important to understand the boot process, so you know what to do when things go wrong. Now that you understand how the GRUB 2 bootloader works, you can easily find more detailed instructions to further customize the GRUB 2 boot process to meet your needs. Remember, use a virtual machine like VirtualBox when experimenting with GRUB 2, EFI, and BIOS.

The Author

Reinhard E. Voglmaier discovered his love for computer science while writing his diploma thesis at the Max Planck Institute for Extraterrestrial Physics in Germany. He started his career as IT project manager, and subsequently took on the role of intranet manager, in a pharmaceutical company, overseeing web services and addressing cybersecurity concerns. Following his retirement, he continues to lead the expert charter GRC in Digital Health for ISACA Germany and is engaged in a computer linguistics project at the University of Innsbruck.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • In-Depth Security

    More than just a boot manager, GRUB 2 can help you add another line of protection to your security defenses.

  • Grub Customizer

    Is the simple black and white GRUB menu causing confusion and obscuring important choices? Why not customize with GRUB themes and the Grub Customizer?

  • Exercise Place

    The GRUB 2 boot manager might seem intimidating at first glance. All the more reason to spin up a virtual playground so you can practice.

  • GRUB GUIs

    A graphical tool might be your best option for configuring or adapting the GRUB2 boot loader.

  • Configuring Dual Boot

    When two systems share a single computer, a boot manager handles the prompts that determine which system to boot. We’ll show you several multiple boot scenarios and describe how to set up your system for dual booting Linux with Windows.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News