Search:  
Gentoo Wiki

HOWTO_Small_Footprint_Gentoo_on_USB_without_catalyst

Contents

Overview

The purpose of this is to make a bootable Gentoo system on a USB flash drive which can be modified using any other Gentoo system running on compatible architecture. This HOWTO assumes the reader is familiar with the steps listed in the Gentoo Handbook, although assistance will be given for those with special needs ;)

On another note its important that you have a working Gentoo distribution to use as your staging OS. I used the server version of Gentoo Vida Linux located below. If you decide on using this distribution as your staging OS make sure you run an emerge --sync when completed with the install as well as emerge the following packages to get upto speed with the rest of this HOWTO: gentoo-sources, cdrtools, mirrorselect, gen-toolkit, portage-utils.

emerge --sync && emerge gentoo-sources cdrtools mirrorselect gentoolkit portage-utils

Make the thumbdrive

Format the stick

Use fdisk or cfdisk (GUI frontend to fdisk) to format the drive. The number and size of the partitions depends on the use of the drive. If you plan to also use the disk as a thumbdrive, make a separate partition, and format that partition vfat. Make an appropriate filesystem for your system. JFFS2 is a good filesystem to use for flash drives, but larger drives will take a long time to mount (and boot). Ext2 works well, also. Avoid anything with journaling, to avoid writing many times to the same spot on the disk.

If you decide on using ext2 as the file system for your usb thumbdrive, use the following command to create the file system on your USB thumbdrive. Repeat for as many ext2 partitions as you have just created.

mkfs.ext2 /dev/<previously created partition> (i.e mkfs.ext2 /dev/sda1)

Lay out the filesystem

Download a stage3 from your favorite mirror. Using an embedded stage3 is recommended if space is at a premium. Unfortunately, embedded stage3s run on uclibc, which doesn't provide everything glibc does, notably iconv.

Unpack the stage3 tarball into the flash drive:

mkdir /mnt/thumdrive
mount /dev/(insert mount  point) /mnt/thumbdrive
cd /mnt/thumbdrive
tar xvjpf stage3-*

Make the kernel

Since your thumbdrive likely is too small to hold an entire kernel source tree, the host must compile the kernel for the target. Using a 2.6 kernel, make sure USB Mass Storage, SCSI Disk support, and UHCI support are all compiled into the kernel (not as modules). Otherwise, set up the kernel to run on your target computer. Compile, and copy the produced kernel into your thumbdrive's /boot. Not to confuse, but this last sentence about copying the kernel into your thumbdrives /boot is very relevant if you plan on booting directly from your USB thumbdrive. Below I will show howto boot directly to your thumbdrive as well as to a floppy or cdrom. All of these options rest on the Kernel having compiled in support for USB Mass Storage, UHCI, and SCSI in order to properly load the root filesystem off your thumbdrive.

Configuring the GRUB Bootloader - Thumbdrive

Below I will provide four ways to boot your thumbdrive; using the actual thumbdrive, a floppy disk, a cdrom, and an unconfigured GRUB. To boot our thumbdrive directly from BIOS we first need to make sure that the BIOS has support for booting USB Thumbdrives. many of the older BIOS's (pre 2000) don't but most stuff nowadays should have support, which will make this process much easier. Here is a link on booting from USB.

USB BIOS Settings

I want you to ignore everything until you reach Step 6: BIOS Settings , this will give you a small idea on how some of the major BIOS makers like Award/Phoenix and AMI have made their BIOS's so that you can boot your USB thumbdrive. I actually have an Award BIOS and had to download a BIOS update which had support for booting to USB thumbdrives. Unfortuanatly this still didn't work and I had to result to booting from CDROM in order to boot my USB thumbdrive. I will show you howto do that in a moment.

Before I show that lets get into booting the thumbdrive. You need to make sure that these directories are created off the root of the thumbdrive: /boot/grub and then that you have copied the entire /boot and all it's subdirectories over to your thumbdrives /boot

mkdir -p /mnt/thumbdrive/boot/grub
cp -r /boot* /mnt/thumbdrive/boot
touch /mnt/thumbdrive/boot/usbthumb

Proceed from here by copying your kernel that you created above with the compiled in SCSI and USB support to the /boot directory on your thumbdrive. As you will see I renamed mine to vmlinuz. This is just common practice I have and the name is irrelevant except when you are referencing it within your /boot/grub/menu.1st.

