How to copy a Hard Drive to USB Flash Drive and Make it Bootable

Ok, that's a really long title, but I couldn't make it any shorter without losing the point!

In my case I already have the system installed to a hard drive in my target PC. I want to move all the files to a usb flash drive and make it bootable. I already know that the target system (DecTop) support USB boot. My workstation is a different linux PC.

  1. insert the destination USB flash drive in my workstation
  2. delete the existing vfat partition and create a single linux partition using fdisk
  3. create a filesystem and synchronize it:
    bash# mkfs.ext3 /dev/sdb1
    bash# sync ; sync
  4. remove the usb flash drive from the workstation, put it in the target PC
  5. mount the usb drive, move the udev filesystem out of the way, and copy the local filesystem:
    bash# cd /
    bash# mkdir /mnt/sda1
    bash# mount /dev/sda1 /mnt/sda1
    bash# mkdir udev
    bash# mount --move /dev /udev
    bash# cp -ax / /mnt/sda1
  6. That copy command might take awhile. When it is done, get rid of the temporary directory /udev
    bash# mount --move /udev /dev
    bash# rm -fr /udev

Now to make the usb drive bootable. It should still be mounted at /mnt/sda1. First, in file /mnt/sda1/boot/grub/device.map set hd(0) to /dev/sda and in /mnt/sda1/boot/grub/menu.lst set the kernel boot options correctly for each boot configuration, eg:

title           Debian GNU/Linux, kernel 2.6.18-6-486
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.18-6-486 root=/dev/sda1 ro vga=792 
initrd          /boot/initrd.img-2.6.18-6-486
savedefault

Finally, install grub on the usb flash drive:

bash# grub-install --root-director=/mnt/sda1 /dev/sda

All done! Now you can reboot into the flash drive.