GRUB/Chainloading

From Gentoo Wiki
< GRUB
Jump to:navigation Jump to:search

GRUB2 was designed with an improved chainload mode when compared to GRUB Legacy.

Another grub.cfg

It is possible to load a grub configuration file from another filesystem, using the UUID of that filesystem as it is visible from /dev/disk/by-uuid/.

FILE grub.cfgChainloading another GRUB configuration file
search --no-floppy --fs-uuid --set=root 00000000-0000-0000-0000-000000000000
set prefix=($root)/grub
configfile /grub/grub.cfg

ISO images

The new ISO (or loop) chainload mechanism makes chainloading a breeze. It is possible to chainload ISO images (LiveCD/DVDs) with GRUB Legacy, however there exists no way to pass kernel cmdline arguments before boot. In any case, the ISO images in question should be built keeping kernel cmdline arguments in mind.

Without kernel cmdline options, booting ISO images with GRUB2 will fail in the physical media check/test stage of the ISOs boot process. Gentoo liveCD is handy because it has a minimal shell which lets the user mount the squashed image to the correct location and then press the Esc key to continue the boot process. This makes it possible to have a handy way to install an operating system with everything in RAM, especially for "light weight" LiveCDs. No more need to listen to a whining CD/DVD drive on each new command!

To chainload an ISO with custom or default kernel command line arguments, an entry similar to the following can be added to GRUB2's grub.cfg file:

FILE /boot/grub/grub.cfgExample entry for chainloading an ISO file
menuentry "SYSRESCUECD" {
	set iso=/systemrescuecd-x86-3.8.1.iso
	loopback loop ${iso}
	linux  (loop)/isolinux/rescue64 nomodeset vga=791 docache setkmap=fr isoloop=${iso}
	initrd (loop)/isolinux/initram.igz
}

For a permanent and automatic entry to GRUB2's grub.cfg file, a custom script could be added to the /etc/grub.d script location:

FILE /etc/grub.d/40_customCustom script to load a CD
#!/bin/sh
exec tail -n +3 $0

menuentry "GRUB4DOS" {
	linux /grub4dos-0.4.4/grub.exe --config-file=/menu.lst
}

menuentry "SYSRESCUECD" {
	set iso=/systemrescuecd-x86-3.8.1.iso
	loopback loop ${iso}
	linux  (loop)/isolinux/rescue64 nomodeset vga=791 docache setkmap=fr isoloop=${iso}
	initrd (loop)/isolinux/initram.igz
}

menuentry "STG3-AMD64" {
	set cmdline="dokeymap looptype=squashfs loop=/image.squashfs cdroot"
	loopback loop /stg3-amd64-<DATE>.iso
	linux  (loop)/isolinux/gentoo $cmdline root=/dev/ram0 init=/linuxrc initrd=gentoo.igz
	initrd (loop)/isolinux/gentoo.igz
}

menuentry "gentoo installation iso" {
        set iso=/install-x86-minimal-DATETIME.iso
        bootoptions="isoboot=$iso root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/image.squashfs cdroot vga=791"
        loopback loop ${iso}
        linux  (loop)/boot/gentoo $bootoptions
        initrd (loop)/boot/gentoo.igz
}

Do not forget to make the script executable:

root #chmod +x /etc/grub.d/40_custom

Finally regenerate GRUB2's grub.cfg file using grub2-mkconfig command.

Another bootloader

Chainloading another bootloader to GRUB2 is fairly easy.

Something as simple as the following example is enough to boot another disk that uses a "Custom Super Bootloader":

FILE /etc/grub.d/40_customChainloading another bootloader
menuentry "Custom Super Bootloader Example" {
     insmod part_msdos
     insmod chain
     chainloader (hd1,1)+1
}

TrueCrypt

Chainloading the TrueCrypt bootloader on a separate disk is relatively simple and can be done in GRUB2 like any other bootloader:

FILE /etc/grub.d/40_customChainloading TrueCrypt bootloader on a separate disk
title Windows7-TrueCrypt-BIOS/MBR
find --set-root /truecrypt_rescue_image.iso
map --mem /truecrypt_rescue_image.iso (hd32)
map (hd0) (hd1)
map (hd1) (hd0)
map --hook
root (hd32)
chainloader (hd32)
Note
The remapping may not be necessary if the system has a single boot disk. The mapping is necessary before chainloading TrueCrypt bootloader in the case that the boot disk (hd1 as shown in the example above) should appear as the primary boot device (hd0), otherwise TrueCrypt would fail to boot because the correct disk will not be found.

