Sunday 2 October 2011

Bootloader and It's options

After the BIOS completes the POST successfully, It transfers the control to the MBR (Master boot record). MBR is the 512 bytes on the first sector of the device in which it is stored(eg Hard Disk). It's first 446 bytes store the first stage of the bootloader, the next 64 bytes contain the partition table for the device. The 2 bytes in the end a are used for other purposes.


A bootloader is a software that boots an operating system. The first stage of the bootloader (Initial Program Loader) contains the location of the second stage of the boot loader, which is stored in the hard disk, and transfers control to it..


Installing and Recovering GRUB(Grand Unified Boot loader):


While working with bootloaders, you often come across a situation when your computer is unbootable, Here is how you recover MBR by installing grub(assuming you have a linux partition already installed!).


You will need a linux distribution stored in a livecd, or a pen drive, and boot from it.


open the terminal and execute:


sudo fdisk -l


fdisk is the partition table manipulator, we are using fdisk -l, which lists the partition table for all devices.

Before trying to install a bootloader, It is very important to understand this partition table.
I have two storage devices working on the computer - Hard disk and a pendrive

Which Disk is /dev/sda? Since I know the size of my hard drive and the partitions I made - So I can easily guess it is my Hard Drive.

I have 3 partitions which are formatted as NTFS - A windows filesystem.
The 2 large ones - /dev/sda2 and /dev/sda4 are my windows C:/ and D:/ ( I can easily make out since I am the one who partitioned them)

What is stored in the smallest NTFS partition /dev/sda1? This is where the windows bootloader - Windows Boot Manager is stored ( Is it possible to have multiple bootloaders?)


As for the rest of the partitions:


/dev/sda3 is an extended partition

/dev/sda5 is where the linux os( in my case ubuntu) is stored.


/dev/sda6 is the swapspace I defined for ubuntu.


The disk /dev/sdb is my pendrive, from which I am booting ubuntu. Since my pendrive is bootable, It also has it's own bootloader whose second stage looks something like:

This bootloader is stored on /dev/sdb1 along with the version of ubuntu stored in the pen drive


Now you have a good idea what these partitions mean, we can go ahead and install grub,


Installing grub is easy, you simply need to run:


sudo grub-install /dev/sda


Here /dev/sda is the device you are installing it for.

We get an error:



Remember, I am booting ubuntu's live image, and I have a version of ubuntu installed on the hard disk, which I cannot access, since my bootloader has crashed.

Now, I am using a terminal whose root is that of the live image, not the linux which is installed on the computer.

Therefore, install-grub it tries to install it into the live image, which is read-only and I get this error.


I want to install grub in my ubuntu partition, which is /dev/sda5. Now the next question comes, how do I make changes in the linux partition stored in the hard drive from my live cd?


For this we use mount command to attach the linux partition into the live cd,


sudo mount /dev/sda5 /mnt


It mounts the linux partition as read-writable, so I can actually have an affect on it,

Next I execute,


sudo mount /dev/sda5 /mnt/boot


Now I need to do another thing, make the /dev folder accessible in /mnt folder.


sudo mount --bind /dev /mnt/dev


--bind makes the same contents is accessible in two places. I kept on getting errors since I skipped this step, so make sure you don't.

What was the purpose of the above 3 commands?


You will notice /mnt now resembles the root of my the ubuntu stored in my hard drive, The purpose was to mirror /mnt folder of the live cd, into the root of the stored ubuntu. Why I did this will be clear in the next step..

I temporarily make the /mnt folder root using chroot command


sudo chroot /mnt


Now execute ls and surprise! you are now accessing the root folder of the linux distribution stored in your hard drive, (in my case ubuntu) from within the live image, and can now make changes to it.


Go ahead and try out the first command,

sudo grub-install /dev/sda


And reboot to find the Grub splash screen, waiting for you.


Short form - Simply all the commands you need to run to install grub from the live image


boot using usb/livecd and open terminal


sudo fdisk -l

sudo mount /dev/sda* /mnt

sudo mount /dev/sda* /mnt/boot

sudo mount /dev /mnt/dev

sudo chroot /mnt

sudo grub-install /dev/sda

exit

sudo reboot


