Bienvenido! - Willkommen! - Welcome!

Bitácora Técnica de Tux&Cía., Santa Cruz de la Sierra, BO
Bitácora Central: Tux&Cía.
Bitácora de Información Avanzada: Tux&Cía.-Información
May the source be with you!

Sunday, November 6, 2011

How to use Grub2 to boot Linux manually

Source oct 2009
Introduction

I notice Grub2 has been shipped by the latest Ubuntu so I have a played with it and reported the following.

For those who are already aware Grub has an unique feature that you can communicate with it before an operating system is booted. The instruction to talk to Grub is given in any text Grub menu screen by pressing the "c" key and return. Then you will be greeted by a command prompt known as Grub prompt. In this mode Grub has an command interpretor very much like Dos or any mini operating system. Both Grub1 and Grub2 have such capability. The biggest secret in Linux I have found is that there in no installed operating system in a PC Grub cannot boot in a Grub prompt. A little bit of knowledge of it will enable you to kiss all your booting problems good bye.
There are significant differences between Grub1 (version 0.9x) and Grub2 (version 1.x) so the best way is to treat them as two different boot loaders. A bit of mixing is possible as described below.
For direct booting Grub2 can boot an installed Linux packaged with Grub1 but not necessarily the other way round. This is because a distro shipped with Grub2 may have Ext4 filing system installed by default. Grub1 cannot read a Ext4 filing system.
For indirect booting both Grub1 and Grub2 can boot each other by chainloading as the filing system is not involved. Indeed this is why Grub1 not able to read a NTFS partition but still manages to boot the boot loader inside of any MS Windows, regardless it is an old WIn2k or the Win7 that is in a trial stage.
How to get hold of Grub2 May be there is a better way but I used a Grub2 floppy or a Grub2 CD. Each can only be made from a Live CD that has been shipped with Grub2, like Ubuntu 9.10. All you need to do is to boot up such a Linux Live CD, get into a root terminal, put a floppy into the drive and issue the following commands (originated from FranklinPiat/grub-mkrescue.manpage - GRUB Wiki)
Code:
grub-mkrescue --image-type=floppy /tmp/grub-rescue.flp
dd if=/tmp/grub-rescue.flp of=/dev/fd0 bs=1024
Basically the grub-mkrescue copies Grub2 image into the specified file and you use the dd command to write the same onto a floppy. For a bootable Grub2 CD the instructions are
Code:
grub-mkrescue --image-type=cdrom /tmp/grub-rescue.iso
wodim /tmp/grub-rescue.iso
You can drop both floppy (any spare used one and no need to format) and a blank CD to do them simultaneously. Bootable Grub floppy and Grub CD are the most lethal weapons in the booting business.
Thereafter either medium boots you to a command prompt in Grub2.
As similar to Grub1 there should be no installed PC system that cannot be booted by a Grub prompt from Grub2. I tried different Linux and MS Windows and can confirm this. This is done by indirect booting using chainloading.
Indirect booting by chainloading
For Grub1 if you want to fire up an OS, which can be any Dos, MS Windows, BSD, Solaris or Linux, installed in the 2nd partition of the 1st disk the Grub Legacy commands will be
Code:
root (hd0,1)
chainloader +1
boot
For Grub2 the commands are slightly different and the changes are highlighted in red
Code:
set root=(hd0,2)
chainloader +1
boot
For some reasons the partition number no longer starts from zero in Grub2. I believe the zero start for disk number in both Grub1 and Grub2 is for compatibility with the MS systems which use the similar numbering arrangement.
Direct booting by naming the kernel and initrd files
Grub1 and Grub2 can boot only OSes they support and since MS Windows are not supported no direct booting is possible.
There are similar small changes in commands between Grub1 and Grub2 here.
Say if I have a Linux in same partition as the above.
In a standard Linux the kernel name always starts with vmlinuz and the initrd file starts with initrd. These files by default are stored in the /boot subdirectory. Both Grub1 and Grub2 can do completion with the tab key so if I type
Code:
kernel /boot/vmli
and follow by the tab key Grub will try to find all the files in /boot subdirectory to match the first "vmli" caharcters. Therefore there is no need to know the kernal and initrd filenames in advance if one knows to use the tab key to complete the command.

Say if my Linux has vmlinuz-2.6.18-6-686 and initrd.img-2.6.18-6-686 in /boot subdirectory one can boot up the Linux with Grub1 commands of
Code:
root (hd0,1)
kernel  /boot/vmlinuz-2.6.18-6-686 root=/dev/sda11 ro 
initrd   /boot/initrd.img-2.6.18-6-686
boot
For Grub2 the corresponding commands will be
Code:
set root=(hd0,2)
linux  /boot/vmlinuz-2.6.18-6-686 root=/dev/sda11  
initrd   /boot/initrd.img-2.6.18-6-686
boot
Grub2 seems to be able to do away with the "read only" (ro) parameter which is required for Grub1.
The above is just an early report of Grub2 which is beginning to show its face in September 2009. It is still an early stage to see how it performs in practice. The current documentation is still in an appalling state. However for a few hours playing with it Grub2 appears to be reasonably complete. It is about 3 to 4 years overdue.

Edited 10/9/09-----------------------
I found out Grub2's can be restored in its root partition by Grub2 commands identical to Grub1. You need to first fire up the Linux with Grub2 using the direct method, say this is in partition sda16, and issue this command in a root console

Code:
grub-install /dev/sda16
Thereafter the Linux in sda16, in Ext4 and has Grub2 can be booted by any version of Grub indirectly. I tried the oldest Grub floppy (Version 0.91) in my collection, using the normal Grub1 commands

Code:
root (hd0,15)
chainloader +1
boot
------------------------------------------
in grub shell type help
You will find "linux" listed there.
Also the command "ls" and "cat" are pretty useful too.
"ls" list the files and directories like

Code:
ls (hd0,1)/
and cat can be used to display any text file. Say if your lucid is in partition (hd0,1) then you can see the grub.cfg from command
Code:
cat (hd0,1)/boot/grub/grub.cfg

No comments: