Tutorial Grub2Win/Grub2 guide.

ERZA

Blog Author
VC
44
GNU GRUB
Introduction:
Grub is one of the most powerful boot loader.It's ability to load drivers, modules on the fly, startup almost any OS and the bash like script support makes it extremely customizable.Yet many doent know how to install and use the basic grub.

What is Grub:
Gurb is an open source bootloader.Its the software which helps you run your fevourite OS like windows or linux or android. Many people think that grub or grub2win(And easy fork of grub) is part of the OS. Though bootloader helps to load the OS, it is not part of it.

Grub Install:
Grub is available in most linux distributions.For windows theres ready to install zip available at https://ftp.gnu.org/gnu/grub. For other systems though it has to be compiled manually.
Grub can be installed simply via
Code:
grub-install DEVICE_NAME
where DEVICE_NAME for linux is "/dev/sda" and for windows "\\?\PhysicalDrive0" or "\\?\C:"
Though this is usable,the default behavour of grub is not always what we want and can be unpredictable in windows.So what we can do is
Code:
grub-install DEVICE_NAME OPTIONS
Some examples:
Code:
#Install grub on 1st harddisk as legacy system
grub-install.exe  \\?\PhysicalDrive0 --target=i386-pc --boot-directory=C:\boot
#Install grub on 2nd harddisk as efi 64bit system
grub-install.exe  \\?\PhysicalDrive1 --target=x86_64-efi --boot-directory=H:\boot --efi-directory=H:\
#Install grub on usb as legacy system
grub-install.exe  \\?\PhysicalDrive2 --target=i386-pc --boot-directory=K:\boot
#Install grub on usb as efi 32bit system
grub-install.exe  \\?\PhysicalDrive2 --target=i386-efi --boot-directory=K:\boot --efi-directory=K:\ --removable
#Install grub on usb in  linux (usb mounted on /mnt/usb)
grub-install.exe  /dev/sdb --target=x86_64-efi --boot-directory=/mnt/usb --efi-directory=/mnt/usb --removable

Note:Index of drives can be found in diskmgmt.msc in startmenu for windows
Also the partiotion needs to be mounted both for linux and windows

Grub Options:
Grub has alot of options to choose from.But we will only discuss about the most importand and general purpose ones.
--target: Sets the bootloader architecture type. By default it is same as the running OS.This can be i386-pc,i386-efi,x86-64-efi and more if the grub installer has files for those architecture.
--boot-directory: Sets the location to use for loading modules during boot.This is by default "/boot" and can be unpredictable in windows.
--efi-directory: Sets the directory to be used as EFI partition.This is only for EFI install.
--force: Ignores any warning.
--removable: Installs the bootloader to be used as removalbe.This is for installing in USB in EFi since EFI treats portable disks differently
--modules: This is for advance users.It pre loads some modules in the bootloader it self.By defalut modules that neede for grub to work are preloaded automatically

Grub Structure:
Grub creates several files under --boot-drecotory folder. Each of theses files has seperate jobs.

We will discuss about the most importand files and folders.
i386-pc,i386-efi,x86_64-efi: These folder contains the modules for the bootloader.One or more may exists according to install option.
themes: This folder contains theme files. With themes grub can be turn from simple black screen to a modern like UI. This may not exists.
grubenv:This files contains environment variable to use them accross reboots.
grub.cfg:This contains the configuration of grub and will be our main file to work on. This may not exists if no aoto config is available.

Grub Modes:
Grub has 3 modes and a hidden mode.Theses 3 modes are terminal mode,edit mode and menu mode.By defalt grub tried to swich to menu mode if available in grub.cfg.If not it will swich to terminal mod.Terminal mode can be also accessed in menu mode by pressing 'c' button.

While highlighted an entry you can edit the entry config by pressing 'e'.This swiches to edit mode. Here you can press escaoe to return or press f10 to load the entry with your modification.But this modification is temporary and not saved in grub.cfg

Grub has a hiddend mode called rescue mode.Generally grub tries to find the module folder(found in prefix variable) and tried to load the mod normal.If grub fails to do so it will swich to rescue mode.Here you need to manually show grub the directory and load normal module to revert back to normal mode

Grub Configuration:
Grub configuratin is mainly stored in grub.cfg. But to keep it safe some linux uses /etc/default/grub to make check and generate grub.cfg. Though this is a safe approch, it is not availale on all sysem. So we will directly learn grub.cfg

Grub has 2 approch to boot an OS.You can either boot the os directly or create a menu UI.
Code:
menuentry "Windows" {
    insmod ntldr
    search -f /bootmgr --set
    ntldr /bootmgr
}
This is a basic menu example.It creates a menu named Windows.On select it execues codes in {}.
Code:
    insmod ntldr
    search -f /bootmgr --set
    ntldr /bootmgr
    boot

This is another type of config.This will not show any menu.Instead it will directly execute the codes and boot windows.

Notice the last word boot. Normaly any menu entry executes the command boot at last if necessary.So if your code needs boot to be executed and your using direct approch instead of menuentry, you will need to add boot at last line of the code.

Boot codes for different OS can be easily found on internet. How to create them your self is something for another day.
Grub also supports sub menus to create menus within menus.

An exmple of menu containing multple menus.
Code:
set timeout=60
set default=0

submenu "Debian ->" {
    menuentry "Debian 9 Stretch - 64bit Netinst+firmware" {
        set iso="/boot/iso/firmware-9.0.0-amd64-netinst.iso"
        loopback loop $iso
        linux (loop)/install.amd/vmlinuz iso-scan/ask_second_pass=true iso-scan/filename=$iso priority=low vga=normal quiet
        initrd /boot/debian/install.amd/initrd.gz
    }

    menuentry "Debian 9 Stretch - 32bit Netinst+firmware" {
        set iso="/boot/iso/firmware-9.0.0-i386-netinst.iso"
        loopback loop $iso
        linux (loop)/install.386/vmlinuz iso-scan/ask_second_pass=true iso-scan/filename=$iso priority=low vga=normal quiet
        initrd /boot/debian/install.386/initrd.gz
    }
}

menuentry "Ubuntu 16.04 LTS - 64bit Mini-Installer" {
    set iso="/boot/iso/ubuntu-1604-amd64-mini.iso"
    loopback loop $iso
    linux (loop)/linux boot=casper iso-scan/filename=$iso noprompt noeject
    initrd (loop)/initrd.gz
}

menuentry "Windows" {
        insmod ntldr
    search -f /bootmgr --set
    ntldr /bootmgr
}

menuentry "Reboot" {
    reboot
}

Grub Config Variable:
Grub has some reserved variables to be used to change the behavour of bootloader. Here are some of them.
timeout: Sets time time after which the defalult menu option will automatically be chosen.
default:Sets which entry to chose when time has ran out.
prefix:This contains the directory in which modules exists. This is set during the installation. If grub cannot find this, it will change to rescue mod.
root:This is the default partition that grub uses as /.By defalult its the partition from which grub was loaded.

A useful link
 
Last edited:

Similar threads

Similar threads

Get Connected

Discord Server

New Threads

Latest From Blog

Top