GRUB tips and tricks

Spicing up a great utility for more IT fun

Download the whole article as PDF

Write a full post in response to this!


The GRand Unified Boot loader, or GRUB, has all but replaced the default boot loader on many GNU/Linux distributions. It includes some conveniences over LILO, the LInux LOader. One advantage is not having to remember to run /sbin/lilo every time you make a configuration change. It also can function as a boot loader for removable media such as floppies, CD-R/W and USB flash memory keys. It is short-sighted to view GRUB only as a boot loader to be installed on a hard drive of a GNU/Linux system. Combined with a few other utilities, GRUB can be a powerful and good-looking tool for your home, organization or workplace.

Introduction

First, what exactly is GRUB? GRUB is a boot loader, which means it passes control of the boot process from the Power-On Self Test (POST) to the kernel of your GNU/Linux distribution. GRUB works in a modular, layered fashion so that any unneeded modules are not loaded. Not only does this reduce execution time, but it saves valuable resources when running from removable media. GRUB optionally loads its configuration file at run/boot time, so you don’t have to type in commands manually each time. However, the command-line option is still available in case there is an error in your configuration file. So why use GRUB when there are other options out there? The beauty of free software is that you have choices. Alternatives to GRUB include LILO, syslinux and isolinux. The benefit of GRUB is that it will work in many different types of boot devices, but you only need to learn one set of menu commands. In addition, GRUB can work on other forms of bootable storage, such as CD-R/W, USB flash memory keys, floppy disks, and even via a TFTP server with PXE ROM booting.

Installing GRUB on a USB flash memory key

Figure 1: you can run GRUB on a USB flash memory key!
Figure 1: you can run GRUB on a USB flash memory key!

I got the inspiration for this article after trying DSLinux (or DSL), which is a fully graphical Linux distribution weighing in around 50 MB. After seeing an advertisement on their website for a USB flash memory drive with DSL installed, I figured I could probably learn how to set DSL up myself on my Lexar 256 MB JumpDrive. The DSL documentation pointed towards installing via syslinux and reconfiguring the cylinder/head/sector information of my JumpDrive, but I didn’t have any luck trying to get my USB flash memory key to boot successfully. Finally, I tried using GRUB and I was up and running with DSL in no time!

First, I recommend creating a directory structure to organize your boot-related files, and keep them separate from any other files you’d like to keep on the USB flash memory key. You could create two partitions, but I couldn’t get both partitions to load correctly when I inserted the key back into Windows. On my USB flash memory key, I created a root folder named boot to hold all the data necessary for USB booting (see figure 2). Under the boot folder, I created a directory named grub for GRUB-related files, images which are initial ramdisk (initrd), floppy, or disk images, and finally kernels to hold all the kernels. You may want to organize your boot folder differently, but make sure that you change the corresponding paths and directory names in GRUB’s menu.lst file. The menu.lst file that I use can be found in Sidebar 1.

Figure 2: this is the file structure on my USB Memory Key
Figure 2: this is the file structure on my USB Memory Key

Sidebar 1: Contents of menu.lst

default=0

timeout=10

root=(hd0,0)

splashimage=/boot/grub/debsplash.xpm.gz

title DSL 1.2 (2.4.26) 1024x768 (save to RAM)

kernel /boot/kernels/dsl-linux24 ramdisk_size=100000 init=/etc/init lang=us apm=power-off vga=791 toram nomce noapic quiet knoppix_dir=images knoppix_name=dsl

initrd=/boot/images/dsl-minirt24.gz

title Debian Sarge Installer

kernel /boot/kernels/di-vmlinuz initrd=/boot/images/di-initrd.gz ramdisk_size=10240 root=/dev/rd/0 devfs=mount,dall rw

initrd /boot/images/di-initrd.gz

title HP nx5000 F0.d BIOS Upgrade

kernel /boot/kernels/memdisk

initrd /boot/images/hpnx5000f0d.img

title Memtest86+ (1.60)

kernel /boot/kernels/memdisk

initrd /boot/images/memtestp.bin

Next, you’ll need to copy some of GRUB’s stage files, including stage1, stage2, and fat_stage1_5, and put them into the boot/grub directory on the USB flash memory key. These will allow GRUB to boot into GNU/Linux and other operating systems. After the files are copied over, it’s time to install GRUB to the Master Boot Record (MBR) of the USB flash memory key.

Luckily, it’s the same process as installing to a hard drive:

# grub
grub> find /boot/grub/stage1
 (hd0,1)
 (hd2,0)

On my system, hd0 is /dev/hda and hd2 happens to be /dev/sda. Just to make sure, we can use a bash-like tab completion to look through a filesystem:

grub> find (hd2,0)/boot/im<TAB>
grub> find (hd2,0)/boot/images/

