GRUB 2
Basic summary of GRUB 2:
- A file called grub.cfg controls the Grub Screen.
- grub.cfg is automatically generated by the scripts in /etc/grub.d.
- Partitions are numbered from 1 instead of 0 and drives are numbered from 0:
Linux grub2 /dev/sda1 (hd0,1) /dev/sda2 (hd0,2) /dev/sda3 (hd0,3) /dev/sdb1 (hd1,1) /dev/sdb2 (hd1,2) /dev/sdb3 (hd1,3)
- As the grub.cfg file is the 'result' file the grub.cfg file should never be altered manually.
The Grub 2 default config file
The /etc/default/grub file contains the grub2 variable settings. For instance, timeout on menu, default entries in the menu to be activated, kernel parameters, the use of grub graphics and so forth.
The Grub 2 script files
/etc/grub.d files control the 'result' file called grub.cfg, which is found in /boot/grub/.
The grub.cfg file should never be altered manually. All alterations are to be done with one or all of the following script files located in /etc/grub.d. os-prober should handle 90% of all cases:
00_header:
05_debian_theme: Sets background, text colors, themes
10_hurd: Locates Hurd kernels
10_linux: Locates Linux kernels based on results of the lsb_release command.
20_memtest86+: If the file /boot/memtest86+.bin exists, it is included in the boot menu.
30_os-prober: Searches for Linux and other OS's on all partitions; includes them in the boot menu.
40_custom: A template for adding custom boot menu entries for other Operating Systems.
60_fll-fromiso: A template for adding custom boot menu entries for fromiso to a USB-stick/SSD-card.
60_fll-fromiso is not to be altered use /etc/default/grub2-fll-fromiso
Read Booting 'fromiso' with Grub 2
After an alteration is made, grub.cfg needs to know about the changes. In the case of aptosid kernel upgrades, the update command is automatically run. In the case of a manual alteration made by you as the system administrator to any of the above files, you need to run:
update-grub
The Debian Grub2 deb package is designed so that a manual alteration by you would be rarely needed.
Editing Grub2 boot options via the edit screen
If, for some reason, you need to make a temporary alteration to the boot options to a kernel highlighted in Grub2, press the letter e to edit the kernel options and by using the using arrow keys, navigate to the line that you wish to edit, then while still in the edit screen use Ctrl+x to boot the computer with your edited options.
For example, to go directly to run level 3, add 3 to the end linux /boot/vmlinuz line.
The edits you make via the edit screen and are not permanent. To make permanent changes, you need to edit the appropriate files. See Grub 2 files.
Dual and multi booting with Grub 2
As Grub2 is modular in configuration, it allows a simple command to seek out any newly installed operating systems and if one is found, it tries to implement the change to update the menu.cfg. The simple command is:
update-grub
Should you have a need to add a custom entry to menu.cfg or if the 30_os-prober fails to write to grub.cfg with chainloader menus use your favourite text editor to make the amendments to /etc/grub.d/40_custom.
Examples of customising the 40_custom file:
menuentry "second mbr"{ set root=(hd1) chainloader +1 }
menuentry "second partition"{ set root=(hd0,2) chainloader +1 }
Then after you have made your changes run:
update-grub
Should it complain that does not know the grub device of a disk, it means that the devicemap must be regenerated.
Ensure that you choose the partition not the MBR when installing the other operating system:
grub-mkdevicemap --no-floppy update-grub
Warnings can be safely ignored.
Should you make a mistake, the update will probably overwrite your MBR and you will need to fix it with Grub2 - MBR Overwritten.
To only re-write grub2 to MBR from the hard drive:
/usr/sbin/grub-install --recheck --no-floppy /dev/sda
This line may need to be run more than once, until its convinced you really mean it.
MBR Bootsector overwritten by Windows, a corrupted and /or to recover Grub2
NOTE: to restore a Grub2 bootloader you need to have an aptosid.iso. Alternatively use chroot with any live.iso.
To rewrite the grub2 to the MBR and/or to recover grub 2 in general, you will need boot up a aptosid.iso:
- To identify and confirm your partitions (e.g. [h,s]d[a..]X) all the following actions need root privileges, so to get root (#):
$ su
- When in root type in:
fdisk -l cat /etc/fstab
This is to get the correct names. - When you have acertained the correct partition, create the mount point:
mkdir -p /media/[hdxx,sdxx,diskx]
- Mount it:
mount /dev/xdxx /media/xdxx
- Now rewrite Grub to MBR of the first harddisk (generic):
/usr/sbin/grub-install --recheck --no-floppy --root-directory=/media/xdxx /dev/sda
Using chroot to recover an overwritten or corrupted Grub in the MBR
To recover Grub, if it has been overwritten or corrupted in the MBR, set up a chroot environment. Any live.iso will suffice as the chroot takes you to your hard disk installed system so you can restore the appropriate grub version, grub1 (grub-legacy) or grub2.
Boot up a live aptosid.iso that is appropriate for your system (32 or 64 bit CD, DVD, USB stick or an SSD card) and open the konsole. Type in su and press enter to give you root permissions.
Using fdisk -l or blkid, ascertain which partition is the boot partition and get the correct names. If you prefer a GUI use Gparted:
blkid
and to check that fstab matches the output of the blkid:
cat /etc/fstab
Let's assume your root filesystem is on /dev/sda2
mkdir /mnt/aptosid-chroot mount /dev/sda2 /mnt/aptosid-chroot
Next, you need to mount /proc, /run, /dev and /sys as follows:
mount --bind /proc /mnt/aptosid-chroot/proc mount --bind /run /mnt/aptosid-chroot/run mount --bind /sys /mnt/aptosid-chroot/sys mount --bind /dev /mnt/aptosid-chroot/dev mount --bind /dev/pts /mnt/aptosid-chroot/dev/pts
If you boot using an EFI system partition you will also need to mount it. Assuming this is /dev/sda1:
mount /dev/sda1 /mnt/aptosid-chroot/boot/efi
Your chroot environment is now set up, access it with:
chroot /mnt/aptosid-chroot /bin/bash
Now you are able to use apt's local cache or alter files that you need to fix as if you were properly booted to the OS, in this case to fix Grub back onto the MBR.
To restore Grub 2
apt-get install --reinstall grub-pc
To ensure that grub was installed to the correct disk or partition, run:
dpkg-reconfigure grub-pc
To restore Grub 2 EFI
apt-get install --reinstall grub-efi-amd64
To restore Grub 1 (grub-legacy)
apt-get install --reinstall grub-legacy
Follow the installers instructions.
To release the chroot:
Ctrl+d
Reboot your PC.