GRUB
From ArchWiki
Article summary |
---|
Covers various aspects of Arch Linux's default bootloader, the GRand Unified Bootloader (GRUB). |
Available in languages |
English |
עברית |
简体中文 |
Русский |
Español |
Français |
Italiano |
Nederlands |
Related articles |
GRUB2 |
grub-gfx |
LILO |
MBR |
GNU GRUB is a Multiboot bootloader. It was derived from GRUB, the GRand Unified Bootloader, which was originally designed and implemented by Erich Stefan Boleyn.
Briefly, the bootloader is the first software program that runs when a computer starts. It is responsible for loading and transferring control to the Linux kernel. The kernel, in turn, initializes the rest of the operating system.
Currently, GRUB is the de facto standard bootloader of Linux, and is expected to be superseded by GRUB2 in the near future. When this happens, "GRUB" will become "GRUB Legacy".
Contents |
Installation
The GRUB package is installed by default when you install Arch Linux. If you previously selected not to install this package, you can install now via:
# pacman -Sy grub
Additionally, GRUB must be installed to the boot sector of a drive or partition to serve as a bootloader. This is covered in the #Bootloader installation section.
Configuration
The configuration file is located at /boot/grub/menu.lst. Edit this file to suit your needs.
- timeout # -- time to wait (in seconds) before the default operating system is automatically loaded.
- default # -- the default boot entry that is chosen when the timeout has expired.
An example configuration:
# Config file for GRUB - The GNU GRand Unified Bootloader # /boot/grub/menu.lst # DEVICE NAME CONVERSIONS # # Linux Grub # ------------------------- # /dev/fd0 (fd0) # /dev/sda (hd0) # /dev/sdb2 (hd1,1) # /dev/sda3 (hd0,2) # # FRAMEBUFFER RESOLUTION SETTINGS # +-------------------------------------------------+ # | 640x480 800x600 1024x768 1280x1024 # ----+-------------------------------------------- # 256 | 0x301=769 0x303=771 0x305=773 0x307=775 # 32K | 0x310=784 0x313=787 0x316=790 0x319=793 # 64K | 0x311=785 0x314=788 0x317=791 0x31A=794 # 16M | 0x312=786 0x315=789 0x318=792 0x31B=795 # +-------------------------------------------------+ # for more details and different resolutions see # http://wiki.archlinux.org/index.php/GRUB#Framebuffer_Resolution # general configuration: timeout 5 default 0 color light-blue/black light-cyan/blue # boot sections follow # each is implicitly numbered from 0 in the order of appearance below # # TIP: If you want a 1024x768 framebuffer, add "vga=773" to your kernel line. # #-* # (0) Arch Linux title Arch Linux [/boot/vmlinuz26] root (hd0,0) kernel /vmlinuz26 root=/dev/sda3 ro initrd /kernel26.img # (1) Windows #title Windows #rootnoverify (hd0,0) #makeactive #chainloader +1
Finding GRUB's root
GRUB must be told where its files reside on the system, since multiple instances may exist (i.e., in multi-boot environments). GRUB files always reside under /boot, which may be on a dedicated partition.
- Hard disks are defined as (hdX); this also refers to any USB storage devices.
- Device and partitioning numbering begin at zero. For example, the first hard disk recognized in the BIOS will be defined as (hd0). The second device will be called (hd1). This also applies to partitions. So, the second partition on the first hard disk will be defined as (hd0,1).
If you are unaware of the the location of /boot, use the GRUB shell find command to locate the GRUB files. Enter the GRUB shell by:
# grub
The following example is for systems without a separate /boot partition, wherein /boot is merely a directory under /:
grub> find /boot/grub/stage1
The following example is for systems with a separate /boot partition:
grub> find /grub/stage1
GRUB will find the file, and output the location of the stage1 file. For example:
(hd1,0)
This value should be entered on the root line in your configuration file. Type quit to exit the shell.
Dual booting with Windows
Add the following to the end of your /boot/grub/menu.lst (assuming that your Windows partition is on the first partition of the first drive):
title Windows rootnoverify (hd0,0) makeactive chainloader +1
If Windows is located on another hard disk, the map command must be used. This will make your Windows install think it is actually on the first drive. Assuming that your Windows partition is on the first partition of the second drive:
title Windows map (hd0) (hd1) map (hd1) (hd0) rootnoverify (hd1,0) makeactive chainloader +1
Dual booting with GNU/Linux
This can be done the same way that an Arch Linux install is defined. For example:
title Other Linux root (hd0,2) kernel /path/to/kernel root=/dev/sda3 ro initrd /path/to/initrd
However, there may be other options that are required, and an initial RAM disk may not be used. Examine the other distribution's /boot/grub/menu.lst to match boot options, or see #chainloader and configfile (recommended):
chainloader and configfile
To facilitate system maintenance, the chainloader or configfile command should be used to boot another Linux distribution that provides an "automagic" GRUB configuration mechanism (e.g. Debian, Ubuntu, openSUSE). This allows the distribution to manage its own menu.lst and boot options.
- The chainloader command will load another bootloader (rather than a kernel image); useful if another bootloader is installed in a partition's boot sector (GRUB, for example). This allows one to install a "main" instance of GRUB to the MBR and distribution-specific instances of GRUB to each partition boot record (PBR).
- The configfile command will instruct the currently running GRUB instance to load the specified configuration file. This can be used to load another distribution's menu.lst without a separate GRUB installation. The caveat of this approach is that other menu.lst may not be compatible with the installed version of GRUB; some distributions heavily patch their versions of GRUB.
For example, GRUB is to be installed to the MBR and some other bootloader (be it GRUB or LILO) is already installed to the boot sector of (hd0,2).
--------------------------------------------- | | | | % | | M | | | B % | | B | (hd0,0) | (hd0,1) | L % (hd0,2) | | R | | | % | | | | | % | --------------------------------------------- | ^ | chainloading | -----------------------------
One can simply include in menu.lst:
title Other Linux root (hd0,2) chainloader +1
Or, if the bootloader on (hd0,2) is GRUB:
title Other Linux root (hd0,2) configfile /boot/grub/menu.lst
The chainloader command can also be used to load the MBR of a second drive:
title Other drive rootnoverify (hd1) chainloader +1
Bootloader installation
GRUB may be installed from a live environment, or directly from a running Arch install. The GRUB bootloader does not usually need to be reinstalled. Installation is only necessary if:
- a bootloader is not already installed
- another operating system overwrites the bootloader
- the bootloader fails for some reason
For clarification, installation is NOT necessary if:
- the configuration file is updated
- the GRUB package is updated
Before continuing, please note:
- Installing the GRUB bootloader may need to be done from within the installed environment (e.g. with RAID configurations). You should Change Root from another Linux environment if needed.
- Be sure that your GRUB configuration is correct (/boot/grub/menu.lst) before proceeding. Refer to #Finding GRUB's root to ensure your devices are defined correctly.
- GRUB must be installed on the MBR (first sector of the hard disk), or the first partition of the first storage device to be recognized by the BIOS. To allow individual distributions the ability to manage their own GRUB menus, multiple instances of GRUB can be used, see #chainloader and configfile.
- The *stage* files are expected to be in /boot/grub, which may not be the case if the bootloader was not installed during system installation. This can be accomplished by: cp -a /usr/lib/grub/i386-pc/* /boot/grub.
First, enter the GRUB shell:
# grub
Use the root command with the output from the find command (see #Finding GRUB's root) to instruct GRUB which partition contains stage1 (and therefore, /boot):
grub> root (hd1,0)
Installing to the MBR
The following example installs GRUB to the MBR of the first drive:
grub> setup (hd0)
Installing to a partition
The following example installs GRUB to the first partition of the first drive:
grub> setup (hd0,0)
After running setup, enter quit to exit the shell. Reboot to test.
Alternate method (grub-install)
Use the grub-install command followed by the location to install the bootloader. For example to install the GRUB bootloader to the MBR of the first drive:
# grub-install /dev/sda
GRUB will indicate whether it successfully installs. If it does not, you will have to use the GRUB shell method.
Tips and tricks
Additional configuration notes.
Graphical boot
For those desiring eye candy, see grub-gfx.
Framebuffer resolution
One can use the resolution given in the menu.lst, but you might want to use your LCD wide-screen at its full native resolution. Here is what you can do to achieve this:
On Wikipedia, there is a list of extended framebuffer resolutions (i.e. beyond the ones in the VBE standard). But, for example, the one I want to use is 1440x900 and vga=867 does not work. This may be because those resolution are outside of the standard and graphic card manufacturers use the codes they prefer.
So instead of using that table, I suggest using this method:
- Install lrmi package from [community] that contains the vbetest tool (not available for 64-bit Arch, install hwinfo from [AUR] instead and run hwinfo --vbe).
- Run vbetest as root and in a console already running on framebuffer. While vbetest will return results in a terminal in X, you will not be able to test using vbetest -m <mode> unless you are running a framebuffer console.
- Then note the number in [ ] corresponding to your desired resolution with vbetest. With hwinfo the six digits following Mode are the values you'll need.
- Press 'q' to quit vbetest interactive prompt.
- Add 512 to the discovered vbetest value for your monitor size and use the total as code to use in vga= kernel option in menu.lst. For hwinfo copy the six digit code to the vga= option.
- Reboot.
For example vbetest on one computer:
[356] 1440x900 (256 color palette) [357] 1440x900 (8:8:8)
So here the number you want is 357. Then, 357 + 512 = 869, so you will use vga=869. Add your value to the end of the kernel line in menu.lst as shown below:
kernel /vmlinuz26 root=/dev/sda1 ro vga=869
On another desktop with hwinfo:
Mode 0x0364: 1440x900 (+1440), 8 bits Mode 0x0365: 1440x900 (+5760), 24 bits
And the kernel line:
kernel /vmlinuz26 root=/dev/sda1 ro vga=0x0365
- (8:8:8) is for 24-bit color (or 32-bit?)
- (5:6:5) is for 16-bit color
- (5:5:5) is for 15-bit color
Alternative method
This is an easy way to find the resolution code using only GRUB itself.
On the kernel line, specify that the kernel should ask you which mode to use.
kernel /vmlinuz26 root=/dev/sda1 ro vga=ask
Now reboot. GRUB will now present a list of suitable codes to use and the option to scan for even more. You can pick the code you would like to use (don't forget it, it is needed for the next step) and boot using it. The number you have picked here is the hexadecimal number, in order to use it at the kernel line you can transform it into a decimal number.
Now replace ask in the kernel line with the correct one you have picked.
e.g. the kernel line for [369] 1680x1050x32 would be:
kernel /vmlinuz26 root=/dev/sda1 ro vga=873
Naming by label
If you alter (or plan to alter) partition sizes from time to time, you might want to consider defining your drive/partitions by a label. You can label ext2, ext3, ext4 partitions by:
e2label </dev/drive|partition> label
The label name can be up to 16 characters long but cannot have spaces for GRUB to understand it. Then define it in your menu.lst:
kernel /boot/vmlinuz26 root=/dev/disk/by-label/Arch_Linux ro
Password protection
You can enable password protection in the GRUB configuration file for operating systems you wish to have protected. Bootloader password protection may be desired if your BIOS lacks such functionality and you need the extra security.
First, choose a password you can remember and then encrypt it:
# grub-md5-crypt Password: Retype password: $1$ZOGor$GABXUQ/hnzns/d5JYqqjw
Then add your password to the beginning of the GRUB configuration file (the password must be at the beginning of the configuration file for GRUB to be able to recognize it):
# general configuration timeout 5 default 0 color light-blue/black light-cyan/blue password –md5 $1$ZOGor$GABXUQ/hnzns/d5JYqqjw
Then for each operating system you wish to protect, add the lock command:
# (0) Arch Linux title Arch Linux lock root (hd0,1) kernel /boot/vmlinuz26 root=/dev/disk/by-label/Arch_Linux ro initrd /boot/kernel26.img
Restart with named boot choice
If you realize that you often need to switch to some other non-default OS (e.g. Windows) having to reboot and wait for the GRUB menu to appear is tedious. GRUB offers a way to record your OS choice when restarting instead of waiting for the menu, by designating a temporary new default which will be reset as soon as it has been used.
Supposing a simple menu.lst setup like this:
# general configuration: timeout 10 default 0 color light-blue/black light-cyan/blue # (0) Arch title Arch Linux root (hd0,1) kernel /boot/vmlinuz26 root=/dev/disk/by-label/ARCH ro initrd /boot/kernel26.img # (1) Windows title Windows XP rootnoverify (hd0,0) makeactive chainloader +1
Arch is the default (0). We want to restart in to Windows. Change default to "saved" (default saved -- this will record the current default in a default file in the GRUB directory whenever the savedefault command is used. Now add the line savedefault 0 to the bottom of the Windows entry. Whenever Windows is booted, it will reset the default to Arch, thus making changing the default to Windows temporary.
Now all that is needed is a way to easily change the default manually. This can be accomplished using the command grub-set-default. So, to reboot into Windows, enter the following command:
sudo grub-set-default 1 && sudo shutdown -r now
For ease of use, you might to wish to implement the "Allow users to shutdown fix" (including /sbin/grub-set-default amongst the commands the user is allowed to issue without supplying a password).
LILO and GRUB interaction
If the LILO package is installed on your system, remove it with
pacman -R lilo
as some tasks (e.g. kernel compilation using make all) will make a LILO call, and LILO will then be installed over GRUB. LILO may have been included in your base system, depending on your installer media version and whether you selected/deselected it during the package selection stage.
GRUB boot disk
First, format a floppy disk:
fdformat /dev/fd0 mke2fs /dev/fd0
Now mount the disk:
mount -t ext2 /dev/fd0 /mnt/fl
Install GRUB to the disk:
grub-install --root-directory=/mnt/fl '(fd0)'
Copy your menu.lst file to the disk:
cp /boot/grub/menu.lst /mnt/fl/boot/grub/menu.lst
Now unmount your floppy:
umount /mnt/fl
Now you should be able to restart your computer with the disk in the drive and it should boot to GRUB. Make sure that your floppy disk is set to have higher priority than your hard drive when booting in your BIOS first, of course.
See also: Super GRUB Disk
Troubleshooting
GRUB Error 17
If your partition table gets messed up, an unpleasant "GRUB error 17" message might be the only thing that greets you on your next reboot. There are a number of reasons why the partition table could get messed up. Commonly, users who manipulate their partitions with GParted -- particularly logical drives -- can cause the order of the partitions to change. For example, you delete /dev/sda6 and resize /dev/sda7, then finally re-create what used to be /dev/sda6 only now it appears at the bottom of the list, /dev/sda9 for example. Although the physical order of the partitions/logical drives has not changed, the order in which they are recognized has changed.
Fixing the partition table is easy. Boot from your Arch CD, login as root and fix the partition table:
# fdisk /dev/sda
Once in disk, enter e[x]tra/expert mode, [f]ix the partition order, then [w]rite the table and exit.
You can verify that the partition table was indeed fixed by issuing an fdisk -l. Now you just need to fix GRUB. See the #Bootloader installation section above.
Basically you need to tell GRUB the correct location of your /boot then re-write grub to the MBR on the disk.
For example:
# grub
grub> root (hd0,6) grub> setup (hd0) grub> quit
See [this page] for a more in-depth summary of this section.
Edit GRUB entries in the boot menu
Once you have selected and entry in the boot menu, you can edit it by pressing key e. Use tab-completion if you need to to discover devices then Esc to exit. Then you can try to boot by pressing b. Note: these settings will not be saved.
device.map error
If an error is raised mentioning /boot/grub/device.map during installation or boot, run:
# grub-install --recheck /dev/sda
to force GRUB to recheck the device map, even if it already exists. This may be necessary after resizing partitions or adding/removing drives.
Reboot pull-down menu in KDE does not have any effect
If you've opened a sub-menu with the list of all operating systems configured in GRUB, selected one, and upon restart, you still booted your default OS, then you might want to check if you have the line:
default saved
in /boot/grub/menu.lst.