Friday, January 9, 2009

Installing Ubuntu onto a USB key without rebooting

This tutorial assumes you are running Ubuntu 8.10 as I am. However it can work on other versions of Ubuntu and Debian. You can probably do this on any linux OS if you know your stuff.

Did you ever want to have a fully installed Ubuntu distribution on a USB stick, but don't think much of the LiveCD on a USB stick?

You can install Ubuntu onto your USB stick without rebooting your computer by booting the LiveCD on a virtual machine.

You need to be root to allow the virtual machine to directly access the usb stick:

sudo bash

You need to install KVM:

apt-get install kvm

You may need to enable virtualization support in you BIOS

To get network access on your virtual machine you need to install tunneling tools:

apt-get install uml-utilities
modprobe tun
iptables -A FORWARD -d 10.0.2.5 -j ACCEPT
iptables -A FORWARD -s 10.0.2.5 -j ACCEPT


Download an Ubuntu install image. I always choose the 32bit version so that it will work on all computers.

Figure out what device name your USB stick is. Do this by doing:

ls /dev/sd*

Then plugging in the usb stick and waiting a moment and running it again. The new device should be the USB stick.

If a volume on the usb stick automounts, unmount it.

Now you can run:

kvm --hda /dev/sdb --cdrom ubuntu-8.10-desktop-i386.iso --boot d -m 512

Where /dev/sdb is the device of your USB stick and ubuntu-8.10-desktop-i386.iso being the image for the install CD

The --boot d means to boot from the disk, the -m 512 is to decide how much ram the vm gets.

The live cd should boot up in a window and you can install onto the "hard drive" it claims to see, that will be your usb stick.

If it does not boot then using an existing copy of ubuntu try running:

sudo dd if=/usr/lib/syslinux/mbr.bin of=/dev/sdb

Once the USB disk has Ubuntu installed the system can be tested without rebooting by running:

kvm --hda /dev/sdb --boot c -m 512

There is an issue where an xorg.conf generated for another system may cause problems later on a new system. To avoid this I have added a script that will regenerate the xorg.conf on every boot up.

To do this create a file as root on your live system at: /etc/init.d/reset_xorg.sh

Place into it:

#!/bin/sh
rm -f /etc/X11/xorg.conf
dpkg-reconfigure -phigh xserver-xorg

Then you need to set it executable:

chmod +x /etc/init.d/reset_xorg.sh

then register it for start up by linking it like so:

ln -s /etc/init.d/reset_xorg.sh /etc/rc2.d/S09reset_xorg