O'Reilly logo

Ubuntu Hacks by Bill Childers, Kyle Rankin, Jonathan Oxer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #10. Install Ubuntu on an External Drive

You can, in fact, install, boot, and run Ubuntu completely from a FireWire, USB, or other external drive, but it does require some special steps. This hack walks you through the process from start to finish.

In the process of working on this book, we realized one disadvantage to using a laptop as a primary computer: it is much more difficult to swap out hard drives for test systems. We wanted to set up an Ubuntu system so that we could test various hacks on a vanilla install, but we didn't necessarily want to repartition and install on the main laptop hard drive if we didn't have to. The solution was to install and run Ubuntu from an external USB drive we had; that way, the regular system stayed intact but we could boot Ubuntu whenever we wanted.

Unfortunately, this sort of install does not automatically work without some tweaking due to a few different reasons:

  • By default, the initrd (initial ram disk) file that Ubuntu uses does not contain all of the drivers you need to boot from a removable drive. Your BIOS will find the drive fine (provided it supports booting from removable drives), but once the kernel loads, Linux won't be able to see or mount the drive to continue the boot process.

  • Even if the initrd has the appropriate drivers, it takes a few seconds for the kernel to load these modules and detect your removable drive before it tries to use it. During this time, the system will likely try to boot and will not be able to find the removable drive because it hasn't finished configuring.

  • The Ubuntu installer is very handy in that it tries to detect other OSes you might have installed on the system and will provide GRUB menu entries for each OS. Unfortunately, this means that it will set up any OS you have on the internal hard drive as being on the first BIOS drive, with the removable drive being second (or third or fourth if you have other drives on the system). When the BIOS boots from the removable drive, it will configure it as the first drive on the system, which will confuse GRUB.

In this hack, we discuss how to fix each of these problems so that you can install and boot Ubuntu from a removable drive.

Set Up the Partitions