Chainloading a disk with TrueCrypt in the MBR or a rescue CD image located in encrypted partitions is not possible with GRUB2 (see bug #385619). Use either GRUB Legacy or GRUB4DOS as workaround. GRUB4DOS has an interface very similar to GRUB Legacy and a menu.lst entry which can be used to chainload the TrueCrypt bootloader or to boot a rescue CD (from an encrypted partition on the same disk).

Another workaround is to boot from TrueCrypt as the main boot loader and then hit the Esc to chainload the following partition (if one exists) or the following disk. Then have GRUB2 installed on the partition instead of in the MBR itself so that GRUB2 is chainloaded.

Windows (MSDOS based boot loaders)

When Windows (or another MS DOS based boot loader) is installed on another disk, then regular chainloading in the grub.cfg may be sufficient to boot it. However, if Windows is on the same disk on a different partition, or if regular chainloading doesn't work, then read on.

Microsoft Windows 8 (and above versions) are no longer installed using MSDOS partitions by default, however they do maintain backwards compatibility with BIOS MBR systems. In order to specify Windows 8 (and above) to use MSDOS partitioning the Windows installation DVD needs to be booted in BIOS mode (a non-UEFI boot mode) in order for Windows to install into MSDOS partitions. Manually create a MSDOS partition layout, then manually boot the Windows DVD using a BIOS option in the boot menu list. Sometimes it is necessary in the BIOS firmware configuration tool to disable UEFI mode completely in order to force BIOS MBR mode.

The simplest way to dual boot Windows (or MS-DOS) is to add an MBR menu entry to GRUB2's grub.cfg file for each Windows operating system installed.

For instance, to boot Windows 7, add the following to the grub.cfg file:

FILE /etc/grub.d/40_customWindows 7 example
menuentry "Windows 7 BIOS MBR" {
     insmod part_msdos
     insmod ntldr
     insmod ntfs
     ntldr (hd0,msdos1)/bootmgr
}

A Windows XP example:

FILE /etc/grub.d/40_customWindows XP example
menuentry "Windows XP BIOS MBR" {
     insmod part_msdos
     insmod ntldr
     insmod ntfs
     ntldr (hd0,msdos1)/ntldr
}

Instead of using GRUB2's device syntax, the UUID of the partition containing the Windows bootloader can be used like so:

FILE /etc/grub.d/40_customUUID example
menuentry "Windows 10" {
     insmod ntfs
     insmod ntldr
     insmod part_msdos
     insmod search_fs_uuid
     search --no-floppy --fs-uuid --set=root 1AECC5A1ECC57811
     ntldr /bootmgr
}

Filesystem UUIDs can be obtained with blkid.

Note
Some users report the above method using UUIDs works better for Windows 7 or 10.

An entry for a GPT hybrid MBR works a bit different than the previous BIOS-MBR examples. Booting multiple versions of Windows can be achieved with remapping and/or hiding partitions with GRUB2's parttool option:

FILE /etc/grub.d/40_customExample for GPT hybrid MBR
menuentry "Windows 7 BIOS MBR" {
     insmod part_msdos
     insmod chain
     parttool hd1,msdos1 hidden+ boot-
     parttool hd1,msdos2 hidden- boot+
     chainloader (hd1,msdos2)+1
}
Note
Remapping the disk with parttool in the manner above is only required if multiple Windows systems are installed on the same disk.

Remapping the devices to set the primary boot disk to other disks can be achieved by using the drivemap option like so:

FILE /etc/grub.d/40_customRemapping devices example
menuentry "Windows 7 BIOS MBR" {
     insmod part_msdos
     insmod chain
     drivemap hd0 hd1
     chainloader (hd1,msdos2)+1
}

Probing

GRUB2 is capable of automatically finding Windows partitions and assigning the root partitions. The Windows partition must first be mounted before the probe will be successful. See notes at the end of this section concerning missing C:\bootmgr and C:\Boot files and folders; it is wise to make sure these folders do exist before trying to boot Windows using GRUB2.

GRUB2's probing feature requires the sys-boot/os-prober package which is not initially pulled in when installing GRUB2.

root #emerge --ask --newuse sys-boot/os-prober
root #grub2-probe --target=hints_string /mnt/windows7/bootmgr
--hint-bios=hd1,msdos1 --hint-efi=hd1,msdos1 --hint-baremetal=ahci1,msdos1
root #grub2-probe --target=fs_uuid /mnt/windows7/bootmgr
2ABF87DC395CFC02

From the output provided by the above two commands, the search line within GRUB2's grub2.cfg file (below) can be constructed. Remapping the drive and partition as the first hard drive and first partition will make Windows XP or Windows 8 more free of silent errors while loading.

FILE /etc/grub.d/40_customConstructing the search line
menuentry 'Microsoft Windows 7 or Windows 8 (on sdb1)' --class windows {
        insmod part_msdos
        insmod ntfs
        insmod search_fs_uuid
        insmod ntldr
        search --fs-uuid --set=root --hint-bios=hd1,msdos1 --hint-efi=hd1,msdos1 --hint-baremetal=ahci1,msdos1 2ABF87DC395CFC02
        drivemap (hd1,msdos1) (hd0,msdos1)
        #Or, "drivemap (hd1,msdos2) (hd0,msdos1)" for those with Windows installed on sdb2)
        ntldr /bootmgr
}
Important
If the C:\bootmgr and the C:\Boot folders through which the bootmgr file is linked do not exist, then the files need to be installed using an Administrator cmd.exe shell (escalated command prompt). Regeneration should be possible by using this command:
C:\>bcdboot c:\Windows /s c:

Seeing a boot error message concerning a missing bootmgr file after attempting to boot one of the previously mentioned grub.cfg entries is the indication the C:\Boot folder is missing. This happens when using Windows 8 since the C:\Boot folder does not seem to be generated by default.

Dual-booting Windows on UEFI with GPT

In the case the Windows bootloader was overwritten with GRUB2 or if bootmgr doesn't do the trick, a UEFI dual boot could be achieved using the following menu entry:

FILE /etc/grub.d/40_customUEFI dual-boot
menuentry "Windows 7 UEFI/GPT" {
    insmod part_gpt
    insmod search_fs_uuid
    insmod chain
    search --fs-uuid --no-floppy --set=root 28cf-35de
    chainloader ($root)/EFI/MICROSOFT/BOOT/bootmgfw.efi
}

See also