* stands for the linux partition number, which you find using fdisk -l


Configuring Grub:


Grub stores its menu information in the file /boot/grub/grub.cfg which is not meant to be edited!

The file which we edit is /etc/default/grub and then we run:


sudo update-grub


which incorporates the changes we made into grub.conf file, now run


sudo gedit /etc/default/grub


I have explained some of the entries we tested:


# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0

# Used to set the default menu entry(corresponding to grub.cfg)

#GRUB_HIDDEN_TIMEOUT=0

# Controls how long a blank screen in displayed, before showing the grub menu

GRUB_HIDDEN_TIMEOUT_QUIET=true

# False will display a countdown

GRUB_TIMEOUT=10

# Seconds grub waits before booting the default entry
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

# Controls the splash image when linux processes are taking place, replacing it with "splash" generates a splash image in the background of running processes
GRUB_CMDLINE_LINUX=""
# Entries mentioned here are added to the linux command line, which are executed while booting, this is used to pass parameters to the kernel
# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"


To make changes permanent, run the previous command after saving the file


sudo gedit /etc/default/grub


and reboot to see them in action.


When grub is installed, it automatically reads the patition table and looks for all linux kernels and operating systems present, and adds them to the configuration file. It is possible to add custom entries, and remove existing entries,


To add a custom entry to the grub menu, we have to create a custom file in /etc/grub.d/ (The name of the file, decides it's order in the menu), we edited the custom file 40_custom


the first 2 lines must be


#!/bin/sh

exec tail -n +3 $0


I have installed an os on /dev/sda5 and I want to chainload it,


I will create the lines:


menuentry "Sample OS" {

set root=(hd0,5)

chainloader +1

}


We found out that an easy way to add custom images is to copy an entry from grb.cfg and manually edit it.

Now I need to make this file executable, I will use the chmod command:


sudo chmod +x /etc/grub.d/40_custom


Now I run


sudo update-grub


Which automatically updates grub.cfg with the sample OS i created, and the next time I reboot, I get



To remove a grub entry, we simply do the reverse, we remove it from /etc/grub.d folder, and run update-grub.





Installing and configuring Lilo (Linux Loader):


open the terminal, and run


sudo apt-get install lilo

sudo liloconfig


follow the onscreen instructions and chose the default settings, which generates the default lilo configuration file,






you have to edit it using:


sudo gedit /etc/lilo.conf


Since every system is different, The configuration file for them will be different.Be careful, one wrong command in its configuration would result in your partition tables getting messed up, and you would have to recover the bootloader, and in some cases, reinstall the operating system.


Here is what my default configuration file looked like after removing the comments:


boot=/dev/sda6
root=/dev/sda6

bitmap=/boot/debian.bmp
bmp-colors=10,0,,7,0,
bmp-table=16,12,1,12,16
bmp-timer=34,28,13,0,

install=bmp
prompt
timeout=50
large-memory
map=/boot/map
vga=normal

image=/boot/vmlinuz-2.6.38-8-generic
label="Lin 2.6.38img0"
initrd=/boot/initrd.img-2.6.38-8-generic
read-only

image=/boot/memtest86+.bin
label="Memory Test+"
read-only


What each part of the code does is explained in the comments, when the file is generated.

I'll highlight what are the important components are, which you need to make necessary changes in the system:

boot=Variable specifies where to install lilo, It can be installed in the boot sector your linux partition, or the master boot record of the hard drive, in which case, we set it to /dev/sda.


root= specifies where the root filesystem is, It is the root of your linux partiton, in my case /dev/sda6

The next few set of variables control the bitmap configuration, or the splash image we will see when we boot up lilo (Remember there were 4 default bitmaps), You can read details about these variables and how to change your bitmap here
bitmap=specifies the path of the bitmap file

delay=specifies the amount of time(In tenths of a second) lilo waits before it loads the first image
timeout=specifies the amount of time(In tenths of a second) lilo waits before it loads the default image

The default image is specified by using,
default=

map=points to the boot map file automatically generated by LILO and is used internally.

image=specifies the linux kernel to boot,arguments (linux 1 to go into single user mode, it will ask for the password)


label=decides its label in the lilo menu

initrd=specifies the path of the initial ram disk image of the image


read only tells the kernel to mount the filesystem as read-only, It is remounted later as readwrite, this is how linux is to be booted by default.



This is the default set up, Now for the changes we made:



You will notice, there is no option to boot windows, we achieve this by a process called chain loading, where the second stage of a bootloader redirects to the second stage of another bootloader.

we add the lines:


other=/dev/sda1

label="Windows 7"


We know that the windows 7 loader was stored in /dev/sda1, these 2 lines simply creates another option named Windows 7 in the menu, which redirects us to the Windows Boot Manager stored in /dev/sda1

Suppose we had another linux based os stored in the bootable partition /dev/sda7 having its own bootloader stored in the same partition, we could have set up a triple boot by using the other command again,


other=/dev/sda7

label=linux


this would chainload the boatloader in /dev/sda7

suppose we knew where the kernel image of this linux was stored, we could have booted it directly from lilo using the image= command.


image=/boot/Image

label=linux

initrd=/boot/initialramdiskimage

read-only


This way we can boot more than two operating systems.


Another important step was securing Lilo,

By default Lilo allows anyone to boot into single user mode by pressing init 1 in the lilo prompt( runlevel 1 allows anyone to give root commands without needing a root password)

so we need to password protect the bootloader, this is achieved by adding the line


password=


We can also secure our boot images with a password, by adding a password= line below the image=line, along with a mandatory/restricted line


image=/boot/vmlinuz-2.6.38-8-generic

password=

restricted

label="Lin 2.6.38img0"

initrd=/boot/initrd.img-2.6.38-8-generic

read-only


mandatory always asks for the password, while restrict only asks if you try to boot it with extra arguments (linux 1 to go into single user mode)


We found out that having a pendrive inserted conflicts with the lilo installation, so we had to add a line which made lilo ignore the device( /dev/sdb)


disk=/dev/sdb inaccessible


After editing the conf file, which looked like


boot=/dev/sda

root=/dev/sda6

bitmap=/boot/debian.bmp

bmp-colors=10,0,,7,0,

bmp-table=16,12,1,12,16

bmp-timer=34,28,13,0,

install=bmp

prompt

timeout=50

large-memory

map=/boot/map

vga=normal


disk=/dev/sdb inaccessible


image=/boot/vmlinuz-2.6.38-8-generic

password=persistent

restricted

label="Ubuntu 11.04"

initrd=/boot/initrd.img-2.6.38-8-generic

read-only


image=/boot/memtest86+.bin

label="Memory Test+"

read-only


other=/dev/sda1

label="Windows 7"


we installed lilo by running:


sudo lilo

in the terminal,


which will replace the MBR with lilo, provided your config file is correct, and become the default bootloader,


The next time we booted up our computer, we were greeted with,






Grub vs Lilo - our personal experience


We found Grub much smarter and easy to use than Lilo,


Lilo stores its information in the MBR. This is why if we add an os , the stage 1 Lilo bootloader has to be manually overwritten, which is risky because a faulty Lilo configuration file may leave the system unbootable.


Grub on the other hand, the other hand is very smart, it can read your partition tables and generates its configuration file taking them into account. It can be installed in a linux partition(It does not need to rewrite the MBR). It's installation is simple and easy , we never ran across an error when installing it.


An interesting feature about grub is that any changes you make into the grub configuration file is automatically taken into account the next time you restart your computer, while the changes in lilo.conf need to be written into the mbr, for which we have to install lilo again.


Grub also has a powerful command line interface( brought up by pressing 'c'), Lilo allows only one command.


Working with Windows bootloaders (A general idea):



Case-1 ( Windows versions uptill Windows NT and Windows Vista/7 installed)

If you choose Windows Vista/7, Windows Boot Manager starts winload.exe ( the bootloader of Windows Vista /7 ) to open Windows.

If you choose an Earlier Version Of Windows Windows Boot Manager starts NTLDR to open Windows.


Case-2 ( Ubuntu and Windows Vista/7 insatlled )

When Windows 7 is selected from the Boot Menu,then GRUB,GRUB2 or Lilo will call the Windows Boot Manager whcih will in turn call winload.exe.


Bcdedit is a powerful tool that Windows Vista and Windows 7 uses to manage the boot loader entries.


Click Start, click All Programs, and then click Accessories.

Right-click Command Prompt and then click Run As Administrator.

Type bcedit.exe



To view detailed information about using BCDEdit, run bcdedit /? from a command prompt.


Making changes to your BCD registry file can render your computer unbootable. Therefore, before making changes to your BCD registry file, you should make a backup copy, have a bootable Windows DVD available, and be prepared to restore the original BCD registry file. To make a backup of your current BCD registry, call the BCDEdit /export command, as shown here.

1.

bcdedit /export backupbcd.bcd

Later, you can restore your original BCD registry file by calling the BCDEdit /import command, as shown here.


bcdedit /import backupbcd.bcd

Note The file name and extension you use are not significant.


2.

The boot menu, by default, is displayed for 30 seconds if you have more than one boot menu entry. If you have only one boot menu entry, the menu is not displayed at all.


To change the time-out for the boot menu, use the bcdedit /timeout seconds command, as shown here.


bcdedit /timeout 10



3. The default operating system can be changed in 2 ways-

Note - This only applies to a system having Windows OSs only . For a machine having Ubuntu and Windows OSs , to set a Windows OS as the default OS boot files of GRUB2/LILO have to be configured.


ONE:Go to Control Panel Home (Start -> Control Panel) -> System and Maintenance -> System.

On the left side pane, under the Tasks, Click Advanced System Settings.

Click Settings under Startup and Recovery.

Under System startup, you can change the Default operating system between Windows 7 and Vista. If you have installed any other OS, you can change between those settings as well.


TWO:To change the default operating system entry through command line interface, first run the following command to view the existing entries and make note of the identifier for the entry that you want to be the default.


bcdedit /enum

Then run the following command to set a new default (where is the identifier for the new entry).

bcdedit /default



4.To change the order of Boot Manager menu items, use the bcdedit /display command, and then list the menu item identifiers in the desired sequence .

Bcdedit.exe /displayorder {ID-OS(1)} {ID-OS(2)}


5.Creating another entry for an OS

The following link can be referred to understand how to create another entry for an OS

http://sourcedaddy.com/windows-7/how-to-create-an-entry-for-another-operating-system.html


6.

Typically, one does not need to remove entries from the BCD registry file. Instead, you should simply remove entries from the Windows Boot Manager menu. To remove an entry from the menu, first run bcdedit /enum and note the boot entry's identifier. Then run the following command, substituting the identifier.


bcdedit /displayorder {ID} /remove

For example, to remove the entry to load the previous version of Windows from the boot menu, you would run this command.


bcdedit /displayorder {ntldr} /remove

You can later re-add the entry to the boot menu by calling the following command.

bcdedit /displayorder {ID} /addlast


An alternate to the bcdedit.exe

EasyBCD:

EasyBCD is a freeware has a number of bootloader-related features that can be used to repair and configure the bootloader. EasyBCD can also load Ubuntu, Linux and Mac OS via chainloading.

It can downloaded from this link- http://download.cnet.com/EasyBCD/3000-2094_4-10556865.html



Boot Manager is Missing - A common boot up error and how to fix it

-Try to restart your PC/ laptop

-Change boot order in bios

Often PC tries to boot from a non bootable cd/dvd ,usb stick which may result in this error message.

-If problem still persists use Windows Startup Repair.

The following link describes in detail on how this tool can be used.

http://pcsupport.about.com/od/toolsofthetrade/ss/windows-7-startup-repair.htm


Our Conclusion


This was one of the most toughest projects we have ever worked on. On many occasions, we found the hard drive unbootable, and had to recover the bootloader, even sometimes reinstall an OS. Many times we thought we were doing something right, but ended up with an unexpected error and we would google it to try to understand and debug it . We were very relieved when it finally worked.


This experience of doing this project was frustrating, but it allowed us to work with intricate details of the system because of which our knowledge of how the computer works, especially the boot sequence, partitions, filesystems and the linux shell grew immensely and we would like to thank Amarjeet Sir wholeheartedly for giving us such a great learning opportunity(And for extending the deadline!)


Samiran Roy

Aishwarya Pant


No comments:

Post a Comment