Thursday, May 14, 2009

Install linux to a usb stick from an existing working install

This is not a live usb install that converts from a live cd to a live usb. I prefer a complete install, with the usb stick used a hard drive. Nowadays bios has no problem booting with usb, and treat it just like a hard disk.

The main advantage is that the stick can be updated and new packages can be installed just like any other hard disk install, and you can carry it around easily. Wherever you can plug it into, you will have your personal workstation.

I have used centos5 and ubuntu 8 and 9, but other installations should be the same.

Please note that a fresh install from dvd, pxe boot, net install etc. to a usb stick is not covered as these methods are no different from standard installation. Rather, I am installing from a working installation that I have already customized.

Here are the steps:


  1. insert the usb stick with enough capacity (twice the size of your current installation is usually good), and make sure you have both grub and lilo packages installed already in your linux installation.
  2. if it not automatically mounted yet, use fdisk -l to find out the new device name. otherwise umount /media/disk (which could vary), and use fdisk to delete all the existing partitions, create a new primary partition say /dev/sdb1
  3. make a file system and mount it
  4. copy your current installation to the mounted usb partition
  5. install boot loader (grub or lilo) , and one of them should work.


Here is an example:



sudo su - # become superuser


# create a single partition. if there are existing ones, keep use d to delete them.
$ fdisk /dev/sdb
n
p


w
# format the partition
$ mke2fs -j -m1 /dev/sdb1
# find out your current rootfs label, if your distribution uses label in grub menu
# and /etc/fstab, say your rootfs is /dev/sda1
$ e2label /dev/sda1
ROOTLABEL
$ e2label /dev/sdb1 ROOTLABEL
# otherwise if it uses UUID
$ vol_id /dev/sda1
uuid
$tune2fs -U uuid /dev/sdb1
# mount it
$ mount /dev/sdb1 /media/disk
# copy your current installation. i use cpio, but dump would work too.
# i assume everything in / filesystem. if you have additional partitions
# such as /boot, you need to repeat for all these. dump/restore works too
find / -xdev cpio -pmd /mnt/disk/
# now chroot to the usb partition
mount -o bind /dev /mnt/disk/dev
chroot /mnt/disk
mount -t proc none /proc
mount -t sysfs none /sys
# try grub install
$ grub
grub> root (hd1,0)
grub> setup (hd1)
grub> quit

# you might want to edit /boot/grub/menu.lst to change the root clauses

# to point to hd0,0 instead, as when you boot into usb, it should be the first disk

# but not always, depending on many factors. if you are wrong you can always

# use the grub e command to edit the root clause on the fly and fix it once you

# have booted successfully

# if you seen errors and cannot fix them, then try lilo
# change root device to /dev/sdb
$ vi /etc/lilo.conf
$ lilo

# either lilo or grub should install fine. if not,
# you should consult grub or lilo documentation.
$ umount /proc

$ umount /sys

$ exit

$ umount /media/disk/dev

$ umount /media/disk

$ sync


now you can boot into your usb stick and enjoy a full linux installation. usually with cli, a 1-2 GB stick suffices. with desktop, a 4GB would work.

This is just my notes. I welcome your comments and can revise it and add more explanations.

No comments:

Post a Comment