Grub: Difference between revisions

From miki
Jump to navigation Jump to search
 
(21 intermediate revisions by the same user not shown)
Line 2: Line 2:
* Official manual [http://www.gnu.org/software/grub/manual/grub.html here].
* Official manual [http://www.gnu.org/software/grub/manual/grub.html here].


== Grub Command-Line ==
== Install / Update Grub ==

To install grub on drive '''/dev/sda''', using '''/dev/sda3''' as <tt>/boot</tt> partition:
<source lang=bash>
sudo mount /dev/sda3 /boot
sudo grub-install /dev/sda # Add option --recheck to force checking again
</source>

Later, to update the grub menu (e.g. new kernel, remove / added external OS):
<source lang=bash>
sudo update-grub
</source>

=== GPT/EFI systems ===
References: https://wiki.archlinux.org/index.php/GRUB

Grub2 support 2 configuration: the old MBR configuration and the new EFI-GPT configuration.

Modern systems usually comes with a GPT partition table and EFI system partition (EPS). Grub2 sould detect automatically, and use the EFI-GPT configuration. The usual method to install grub still works, but requires that the EFI partition be mounted at {{file|/boot/efi}}:
<source lang=bash>
sudo mount /dev/sda2 /boot # OPTIONAL - ONLY IF USING SEPARATE BOOT PARTITION (assuming /dev/sda2)
sudo mount /dev/sda1 /boot/efi # Assume /dev/sda1 is the EFI partition
sudo groub-install /dev/sda
sudo update-grub # To repeat every time a new kernel / boot driver is installed
</source>

One can override the default configuration using parameter <code>--target</code>:

* Use the target '''x86_64-efi''' to force install grub using EFI-GPT configuration:
<source lang=bash>
# Assume ESP mounted at /boot/efi
# Optionally a boot partition mounted at /boot
# The target library files must exist at /usr/lib/grub/x86_64-efi
sudo grub-install --target=x86_64-efi --efi-directory=/boof/efi --bootloader-id=grub --recheck
</source>
* Use the target '''--target=i386-pc''' to use the old MBR configuration
<source lang=bash>
sudo grub-install --target=i386-pc --recheck
</source>

if {{file|/usr/lib/grub/x86_64-efi}} does not exist when booting from a LIVE usb, the best method to reinstall grub is to chroot on the hdd partition (see below).

== Grub Shell ==


=== Boot a LINUX partition ===
=== Boot a LINUX partition ===
Line 18: Line 60:
* Press '''c''' to go to command-line mode.
* Press '''c''' to go to command-line mode.
* Enter the following commands (assuming CDROM is on hd2 / Windows is on hd0). Note that Grub uses hd0,hd1... not /dev/sda...):
* Enter the following commands (assuming CDROM is on hd2 / Windows is on hd0). Note that Grub uses hd0,hd1... not /dev/sda...):
<source lang="bash">
grub>'''rootnoverify (hd2)''' ''#or '''(hd0,0)''' for Windows partition''
grub> rootnoverify (hd2) #or (hd0,0) for Windows partition
grub>'''chainloader +1'''
grub>'''boot'''
grub> chainloader +1
grub> boot
</source>
Note that this might be actually a hack that only works on my machine. More information:
Note that this might be actually a hack that only works on my machine. More information:
* [http://www.gnu.org/software/grub/manual/grub.html#Booting GRUB Manual - Boot]
* [http://www.gnu.org/software/grub/manual/grub.html#Booting GRUB Manual - Boot]
Line 31: Line 75:
By default, '''grubonce''' can only be run by root, but we can use '''sudo''' to allow any user to use '''grubonce'''. Make the following change to the <tt>/etc/sudoers</tt> file ('''use command <font color="red">visudo</font> to edit this file'''):
By default, '''grubonce''' can only be run by root, but we can use '''sudo''' to allow any user to use '''grubonce'''. Make the following change to the <tt>/etc/sudoers</tt> file ('''use command <font color="red">visudo</font> to edit this file'''):


<source lang="bash">
<pre>
Defaults env_keep = "DISPLAY ..." # add DISPLAY env var to the list of kept var
Defaults env_keep = "DISPLAY ..." # add DISPLAY env var to the list of kept var
ALL ALL = (ALL) NOPASSWD: /usr/sbin/grubonce
ALL ALL = (ALL) NOPASSWD: /usr/sbin/grubonce
</pre>
</source>


Now, you can reboot immediately a grub entry with:
Now, you can reboot immediately a grub entry with:
<source lang="bash">
<source lang="bash">
$ sudo grubonce
sudo grubonce
# 0: openSUSE 11.0 - 2.6.25.20-0.1
# 0: openSUSE 11.0 - 2.6.25.20-0.1
# 1: Failsafe -- openSUSE 11.0 - 2.6.25.20-0.1
# 1: Failsafe -- openSUSE 11.0 - 2.6.25.20-0.1
# 2: windows
# 2: windows
# 3: Floppy
# 3: Floppy
$ sudo grubonce 2
sudo grubonce 2
</source>
</source>


Line 101: Line 145:
* Always run 'setup (hd0)' to install grub on the MBR of the first drive. For second drive use (hd1) etc
* Always run 'setup (hd0)' to install grub on the MBR of the first drive. For second drive use (hd1) etc


=== Reinstalling GRUB 1 ===
=== Reinstalling GRUB 2 ===
Here the procedure in the case of '''GRUB 2'''. Let's assume the boot partition is on '''dev/sda6''' (use <tt>sfdisk</tt> to know) (see [http://www.ohbuntu.blogspot.com/2009/11/repair-grub2-after-install-windows-7.html]).
Here the procedure in the case of '''GRUB 2'''. Let's assume the '''root''' partition is on '''/dev/sda6''' (use <tt>sfdisk</tt> to know) (see [http://www.ohbuntu.blogspot.com/2009/11/repair-grub2-after-install-windows-7.html]).
<source lang="bash">
<source lang="bash">
sudo mount /dev/sd6 /mnt/myroot
sudo mount /dev/sda6 /mnt
sudo grub-install --root-directory=/media/myroot /dev/sda
sudo grub-install --root-directory=/mnt /dev/sda
#reboot
#reboot
sudo update-grub2 # Optional - in case entry are missing from the grub2 menu
sudo update-grub # Optional - in case entry are missing from the grub2 menu
</source>
If '''/dev/sda6''' is a {{red|'''boot'''}} partition, you have to mount it in a directory named <tt>boot/</tt>, and specify the parent directory instead:
<source lang="bash">
sudo mount /dev/sda6 /mnt
sudo mount /dev/sda1 /mnt/boot
sudo grub-install --root-directory=/mnt /dev/sda
#reboot
sudo update-grub # Optional - in case entry are missing from the grub menu
# can also be done from live cd using chroot (as in example below)
</source>
</source>


Another solution involve '''chroot'''ing to the mounted media ([http://tolearnfree.blogspot.com/2009/12/how-to-fix-grub2-on-ubuntu-910.html]). <font color="red">But this method does not work if the current linux architecture (32-bit/64-bit) does not match the architecture of the target (because chroot will re-run the current shell from target and the architecture must match)</font>:
Another solution involve '''chroot'''ing to the mounted media ([http://tolearnfree.blogspot.com/2009/12/how-to-fix-grub2-on-ubuntu-910.html]). <font color="red">But this method does not work if the current linux architecture (32-bit/64-bit) does not match the architecture of the target (because chroot will re-run the current shell from target and the architecture must match)</font>:
<source lang="bash">
<source lang="bash">
sudo mount /dev/sd6 /mnt/myroot
sudo mount /dev/sda6 /mnt/root
sudo mount --bind /dev/ /media/myroot/dev
sudo mount /dev/sda2 /mnt/root/boot # Optional - if exists
for a in dev proc sys run; do sudo mount --bind /$a /mnt/root/$a; done
sudo chroot /media/myroot
sudo chroot /mnt/root
sudo grub-install /dev/sda
sudo grub-install /dev/sda
sudo grub-install --recheck /dev/sda
sudo grub-install --recheck /dev/sda
sudo exit
exit
sudo umount /mnt/root/{boot,dev,proc,sys,run} /mnt/root
</source>
</source>

=== Restore /boot partition and install grub ===

To restore the {{file|/boot}} [http://pivica.me/blog/reinstalling-erased-boot-partition-linux] and [https://linoxide.com/linux-how-to/fixing-broken-initrd-image-linux/], we first chroot into the system:

<source lang="bash">
sudo mount /dev/sda6 /mnt/root
sudo mount /dev/sda2 /mnt/root/boot # Optional - if exists
for a in dev proc sys run; do sudo mount --bind /$a /mnt/root/$a; done
# Run needed to recover /etc/resolv.conf
sudo chroot /mnt/root
</source>

To rebuild {{file|/boot}}, we either reinstall the kernel package:
<source lang="bash">
mv initrd.img initrd.img.OLD # Or might stop apt-get command
dpkg -l | grep linux-image
apt-get install --reinstall linux-image-3.0.0-17-generic # ... adapt as necessary
sudo apt-get --reinstall install memtest86+ # ... if complainst that it is missing
</source>

Or we can simply rebuild the initramfs image:
<source lang="bash">
ls /lib/modules/ # Get kernel version -- we cannot use 'uname' in chroot of course
cd /boot
mv /boot/initrd.img-3.11.0.12-generic 3.11.0.12-generic-old # Backup
update-initramfs -c -k 3.11.0.12-generic
</source>

Then we install and update grub:
<source lang="bash">
sudo grub-install /dev/sda
# If grub complains with 'grub-install: error: cannot find EFI directory":
# * Check we have EFI partition mounted as /boot/efi:
# mount | grep efi
# * Install grub with EFI path:
# sudo grub-install /dev/sda --efi-directory=/boot/efi
sudo update-grub
</source>

Then we leave chroot and unmount everything:
<source lang="bash">
exit
sudo umount /mnt/root/{boot,dev,proc,sys,run} /mnt/root
</source>

== Grub 2 Configuration ==
* See [https://help.ubuntu.com/community/Grub2 here] for reference
* Never edit <tt>/boot/grub/grub.cfg</tt>. Instead edit <tt>/etc/default/grub</tt> and run <code>sudo update-grub</code> to udpate:
<source lang="bash">
sudo vi /etc/default/grub
# ... edit as necessary ...
sudo update-grub
</source>
=== Always show the menu at boot ===
* By default Grub hides the boot menu if there is only one OS. To always show the menu, comment out the lines as shown here:
<source lang="bash">
#GRUB_HIDDEN_TIMEOUT=0
#GRUB_HIDDEN_TIMEOUT_QUIET=true
</source>

=== Change keyboard layout during rescue shell ===
See [https://forums.openmandriva.org/en/discussion/441/grub2-keyboard-layout]

== Uninstall Grub ==
Uninstalling Grub simply consists in restoring the original MBR. See [[Linux Disk Management]].

Latest revision as of 12:57, 24 February 2020

Documentation

  • Official manual here.

Install / Update Grub

To install grub on drive /dev/sda, using /dev/sda3 as /boot partition:

sudo mount /dev/sda3    /boot
sudo grub-install /dev/sda          # Add option --recheck to force checking again

Later, to update the grub menu (e.g. new kernel, remove / added external OS):

sudo update-grub

GPT/EFI systems

References: https://wiki.archlinux.org/index.php/GRUB

Grub2 support 2 configuration: the old MBR configuration and the new EFI-GPT configuration.

Modern systems usually comes with a GPT partition table and EFI system partition (EPS). Grub2 sould detect automatically, and use the EFI-GPT configuration. The usual method to install grub still works, but requires that the EFI partition be mounted at /boot/efi:

sudo mount /dev/sda2 /boot            # OPTIONAL - ONLY IF USING SEPARATE BOOT PARTITION (assuming /dev/sda2)
sudo mount /dev/sda1 /boot/efi        # Assume /dev/sda1 is the EFI partition
sudo groub-install /dev/sda
sudo update-grub                      # To repeat every time a new kernel / boot driver is installed

One can override the default configuration using parameter --target:

  • Use the target x86_64-efi to force install grub using EFI-GPT configuration:
# Assume ESP mounted at /boot/efi
# Optionally a boot partition mounted at /boot
# The target library files must exist at /usr/lib/grub/x86_64-efi
sudo grub-install --target=x86_64-efi --efi-directory=/boof/efi --bootloader-id=grub --recheck
  • Use the target --target=i386-pc to use the old MBR configuration
sudo grub-install --target=i386-pc --recheck

if /usr/lib/grub/x86_64-efi does not exist when booting from a LIVE usb, the best method to reinstall grub is to chroot on the hdd partition (see below).

Grub Shell

Boot a LINUX partition

Use the following procedures to boot manually from the command-line:

root (hd0,6)                         # Select partition where /boot is. Type "root (hd0," and then TAB to get a list of partitions
linux /boot/linux                    # ... or kernel /boot/linux for GRUB 1
initrd /boot/initrd.gz
boot

Boot a CDROM / Windows

When booting on HDD, it is possible to tell Grub to boot from CDROM instead. The same command-set can be used to boot the Windows partition (in case it has been removed from Grub menu)

  • Press esc to quit Grub GUI menu.
  • Press c to go to command-line mode.
  • Enter the following commands (assuming CDROM is on hd2 / Windows is on hd0). Note that Grub uses hd0,hd1... not /dev/sda...):
grub> rootnoverify (hd2)             #or (hd0,0) for Windows partition
grub> chainloader +1
grub> boot

Note that this might be actually a hack that only works on my machine. More information:

Grubonce

grubonce is an handy tool to force the system to immediately boot a given grub entry at the next reboot.

By default, grubonce can only be run by root, but we can use sudo to allow any user to use grubonce. Make the following change to the /etc/sudoers file (use command visudo to edit this file):

Defaults        env_keep = "DISPLAY ..."             # add DISPLAY env var to the list of kept var
ALL     ALL = (ALL) NOPASSWD: /usr/sbin/grubonce

Now, you can reboot immediately a grub entry with:

sudo grubonce
# 0: openSUSE 11.0 - 2.6.25.20-0.1
# 1: Failsafe -- openSUSE 11.0 - 2.6.25.20-0.1
# 2: windows
# 3: Floppy
sudo grubonce 2

Rescue

Some tips on how to restore / troubleshoot a GRUB installation or on how to use GRUB to fix a broken Linux installation.

Using GRUB to boot minimal Bash shell

  1. Press ESC when GRUB menu appears. If menu does not appear, press SHIFT while booting to force GRUB showing the menu.
  2. Select any line and press e for Edit.
  3. Edit the second line (the one that has 'kernel' at the beginning):
    • replace ro with rw so you can read and write in the file-system
    • add init=/bin/bash at the end of the line
  4. Press Enter to save the line
  5. Press b to boot.
  6. Now, you can edit any file with
nano <filename>

Using the GRUB Rescue shell

See the manual at [1].

Grub only offers a rescue shell if for some reason it failed to load the normal module (error: no such partition / grub rescue>). The reason could be for instance that the /boot partition moved. From the manual:

# Inspect the current prefix (and other preset variables):
set
# Set to the correct value, which might be something like this:
set prefix=(hd0,1)/grub
set root=(hd0,1)
insmod normal
normal

Reinstalling GRUB 1

To repair a broken / lost GRUB, do from any grub prompt (live CD...) [2]:

grub> find /boot/grub/stage1
# find /boot/grub/stage1
#  (hd0,4)
grub> root (hd0,4)
# root (hd0,4)
#  Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
# setup (hd0)
#  Checking if "/boot/grub/stage1" exists... yes
#  Checking if "/boot/grub/stage2" exists... yes
#  Checking if "/boot/grub/e2fs_stage1_5" exists... yes
#  Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  23 sectors are embedded.
# succeeded
#  Running "install /boot/grub/stage1 (hd0) (hd0)1+23 p (hd0,4)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
  • try find /grub/stage1 if the first find command doesn't work
  • In my example I set root to (hd0,4) make sure you set it to whatever the find command returns
  • Always run 'setup (hd0)' to install grub on the MBR of the first drive. For second drive use (hd1) etc

Reinstalling GRUB 2

Here the procedure in the case of GRUB 2. Let's assume the root partition is on /dev/sda6 (use sfdisk to know) (see [3]).

sudo mount /dev/sda6 /mnt
sudo grub-install --root-directory=/mnt /dev/sda
#reboot
sudo update-grub                       # Optional - in case entry are missing from the grub2 menu

If /dev/sda6 is a boot partition, you have to mount it in a directory named boot/, and specify the parent directory instead:

sudo mount /dev/sda6 /mnt
sudo mount /dev/sda1 /mnt/boot
sudo grub-install --root-directory=/mnt /dev/sda
#reboot
sudo update-grub                       # Optional - in case entry are missing from the grub menu
                                       #            can also be done from live cd using chroot (as in example below)

Another solution involve chrooting to the mounted media ([4]). But this method does not work if the current linux architecture (32-bit/64-bit) does not match the architecture of the target (because chroot will re-run the current shell from target and the architecture must match):

sudo mount /dev/sda6 /mnt/root
sudo mount /dev/sda2 /mnt/root/boot  # Optional - if exists
for a in dev proc sys run; do sudo mount --bind /$a /mnt/root/$a; done
sudo chroot /mnt/root
sudo grub-install /dev/sda
sudo grub-install --recheck /dev/sda
exit
sudo umount /mnt/root/{boot,dev,proc,sys,run} /mnt/root

Restore /boot partition and install grub

To restore the /boot [5] and [6], we first chroot into the system:

sudo mount /dev/sda6 /mnt/root
sudo mount /dev/sda2 /mnt/root/boot            # Optional - if exists
for a in dev proc sys run; do sudo mount --bind /$a /mnt/root/$a; done
                                               # Run needed to recover /etc/resolv.conf
sudo chroot /mnt/root

To rebuild /boot, we either reinstall the kernel package:

mv initrd.img initrd.img.OLD                   # Or might stop apt-get command
dpkg -l | grep linux-image
apt-get install --reinstall linux-image-3.0.0-17-generic   # ... adapt as necessary
sudo apt-get --reinstall install memtest86+    # ... if complainst that it is missing

Or we can simply rebuild the initramfs image:

ls /lib/modules/                               # Get kernel version -- we cannot use 'uname' in chroot of course
cd /boot
mv /boot/initrd.img-3.11.0.12-generic 3.11.0.12-generic-old   # Backup
update-initramfs -c -k 3.11.0.12-generic

Then we install and update grub:

sudo grub-install /dev/sda
# If grub complains with 'grub-install: error: cannot find EFI directory":
# * Check we have EFI partition mounted as /boot/efi:
#    mount | grep efi
# * Install grub with EFI path:
#    sudo grub-install /dev/sda --efi-directory=/boot/efi
sudo update-grub

Then we leave chroot and unmount everything:

exit
sudo umount /mnt/root/{boot,dev,proc,sys,run} /mnt/root

Grub 2 Configuration

  • See here for reference
  • Never edit /boot/grub/grub.cfg. Instead edit /etc/default/grub and run sudo update-grub to udpate:
sudo vi /etc/default/grub
# ... edit as necessary ...
sudo update-grub

Always show the menu at boot

  • By default Grub hides the boot menu if there is only one OS. To always show the menu, comment out the lines as shown here:
#GRUB_HIDDEN_TIMEOUT=0
#GRUB_HIDDEN_TIMEOUT_QUIET=true

Change keyboard layout during rescue shell

See [7]

Uninstall Grub

Uninstalling Grub simply consists in restoring the original MBR. See Linux Disk Management.