UPDATED 2011-08-03 12:20
This is my recipe for installing Linux on a USB flash drive (a.k.a. memory stick). It creates two partitions on your flash drive: one with a full Linux system and a separate FAT32 partition for sharing files with other systems. If you plug the drive into a running computer, it looks like a normal USB drive but with the reduced capacity of the FAT32 partition (if the computer is running Linux then it will show both partitions).
When the computer boots from the flash drive, it runs Linux which can access the FAT32 partition and any other drives in the computer as well as its own partition. As it is a normal Linux system, it remembers your settings and you can store files in the home directories, in the shared FAT32 partition and on the other drives in the computer.
This recipe borrows ideas from my earlier article How to make a virtual computer real. The method used is to create a virtual machine and then transfer the virtual disc image to the USB drive. My computer runs Debian 6 but the procedure should work on most Linux distributions. This recipe works for me but I offer no guarantees!
CAUTION: the Linux system on the flash drive is only intended for incidental use because heavy use would eventually exceed the maximum number of write-erase cycles of the flash memory chip resulting in corruption of the data. Consequently, you should keep good backups of any important files.
TO DO: techniques that could be used to extend the life of the flash drive include: moving /var to ramdisc on startup, enabling the ‘noatime’ option in /etc/fstab for the root file system and using a file system type that is optimised for flash devices.
QEMU/KVM or VirtualBox?
I have tried this recipe with QEMU/KVM and VirtualBox and give instructions for both. I’ve used it to install several versions of Debian and Linux Mint. The procedure should also work with most other Linux distributions although I have not tested them.
With Linux Mint, I found that the graphical desktop interaction is very slow and ‘sticky’ when running under QEMU/KVM so you might prefer VirtualBox if this is a problem, although it is only an issue while running on the virtual machine.
Items Needed
- ISO image for a Linux live CD
- ISO image for the installation disc of the system you intend to install
(these may be one and the same disc)
I use a virtual machine with a disc size slightly smaller than the size of the USB flash drive. My flash drive is 8GB, if yours is different then adjust the size of your virtual machine’s disc accordingly.
1a. Create and start the virtual machine (QEMU/KVM)
Install needed packages for KVM
apt-get install qemu-utils qemu-kvm
If you want to use QEMU then install the following packages instead
apt-get install qemu-utils qemu
and replace each ‘kvm’ command with ‘qemu’.
Create a 7.5GB sparse file called ‘usb.img’ for the virtual disc image
dd if=/dev/zero of=usb.img bs=1M count=0 seek=7500
Create a virtual machine and boot it from the live CD
kvm -k en-gb -monitor stdio -m 512 -cdrom livecd.iso -boot d usb.img
1b. Create and start the virtual machine (VirtualBox)
Install needed packages
apt-get install qemu-utils virtualbox-ose
Then:
- Launch VirtualBox
- Register the ISO images for the live CD and the installation CD as virtual CD-ROMs (File > Virtual Media Manager)
- Create a new virtual machine with 512MB RAM and a new virtual hard disc, called ‘usb.vdi’, with a size slightly smaller than your USB flash drive
- Attach the live CD ISO image to the CD-ROM drive of the virtual machine
- Start the virtual machine
2. Partition the virtual disc
In the virtual machine, type the fdisk command
fdisk /dev/sda -H 64 -S 32
The arguments “-H 64 -S 32” align the partitions to 1MB boundaries.
Create two partitions (command n):
- partition 1 = FAT32 (type code 0c), size = 1GB
- partition 2 = Linux (the default type), uses all of the remaining space
use the t command to set the type code for partition 1
use the p command to list the partitions
use the w command to save the changes
3a. Reboot (QEMU/KVM)
If you will be installing Linux from the live CD then skip this step.
Shut down the virtual machine.
Boot the virtual machine from the installation CD using the following command
kvm -k en-gb -monitor stdio -m 512 -cdrom installcd.iso -boot d usb.img
3b. Reboot (VirtualBox)
If you will be installing Linux from the live CD then skip this step.
- Shut down the virtual machine
- In the storage settings for the virtual machine, select the installation CD ISO image instead of the live CD ISO image.
- Start the virtual machine
4. Install Linux
Install the Linux system of your choice onto the virtual machine.
Choose manual partitioning and set the options as follows:
- partition 1: mountpoint = /windows or /shared, format = yes
- partition 2: mountpoint = / , format = yes
Ignore any warnings about the lack of a swap partition.
When the installation process is finished, shut down the virtual machine.
5. Convert the image disc format (VirtualBox only)
The file containing the virtual disc image needs to be converted from the .vdi format used by VirtualBox to the ‘raw’ format required by the physical drive (the USB flash drive).
Use the following command to convert the disc image file
qemu-img convert -f vdi -O raw ~/.VirtualBox/HardDisks/usb.vdi usb.img
6. Copy to USB flash drive
Plug in the USB flash drive. If a window opens showing the contents of the flash drive, close the window. If there is a desktop icon for the flash drive, right-click the icon and choose “unmount”.
Examine the output of the dmesg command to find the device file for the flash drive
dmesg | tail -n 20
in my case it is /dev/sdd
Copy the contents of the virtual disc to the flash drive
dd if=usb.img of=/dev/sdd
This will take some time as it has to copy 8GB (in my case it took 30 minutes). When the copying has finished, the fdisk command should show the partitions on the flash drive
fdisk -l /dev/sdd
Disk /dev/sdd: 8153 MB, 8153726976 bytes
64 heads, 32 sectors/track, 7776 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000dcdb2
Device Boot Start End Blocks Id System
/dev/sdd1 1 1025 1049584 c W95 FAT32 (LBA)
/dev/sdd2 * 1026 7168 6290432 83 Linux
Now unplug the flash drive and try using it to boot a computer (no need to unmount because you did that earlier and the copying process did not require it to be mounted).
Great tutorial!!
One question, is then possible to convert the usb back to an img file and import it back to Virtualbox?
Thanks a lot and cheers
— e · 2011-08-03 08:04 · #
Certainly:
dd if=/dev/sdd of=usb.img
then, to run this on a QEMU/KVM virtual machine
kvm -monitor stdio -m 512 usb.img
If you want to run it on virtualbox then convert the image to VDI format:
qemu-img convert -f raw -O vdi usb.img usb.vdi
Paul.
— Paul Zarucki · 2011-08-03 11:07 · #
This seems to be unnecessarily complicated. How is this better than simply using UNetbootin?
— Al Nonymous · 2011-08-03 11:18 · #
With UNetbootin, it is a live cd only. No persistent storage. With the method described above, it appears it would be a normal installation once booted.
— Jeremy Runner · 2011-08-03 14:20 · #
Would it work to just install directly to usb disk and choose to install Grub to usb partition instead of mbr. Wouldn’t that accomplish the same thing but being much easier??
— Jeremy Runner · 2011-08-03 14:22 · #
Al & Jeremy – I wanted a normal installation with persistent storage and easy installation of extra packages. From my brief reading of the UNetbootin web site it doesn’t seem to do this.
Jeremy – I agree, I could simply have booted my computer from the Linux install disc with the USB drive connected and installed directly to the USB drive. I just didn’t want to reboot my computer because it is a busy workplace with several VMs running and a lot going on! Also, sometimes I have a VM already set up in a particular way and would like to transfer it to a USB drive so that I have it handy when visiting a customer, for example.
— Paul Zarucki · 2011-08-03 15:14 · #
You can install any number distros in usb drive.You can use native installer present in any live cd for a normal installation as in a hard drive.I was successful enough to install all the major distros except Chakra Linux.The only thing is to be careful in choosing partitions.Chances are there for ruining hard drive installations.
By dd command we can copy the usb installation into hard drive as a back-up.We can try installation of another distribution in the same usb drive. If you do not like the new distro,you can swap the old one into usb drive. Trying various distributions into usb drives will greatly improve your Linux knowledge.— Dr. V.Seenivasan · 2011-08-03 18:20 · #
Nice, and with those advantages. Such as no rebooting, of your production system.
However, it is of limited usage on other satellite systems. Unless those systems have plenty of RAM, such that 512MB of it, wouldn’t be missed.
— Spanky · 2011-08-03 19:46 · #
Dr. V.Seenivasan – thanks for that. I had not thought of installing multiple distros on the USB drive. I can see the value of it as a learning aid and as a way of demonstrating different distros or testing them on different computers.
— Paul Zarucki · 2011-08-04 08:00 · #
The OpenSuSe way:
http://jason.ferrer.com.ph/2011/08/opensuse-on-usb-reloaded_16.html
— Jetchisel · 2011-11-24 22:57 · #