Grub command line how to

From thelinuxwiki
Jump to: navigation, search

this page content taken from http://members.iinet.net/~herman546/p20/GRUB2%20How%20To%20Boot%20From%20CLI%20Mode.html

GRUB2 How To Boot From CLI Mode

Edited Saturday, October 02 2010 Document made with KompoZer This web-page is part of a larger site giving examples of how to install Windows+Ubuntu Linux operating systems 'dual boot' in a computer. Illustrated Dual Boot HomePage

GNU GRUB's Command Line Interface (CLI) is one of GRUB's most useful and powerful features. GNU GRUB is a boot loader but it is also something close to a miniature operating system in its own right. GNU GRUB can take a rich variety of commands and provide useful feedback to the user. GRUB's CLI Mode is one feature that sets GRUB head and shoulders above all other boot loaders or boot managers. GNU GRUB is a work in progress and the information in this website is incomplete and may be wrong and/or out of date. Please consult the official GNU GRUB 1.98-r2692 manual. This web page is about how to use GNU GRUB's Command Line Interface Mode to boot from. We will all need to know how to use the GRand Unified Bootloader's Command Line Interface sooner or later. There are times when we cannot just boot from the menu in the normal way. It could be because we just moved our operating system to a different disk with a dd command. Maybe we re-installed some other operating system and it has taken control of our first hard disk's MBR with an ordinary boot loader. Hopefully, we made a GNU GRUB Rescue CD, USB or floppy disk beforehand so we can boot our operating system easily providing we know the right commands.

You did make your own GRUB2 Rescue Disk didn't you?

If you forgot to make your own GRUB2 Rescue disk, 'shame on you', - but ... you're still not totally out of luck. You can download a Super Grub2 Disk (with GRUB2 in) it from Super Grub Disk - Super Grub Disk. Super Grub2 Disk is also included in Parted Magic Live CD, so if you have Parted Magic Live CD you can use that for a rescue disk, Parted Magic Live CD.


GNU GRUB version 1.97~beta4

[ Minimal BASH-like line editing is supported. For the first word, TAB lists possible command completions. Anywhere else TAB lists possible device/file completions. ESC at any time exits. ]


grub> _








GNU GRUB's Command Line Interface. (normally black and white by default)

How To Enter GRUB's CLI MODE

Your computer should automatically boot into GRUB's Command Line Interface when there is no /boot/grub/grub.cfg file present, or if GRUB can't find a grub.cfg file. Menu mode is for wimps. Real GNU Linux users know how to use GNU GRUB's Command Line Interface for booting with in an emergency, and for all kinds of special purposes. It's easy to learn how to use GRUB from the command line, and it should only take a few minutes for most people to get to understand the basics of it. If your computer boots to a GRUB Menu, you can switch into CLI mode instantly by pressing the 'c' key on your keyboard any time you like. To return to your menu, just press 'Esc'.

Summary - in case you're already an expert and you're in a hurry! There are just three commands necessary for booting a GNU-Linux operating system with GNU-GRUB. grub> linux (hd0,1)/vmlinuz root=/dev/sda1 grub> initrd (hd0,1)/initrd.img grub> boot "It's as easy as one, two, three!" The exact details to include in these commands will vary between one computer and another. If you don't understand the meaning of the above commands, read further down this page for a verbose explanation.

List of Commands for GRUB's Command Line In case you haven't already seen it, there's a page in this website containing a list of commands we can use, GRUB2 CLI Mode Commands. It's incomplete but you may refer to it while we're waiting for the official GNU GRUB Manual to be completely re-written.

Use the help command If you have more than one computer available, you can look at this website with one computer while you're working with the other one. I presume most people will be doing that. If you're stuck somewhere with no internet or there's no other computer handy, you can use GRUB's built in help simply by typing the help command. For help on the help command, type help help. For more info see: help command. The information in this website mostly came from the help command.

How to turn off a splashimage If you have a splashimage in the background, it can make some of the text on your screen difficult to see properly. You might want to turn that off and revert to a plain background while you're in CLI Mode. To turn off your background image, just type the background_image command without specifying any arguements. grub> background_image Your background image will disappear!

