GRUB Legacy: Difference between revisions

m
no edit summary
[unchecked revision][unchecked revision]
mNo edit summary
 
(47 intermediate revisions by 23 users not shown)
Line 1:
{{You}}
==What is GRUB==
GRUB stands for GRand Unified Bootloader and is a GNU [[bootloader]] and the reference implementation of the [[Multiboot|Multiboot Specification]]. thatGRUB can boot aany varietyMultiboot-compliant ofOS operating(e.g. systemsHurd, from[http://grub.enbug.org/MultibootSystems Linux,among Mach4others]), vSTA,however DOS,it NTcan 3also boot some non-Multiboot-compliant OSes, e.51g. Windows, thevia *BSDa variantschain-loading function, and anysome genericothers via special support, e.g. Linux. OS youmbchk can addbe used to itdetermine withif itsa easy-to-usefile bootis menuMultiboot-compliant.
 
NOTE: This FAQarticle concentrates on theGRUB recentlyLegacy. renamedInformation toregarding "GRUB legacy".modern [[GRUB]], 2a doesn'tfull soundrewrite, 'released'can enoughbe atfound thein timea ofseparate writing..article.
 
==Features==
GRUB takes away all the complexities out of trying to boot your OS by doing it for you. GRUB can handle a variety of filesystems from MS-DOS FAT systems over Linux ext2fs (Second Extended File System) to BSD FFS, and can load kernels in various binary formats, including "generic" [[ELF]], FreeBSD a.out, flat binary, and "generic" executable's (any file with a valid multiboot header). One major advantage of GRUB (or more precisely, a multiboot standard bootloader) is that the kernel will be entered in a known state, which includes the [[A20Line|A20 Line]] having been enabled, and [[Protected mode]] having been entered. This takes a lot of the pain out of writing a kernel, rendering GRUB a very useful tool for the amateur, or anyone who wants to spend more time on the intricacies of the kernel rather than worrying about these generic start-up procedures.
GRUB takes away all the complexities out of trying to boot your OS by doing it for you. GRUB can handle a variety of filesystems: from MS-DOS FAT systems over Linux ext2fs (Second Extended File System) to BSD FFS, and can load kernels in various binary formats, including "generic" [[ELF]], FreeBSD a.out, flat binary, and "generic" executables (any file with a valid Multiboot header).
 
One major advantage of GRUB (or more precisely, a Multiboot-compliant bootloader) is that the kernel will be entered in a known state, which includes the [[A20 Line]] having been enabled, and [[Protected mode]] having been entered. This takes a lot of the pain out of writing a kernel, rendering GRUB a very useful tool for the amateur, or anyone who wants to spend more time on the intricacies of the kernel rather than worrying about these generic start-up procedures.
Check out informations about the [http://www.gnu.org/software/grub/manual/multiboot/ multiboot standard] if you want to know what GRUB expects and what you can expect from it ...
 
Consult the [http://www.gnu.org/software/grub/manual/multiboot/ Multiboot Specification] if you want to know what GRUB expects and what you can expect from it.
==GRUB HOWTO==
 
==Installation and booting==
This section describes how to install GRUB on your <em>actual computer</em>, if you are using some kind of emulator (e.g. [[Bochs]]) to test your OS in you're probably more interested in the setup described in e.g. [[Bare bones#Booting the kernel|Booting the kernel]] or [http://www.jamesmolloy.co.uk/tutorial_html/1.-Environment%20setup.html Environment setup]. However, you might still be interested in [[#Load your kernel over network (TFTP)|how to load your kernel over TFTP]].
 
===Installing to floppy===
This tutorial will show you how to install GRUB and your self-made kernel on a floppy disk, to avoid screwing up your hard drive. Installing GRUB as your system bootloader depends heavily on your system setup and is beyond the scope of this tutorial.
 
GRUB cannot be installed directly to the intended boot device. Once an operating system is up and running, GRUB's probing the BIOS for available drives might yield wrong results. Thus, a four-step process is used (using two floppy disks: the boot disk to-be, and an auxiliary disk).
 
NOTE: creating your own GRUB bootdisk requires a unixUnix-like environment. If you're working under MS Windows (or don't feel like setting up a GRUB disk with linuxLinux), check out our [[Disk Images Under Windows]] page to get a ready-to-use image.
 
# preparePrepare the boot disk;
# create a rudimentary GRUB installation on an auxiliary disk;
# boot from the auxiliary disk, and complete the GRUB installation on the boot disk.
 
You will find the GRUB homepage at [httpftp://wwwalpha.gnu.org/softwaregnu/grub]; the/ source as well as precompiled binaries] canat be found atthe [ftphttp://alphawww.gnu.org/gnusoftware/grub/ GRUB web page]. (Windows users will want the <code>grub-0.9x-i386-pc.tar.gz</code> package.) Download and unpack the archive.
 
===Step one: ==Preparing the boot disk=====
Format a floppy disk; this will be your boot disk. Create a directory "<code>boot"</code> on that disk, and copy the files "<code>stage1"</code> and "<code>stage2"</code> from the tarball (or your build directory) to "<code>boot"</code>. Note that, after the installation, "<code>stage2"</code> must not be relocated (e.g. by Windows' disk defragmentation), or the bootloader will be unable to find it.
 
=====Auxiliary GRUB install=====
Remove the disk from the floppy drive.
Remove the boot disk from the floppy drive and insert the auxiliary disk. The files <code>stage1</code> and <code>stage2</code> from the GRUB distribution must be written to this disk, to sector 0 and sector 1+ respectively. In your shell (for Windows users, this means the [[Cygwin]] shell), type:
 
<syntaxhighlight lang="bash">
===Step two: Auxiliary GRUB install===
dd if=<stage1> of=/dev/fd0 bs=512 count=1
Now insert the auxiliary disk into the drive. The files "stage1" and "stage2" from the GRUB distribution must be written to this disk, to sector 0 and sector 1+ respectively. In your shell (for Windows users, this means the Cygwin shell!), type:
dd if=<stage2> of=/dev/fd0 bs=512 seek=1
</syntaxhighlight>
 
Now GRUB will be able to boot stage1 and stage2 from this disk. We cannot use it as our boot disk, though, since writing stage2 to sector 1 destroyed the floppy's format structure (you can't write files to the disk anymore, and any previously written <code>boot</code> directory could no longer be found). That's why we will now set up the other disk to boot GRUB and have a valid structure.
$> dd if=<stage1> of=/dev/fd0 bs=512 count=1
$> dd if=<stage2> of=/dev/fd0 bs=512 seek=1
 
Now GRUB will be able to boot stage1 and stage2 from this disk. We cannot use it as our boot disk, though, since writing stage2 to sector 1 destroyed the floppy's format structure (you can't write files to the disk anymore, and any previously written "boot" directory could no longer be found). That's why we will now set up the other disk to boot GRUB and have a valid structure.
 
Leave the auxiliary disk in the drive, and boot from that floppy.
 
===Step three: ==Completing the install=====
GRUB will boot into its command shell. (If your system does boot from hard drive instead, check your BIOS boot drive settings.) When the "<code>grub >"</code> prompt appears, remove the auxiliary disk from the drive and replace the boot disk. From the GRUB shell, you can now complete the installation on the boot disk:
 
<syntaxhighlight lang="bash">
install (fd0)/boot/stage1 (fd0) (fd0)/boot/stage2 (fd0)/boot/menu.cfg
install (fd0)/boot/stage1 (fd0) (fd0)/boot/stage2 (fd0)/boot/menu.cfg
</syntaxhighlight>
 
The parameters tell GRUB:
 
* where to find the stage1 file;
* which device to install GRUB on (the floppy drive) - this writes stage1 to the boot sector, you can delete <code>boot/stage1</code> afterwards;
* where to find the stage2 file (hardcoding the sector number of that file into the boot sector stage1; that's why <code>stage2</code> must not be moved after this procedure);
* where stage2 should look for the configuration file.
 
Remove the boot disk from the drive, and reboot your operating system so you can edit <code>/boot/menu.cfg</code>.
 
===Step four: ==Configuring GRUB=====
When you boot from the just-created boot floppy, GRUB stage1 is read from the floppy's boot sector. This stage1 "knows" where to find stage2 (it was told so during installation), and loads that. The stage2 does lots of interesting stuff (we'll come to that), but most importantly, it "knows" how to read file systems, so it can find the configuration file, "<code>boot/menu.cfg"</code>.
 
The configuration file tells GRUB where to look for the actual kernel image(s). You can set it up so it offers you a selection of various different kernels / operating systems, even allowing you to boot your "real" OS from hard drive after booting GRUB from floppy. (In the end, you will probably want to [[#Installing to hard disk|install both GRUB and your own OS kernel on the hard drive]], but we'll stick with the floppy solution for now.)
 
Add an entry like this to the configuration file:
 
<pre>
title MyOS
roottitle (fd0)MyOS
root (fd0)
kernel /boot/kernel.bin
kernel /boot/kernel.bin
</pre>
 
Now all we have to do is to compile a kernel image and save it as <code>/boot/kernel.bin</code>. GRUB will find, load, and start it. For more details, menu options etc., refer to the GRUB manuals.
 
===Installing to USB===
==Network booting with GRUB==
'''NOTE''': I have tried many tools to install GRUB onto a USB device under windows, however, I have not been able to figure out how. So, to install, either use a Linux distro on your PC of use a Linux Live CD.
You can also use grub's builtin network support to increase your build/test turn around time. You can boot another machine with a kernel directly from your build directory without having to copy it over manually.
 
Putting GRUB onto a USB device is as easy as following the above example of installing to a standard hard disk. There is one extra step involved, finding out under which mount point your USB device is located. You may even have to mount it yourself if it is not already mounted. To install use the following steps:
===Geting Grub to support your card===
First you need a version of grub that supports your network card. Most likely you will have to build one from source, so grab it from [ftp://alpha.gnu.org/gnu/grub/] and extract it to a work directly.
 
If you need to mount your USB device: (Exchange <code>sdb1</code> with your device name.)
Run
 
configure --help
 
to see a list of options. In my case, I use Netgear tulip cards, so I execute:
 
configure --enable-tulip
make
make install
 
You may want to consider using the --prefix option if you don't want to overwrite your existing grub install or do not have root access.
 
NOTE: this was done on a Linux box, I assume using Cygwin under Windows would work as well.
 
===Set up a TFTP server===
You will need a tftp server running on your build machine and pointing to your build directory. I've been using the 3CServer utility from 3COM, [http://support.3com.com/software/utilities_for_windows_32_bit.htm], but there are several out there for most platforms.
 
Make sure any firewalls you have installed allow incoming connections on the tftp ports.
 
===Install as you would normally.===
On your target machine install the grub binary you just built as documented above.
 
===Configure GRUB===
 
I use the following entry in my menu.lst to boot my kernel.
 
title Network Boot
# obtain an ip address for this card
dhcp
# I don't have dhcp setup to deliver the tftp server's ip address, so I set it manually
# this is my build machine
tftpserver 192.168.0.50
# the root device is the network
root (nd)
# my kernel is directly under the tftp directoy
kernel /kernel.exe
# I also load a module for testing out multitasking
module /module.exe
 
And that should be it. If you're not running dhcp you can use ifconfig to manually set addresses, gateways, etc. Your can also use the bootp or rarp protocols.
 
==Booting from USB with GRUB==
 
NOTE: I have tried many tools to install GRUB onto a USB device under windows, however I have
not been able to figure out how. So, to install, either use a Linux distro on your PC of use a Linux Live CD.
 
Putting grub onto a USB Pen/Disk is as easy as following the above example of installing to a
statndard hard disk.
 
There is one extra step involved, finding out under which mount point you USB device is located.
You may even have to mount it yourself if it is not already mounted.
 
To install use the following steps:
 
If you need to mount your USB device: (Exchange 'sdb1' with your device name)
 
<syntaxhighlight lang="bash">
mkdir /mnt/sdb1
mount /dev/sdb1mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1
</syntaxhighlight>
 
Create required directory'sdirectories:
 
<syntaxhighlight lang="bash">
mkdir /mnt/sdb1/boot
mkdir /mnt/sdb1/boot/grub
mkdir /mnt/sdb1/boot/grub
</syntaxhighlight>
 
Locate the <code>stage1</code>, <code>stage2</code>, and ???<code>*_stage1_5</code> files:
 
<syntaxhighlight lang="bash">
find / -name stage1
find / -name stage1
</syntaxhighlight>
 
Copy stage1 and stage2 files:, <code>/usr/lib/grub/</code> is an example location, use the location returned by the find command:
NOTE: /usr/lib/grub/ is an example location, use the location returned by the find command.
 
<syntaxhighlight lang="bash">
cp /usr/lib/grub/stage1 /mnt/sdb1/boot/grub/stage1
cp /usr/lib/grub/stage2stage1 /mnt/sdb1/boot/grub/stage2stage1
cp /usr/lib/grub/stage2 /mnt/sdb1/boot/grub/stage2
</syntaxhighlight>
 
For FAT formatted USB device use:
 
<syntaxhighlight lang="bash">
cp /usr/lib/grub/fat_stage_1_5 /mnt/sdb1/boot/grub/fat_stage_1_5
cp /usr/lib/grub/fat_stage1_5 /mnt/sdb1/boot/grub/fat_stage1_5
</syntaxhighlight>
 
For Ext2fsext2 formatted USB device use:
 
<syntaxhighlight lang="bash">
cp /usr/lib/grub/e2fs_stage_1_5 /mnt/sdb1/boot/grub/e2fs_stage_1_5
cp /usr/lib/grub/e2fs_stage_1_5 /mnt/sdb1/boot/grub/e2fs_stage_1_5
</syntaxhighlight>
 
Then execute the grubGRUB install application:
 
<syntaxhighlight lang="bash">
sudo grub
sudo grub
</syntaxhighlight>
 
Enter the following commands:
 
<syntaxhighlight lang="bash">
root(sdb1,1)
root (hd1,1)
setup(sdb1)
setup (hd1)
quit
quit
</syntaxhighlight>
if you don't know where your kernel is, you can use the command "find /boot/grub/e2fs_stage_1_5" to get the parameters which would be used in the command root (sdb1,1) and setup (sdb1).
 
===Installing to hard disk===
==Booting from CD with GRUB==
See [http://www.gnu.org/software/grub/manual/html_node/Installation.html Installation] in the official GRUB manual.
 
===Installing to CD===
NOTE: This tutorial required the mkisofs application to generate a CD image. This tutorial
See [[Bootable El-Torito CD with GRUB Legacy]].
also requires the stage2_eltorito file.
 
==Load your kernel over network (TFTP)==
This tutorial can be run on windows (mkisofs is available under the cygwin distribution) or under linux.
{{FirstPerson|section=1}}
You can also use GRUB's builtin network support to increase your build/test turn around time. You can boot another machine with a kernel directly from your build directory without having to copy it over manually. See also, [http://www.gnu.org/software/grub/manual/html_node/General-usage-of-network-support.html#General-usage-of-network-support How to set up your network] in the official GRUB manual.
 
=====Getting GRUB to support your card=====
To install use the following steps:
First you need a version of GRUB that supports your network card. Most likely you will have to build one from source, so [ftp://alpha.gnu.org/gnu/grub/ grab it] and extract it to a work directly.
 
Run
Create the required directory structures...
 
<syntaxhighlight lang="bash">
Windows:
./configure --help
</syntaxhighlight>
 
to see a list of options. In my case, I use Netgear tulip cards, so I execute:
mkdir c:\grubcd
mkdir c:\grubcd\iso
mkdir c:\grubcd\iso\boot
mkdir c:\grubcd\iso\boot\grub
 
<syntaxhighlight lang="bash">
Linux:
./configure --enable-tulip
make
make install
</syntaxhighlight>
 
You may want to consider using the <code>--prefix</code> option if you don't want to overwrite your existing GRUB install or do not have root access.
mkdir -p /grubcd/iso/boot/grub
 
NOTE: this was done on a Linux box, I assume using Cygwin under Windows would work as well.
Copy the stage2_eltorito file:
 
=====Set up a TFTP server=====
Windows:
You will need a TFTP server running on your build machine and pointing to your build directory. I've been using the [http://support.3com.com/software/utilities_for_windows_32_bit.htm 3CServer utility from 3COM], but there are several out there for most platforms.
 
Make sure any firewalls you have installed allow incoming connections on the TFTP ports.
NOTE: The path c:\downloads\grub\ should be changed with the actual location of stage2_eltorito
 
=====Install as you would normally=====
copy c:\downloads\grub\stage2_eltorito c:\grubcd\iso\boot\grub
On your target machine install the GRUB binary you just built as documented above.
 
=====Configure GRUB=====
Linux:
I use the following entry in my <code>menu.lst</code> to boot my kernel.
 
<pre>
cp /lib/grub/i386-pc/stage2_eltorito /grubcd/iso/boot/grub
title Network Boot
# Obtain an IP address for this card
dhcp
# I don't have DHCP setup to deliver the TFTP server's IP address,
# so I set it manually this is my build machine
tftpserver 192.168.0.50
# The root device is the network
root (nd)
# My kernel is directly under the TFTP directory
kernel /kernel.exe
# I also load a module for testing out multitasking
module /module.exe
</pre>
 
And that should be it. If you're not running DHCP you can use <code>ifconfig</code> to manually set addresses, gateways, etc. Your can also use the BOOTP or RARP protocol.
Next, create the iso image:
 
==Other loaders that are Multiboot compliant==
Windows:
Chris Giese's [http://my.execpc.com/~geezer/temp/mbload.zip MBload] also works with Multiboot-compliant kernels. Check out the [[Topic:8549|related thread]] for more info.
 
==See Also==
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o grub.iso C:/grubcd/iso/
===Wiki Pages===
*[[Grub Error 13]]
*[[Multiboot]]
 
Linux:
 
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o grub.iso grubcd/iso/
 
Edit this image to include your menu.lst and kernel, then burn to a CD.
 
==Other Loaders that are multiboot-compliant==
Chris Giese's [http://my.execpc.com/~geezer/temp/mbload.zip MBload] also works with multiboot-compliant kernels. Check out [http://www.osdev.org/phpBB2/viewtopic.php?t=8549 related thread] for more info.
 
==See Also==
===Tutorials===
*[[Bare bones]]
Line 217 ⟶ 205:
*[[Topic:9548|Switching back to real mode from GRUB]]
*[[Topic:9586|Setting boot-time graphic video mode with GRUB]]
*[[Topic:1744118255|GRUB VBE Patch]]
 
===External Links===
 
*[http://www.gnu.org/software/grub/manual/html_node/ GRUB Manual]
*[http://www.gnu.org/software/grub GRUB Homepage]
*[http://grub.enbug.org/ GRUB wiki]
*[http://tldp.org/HOWTO/Linux+Win9x+Grub-HOWTO/index.html Linux+Win9x+Grub HOWTO]
*[http://promote-opensource.org/modules/mylinks/singlelink.php?cid=14&lid=94 Linux Recovery and Boot Disk Creation with Grub]
*[http://www.skyjammer.com/files/knoppix/ Win32 GRUB]
*[http://www.ameritech.net/users/gholmer/booting.html Booting with GRUB]
*[http://grub4dos.sourceforge.net/ WinGRUB]
*[http://www.geocities.com/lode_leroy/grubinstall/ Grub installer for Windows]
*[http://grub.linuxeden.com/ GRUB for DOS]
*[http://www.pixelbeat.org/docs/disk/ GRUB PC Disk details]
*[http://www.gnu.org/software/grub/manual/legacy/grub.html GRUB Legacy manual]
 
*[https://gna.org/projects/grub4dos/ GRUB for DOS]
 
[[Category:Bootloaders]]
[[Category:GRUB]]
[[de:Grand Unified Bootloader]]