cp /usr/src/linux/arch/i386/boot/bzImage /mnt/thumbdrive/boot/vmlinuz

Our final step is to modify our menu.1st which is our Grub Title screen that will make life easier for us to boot our USB Thumbdrive w/o user intervention. so lets load this up.

nano -w /mnt/thumbdrive/boot/grub/menu.1st

Below is how mine looks and your should be similiar. notice how my root is pointing to /dev/sda1 as I only created one partition the size of my thumbdrive. The key with this is the rootdelay=x option. This is the most important part of the menu.1st because it tells grub to wait x seconds before trying to mount your root filesystem on /dev/sda1. To be safe, start at 20 seconds. This gives your system plenty of time to recognize your USB drive and prepare it to answer when grub calls for it. If your system still hasn't detected your USB drive, increase the rootdelay to 25 or 30 as well as verify that you have compiled in kernel support for SCSI and USB Mass Storage. If it is detected, you can nudge this down (to as little as 5 seconds, maybe less) for a faster boot time. Grub makes it easy to edit the command line before booting, so you can experiment with it to find the optimal value.

timeout 20
default 0

title=USB Thumbdrive Linux
root (hd0,0)
kernel /vmlinuz root=/dev/sda1 rootdelay=20

Upon finishing all of this we need to unmount our thumbdrive and invoke grub from the command line. it's time to write the MBR to our thumbdrive

unmount /mnt/thumbdrive
grub

Once in, we need to tell grub where to look in order to write the MBR. be very, very careful with this. You need to make sure that you choose the correct hard drive/usb drive for grub to write this MBR. Here is an easy way to determine which drive sees as your USB thumbdrive. type in:

root (hd1,0)

This next part is a little tricky. You will need to use autocompletion to verify the existence of a file which we placed on your thumbdrive earlier. to do this we will type:

find /boot/usb
*tab*

Here is the above command translated. type in find /boot/usb and then hit the tab key. Grub will auto populate the rest of this file as usbthumb. If it does so then you have found your USB thumb and can proceed. If not go back to where we typed in, root (hd1,0) and try root (hd2,0) etc. and run the find command until you locate that usbthumb file.

Since we have told grub where our usbthumb is located, we now need to write the MBR to the thumb. type this command in to do so:

setup (hd#)

The hd# might be hd1, hd2, or hd3 etc in your case. Whatever hd# you found grub was on from the above commands is the one you will provide for the setup command. Once you have completed this command skip ahead to the section entitled: Chroot into Thumbdrive

Configuring the GRUB Bootloader - CD-ROM

This is actually the way I am currently booting to my USB thumbdrive and is the best alternative way to booting the USB thumbdrive, in my opinion. For this we will be using the mkisofs (emerege cdrtools) tool as well as a cd burner in order to burn the grub created ISO to a CDROM. Start out by creating the directory structure:

mkdir -p /iso/boot/grub

Proceed by copying all your boot files from /boot to the /iso directory

cp -r /boot* /iso

We then need to copy the kernel to /iso/boot

cp /usr/srv/linux/arch/i386/boot/bzImage /iso/boot/vmlinuz

Now onto modifying our menu.1st

nano -w /iso/boot/grub/menu.1st

Here is what it should look like:


default=0
timeout=20

title USB Thumbdrive Linux
	root (cd)
	kernel /boot/vmlinuz root=/dev/sda1 ro rootdelay=20

That wraps up configuring Grub for the CDROM. We will now create the ISO that you will need to burn to a CDROM using your favorite CD burning program. I used Alchohol 120% which isn't a free utility but any CD-Burning program should be able to burn an ISO to a cd-r. Here is the command to create the ISO

cd /iso
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o /grub.iso /iso

After creating the ISO here you just have to burn it to a cd-r and set your BIOS to boot to the CDROM. Grub will take care of it for you. Once you have completed this command skip ahead to the section entitled: Chroot into Thumbdrive

Configuring the GRUB Bootloader - Floppy

This is the trickiest medium to load grub onto in the sense that your kernel needs to be under less than or equal to 1.3mb. If you create the GRUB boot loader on the CDROM I would highly recommend this because you have 700mb of space to work worth. I've never seen a kernel this big and if you find one that is larger than 700mb please let me and Linux Trovolds know. Anyways, I digress. Creating the GRUB boot floppy is almost the same as placing GRUB onto the USB thumbdrive. As I said, the kernel is are only issue. In order for me to get mine down I basically ended up with only having the SCSI and USB support compiled in and stripping out everything else which wasn't needed. If you need to you can load the modules after the OS has started from the thumbdrive. I won't get into that now though.

To start insert a floppy disk and format it

fdformat /dev/fd0H1440

Then create the file system. I generally use ext2

mkfs.ext2 /dev/fd0

And Mount it to /mnt/floppy

mount -t ext2 /dev/fd0 /mnt/floppy

Lets now create the directory structure for GRUB and copy only the stage1 and stage2 files from the /boot/grub directory

mkdir -p /mnt/boot/grub
cp /boot/grub/stage1 /mnt/floppy/boot/grub
cp /boot/grub/stage2 /mnt/floppy/boot/grub
touch /mnt/floppy/boot/grub/menu.1st

Proceed from here by copying your small kernel that you created above with the compiled in SCSI and USB support to the /boot directory on your floppy drive. As you will see I renamed mine to vmlinuz. This is just common practice I have and the name is irrelevant except when you are referencing it within your /boot/grub/menu.1st.

cp /usr/src/linux/arch/i386/boot/bzImage /mnt/floppy/boot/vmlinuz

Our final step is to modify our menu.1st which is our Grub Title screen that will make life easier for us to boot our USB Thumbdrive w/o user intervention. so lets load this up.

nano -w /mnt/floppy/boot/grub/menu.1st

Below is how mine looks and your should be similiar. notice how my root is pointing to /dev/sda1 as I only created one partition the size of my thumbdrive. The key with this is the rootdelay=20 option. This is the most important part of the menu.1st because it tells grub to wait 20 seconds before trying to mount your root filesystem on /dev/sda1. This gives your system plenty of time to recognize your USB drive and prepare it to answer when grub calls for it. If your system still hasn't detected your USB drive, increase the rootdelay to 25 or 30 as well as verify that you have compiled in kernel support for SCSI and USB Mass Storage.

timeout 20
default 0

title=USB Thumbdrive Linux
root (fd0)
kernel /boot/vmlinuz root=/dev/sda1 rootdelay=20

Upon finishing all of this we need to unmount our floppy and invoke grub from the command line. it's time to write the MBR to our floppy

unmount /mnt/floppy
grub

Once in, we need to tell grub where to look in order to write the MBR. be very, very careful with this. You need to make sure that you choose the correct hard drive/usb drive for grub to write this MBR. Here is an easy way to determine which drive sees as your USB thumbdrive. type in:

root (fd0)

Since we have told grub where our floppy is located, we now need to write the MBR to the floppy. type this command in to do so:

setup (fd0)

Once you have completed this command skip ahead to the section entitled: Chroot into Thumbdrive

Booting from an unconfigured GRUB

You might want this if you don't want to or can't modify your current GRUB configuration. This is the simplest method if you've already got GRUB on your system. If you press Escape during the GRUB splash or menu it will halt the timeout delay, pressing the "c" key will then give you the GRUB command line. Enter the next lines below, replacing hdX with your usb key as necessary to boot your kernel.

root (hdX)
kernel /vmlinuz root=/dev/sda1 rootdelay=20
boot

Chroot into Thumbdrive

Follow the gentoo handbook on chapters 6,7,8 but run these commands first.

mirrorselect -i -r -o >> /mnt/thumbdrive/etc/make.conf ## Select desired mirrors for portage
cp -L /etc/resolv.conf /mnt/thumbdrive/etc/resolv.conf ## Copy DNS information for Internet usage
mount -t proc none /mnt/thumbdrive/proc
mount -o bind /dev /mnt/thumbdrive/dev
mount -o bind /usr/portage /mnt/thumbdrive/usr/portage
mount -t tmpfs none /mnt/thumbdrive/tmp
mount -t tmpfs none /mnt/thumbdrive/var/tmp

After entering the above mounts continue on chapter 6 with chrooting into your thumbdrive

Configuring your system and installing necessary tools

Follow the Gentoo handbook, chapters 7 and 8.

Testing and debugging the thumbdrive

Unmount everything related to the thumbdrive:

umount /mnt/thumbdrive/usr/portage
umount /mnt/thumbdrive/proc
umount /mnt/thumbdrive/dev
umount /mnt/thumbdrive/tmp
umount /mnt/thumbdrive/var/tmp
umount /mnt/thumbdrive

Plug the thumbdrive into your target system and boot it up.

If all goes well, you should see a message telling you that the kernel is waiting for usb-storage to settle. If not, check to make sure your lilo.conf has rootdelay in it. You should then see a message from USB storage telling you that it has detected a thumbdrive, including the name of the device. If not, make sure you have USB support and USB Mass Storage support compiled into your kernel. You should then see a message from SCSI disk support telling you about the partitions on the thumbdrive and which device they were mapped to.

If you see a kernel panic where VFS is unable to mount the root partition, first check the above 3 things, then try increasing rootdelay.

Most other errors should be the same as on any hard drive based Gentoo system.

Installing new software on the thumbdrive

Making scripts

There are 6 scripts which we use to modify our thumbdrive system. Copy these scripts into your work directory, and make all of them except env.sh executable. If desired, keep a copy of these scripts on the thumbdrive.

env.sh:

# It is incredibly important not to put trailing slashes at the end of these

# Points to the directory where our thumbdrive is mounted.
THUMBDRIVE=/mnt/thumbdrive

# Points to the directory which holds the working copy of the thumbdrive. Needs to already exist.
THUMBDRIVECOPY=/root/wardrivepc/thumbdrivecopy

mountstuff.sh: This mounts portage, dev, proc, tmp, and var/tmp.

#!/bin/bash
. env.sh
mount -o bind /usr/portage $THUMBDRIVECOPY/usr/portage
mount -o bind /dev $THUMBDRIVECOPY/dev
mount -t proc none $THUMBDRIVECOPY/proc
mount -t tmpfs none $THUMBDRIVECOPY/tmp
mount -t tmpfs none $THUMBDRIVECOPY/var/tmp

umountstuff.sh: This unmounts the directories mounted by mountstuff.sh

#!/bin/bash
. env.sh
umount $THUMBDRIVECOPY/var/tmp
umount $THUMBDRIVECOPY/tmp
umount $THUMBDRIVECOPY/proc
umount $THUMBDRIVECOPY/dev
umount $THUMBDRIVECOPY/usr/portage

rsync_from_thumbdrive.sh: This copies the data from your thumbdrive into your working copy. This should be run before mountstuff.sh

#!/bin/bash
. env.sh
rsync -aH $THUMBDRIVE/ $THUMBDRIVECOPY

rsync_to_thumbdrive.sh: This copies the changes in your working copy back to your thumbdrive. This should be run after umountstuff.sh

#!/bin/bash
. env.sh
rsync -aH $THUMBDRIVECOPY/ $THUMBDRIVE

editthumbdrive.sh: This runs each of the setup scripts in proper order and chroots into the working copy, to allow you to emerge and modify the system at will, then cleans up after itself.

#!/bin/bash

set -e #So that we don't clobber anything after an error.

. rsync_from_thumbdrive.sh
. mountstuff.sh
chroot $THUMBDRIVECOPY /bin/bash
. umountstuff.sh
. rsync_to_thumbdrive.sh

Using the scripts to modify the system

Most of the time, you should just have to run editthumbdrive.sh. However, the others may come in handy at times. For example, if you clobber your thumbdrive, an easy way to recover is to format properly, then run rsync_to_thumbdrive.sh.

Saving space on the system

USE flags

The following USE flags may prove useful in saving space on your thumbdrive:

minimal
-gtk
-qt
-X
-kde
-gnome

Compressing GCC when not in use

There is no guarantee that this will not break your thumbdrive system. Back up if you know what's good for you. Modify your /etc/make.conf, and add the line PKGDIR=/root/packages. Make the directory /root/packages Put the following scripts in your thumbdrive's /root:

compressgcc.sh:

#!/bin/bash
set -e #Don't delete gcc if quickpkg fails
quickpkg gcc
emerge unmerge gcc

expandgcc.sh:

#!/bin/bash
set -e
emerge /root/packages/sys-devel/gcc-*
rm /root/packages/All/gcc-*

Before closing your chroot session, run compressgcc.sh. This will compress gcc into a package. The next time you mount your thumbdrive for editing, chroot and run expandgcc.sh, which will restore gcc to the system.

See also

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

Last modified: Thu, 02 Oct 2008 10:53:00 +0000 Hits: 8,710