Changing Your Text Color If you still can't see your text, try using the set command to change your menu colors, set command.

TIP: 'Tab completion' is another of GNU GRUB's powerful features. Instead of typing a complete command or file name, we only need to type the first three or so letters, then when we press the 'Tab' key on our keyboard, GRUB will intelligently guess what we're trying to type and complete the rest of the command or file name for us. This is not only helpful for saving the work of typing and preventing spelling errors. It can also be used craftily for finding out what devices GRUB can see in a computer and what files they contain. It's a good idea to get into the habit of using tab completion whenever possible while we're using GRUB in CLI mode.

Taking A Look Around

1) You can use the ls command to take a look around and find out what disks are available, ls command.
2) If you gave your the took the time to set file system labels in your computer, you can use the search command with the -l option to look for your operating system by name, search command.
3) You can also use the chainloader command and the 'tab' key to get a list of disks and then a list of partitions in any of your disks, chainloader command.


How To Boot An Operating System Directly

Direct kernel booting is the most reliable way to boot a GNU LINUX operating system. If you're doing this from a rescue disk, it doesn't matter if you have GRUB in a CD, USB or some other hard drive partition or whatever, you can bypass all of the possible trouble spots by booting the Linux kernel directly. This method will work even if the boot loader part of the MBR has been corrupted or overwritten by some other boot loader. This method will also work even if you moved your entire Ubuntu operating system to a different partition with a dd command and haven't updated your grub.cfg yet. It will work if there's a mistake on your grub.cfg, or if boot loader files are corrupt or missing. It will even work if GRUB wasn't installed or has been completely deleted from the operating system you're trying to boot.

As far as I know, GNU GRUB does not boot Windows directly, see How To Chainload and Boot if you're trying to do that.

There are two ways of booting a Gnu-Linux operating system (kernel) directly using GNU GRUB. One is by using symlinks, (aka 'shortcuts') to the kernel and initrd files. The other way is by typing in the precise file path and file name for exactly the kernel and initrd you want to specify.

First, we'll try booting by the symlinks because that's the easiest.

We need two vital bits of information before we can perform a direct (kernel) boot.

 (i) the location of a kernel and intird.img or symlinks to a kernel and initrd.img files.
(ii) the location of /sbin/init, which will be in the operating system's 'root' file system.

A fast way to find that information will be to use the search command, see search command for more info. In the example below I typed the search command with the -f option, ('f' for 'file'), and I searched for 'vmlinuz', which is the name of the symlink to our Linux kernel in Ubuntu.Then I made a search for /sbin/init. search -f /vmlinuz hd0,1

search -f /sbin/init hd0,1 In this example, GRUB has returned the same answers, 'hd0,1' for both searches. Sometimes the kernel is in a different partition than the operating system's root file system, usually when we install Linux with a separate /boot partition.

TIP: It's also possible to boot with a kernel that's even located in some other disk, such as a USB flash memory drive if you need to for some reason.

The next command we'll use will be the linux command, for loading a kernel, grub> linux (hd0,1)/vmlinuz root=/dev/sda1 The linux command is for loading the kernel or loading a kernel via a symlink to a kernel. After the path and name of the kernel or symlink is where we put in kernel options. The most important kernel option is the root=(path to our /sbin/init), which we found with our search command a few minutes ago. The difference is, here we have converted it to Linux type device numbering. (hd0,1) is equivalent to /dev/sda1.

We can optionally add more kernel options after the root= option. I suggest the same ones we see in our grub.cfg files, 'ro','quiet' and 'splash' if you like. Other kernel options may be wanted or needed for special purposes, see Common Boot Options - Ubuntu Community Docs, and Kernel Parameters, www.kernel.org for a complete list.

