To rewrite Grub

To only re-write grub to MBR from the hard drive, ie due to a newer grub-version:

/usr/sbin/grub-install --recheck --no-floppy /dev/sda

This line may need to be run more than once, until its convinced you really mean it.

Bootsector overwritten by Windows MBR and /or to recover grub

To rewrite the grub to the MBR and/or to recover grub in general, you will need boot up a sidux live-cd, first:

  1. To identify and confirm your partitions (e.g. [h,s]d[a..]X) all the following actions need root privileges, so to get root (#):
    $ sux
    
  2. When in root type in:
    fdisk -l
    cat /etc/fstab
    
    This is to get the correct names.
  3. When you have acertained the correct partition, create the mount point:
    mkdir -p /media/[hdxx,sdxx,diskx]
    
  4. Mount it:
    mount /dev/xdxx /media/xdxx
    
  5. Now rewrite Grub to MBR of the first harddisk (generic):
    /usr/sbin/grub-install --recheck --no-floppy --root-directory=/media/xdxx /dev/sda
    
Example
root@sidux:/home/sidux# fdisk -l
# fdisk -l
Disk /dev/sda: 115.9 GB, 115989736960 bytes
255 heads, 63 sectors/track, 14101 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x40444044
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3673    29503341    7  HPFS/NTFS
/dev/sda2            3674       14101    83762910    5  Extended
/dev/sda5            3674        7239    28641784+  83  Linux
/dev/sda6            7239       10842    28934104+  83  Linux
/dev/sda7           10843       14101    26177886    b  W95 FAT32
Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa41e667f
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3574    28708123+  83  Linux
/dev/sdb2            3575        7247    29503372+  83  Linux
/dev/sdb3            7248       10845    28900935   83  Linux
/dev/sdb4           10846       14593    30105810    5  Extended
/dev/sdb5   *       10846       14338    28057491   83  Linux
/dev/sdb6           14339       14593     2048256   82  Linux swap / Solaris
Disk /dev/sdc: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00077541
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1       30401   244196001   83  Linux
Disk /dev/sdd: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x1c01ad92
   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1       30401   244196001   83  Linux

root@sidux:/home/sidux# cat /etc/fstab
# /etc/fstab - static information about the filesystems - fstab(5)
#
# /etc/fstab is only read by programs, and not written; it is the duty of the
# system administrator to properly maintain this file.
#
# Instead  of giving the device explicitly, one may indicate the filesystem
# that is to be mounted by its UUID or VOLUME label. This will make the
# system more robust: adding or removing a disk changes the disk device name
# but not the filesystem UUID or VOLUME label.

# automatically added, ST3120026A-3LJ093B4-part1
/dev/sda1       /media/disk0    ntfs    noauto,users,exec,ro,dmask=0022,fmask=0133      0       0

# automatically added, ST3120026A-3LJ093B4-part5
/dev/sda5       /media/disk1    jfs     noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ093B4-part6
/dev/sda6       /media/disk2    reiserfs        noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ093B4-part7
/dev/sda7       /media/disk3    vfat    noauto,users,exec,shortname=lower,quiet,umask=000       0       0

# automatically added, ST3120026A-3LJ09RBF-part1
/dev/sdb1       /media/disk4    reiserfs        noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ09RBF-part2
/dev/sdb2       /media/disk5    ext3    noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ09RBF-part3
/dev/sdb3       /media/disk6    reiserfs        noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ09RBF-part5
/dev/sdb5       /media/disk7    ext3    noauto,users,exec,noatime       0       2

# automatically added, ST3120026A-3LJ09RBF-part6
/dev/sdb6       none    swap    sw      0       0

# automatically added, ST3250824AS-4ND1V199-part1
/dev/sdc1       /media/disk9    ext3    noauto,users,exec,noatime       0       2

# automatically added, ST3250620AS-9QE13XVF-part1
/dev/sdd1       /media/disk10   ext3    noauto,users,exec,noatime       0       2

# automatically added, _NEC_DVD_RW_ND-4550A
/dev/cdrom      /media/cdrom    udf,iso9660     user,noauto     0       0

# automatically added, ASUS_DVD-ROM_DVD-E616P_0104
/dev/cdrom1     /media/cdrom1   udf,iso9660     user,noauto     0       0

# automatically added
/dev/fd0        /media/fd0      auto    rw,user,noauto  0       0

# temporary filesystem in virtual memory
#tmpfs  /tmp    tmpfs   defaults        0       0
root partition is /dev/sdb1  and your MBR goes on sda
mkdir -p /media/sdb1
mount /dev/sdb1 /media/sdb1
/usr/sbin/grub-install --recheck --no-floppy --root-directory=/media/sdb1 /dev/sda

On older linux systems, the IDE disks devices (PATA) were differentiated from those using current standard by an hdaX name instead of sdaX

Creating a bootable Floppy

To make yourself a boot floppy, use the tools provided in UBCD (Ultimate Boot Disk CD)

Removing grub from MBR

It's very simple: as root enter

lilo -M /dev/hda -s /dev/null

in a terminal.

GRUB the Boot Manager - what it is

General

NOTE Grub for kernels 2.6.22 and up uses sdax
and the line
initrd (hd0.x)/sidux/miniroot.gz
has changed to:
initrd		/boot/initrd.img-2.6.xx.x-slh-smp-x

Grub is a boot manager that can boot operating systems from different partitions. Grub does not rely on block lists to find the needed files for a system to start up, and it can read a bunch of file-systems by default: ext2, ext3, reiserfs, ... (Linux), Fat32, Fat16 (Windows), UFS (BSD), Minix and many others.

Grub knows BIOS-extensions (meaning it can boot partitions above 8 GByte!) and has no 1024-cylinder-barrier. Grub doesn't really care, where the kernel physically resides, all it needs is the declaration of the partition and the path to the kernel it's supposed to boot (this can even be a symlink). After a change in the configuration (i.e. the menu.lst) it doesn't need a program-call like Lilo does, the new configuration is applied at next boot.

sidux uses Grub to boot from CD, and when installing to harddisk, Grub is used by default. Nonetheless Grub can be installed afterwards without a problem. Grub is installed to the MBR from a root-shell with the following command:

grub-install --root-directory=/media/hdaX /dev/hdY

/media/hdaX stands for the readable-mounted partition, on which, in the directory "/boot/grub" the necessary files (stage2, stage1_5xxx) will be placed. The configuration file "menu.lst" must also be created in that directory. /dev/hdY is the harddisk, in whose MBR Grub will be installed, normally /dev/hda.

Drivenames/Devicenames

Please Note: hda is in the process of being depreciated, therefore you need to start substituting hda with sda for kernels 2.6.22 and onward.

Grub does not differentiate between SCSI- and IDE-Harddisks. (hd0) is always the first hard disk that is first in the boot sequence (setting in BIOS), no matter if IDE or SCSI. Unusual is the counting method; the first partition on the first hd is (hd0,0). Primary partitions are numbered from 0-3, logical partitions count from 4 on up.

Grub Linux Label
(fd0) /dev/fd0 Floppy (under Windows)
(hda0) /dev/hda first hd
(hd0,0) /dev/hda1 Part.1 on hd1
(hd0,5) /dev/hda6 Extended. part. on hd 1

All nominations for devices for Grub are to be put in brackets (). Be aware of the entries behind the Kernel-line though. In contrast to Grub the Kernel needs the data in the normal linux way. The entries in the following example both point to the same partition, namely the fourth partition on the first hd. The first line is in Grub notation, the second one in typical linux notation:

root (hd0,3)
kernel          /boot/vmlinuz root=UUID=bf955abf-ce1f-45a3-9ee5-93b5c5dc4fe0 ro quiet vga=791
initrd          /boot/initrd.img

Command-Mode (Grub-Shell)

A very comfortable feature is the Command-Mode. It is accessed by "c", and here comes the Grub-Shell. This enables you to find and boot different systems and files (i.e. Kernel) even with a faulty menu.lst.

To do a little bit of testing, you can even run Grub from a booted system from within a root-shell with "grub". In the Grub-Shell you can see all possible commands with "help". Detailed help for each command is obtained with help command", back to the Linux-Shell you go with "quit"( Grub-Shell is terminated). In the Grub-Shell the tab-key has a similar function as in the Linux-Shell, which is a great ease. Commands, Drivenames and Filenames can be auto-completed with the tab-key.

Grub gives you the opportunity to locate the Kernel and the initrd even before the actual boot by "find" , e.g.with the following command

find /boot/vmlinuz

Grub shows the partitions, that have a Kernel with the name "vmlinuz" in the directory "/boot":

(hd0,3) (hd1,0)

All available partitions can be found with "geometry", e.g.

geometry (hd0)

shows this:

"drive 0x80: C/H/S = 3648/255/63, The number of sectors = 58605120, /dev/hda Partition num: 0,
Filesystem type is fat, partition type 0xb Partition num: 1,
Filesystem type is fat, partition type 0xb Partition num: 2,
Filesystem type is fat, partition type 0xc Partition num: 4,
Filesystem type is ext2fs, partition type 0x83 Partition num: 5,
Filesystem type unknown, partition type 0x82 Partition num: 6,
Filesystem type is fat, partition type 0xb"

After the Kernel or an existing initrd (or miniroot.gz) has been found, you can start with "boot".

Edit-Mode

This mode enables you to change entries in the "menu.lst" while booting. This works even when Grub is booted from CD and the menu.lst is write-protected . To do so you first hit ESC, confirm with Return and then hit "e". The parameters are then added to the end of the line or you move within the line with the Cursor-keys.

menu.lst (examples for possible entries)

Lines with comments are started with # and don't interfere with the bootprocess, so they can stay in the file.

# Example /boot/grub/menu.lst
################################################
# setkey changes the keyboardlayout to german (as an example)
setkey y z
setkey z y
setkey Y Z
setkey Z Y
setkey equal parenright
setkey parenright parenleft
setkey parenleft asterisk
setkey doublequote at
setkey plus bracketright
setkey minus slash
setkey slash ampersand
setkey ampersand percent
setkey percent caret
setkey underscore question
setkey question underscore
setkey semicolon less
setkey less numbersign
setkey numbersign backslash
setkey colon greater
setkey greater bar
setkey asterisk braceright
# menu.lst - See: grub(8), info grub, update-grub(8)
#            grub-install(8), grub-floppy(8),
#            grub-md5-crypt, /usr/share/doc/grub
#            and /usr/share/doc/grub-doc/.

## default num
# Set the default entry to the entry number NUM. Numbering starts from 0, and
# the entry number 0 is the default if the command is not used.
default		0

## timeout sec
# Set a timeout, in SEC seconds, before automatically booting the default entry
# (normally the first entry defined).
timeout		5

# Pretty colours
color cyan/blue white/blue
gfxmenu (hd0,5)/boot/message

## password ['--md5'] passwd
# If used in the first section of a menu file, disable all interactive editing
# control (menu entry editor and command-line)  and entries protected by the
# command 'lock'
# e.g. password topsecret
#      password --md5 $1$gLhU0/$aW78kHK1QfV3P2b2znUoe/
# password topsecret

#
# examples
#
# title		Windows 95/98/NT/2000
# root		(hd0,0)
# makeactive
# chainloader	+1
#
# title		Linux
# root		(hd0,1)
# kernel	/vmlinuz root=/dev/hda2 ro
#

#
# Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST

### BEGIN AUTOMAGIC KERNELS LIST
## lines between the AUTOMAGIC KERNELS LIST markers will be modified
## by the debian update-grub script except for the default options below

## DO NOT UNCOMMENT THEM, Just edit them to your needs

## ## Start Default Options ##
## default kernel options
## default kernel options for automagic boot options
## If you want special options for specific kernels use kopt_x_y_z
## where x.y.z is kernel version. Minor versions can be omitted.
## e.g. kopt=root=/dev/hda1 ro
##      kopt_2_6_8=root=/dev/hdc1 ro
##      kopt_2_6_8_2_686=root=/dev/hdc2 ro
# kopt=root=UUID=1885d68b-cc9c-49f8-9f98-a6874e7b3e7e ro  quiet vga=791

## default grub root device
## e.g. groot=(hd0,0)
# groot=(hd0,5)

## should update-grub create alternative automagic boot options
## e.g. alternative=true
##      alternative=false
# alternative=false

## should update-grub lock alternative automagic boot options
## e.g. lockalternative=true
##      lockalternative=false
# lockalternative=false

## additional options to use with the default boot option, but not with the
## alternatives
## e.g. defoptions=vga=791 resume=/dev/hda5
# defoptions=

## should update-grub lock old automagic boot options
## e.g. lockold=false
##      lockold=true
# lockold=false

## Xen hypervisor options to use with the default Xen boot option
# xenhopt=

## Xen Linux kernel options to use with the default Xen boot option
# xenkopt=console=tty0

## altoption boot targets option
## multiple altoptions lines are allowed
## e.g. altoptions=(extra menu suffix) extra boot options
##      altoptions=(single-user) single
# altoptions=(single-user mode) single

## controls how many kernels should be put into the menu.lst
## only counts the first occurence of a kernel, not the
## alternative kernel options
## e.g. howmany=all
##      howmany=7
# howmany=all

## should update-grub create memtest86 boot option
## e.g. memtest86=true
##      memtest86=false
# memtest86=true

## should update-grub adjust the value of the default booted system
## can be true or false
# updatedefaultentry=false

## ## End Default Options ##

title		Debian GNU/Linux, kernel 2.6.22.1-slh-smp-7
root		(hd0,5)
kernel		/boot/vmlinuz-2.6.22.1-slh-smp-7 root=UUID=1885d68b-cc9c-49f8-9f98-a6874e7b3e7e ro  quiet vga=791 
initrd		/boot/initrd.img-2.6.22.1-slh-smp-7

title		Debian GNU/Linux, kernel 2.6.22.1-slh-smp-7 (single-user mode)
root		(hd0,5)
kernel		/boot/vmlinuz-2.6.22.1-slh-smp-7 root=UUID=1885d68b-cc9c-49f8-9f98-a6874e7b3e7e ro  quiet vga=791 single
initrd		/boot/initrd.img-2.6.22.1-slh-smp-7

### END DEBIAN AUTOMAGIC KERNELS LIST

#### MS (TM)
title Windows 2K/XP/2003 (hda1)
chainloader (hd0,0)+1

### ISO boot
title sidux 32bit from gaiaISO
kernel (hd0,3)/sidux3/vmlinuz-2.6.22.1-slh-smp-7 boot=fll quiet vga=791 fromiso=/sidux3/siduxgaia.iso
initrd (hd0,3)/sidux3/initrd.img-2.6.22.1-slh-smp-7


title Debian GNU/Linux, kernel 2.6.18-1-686 (hda5)
root (hd0,4)
kernel (hd0,4)/boot/vmlinuz-2.6.18-1-686 root=/dev/hda5 ro vga=791
initrd (hd0,4)/boot/initrd.img-2.6.18-1-686
savedefault

Dual and Multiple Booting

NOTE Grub for kernels 2.6.22 and up uses sdax
and the line
initrd (hd0,x)/sidux/miniroot.gz
has changed to:
initrd		/boot/initrd.img-2.6.xx.x-slh-smp-x

To enable multiple grub OS booting is very simple with the use of chainloading. If for example you want to run a MS Windows (TM) and Debian etch OS along with a sidux 'fromiso' installation , you will need to customise grub a bit, Therefore edit the file /boot/grub/menu.lst and add the following lines after the ### END DEBIAN AUTOMAGIC KERNELS LIST:

####MS Windows (TM) perhaps even MS Vista (TM)
Title Windows (95/98/XP/Vista
rootnoverify (hd0,1)
makeactive
chainloader +1

### Debian etch
title Debian etch
kernel (hd0,3)/debian/vmlinuz root=/dev/hda4 ro (or whatever)
initrd (hd0,3)/debian/miniroot.gz (or (whatever)

### ISO boot
title sidux 32bit from gaiaISO
kernel (hd0,3)/sidux3/vmlinuz-2.6.22.1-slh-smp-7 boot=fll quiet vga=791 fromiso=/sidux3/siduxgaia.iso
initrd (hd0,3)/sidux3/initrd.img-2.6.22.1-slh-smp-7

Some people need/want several MS Windows (TM)Installations. MS Windows (TM) always wants to boot from disk 1. We will have to hide one partition and activate another. The following commands should be entered in the GRUB-Commandline and, if successfull, copied to the menu.lst. This should be done for every Windows-partition. Don't forget to do a Backup:

hide (hd0,0) # hidden partition 1 of disk 1
unhide (hd0,1) # makes partition 2 of disk 1 visible
rootnoverify (hd0,1) # makes partition 2 of disk 1 the Root-FS
makeactive # makes partition 2 of disk 1 active
chainloader +1 # points to the first sector of the start-partition
boot # boots the operating system

If you want to start a 2nd Windows from the 2nd disk, you virtually swap disk 1 and 2. Thereby Windows sees a simulated 1st disk:

map (hd0) (hd1)
map (hd1) (hd0)

Pitfalls

As Grub has no 1024-cylinder barrier and can boot systems above 1024 cylinders, the "/boot/grub/stage2" must be accessible by the bios (must be before 1024 cylinders). Stage2 may not be moved after installation (e.g. Defrag could try that). Setup data will get written that would not be congruent anymore. It is reasonable to make stage2 read-only (you must be in the current directory):

Dos: attrib +r +s stage2
LINUX: # chmod a -w ./stage2

Where to find out more

The official FAQ for Grub.

If you have a supported network card, Grub can boot over the network. An appropriate Boot-Disk can be obtained here (allthough with an older version of Grub).

Content last revised 13/09/2008 1200 UTC