Showing posts with label GRUB. Show all posts
Showing posts with label GRUB. Show all posts

Saturday, October 10, 2015

How to boot into BOSS from the Grub Rescue prompt?

This guide will detail how to boot from the "grub rescue>" prompt for grub2 users.
Boot Procedure:
1. Locate the BOSS partition and the folder containing the Grub modules.
The Grub folder containing the modules must be located so the correct modules can be loaded. This folder was created during BOSS installation and should be located in the BOSS partition. This folder would be located at either (hdX,Y)/boot/grub or (hdX,Y)/usr/lib/grub/i386-pc.
Commands:
ls                               # List the known drives (hdX) and partitions  (hdX,Y).

ls (hdX,Y)/                      # List the contents of the partition's root.

ls (hdX,Y)/boot/grub             # Normal location of the Grub 2 modules.

ls (hdX,Y)/usr/lib/grub/i386-pc  # Alternate location of the Grub 2 modules.

2. Load the modules
commands:
set prefix=(hdX,Y)/boot/grub
insmod linux
eg:
 set prefix=(hd0,1)/boot/grub 
 insmod linux
3. Load the Linux kernel and initrd image using the following commands.
 set root=(hd0,1)
 linux /boot/vmlinuz-2.6.32-5-686 root=/dev/sda1
 initrd /boot/initrd.img-2.6.32-5-686
 boot

Sunday, February 12, 2012

The PC hard disk layout (About MBR and GRUB)

The PC hard disk layout
The diagram below is the disk layout of my linux laptop which is very standard. There are 4 primary partitions whose size and function are labeled, but this document is going to concentrate on the expanded area in the diagram at the start of the disk. I will describe its structure and how GRUB fits in. Note the structure is the same for windows systems, only the content (grub) is different.
PC hard disk layout

The Master Boot Record

