Install linux on a USB disk

From Linuxintro
Revision as of 11:47, 20 December 2008 by imported>ThorstenStaerk (→‎TroubleShooting)

Modern computers can boot from USB storage like disks and sticks. You may even try to boot from a digital camera if you like. The SUSE 10.1 distribution allows you to chose seamlessly if you want to install to a USB disk or a "normal" harddisk (SCSI or IDE). This article is for owners of less modern distributions. It shows how to install Linux on a USB disk so that you can boot from it.

Boot your favorite Linux

This has been tested with SUSE Linux 10.0 and Ubuntu 7.04.

Connect the USB disk

Attach the USB disk and type dmesg. At the end of the messages, you will find something like "Attached SCSI device to /dev/sda". Then you know your USB disk's device name is /dev/sda. In this tutorial, we assume the device name is /dev/sda.

Partition the USB disk

fdisk /dev/sda

In this tutorial, we will assume you create a partition /dev/sda1 for your root ( / ) directory.

Format the USB disk

mkfs -t ext3 /dev/sda1

Clone your system

Copy over all files from / to your USB disk. See also Cloning.

mkdir -p /mnt/sda1
mount /dev/sda1 /mnt/sda1
cd /
tar -c $(ls -1 | grep -Ev "proc|sys|tmp|media|mnt") | (cd /mnt/sda1; tar -xv)

You should know that you will need the /dev folder to create the ramdisk later. You can also use tar -cl (local filesystems only), but that might exclude your /boot partition.

Make your USB disk bootable

Here are two possibilities for making the disk bootable, it is not unusual that one fails. Try the uppermost first.

Possibility (1)

$ cp -pr /dev/sda* /mnt/sda1/dev
$ cd /mnt/sda1
$ chroot .
$ mount /proc
$ grub-install /dev/sda
$ exit

Possibility (2)

$ grub-install --recheck --root-directory=/mnt/sda1 /dev/sda
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/sda1/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/hda
(hd1)   /dev/hdb
(hd2)   /dev/sda

TroubleShooting

The file /boot/grub/stage1 not read correctly

  • Symptome is that grub-install fails with the message
The file /boot/grub/stage1 not read correctly
  • Reason can be that your partition type is incorrect, e.g. like this:
$ fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       19456   156280288+  c  W95 FAT32 (LBA)

  • Solution can be to correct your partition type to 83 (Linux) using fdisk.

Edit your initial ramdisk

The initial ramdisk (initrd) must contain the modules needed to load usb storage. We will use mkinitrd to rebuild your initial ramdisk. First, change to your system on the USB disk:

cd /mnt/sda1
chroot .
mount /proc

Now have a look at the drivers that are loaded by the initial ramdisk.

SUSE

For SUSE, your drivers file is /etc/sysconfig/kernel. You will find a line starting with INITRD_MODULES= in /etc/sysconfig/kernel. It should look like this:

INITRD_MODULES="jbd reiserfs ext3 usbcore usb_storage scsi_mod sd_mod uhci_hcd ehci-hcd sbp2 sr_mod"

it can also contain more modules. Verify this line exists, then, create the initial ramdisk:

mkinitrd

Ubuntu

For Ubuntu, your drivers file is /etc/modules. It should look at least like this (it can also contain more modules):

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

fuse
lp
usb_storage
uhci_hcd
usbcore
usbhid
sr_mod
sd_mod
scsi_mod

After you verified the file exists, create your initrd with the command:

mkinitramfs -o /boot/initrd.img

Use a unique device name

If you now boot from your USB disk, it might be that it appears under another name than /dev/sda. To circumvent this problem, we will now use the disk's unique ID instead of /dev/sda. Try

hwinfo --partition

You will get a line similar to the following:

  Device Files: /dev/sda1, /dev/disk/by-id/usb-CompanyXXXXXX,_Inc._USB_Mass_Stor
age_Device_100-part1, /dev/disk/by-path/pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:
0-part1, /dev/disk/by-uuid/84ff6790-9b69-4401-9ba2-43d044af1d30, /dev/disk/by-la
bel/Whatever

Now you know your partition is not only accessible via /dev/sda1, but also via /dev/disk/by-uuid/84ff6790-9b69-4401-9ba2-43d044af1d30.

/etc/fstab

You can now edit the fstab on your USB disk:

kwrite /mnt/sda1/etc/fstab

there is one line for the mountpoint /, maybe:

/dev/sda1               /               ext2            defaults        1  1

In our example, you would change this line to

/dev/disk/by-uuid/84ff6790-9b69-4401-9ba2-43d044af1d30  /   ext2   defaults   1  1

/boot/grub/menu.lst

In /mnt/sda1/boot/grub/menu.lst, you will find some lines telling the kernel where to find the root (/) file system, maybe:

kernel          /boot/vmlinuz-2.6.20-16-generic root=/dev/sda1 ro quiet splash

not to boot from the wrong partition, you can also replace them:

kernel          /boot/vmlinuz-2.6.20-16-generic root=UUID=84ff6790-9b69-4401-9ba2-43d044af1d30 ro quiet splash

Try booting from your disk

If there are problems, continue with TroubleShooting.

TroubleShooting

GRUB - nothing else

  • When booting from USB, I get a line saying
GRUB

nothing else.

This can be a problem with your device map. Keep in mind that, when booting from USB, your USB disk corresponds to (hd0), the first harddisk. grub-install will tell you where its device map is - try correcting it.

Error 17 after boot menu

  • After choosing an item from the grub menu, I get a line saying
Error 17: Cannot mount selected partition
most probably, your device order has changed. For example, if you installed linux from CD and your IDE harddisk was device 0, it may now be device 1 after Booting from USB. In this case change
root (hd1,1)

to

root (hd0,1)

Please note that 1 is simply an example for a partition, it may also be any other number.

Error 17 before boot menu

  • Instead of getting the grub menu, I get a line saying nothing else but
Error 17
most probably, your device order has changed and grub searches its menu (stage) on the wrong disk. You will have to do grub-install again.

Waiting for /dev/sda to appear

  • After selecting the boot entry in the grub menu, you get some lines of messages then the boot process stops with the message
Waiting for /dev/sda to appear
your USB disk is recognized and supported by your BIOS, but your initrd's drivers do not recognize it. You will have to edit your initial ramdisk to contain the correct drivers, as discussed under initrd.

No bootable partition in table

  • After starting the computer, you get a line saying
No bootable partition in table
you do not have a partition that is marked as bootable. Use fdisk's "a" command to set the bootable flag of /dev/sda1.

How do you like this site? <html> <form action="http://www.linuxintro.org/feedback.php"> <textarea name=feedbacktext cols="30" rows="5"> </textarea> <button>submit</button> </form> </html>

What comes next

Now that you can boot from your USB disk, you should be aware that you cannot write very often to a specific sector, so you should make /tmp a ramdisk like this:

mount -t tmpfs tmpfs /tmp -o size=512M