Search:  
Gentoo Wiki

HOWTO_Install_Gentoo_from_USB_stick_using_GRUB

Contents

Why another "Install from USB HOWTO"?

Good question. And why GRUB? Another good question. There are bootloaders like syslinux, isolinux, pxelinux, extlinux etc. that can do GRUBs job pretty well. But in my case all of them didn't work. I also believe I did something that's quite uncommon, so maybe it will help some people.

My story

I bought myself a new computer that has no CD-ROM drive. So I thought that I would boot it and install Gentoo using USB stick. I followed some HOWTO trying to port LiveCD to my stick. Unfortunately, my stick is too small for the LiveCD image- 512MB only. So I tried the same with MinimalCD. Unfortunately the kernel couldn't mount the VFAT partition on the stick, because of no codepage 437 support. So I tried to mix the kernel from the LiveCD and the rest of the image from MinimalCD, but I still couldn't make it work. So I thought about changing VFAT to EXT2. But then again, the syslinux bootloader complained that it needs VFAT to work. So I changed syslinux to extlinux. But extlinux didn't boot at all... It seemed like I was running out of options, but I thought I'd give GRUB a try... So here's what I did:

Before you begin

Before you begin, ask yourself if you really need this - maybe you can borrow CD-ROM drive, maybe you can take your HDD and go to a friend. I took me four days to make this work and the procedure is kind of troublesome...

Here's a list of what you will need:

Set up your BIOS

First of all you have to check if your BIOS can boot an OS from an USB stick. In my case this step required experimenting with some options in the BIOS setup. If you think that your BIOS doesn't support booting from USB, plug the stick in before you turn the computer on and check the BIOS again - in my case some additional options appeared.

Prepare the USB stick

Plug your USB stick into your working Linux box.

Note: If your Linux auto-mounted your USB stick, unmount it.
Note: I assume that your Linux sees your stick as /dev/sdb.
Warning: Double check the USB stick device name and don't confuse it! If you have SCSI or SATA disk your Linux will recognize it as /dev/sd? and so will it recognize the USB stick. If you get it wrong, you can kiss your data good-bye...

It may be easier if you type all commands as root, so:

sudo su -

Clean the stick

Warning: This will destroy all data on your USB stick. Make a backup!

First of all, erase all data from the stick, including the partition table.

dd if=/dev/zero of=/dev/sdb

Create an EXT2 partition on the stick

Run fdisk:

fdisk /dev/sdb

Because we cleared the whole disk, partition table contains errors, so type o to create new partition table:

Command (m for help): o
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Now, create one primary partition by typing n, p, 1:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1

Make sure that the partition begins far away from the first sector and that it's big enough (you may need to adjust the values to fit your stick):

First cylinder (1-1020, default 1): 200
Last cylinder or +size or +sizeM or +sizeK (200-1020, default 1020): 400

Save it:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

And create EXT2 filesystem on it:

mkfs.ext2 /dev/sdb1

Copy the ISO image to the USB stick

Mount the newly created EXT2 partition:

mount -t ext2 /dev/sdb1 /mnt/stick

Mount the Gentoo CD ISO image:

mount -t iso9660 -o loop /path/to/your/gentoo-cd-image.iso /mnt/cdimg

Copy the content of the ISO image to the stick:

cp -R /mnt/cdimg/* /mnt/stick

Unmount the stick, but don't unmount the CD image yet:

umount /mnt/stick

Prepare GRUB

Warning: In this part you will have to compute some numbers manually basing on what dd tells you. It's important that you compute those numbers correct and that you understand what's going on in here. Otherwise the USB stick may not boot.

Now comes the hard part. I couldn't use grub command to make things work. So we will have to copy the bootloader to the stick manually. We'll arrange the space on the stick in the following manner:

Now you see why we had to make our EXT2 partition begin far away from the disk start - we need place for the bootloader and kernel! Okay, here we go:

Install GRUB stage1:

dd if=/boot/grub/stage1 of=/dev/sdb bs=1 count=446

Install GRUB stage2:

dd if=/boot/grub/stage2 of=/dev/sdb bs=512 seek=1

Hey, what's going on? The first command transfered stage1 to the stick. /boot/grub/stage1 file is 512B big and it would overwrite our partition table, so we copy only first 446 bytes. The second command transfered stage2 to the stick, right after the partition table. It copied data in 512B chunks (bs=512) and stored them on /dev/sdb starting at offset of 1 chunk (seek=1). So we didn't overwrite the MBR.

Now, the second dd produced some output like:

196+1 records in
196+1 records out
100458 bytes (100 kB) copied, 0.0496421 seconds, 2.0 MB/s

It means that dd transfered a total of 197 (196+1) chunks, 512B each. First 196 chunks were full, the last one was a bit smaller. That's because 100458 = 196*512+106.

The next thing to copy to the stick is the kernel. So:

dd if=/mnt/cdimg/isolinux/gentoo of=/dev/sdb bs=512 seek=198

What does this command do? It transfers the kernel to the stick in 512B chunks (bs=512). Additionally, it stores the kernel after first 198 chunks of the stick. Why 198? Because MBR is one 512B chunk and we just copied stage2 that was 197 chunks big. 1+197=198. First 198 chunks are occupied, next one is free.

3335+0 records in
3335+0 records out
1707520 bytes (1.7 MB) copied, 0.412498 seconds, 4.1 MB/s

As we can see, the kernel is 3335 (3335+0) chunks big. Therefore, the initrd that goes next, will be copied with 198+3335=3533 chunk offset:

dd if=/mnt/cdimg/isolinux/gentoo.igz of=/dev/sdb bs=512 seek=3533

You can see that the initrd is pretty big - 9423 chunks:

9422+1 records in
9422+1 records out
4824103 bytes (4.8 MB) copied, 1.28447 seconds, 3.8 MB/s

That's it! Your USB stick should be ready to boot! But before you do that write some stuff on a piece of paper:

Boot your machine using the USB stick

Plug the stick out of your working Linux, plug it into your new computer and turn it on. If everything goes OK, you should see GRUBs prompt:

grub>

If you don't see this message, there are several things that may have went wrong:

Now you have to guess what name did GRUB give to your USB stick. Try typing in following commands:

root (fd0)
root (fd1)
root (hd1)
root (hd0)
root (hd3)
root (fd7)
etc.

For most of these commands GRUB should answer Selected disk does not exist, but for two of them it should report something else. One of these devices is the HDD in your computer (most likely it will be (hd0)), one of them will be your USB stick (suppose it's (fd0)).

Let's boot the damn thing! Load the kernel and provide it with necessary arguments (taken from the isolinux.cfg file and slightly adjusted), use your offset and size:

kernel (fd0)198+3335 root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/image.squashfs cdroot slowusb initrd=gentoo.igz vga=791 CONSOLE=/dev/tty1 quiet

Load the initrd, use your offset and size:

initrd (fd0)3533+9423

Boot the kernel and pray:

boot

If everything went well, you should see Gentoo Minimal CD boot. You can proceed with official Gentoo Handbook.

See also

Retrieved from "http://www.gentoo-wiki.info/Live_USB_stick_using_GRUB"

Last modified: Thu, 02 Oct 2008 10:38:00 +0000 Hits: 9,474