Since the /boot directory on /dev/hda doesn’t have an images directory, I know that (hd2) is the hard drive that I want to install GRUB on:

grub> root (hd2,0)
 Filesystem is type fat, partition type 0xb
grub> setup (hd2)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/fat_stage1_5" exists... yes
 Running "embed /boot/grub/fat_stage1_5 (hd2)"...  15 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd2) (hd2)1+15 p (hd2,0)/boot/grub/stage2
/boot/grub/menu.lst"... succeeded
Done.
grub> quit

Great! Now we have GRUB in the USB flash memory key’s MBR. Now, we have to put some files on the memory key to boot into and create a menu.lst file!

Don't miss out on the other pages!
12next ›last »

Write a full post in response to this!

Similar articles

0

Do you like this post?
Vote for it!

Copyright information

This article is made available under the "Attribution-NonCommercial-NoDerivs" Creative Commons License 3.0 available from http://creativecommons.org/licenses/by-nc-nd/3.0/.

Biography

Jeremy Turner: Jeremy Turner enjoys freelance writing when given the opportunity. He often plays system administrator, hardware technician, programmer, web designer, and all-around nice guy. You contact him by visiting his web site.

admin's picture

Comments from the old system

Submitted by admin on Thu, 2006-03-30 14:43.

Vote!
0

From: pierre laplante (SUBSCRIBER!)
Url:
Date: 2006-01-29
Subject: files

Do you have a places where we can find all the files. So it would be easier to test.

thanks

From: Matt
Url:
Date: 2006-01-30
Subject: Typo

4th sentance, 1st paragraph. "short-sided" => "short-sighted"

From: Dave Guard (SUBSCRIBER!)
Url: www.freesoftwaremagazine.com
Date: 2006-01-31
Subject: Typo - fixed.

Thanks for spotting that. Somehow, everyone in the editing process missed it. It must be because it doesn't look like a typo.

Thanks again and thanks for reading.

From: Stuart Guthrie
Url: http://www.polonious.com.au
Date: 2006-02-09
Subject: Can you boot from a disk.iso?

Is there a way to boot from a disk version of an iso? ie to cut-out the step where you burn a CD. This would be most handy..

Jeremy Turner's picture

Booting from iso

Submitted by Jeremy Turner on Tue, 2006-04-25 13:08.

Vote!
0

Hi Stuart,

No, I was not able to find a way to boot directly from an iso image. But it was also not the intended goal. Since grub works independently of the kernel and disk image, I can copy the compressed-cloop file (usually KNOPPIX or such), along with the kernel and minirt file, save it to my flash drive, and it would work great. Some distributions, like DamnSmallLinux rarely ever change kernels (DSL 2.3 still uses 2.4.26), I don't even need to copy over those files.

Although not much, copying the entire iso would take up a little more space. Not only are there extra files on the iso image, but there's the properties of the iso image files (Joliet and RockRidge extension information) that would all need to be saved on my flash drive. This is probably not much, but I'm working with a 256 MB drive.

So the short answer is no. But let me know if you come across a way to do it!

Anonymous visitor's picture

The short answer is yes :).

Submitted by Anonymous visitor on Wed, 2006-10-04 07:35.

Vote!
0

The short answer is yes :). I just spent the past two days to find out how.