The first sector (512 bytes) is the MBR and consists of, 446 bytes bootloader, 64 bytes partition table and a 2 byte signature (0xAA55). It get its name because it is the first boot record that is loaded, which can then load other boot records from various locations on the disk. To give a hexdump of this first sector of the disk you can do dd bs=512 count=1 if=/dev/hda | od -Ax -tx1z -v (changing /dev/hda as appropriate for your setup):
000000 eb 48 90 d0 bc 00 7c fb 50 07 50 1f fc be 1b 7c  >.H....|.P.P....|<
000010 bf 1b 06 50 57 b9 e5 01 f3 a4 cb be be 07 b1 04  >...PW...........<
000020 38 2c 7c 09 75 15 83 c6 10 e2 f5 cd 18 8b 14 8b  >8,|.u...........<
000030 ee 83 c6 10 49 74 16 38 2c 74 f6 be 10 07 03 02  >....It.8,t......<
000040 80 00 00 80 b8 85 64 00 00 08 fa 80 ca 80 ea 53  >......d........S<
000050 7c 00 00 31 c0 8e d8 8e d0 bc 00 20 fb a0 40 7c  >|..1....... ..@|<
000060 3c ff 74 02 88 c2 52 be 79 7d e8 34 01 f6 c2 80  ><.t...R.y}.4....<
000070 74 54 b4 41 bb aa 55 cd 13 5a 52 72 49 81 fb 55  >tT.A..U..ZRrI..U<
000080 aa 75 43 a0 41 7c 84 c0 75 05 83 e1 01 74 37 66  >.uC.A|..u....t7f<
000090 8b 4c 10 be 05 7c c6 44 ff 01 66 8b 1e 44 7c c7  >.L...|.D..f..D|.<
0000a0 04 10 00 c7 44 02 01 00 66 89 5c 08 c7 44 06 00  >....D...f.\..D..<
0000b0 70 66 31 c0 89 44 04 66 89 44 0c b4 42 cd 13 72  >pf1..D.f.D..B..r<
0000c0 05 bb 00 70 eb 7d b4 08 cd 13 73 0a f6 c2 80 0f  >...p.}....s.....<
0000d0 84 f0 00 e9 8d 00 be 05 7c c6 44 ff 00 66 31 c0  >........|.D..f1.<
0000e0 88 f0 40 66 89 44 04 31 d2 88 ca c1 e2 02 88 e8  >..@f.D.1........<
0000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04  >..@.D.1......f..<
000100 66 a1 44 7c 66 31 d2 66 f7 34 88 54 0a 66 31 d2  >f.D|f1.f.4.T.f1.<
000110 66 f7 74 04 88 54 0b 89 44 0c 3b 44 08 7d 3c 8a  >f.t..T..D.;D.}<.<
000120 54 0d c0 e2 06 8a 4c 0a fe c1 08 d1 8a 6c 0c 5a  >T.....L......l.Z<
000130 8a 74 0b bb 00 70 8e c3 31 db b8 01 02 cd 13 72  >.t...p..1......r<
000140 2a 8c c3 8e 06 48 7c 60 1e b9 00 01 8e db 31 f6  >*....H|`......1.<
000150 31 ff fc f3 a5 1f 61 ff 26 42 7c be 7f 7d e8 40  >1.....a.&B|..}.@<
000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30  >.....}.8.....}.0<
000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47  >...}.*...GRUB .G<
000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65  >eom.Hard Disk.Re<
000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd  >ad. Error.......<
0001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00  >..<.u...........<
0001b0 00 00 00 00 00 00 00 00 5f 00 5f 00 00 00 00 01  >........_._.....<
0001c0 01 00 0b fe 7f 97 3f 00 00 00 59 03 64 00 00 00  >......?...Y.d...<
0001d0 41 98 83 fe 7f a4 98 03 64 00 cd 2f 03 00 00 00  >A.......d../....<
0001e0 41 a5 83 fe ff ff 65 33 67 00 fc 08 fa 00 80 fe  >A.....e3g.......<
0001f0 ff ff 0f fe ff ff 61 3c 61 01 1f ed f2 00 55 aa  >......aU.<
Note there is no mystery about or dissassembly required of the blue section above as GRUB is open source and one can download, inspect and modify its stage 1 source code.

The DOS compatibility region

This region is optional as far as linux is concerned at least, but is added by default by most partition managers. To understand why this region was required we need to describe how disks used to be addressed. Generally now disks are addressed in LBA mode which allows for greater capacity disks while abstracting software away from the specifics of the disk itself. Previously though disks were addressed inCHS mode, which represented the physical construction of the disk as can be seen in the diagram below:
Cylinders Heads Sectors
DOS had the requirement that its image did not span across cylinders, and so this region was added by partition managers so that the first partition was aligned on a cylinder boundary. Therefore this region's size is determined by the number of sectors (512 bytes) per cylinder. The maximum (and usual given todays disk sizes and LBA) sectors per cylinder is 63, which leaves 62 sectors free after the MBR (31,744 bytes).
GRUB uses this region to store its stage 1.5, which is filesystem specific code used to find the operating system image on the "boot" filesystem. Currently GRUB does not need all this space as can be seen for the copies of all the stage 1.5 files in the boot partition:
$ ls -lS /boot/grub/*stage1_5
-rw-r--r--  1 root root 9428 Mar  8 14:27 /boot/grub/reiserfs_stage1_5
-rw-r--r--  1 root root 9308 Mar  8 14:27 /boot/grub/xfs_stage1_5
-rw-r--r--  1 root root 8448 Mar  8 14:27 /boot/grub/jfs_stage1_5
-rw-r--r--  1 root root 7956 Mar  8 14:27 /boot/grub/e2fs_stage1_5
-rw-r--r--  1 root root 7684 Mar  8 14:27 /boot/grub/fat_stage1_5
-rw-r--r--  1 root root 7272 Mar  8 14:27 /boot/grub/ufs2_stage1_5
-rw-r--r--  1 root root 7188 Mar  8 14:27 /boot/grub/minix_stage1_5
-rw-r--r--  1 root root 7028 Mar  8 14:27 /boot/grub/iso9660_stage1_5
-rw-r--r--  1 root root 6996 Mar  8 14:27 /boot/grub/ffs_stage1_5
-rw-r--r--  1 root root 6612 Mar  8 14:27 /boot/grub/vstafs_stage1_5
Therefore one could theoretically use the last 43 sectors of this region (22,016 bytes) for anything. There is no point in creating a filesystem in here as the overhead would be too much, but you could dd stuff in and out like:
dd bs=512 seek=20 count=43 if=myfile of=/dev/hda
dd bs=512 skip=20 count=43 if=/dev/hda of=myfile
Note be very sure you know what your doing before running these commands.
Note on my laptop the last sector of this region contains the first sector of a MSWIN4.1 boot record (which I understand is 3 sectors in total), as it came with winxp installed (in a FAT32 partition). GRUB makes this sector redundant even in a dual boot situation, so don't worry about overwriting it. You can inspect this sector using: dd bs=512 skip=62 count=1 if=/dev/hda | od -Ax -tx1z -v

GRUB 1 boot process

GRUB or the GRand Unified Bootloader is the usual bootloader used on linux systems, and resides on the system as described above. The boot process with GRUB is as follows:
  • BIOS code reads MBR from a disk (looks at last 2 bytes to verify if MBR).
  • Starts executing bootloader code (GRUB stage 1).
  • Bootloader jumps to location (sector num) of next stage. This sector num is stored at a particular location in the bootloader "code" at GRUB install time and usually points to a stage 1.5 located in the "DOS compat space" immediately after the MBR.
  • Stage 1.5 knows about the boot filesystem so it opens the filesystem on the specified (at install time) partition. It looks for the stage 2 executable here and executes it. Note since stage 1.5 knows about the boot filesystem it gives much greater flexibility in upgrading stage 2 and the kernel etc. as their new locations don't need to be written to the earlier GRUB stages.
  • Stage 2 contains most of the GRUB logic. It loads the menu.lst file and executes the statements, usually providing a menu to the user etc.
  • Subsequent steps are distro specific but for completeness I'll describe them for my fedora linux distribution:
  • When GRUB starts booting one of the entries, it reads the initial ramdisk and starts the kernel running telling it about the ramdisk.
  • In the initial ramdisk, the nash shell is run to parse the /linuxrc file. It essentially finds the location of the filesystem it itself is on and passes that to the kernel as its root filesystem. This allows for greater flexibility of the devices the kernel resides on.
  • The kernel reads its root filesystem and executes /bin/init by default. This in turn parses /etc/inittab which usually sets up the login consoles and starts executing the scripts in /etc/init.d
  • These scripts start various subsystems, culminating in starting X. X in turn starts the display manager which gives a login prompt.

GRUB 2 boot process

The structure of GRUB has changed quite a bit with version 2, (which is still in development at the time of writing). Instead of stage 1, stage 1.5 and stage 2 images it has a collection of modules that can be combined in different ways. To mirror the functionality the original GRUB's stages as described above, you would have for example:
stage 1 = boot.img
stage 1.5 = diskboot.img+kernel.img+pc.mod+ext2.mod (the core image)
stage 2 = normal.mod+_chain.mod
The boot process for GRUB 2 then would be:
  • BIOS code reads MBR from a disk (looks at last 2 bytes to verify if MBR).
  • Starts executing bootloader code (boot.img).
  • This loads the first sector of the core image (diskboot.img), whose location was stored at a particular location in boot.img at GRUB install time and usually points to the core image located in the "DOS compat space" immediately after the MBR. diskboot.img in turn loads and executes the rest of the core image.
  • These core image modules know about the boot filesystem and can open the filesystem on the specified (at install time) partition. It looks for and loads modules there, including normal.mod
  • normal.mod loads the grub.cfg file and executes the statements, usually providing a menu to the user etc.
This is a more flexible mechanism. For example one can prepend pxeboot.img to the core image instead of diskboot.img. This will then load the whole core image from the network and then start executing kernel.img.

Friday, September 2, 2011

Disable USB in Grub


Now a days USB storage devices are common in the industry. This is very halmful in any unsecured network. Because this devices are considered to be the virus spreading agents. In corporates USB media is prevented because of above said reasons. So here a small tip to prevent USB storage in Linux machines through GRUB.

Scenario
OS : Debian / Ubuntu

Open the Grub configuration file

#vi /boot/grub/menu.lst

Add "nousb" at the end of the Kernel Line. Like the following

kernel /vmlinuz-2.6.18-128.1.1.el5 ro root=LABEL=/ console=tty0 console=ttyS1,19200n8 nousb

Secure GRUB with a strong Password !!!

#/sbin/shutdown -r now

So nobody can use USB storage unless you allowed to do so.

Thursday, September 1, 2011

About GRUB ? Red Hat GRand Unified Bootloader ?

Before linux is installed on a system. Special instructions must be Placed in
Boot Loader (Its a programme that exists on Systems Primary Hard Drive or other Media
that knows how to start the linux Kernel)used to Boot the system.Red Hat linux installation
Programme allows user to quickly and easily configure the boot loader in the
Primary hard dirve's Master Boot Record (MBR) to load the OS.

Define GRUB :

GRUB (GRand Unified Bootloader) is a programme that installs a Boot Loader to the MBR. This MBR
exists at the beginning sector of the disk.
The Grub menu is also command environmental means we can pass specific arguments as instructions.

The process of loading operating GRUB and OS involves several stages :

1)Load the primary boot Loader (stage 1):

It exists in very small space allocated for MBR ( < style="font-weight: bold;">2)Load the secondary boot Loader (stage 2):

Choosing of operating system. or give commands or modify commands

3)Load the Operating System (linux kernel on specified partition):

After receiving correct instructions for OS to start from command line or
Configuration file

Thursday, August 18, 2011

GRUB 2 Features

GRUB 2 in default Boot Loader for Ubuntu and many other operating system now. Grub 2 introduces many new welcoming changes. Grub 2 is almost complete re-writing of Grub interface. Grub 2 in more modular and portable than its earlier version. It support dynamic loading of modules. If we talk about look, Grub2 is going to include Theme support. If you working on latest releases of Ubuntu you may become little bit confused because Grub2 working is diffrent from Grub 0.9(Old grub version). 
How GRUB 2 Works ?
Grub2 layout is totally diffrent. Grub2 places its core files on three locations./boot/grub/grub.cfg/etc/grub.d and /etc/default/grub. You may need to edit /etc/default/grub which contains customization and/or /etc/grub.d/ files which contain Menu information and booting scripts. You shouldn't edit grub.cfg file directly but when the update-grub command is run, it reads the contents of the grub file and the grub.d scripts and creates the grub.cfg file.

Tuesday, July 12, 2011

How to put the grub password in linux!

In Linux anyone can reset the root password from single usermode . So it is considered as a security fault if the machine is public. There comes the importance of putting the grub password, so that only admin is allowed to login if the machine is rebooted.

Here are the steps to put the grub password.

1) open the command prompt and type the following commands

2) grub

3) md5crypt

4) type the password

5) copy the encrypted password generated

6) Ctrl c

7) vi /boot/grub/grub.conf

8) paste the following line just above the 'title'

password --md5 "encrypted password here"

9) save and quit. Done.

I have give the sample grub file here.

======================================

#boot=/dev/hdb
default=0
timeout=15
splashimage=(hd1,8)/boot/grub/splash.xpm.gz
hiddenmenu
password --md5 %&mkj89(*$*J)$OO*=*

title CentOS (2.6.18-92.el5)
root (hd1,8)
kernel /boot/vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet
initrd /boot/initrd-2.6.18-92.el5.img

Tuesday, May 17, 2011

GRUB – define and working


Linux boot loader is GRUB – Grand Unified Boot loader
originally designed and implemented by Erich Stefan Boleyn.
GRUB is dynamically configurable. This means that the user can make changes during the boot time, which include altering existing boot entries, adding new, custom entries, selecting different kernels, or modifying initrd. GRUB also supports Logical Block Address mode. This means that if your computer has a fairly modern BIOS that can access more than 8GB (first 1024 cylinders) of hard disk space, GRUB will automatically be able to access all of it.
GRUB can be run from or be installed to any device (floppy disk, hard disk, CD-ROM, USB drive, network drive) and can load operating systems from just as many locations, including network drives. It can also decompress operating system images before booting them.
How does GRUB work?
When a computer boots, the BIOS transfers control to the first boot device, which can be a hard disk, a floppy disk, a CD-ROM, or any other BIOS-recognized device. We’ll concentrate on hard disks, for the sake of simplicity.
The first sector on a hard is called the Master Boot Record (MBR). This sector is only 512 bytes long and contains a small piece of code (446 bytes) called the primary boot loader and the partition table (64 bytes) describing the primary and extended partitions.
By default, MBR code looks for the partition marked as active and once such a partition is found, it loads its boot sector into memory and passes control to it.
GRUB replaces the default MBR with its own code.
GRUB works in stages.
Stage 1 is located in the MBR and mainly points to Stage 2, since the MBR is too small to contain all of the needed data.
Stage 2 points to its configuration file, which contains all of the complex user interface and options we are normally familiar with when talking about GRUB. Stage 2 can be located anywhere on the disk. If Stage 2 cannot find its configuration table, GRUB will cease the boot sequence and present the user with a command line for manual configuration.

Booting a Windows OS using GRUB


Booting a MS Windows OS using GRUB is a quite simple process. However, we must follow some rules in order to succeed. The first and foremost rule is:
* Windows OS always wants to be installed on a first hard drive and first partition.
* if you have your windows installation on second hard drive you need to use grub’s map command to re-map first hardrive as seccond and vice versa
Grub’s Naming convetion
Grub starts its hard drive and partition numbering system from 0. This means that (hd0,0) refers to a first hard drive and first primary partition. Whereas, (hd2,2) refers to third hard drive and third primary partition. All extended partitions starts from number 4. Therefore, (hd1,5) refers to a second hardrive and second extended partition.
Booting a Windows OS with grub
In case you have indeed installed your MS Windows on a first hard drive and first partition the whole process is rather simple. Open up a Grub’s menu config file:
vi  /boot/grub/menu.lst
and add a following code into appropriate boot menu section :
title           Microsoft Windows XP Professional
root    (hd0,0)
makeactive
chainloader     +1
save file and run command:
# update-grub
and you are done.
* root: the root partition of your MS Windows installation
* title: title name as it will be displayed on grub’s boot menu
* makeactive: make this entry bootable
* chainloader: do not boot but let partition’s boot loader to boot instead
In case you have a MS Windows installation on a third hard drive, things are little bit more complicated. Here is a /boot/grub/menu.lst code for booting a MS Windows from a third hard drive and first partition.
title           Microsoft Windows XP Professional
rootnoverify    (hd2,0)
map             (hd0) (hd2)
map             (hd2) (hd0)
savedefault
makeactive
chainloader     +1
* rootnoverify: do not mount but execute another command
* map: this makes a third hard drive a first and first hard drive third
After you have updated /boot/grub/menu.lst run:
# update-grub