I have a computer with 2 IDE and 2 SATA hard discs in it and I have the old problem of GRUB and the BIOS numbering my hard drives differently from the Linux kernel. Although for most computers (hd0,1) is equivalent to /dev/sda1, in my computer (hd0,1) is /dev/sdc1. In testing this how-to I found that I was most comfortable with root=UUID=xxxxxxxxxxxxxxx (the entire file system UUID typed in by hand). The file system UUID numbers can be found with tab completion after the (hd0, ... but unfortumately I wasn't able to use tab completetion for the UUID number, I had to type it all manually. If you have IDE and SATA drives and have trouble, try using the UUID number like I did.

The hard part's done, now just the initrd command, grub> initrd (hd0,1)/initrd.img After typing the initrd command, you type in the path and filename of the initrd.img that matches the kernel you're trying to boot or the symlink to a matching initrd.img. Kernels and their initrd.img files always come in pairs.

Finally, the boot command, grub> boot The boot command gives GRUB the final go-ahead to actually go and boot the specified operating system. This command is implicit (implied) at the end of grub.cfg menu entries, but when we're using GRUB from the Command Line we need to use the boot command to tell GRUB when we're ready to boot.

Press your Enter (or Return) key, and your operating system should boot!

The proper direct kernel boot is the same thing as direct booting via symlinks except we type in the exact filepath and filenames of the specific kernel and initrd.img files, like this, grub> linux (hd0,1)/boot/vmlinuz-2.6.27-9-generic root=/dev/sda1 grub> initrd (hd0,1)/boot/initrd.img-2.6.27-9-generic grub> boot We can use 'tab completion' for finding out the path and exact filename for the kernel and initrd.img files.

Now you may have noticed that the commands we use in CLI mode are exactly the same commands we have in our grub.cfg files which GRUB reads and executes automatically. That brings us to our next command, the configfile command.

How To Configfile and Boot

The configfile command tells GRUB to read commands from a specified configuration file. We can only use the config file command if the file contains commands our GRUB will be able to understand. If we're using GRUB2 and the config file we load contains GRUB2 commands it should be okay. It's no use trying to load a file full of LiLo commands or Legacy GRUB commands or commands for any other boot loader than GRUB2. Maybe a newer version of GRUB2 will boot an older version, but things might not work the other way around.

Providing the commands in a GNU - Linux operating system's GRUB2 grub.cfg are correct, and they're compatible with the version of GRUB2 we're using, we can use the configfile to load the file and make use of the commands it contains.

Here's an example, configfile (hd0,1)/boot/grub/grub.cfg Most of the time we can do this in one command because most people already know they have a /boot/grub/grub.cfg and besides, we will be using tab completion.

The configfile command will bring up our entire GRUB Menu for whatever operating system happens to be associated with the grub.cfg file we directed GRUB to open. Now we can select any title from that GRUB Menu and we should be able to boot an operating system or at least run the commands that are associated with that menu title.


search -f /boot/grub/grub.cfg hd0,1 If we just plugged in some new hard disks or USB drives and we're not sure which disk is which, we can use the search command if we want, it might make things easier.

An important thing to remember is it's still the same GRUB we started with will be doing the work of booting, and not the operating system's GRUB. That's the reason why the use of the configfile command is pretty much restricted to only other operating systems with the same version of GRUB as we have, or the same GRUB as in the rescue disk we're using. We could run into trouble if the GRUB2 we're using in our rescue disk is an older version than the operating system's and doesn't accept the same commands as the operating system's version of GRUB2. We can't use the configfile command to bring up a menu.lst from GRUB Legacy either, again because the commands will be all different.

What we can do though, is load a file with some other file name than grub.cfg. - such as a backup copy of grub.cfg, if you were careful enough to make one before the last changes you made to your grub.cfg file. If we used grub-mkconf with the -o option and typed a different path and file name than /boot/grub/grub.cfg we'd be able to find that file and load it instead of our regular grub.cfg file. It could be any file we made ourselves. The other file still needs to have valid GRUB commands in it, or commented lines, which will be compatible with the GRUB we're reading it with.

We can't use the configfile command to boot another boot loader either, it just doesn't work that way. The chainloader command is the one we use for booting another boot loader.


How To Chainload and Boot

The chainloader command is most commonly directed at either a hard disk's master boot record or the boot sector of a partition. Actually, we can chainload a file too and there's nothing wrong with doing that if it works, but thats not as well known as chainloading a sector.

If there's code written in the MBR or partition boot sector pointing to a boot loader then we can get GRUB to boot the other boot loader. GRUB can boot just about any other boot loader by chainloading. The other boot loader then takes over and does the booting. We can boot other Gnu - Linux loaders like LiLo or GRUB Legacy, GAG Boot Manager, and we can even boot foreign proprietary boot loaders too. In fact, chainloading is the only way to boot a Microsoft Windows system from GNU GRUB.

There needs to be code in the MBR or boot sector we're trying to chainload for the chainloader command to do any good.

   The MBR of the first hard disk almost always contains boot loader code for some boot loader.
   Non-first hard disks have MBRs but do not have code installed in them unless you installed it there yourself. If you did then you can chainload a boot loader by any non-first MBR. This includes USB external drives.
   The boot sectors of Gnu-Linux operating partitions do not contain code unless you installed it there. If you did then you can also boot a GNU-Linux operating system by chainloading.

That means we can't chainload those unless we installed boot loader code there ourselves at some time previously with linux commands. See How To Boot An Operating System Directly. Windows does install code in both the hard disk's MBR and also in the Windows partition boot sector, so it's normally easy to chainload a Windows installation.

Examples, grub> chainloader (hd0)+1

grub> boot This boots another boot loader by the code in the MBR of the first hard disk.

grub> chainloader (hd0,1)+1

grub> boot This boot another boot loader by the first sector of partition 1, if the boot sector of partition 1 contains boot loader code. If it doesn't work, try a different boot sector or file or try loading the kernel and initrd.img directly.

We can chainload any disk by the MBR or any partition by its boot sector.

Here's an example of a chainloader command in action, grub> chainloader (hd, <tab> Possible devices are: hd0 hd1 hd2 hd3 hd4 In this example I typed 'chainloader hd ' without completing the details after the command and pressed my 'tab'key. GRUB looked around and found five disks attached to this computer. I could try booting the MBR of any of these disks with the chainloader command.

For example, grub> chainloader (hd1) +1 That will boot any boot loader whose code is installed in the MBR of the (hd1), which means the second disk attached to this computer at the moment.

If I 'm not sure which hard disk I want to boot, or if the operating system I want to boot isn't bootable from and loader installed in the MBR, I could chainload a partition boot sector instead.

First I'll use the 'tab' key again to take a look at my partitions in (hd0), my first hard disk. I will type: chainloader (hd0, ...and press my 'tab' key. grub> chainloader (hd0, <tab> Possible partitions are:

       Partition hd0,1: Filesystem ntfs
       Partition hd0,2: Filesystem reiserfs, Label INTREPID
       Partition hd0,3: Filesystem reiserfs, Label GRUB
       Partition hd0,5: Filesystem reiserfs, Label JAUNTY
       Partition hd0,6: Unknown filesystem 

The result is, GRUB lists all the partitions I have in (hd0), which is my first hard disk, along with quite a few interesting details about what each partition contains. Details now include last modification time of the file system, and the day and the file system UUID numbers. The extra details have been left out of this illustration for clarity.

I have a Windows operating system in (hd0,1), I could chainload that if I wanted to. I could chainload the other GRUB partition, (hd0,3) and boot from that GRUB. The unknown file system is probably a swap area. I'll try chainloading my old Jaunty Jackalope installation in (hd0,5), I remember I have installed GRUB to the partition boot sector in Jaunty Jackalope a while back, so I should be able to chainload it okay. grub> chainloader (hd0,5)+1 grub> boot

 .... and Jaunty Jackalope's GRUB menu appears.

I'm now using Jaunty Jackalope's GRUB to boot with and I can boot any item in Jaunty Jackalope's GRUB menu. If I had GRUB Legacy in Jaunty Jackalope and I press my 'c' key from Jaunty's GRUB Menu I would need to be prepared to revert to commands for GRUB Legacy's Command Line Interface. GRUB's Command Line Interface.