use grub for dos (http://grub.blogeden.cn/post/23308/2412 - it's in chinese though)

If you just want to do something like in linux live cd, i.e., the iso file is used to host the systems files, it's easy and straightforward to use iso.

If you want to boot from a iso file on a harddisk, do something in menu.lst like

title Boot from iso on a harddisk
map (hdX,Y)/your.iso (hdZ)
map --rehook
chainloader (hdZ)+1
rootnoverify (hdZ)
boot

The iso file must be in one chunk - contig from sysinternal http://www.sysinternals.com/Utilities/Contig.html may come handy. It's a good idea to use fat32 to host the iso file.

Also check out
http://www.911cd.net/forums//index.phpshowtopic=15617&st=20&p=101928&#entry101928
and
http://www.911cd.net/forums//index.php?showtopic=18045

Anonymous visitor's picture

It appears the short answer is not yet but soon!

Submitted by Anonymous visitor on Mon, 2007-02-26 18:41.

Vote!
0

http://sarovar.org/forum/forum.php?thread_id=1767&forum_id=1099

Anonymous visitor's picture

It appears the short answer is not yet but maybe soon!

Submitted by Anonymous visitor on Thu, 2007-03-29 09:25.

Vote!
0

I'm not sure if grub4dos will work with linux iso images. As it is said in the URL you posted, the work of grub4dos in this item may be followed in the same way it is at ISOEMU (by Gandalf).

I've taked a look at:
http://www.supinfo-projects.com/en/2005/vd_boot_multi_os/2/

I've donloaded ISOEMU
http://sysoft.zdwx.com/isoemu/bin/isoemu_109_eng.zip
... and even when I read the following at the README, I tried. Not too hard because of this:


Known Limitation

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

* Support FAT32/NTFS partition only;

* Works on x86's RealMode only(so this app cannot be used to boot Windows PE,

OR, WinNT based OS), `cause it hooks INT13h;

* Support 8.3 file format only;

so.... will it work with linux? I think not at the moment.
http://en.wikipedia.org/wiki/Protected_mode

Anonymous visitor's picture

boot from iso image

Submitted by Anonymous visitor on Fri, 2006-08-25 19:43.

Vote!
0

I'm searching for a way to boot from iso images...

I found the avlgo to boot iso, but this don't work very well, don't support linux live cd and the compression type is incompatible for linux boot cds testeds...

Jan Macek's picture

Sidebar 1: Contents of menu.lst

Submitted by Jan Macek on Thu, 2006-04-13 20:38.

Vote!
0

knoppix_dir=/boot/images instead of knoppix_dir=images for correct DSL booting. Really nicely written and useful article.

Anonymous visitor's picture

very nice article...!

Submitted by Anonymous visitor on Fri, 2006-08-25 18:06.

Vote!
0

very nice article...!

Anonymous visitor's picture

awesome :) Thanks

Submitted by Anonymous visitor on Mon, 2006-09-25 12:58.

Vote!
0

Works perfectly and so easy & simple. Thank you.

Anonymous visitor's picture

Freeze :(

Submitted by Anonymous visitor on Tue, 2006-10-17 13:15.

Vote!
0

Boot simply freezes on my machine juste before grub is loaded (or just when it loads ?). BIOS says it has found the USB mbr and will boot on it, then nothing. I must switch off my computer.
Have searched for reasons but found nothing. If someone has hints...

Anonymous visitor's picture

Grub install

Submitted by Anonymous visitor on Thu, 2006-11-16 23:30.

Vote!
0

When installing grub you were able to find your boot device by:
# grub
grub> find /boot/grub/stage1
(hd0,1)
(hd2,0)

Mine only shows shows my main hard disk. I am using Fedora Core 5 I have been trying to install Grub directly to the devices but it gives me a wierd error about my BIOS. My drive is a 2G noname brand. Any tips?

Anonymous visitor's picture

run as root

Submitted by Anonymous visitor on Wed, 2007-01-17 21:00.

Vote!
0

Be sure to run the grub command as root.

On Ubuntu if I run as user it will only list hd0,0 but if I do 'sudo grub' it finds all disks.

Anonymous visitor's picture

Few corrections

Submitted by Anonymous visitor on Mon, 2007-01-08 22:50.

Vote!
0

First, the latest version of grub I've got on FC6 actually looks for grub.conf NOT menu.lst. So you might have to rename that file.

Second, the error mentioned above is huge...the path for knoppix_dir is absolute, not relative, so it has to be /boot/images as mentioned.

Third, for the last poster on the Grub install...I've noticed that sometimes grub doesn't find the drive. I think if you do a bad search it can get confused. Quit and restart doing the commands in order. I also think that tabbing can confuse it. Took me a few tries but it worked.

Anyway, thanks for the article, it really helped!

Anonymous visitor's picture

IT FREEZES, PLEASE HELP

Submitted by Anonymous visitor on Wed, 2007-02-14 16:17.

Vote!
0

I can load GRUB, but when i choose DSL, it starts loading, then it says "cheking for USB devices" and freezes. I am using a centrino Toshiba laptop with 4 USB ports. Can anyone give some hints? Thanks.

Anonymous visitor's picture

Relativity

Submitted by Anonymous visitor on Tue, 2007-03-06 09:53.

Vote!
0

How could the resulting USB stick work on any computer, if the disk location is hard-coded into menu.lst?

Ixian's picture

It isnt hardcoded into

Submitted by Ixian (not verified) on Thu, 2007-05-10 09:47.

Vote!
0

It isnt hardcoded into menu.lst.
it uses hd0,0 which is what the USB stick becomes when you boot from it.

Anonymous visitor's picture

hello there i'm trying to

Submitted by Anonymous visitor (not verified) on Fri, 2007-11-30 01:37.

Vote!
0

hello there i'm trying to boot *.iso files from my bootdirectory

however its not accepted when edit

chainloader (hd0,1)/knoppix.iso

into grub.conf, it says it doest know the file system, but iso966- is in /grub.

it says version 0.97, is this too old ?this came with my gentoo install cd

can anyone give us their grub.conf with some working isos in it ?

thanx,
qedqubit