The first step is to start the Ubuntu install process as you would with any other system, so read through "Install Ubuntu" [Hack #5] until you get to the section about partitioning a drive. When Ubuntu gets to the disk-partitioning tool, note that by default it will probably pick any internal IDE or SCSI drive currently in the system. If your system uses an IDE drive, you can choose your external drive by selecting the SCSI drive the system has detected. The line will probably refer to a disk called "SCSI (0,0,0) (sda)." If you already have a SCSI hard drive in the system, it will be a bit more difficult to locate the USB drive, but chances are it will be the last SCSI drive on the system.

Warning

Be absolutely sure you pick the correct drive in this phase, because Ubuntu will format and repartition the drive you choose and wipe out any data that might have been there. If you are uncertain which drive is the appropriate one, boot from the Ubuntu Live CD and confirm which device names (sda, sdb, etc.) it assigns the different drives on your system.

Install GRUB

Once you choose the correct drive to format, continue with the Ubuntu installation process until it gets to the GRUB bootloader stage. Here, you will be asked whether you want to load GRUB to the MBR of the internal hard drive. You don't want to do this because it will overwrite any bootloader you are currently using on the system. Instead, say no and then specify /dev/sda (or whatever Linux device was assigned to your removable drive) in the next screen that appears in order to install GRUB directly on the removable drive.

Use chroot

Next, complete the Ubuntu installation up until when it prompts you to select Continue to reboot the system. Before you reboot, you will need to make some tweaks to the system. The Ubuntu installer actually provides a basic console you can use to run a few limited commands on the system. Hit Alt-F2 to switch to this console, and then hit Enter to activate it.

Now you need to prepare the removable disk so that you can chroot into it and change some files. The removable drive will actually be mounted under the /target directory, and the first step is to mount the special /proc filesystem within that drive:

# mount -t proc /target/proc

Now you can use the chroot tool to turn the /target directory into the effective / partition on the system. This way, you can run commands as though you had booted off of that drive:

# chroot /target

Tweak initrd

Once inside the chroot environment, the first thing to do is to add the modules Linux uses to make your removable drive accessible to the initrd. The /etc/mkinitramfs/modules file lets you configure extra modules to add to an initrd, so use your preferred console text editor to edit this file. If you don't have a preferred console text editor, just use vim (if you are unfamiliar with vim, check out "Edit Configuration Files" [Hack #74] for a vim primer):

# vim /etc/mkinitramfs/modules

Once this file is opened, move to the very bottom of the file and add the following lines and then save and close the file:

ehci-hcd
usb-storage
scsi_mod
sd_mod

Tip

If your removable drive is connected via IEEE1394, also add the following lines:

ieee1394ohci1394sbp2

and for any other devices, simply add the modules they need to this file.

With the correct modules configured, the next step is to set up a initrd so that it will wait a number of seconds before continuing to boot. That way, Linux has time to detect and configure the removable drive. Open /etc/mkinitramfs/initramfs.conf in a text editor:

# vim /etc/mkinitramfs/initramfs.conf

Now add a new configuration option to the very top of the file so that Linux will wait for a few seconds before finishing the boot process:

WAIT=10

In our experience, 10 seconds is enough time for Linux to load a USB drive, but feel free to change this to a longer or shorter value if you need to. Save your changes and close the file.

Now you are ready to re-create an initrd file that incorporates the new settings using the mkinitramfs tool:

# mkinitramfs -o /boot/initrd.img-
               
                  2.6.15-16-386
                
               /lib/modules/
               
                  2.6.15-16-386
               

Change the initrd.img and /lib/modules paths to match the kernel version included in your Ubuntu install CD.

Update GRUB

The final step is to change a few settings in the GRUB configuration file. The Ubuntu installer sets up the external device as (hd1) (or second BIOS drive), but you need to change that to (hd0) because the drive will be the first BIOS drive in the system when the BIOS boots from it. Open the GRUB menu.lst file with a text editor:

# vim /boot/grub/menu.lst

and find the lines that refer to the GRUB root device. They will look something like the following:

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

Change the last line to refer to hd0 instead:

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

Next, find the section in the file that refers to different Ubuntu kernels. It should look something like the following:

title       Ubuntu, kernel 2.6.15-16-386
root        (hd1,0)
kernel      /boot/vmlinuz-2.6.15-16-386 root=/dev/sda1 ro quiet splash
initrd      /boot/initrd.img-2.6.15-16-386
boot

title       Ubuntu, kernel 2.6.15-16-386 (recovery mode)
root        (hd1,0)
kernel      /boot/vmlinuz-2.6.15-16-386 root=/dev/sda1 ro single
initrd      /boot/initrd.img-2.6.15-16-386
boot

title       Ubuntu, memtest86+
root        (hd1,0)
kernel      /boot/memtest86+.bin
boot

Now change all of the references to hd1 to hd0:

title       Ubuntu, kernel 2.6.15-16-386
root        (hd0,0)
kernel      /boot/vmlinuz-2.6.15-16-386 root=/dev/sda1 ro quiet splash
initrd      /boot/initrd.img-2.6.15-16-386
boot

title       Ubuntu, kernel 2.6.15-16-386 (recovery mode)
root        (hd0,0)
kernel      /boot/vmlinuz-2.6.15-16-386 root=/dev/sda1 ro single
initrd      /boot/initrd.img-2.6.15-16-386
boot

title       Ubuntu, memtest86+
root        (hd0,0)
kernel      /boot/memtest86+.bin
boot

If Ubuntu has detected and configured other OSes and you want to be able to choose them as well, simply repeat the same changes to the root config option for each OS—only change hd0 to hd1. Then save your changes and close the file.

Now you can leave the chroot environment, so type exit in the console and then hit Alt-F1 to return to the main Ubuntu install console. Now you can select Continue to reboot the machine into your new install.

Tip

Keep in mind that most computers won't boot from a removable drive by default if a CD-ROM or other hard drive is present. Some BIOSes let you configure which device to boot from via a special key at boot time (such as F12). In other BIOSes, you may have to hit Esc, F2, or Del to enter the BIOS and configure the boot device order.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required