TimeLinux1

Wednesday, September 22, 2010

Linux HowTo : Disk Partitions and Boot Loaders

Linux Notes on Disk Partitions and Boot Loaders.
In this Post, we will be talking of Two predominant Boot Loaders for Linux - GRUB (GRand Unified Bootloader) and LILO (LInux LOader):

First the basics about Linux Partitions:
-disk partition names:
    . /dev/hdx        - ide
    . /dev/sdx        - scsi / sata
-sector = 512 bytes
-track   = sum (sectors)      in one read of disk arm
-cylind = sum (tracks)        in one read of disk arm

-partition types:
    . primary        - one of the 4 partitions limited by the master boot record (mbr); mbr resides in the 1st sector of the disk (ie first 512 bytes).
    . extended      - one of the primary partitions that is logically broken to create more than 4 partitions.
    . logical          - one of constituents of the extended partition.

-the boot partition must be a primary partition and reside completely in the first 1024 cylinders;
-this is because the bios can't read or boot from the boot partition, if this condition is not met.
-usually 100 MB for boot partition is ok.

-partition recommendations:
    . first define boot
    . then define swap
    . then define /usr, /opt, /var in a single large partition - perhaps /  ?
    . after that define rest of the system like /home etc.

MBR = Master Boot Record.

-MBR (or simply mbr) lives in the first sector of the first primary partition. the mbr contains the partition table, info about the partitions in the system.
-Since a sector = 512 bytes, mbr = 512 bytes & in turn partition table = 512 bytes.
-every media (disk, floppy, cd) contains an executable code in the mbr even if the code is only to put a message "Non-bootable disk in drive A:".
-this is the code that is loaded by bios during the bootstrap. this is called 'stage1 boot loader'.
-this code from mbr / stage1 boot loader (ie first sector) looks for active primary partition and loads the first few blocks of that partition into RAM.
-these few blocks from active primary partition comprise 'stage 2 boot loader'.
-stage 1 + stage 2    =    boot strapping.
-the above works fine if there is only one os in the system. but if there are multiple os, then another piece of code called boot-loader is needed.
-the boot-loader allows the user to select one of the os to boot, ie choose which set of first os-disk-blocks to load into ram.
-note:    even if a system can have 4 primary partitions, it can still have more than 4 bootable os partitions; this is possible bec of boot-loaders.
-eg of boot loaders = grub, lilo, bootmagic.
-bootloader lives in an os partition and is invoked by the mbr.    [[ (mbr.exe) ] -->    (bootloader.exe)    -->    (rest of os partition)    ]
-why is grub considered Better than lilo?   
    Because when changes are made to the system (new os, new kernel) lilo boot-setup needs to be recreated from the cmd line whereas for
    grub only the grub.conf file needs to be re-edited.

Ok, now that we have covered partitions and basics of boot loaders, heres the specifics of Lilo and Grub.

-Lilo    can be installed in the
    . MBR                     or
    . the partition boot record of a partition    or
    . on removable media (floppy, cd, usb key)
-lilo    config file is /etc/lilo.conf

-Grub    can be installed in the
    . MBR                        or
    . the partition boot record of a partition    or
    . on removable media (floppy, cd, usb key)
-grub    config file is /boot/grub/grub.conf
-grub     cmd    /sbin/grub    or    /usr/sbin/grub    is a small but powerful shell that supports several grub cmds.
-grub.conf is generated by anaconda, the linux installer.

-In the grub.conf file    :
    . all counting in grub.conf starts with 0.    eg default=2    => 3rd stanza.
    . splashimage    = the background image for the grub boot menu.
    . root            = partition that will be booted (ie /boot partition).   
        eg:    root (hd0, 6)    =>    /dev/hda7    = /boot partition.
            root (hd1, 10)=>    /dev/hdb11    = /boot partition.
            root (hd2, 7)    =>    /dev/hdc8    = /boot partition.
    . initrd    => initial RAM disk    => the disk partition that contains modules needed by kernel before file systems can be mounted.
   
-To install grub to a removable disk use the 'grub-install' cmd
-eg: for floppy disk:
    # grub-install        /dev/fd0
-note:    this loads the stage 1 boot loader to the first sector of the floppy disk which loads stage2 boot loader (which lives on the hard disk)
-stage1 bootloader on floppy will still show empty when mounted as the first sector does not show up in the filesystem.
-stage1 bootloader only has a list of block addresses for stage2 bootloader.
-So if a partition address changes, grub needs to be reconfigured in order for stage1 to locate stage2 bootloader.

-anyone having access to the grub cmd line also has access to files on the filesystems without the restrictions of file / owner permissions.

-the habit of creating a boot floppy or usb disk is good because it can help in case the mbr gets overwritten by another os install.
-even if the boot floppy or usb disk are not available, then linux install disk can be used to go in recovery mode and then mbr reinstalled.
-eg:    # chroot    /mnt/sysimage            [ on the recovery window, to make /mnt/sysimage as root mount directory ]
    # grub-install                        [ reinstalls mbr ]
-remember:
    [[ mbr    = 1st sector = stage1 boot loader ]]
                |->    stage2 bootloader partition 1                -> grub menu option 1
                |->    stage2 bootloader partition 2                -> grub menu option 2
                |->    stage2 bootloader partition 3                -> grub menu option 3
                . . .                                    . . .

No comments:

Post a Comment