July 24, 2008, 7:39 a.m.
posted by void
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:
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 PartitionsThe 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.
Install GRUBOnce 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 chrootNext, 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 initrdOnce 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
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 GRUBThe 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 OSonly 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. |
- Comment

