The article describes my recipe for installing Linux on a USB flash drive. In this case, two partitions are created on the flash drive: one with a full Linux system, and one separate FAT32 partition for files and data. If you insert a USB flash drive into a working Windows computer, it will look as usual, but with a capacity reduced to the size of a partition with FAT32. If the computer is running Linux, then you will see both sections. 
When you boot your computer from a flash drive, you will get access to the FAT partition and to any other drives on this computer. Since this is a normal Linux system, all changes to its settings will be saved. Files can be saved both in the home directory, and on the FAT-section, as well as on other drives. 
This method uses the ideas outlined Migrating a virtual machine to physical hard disk that is, a virtual machine is created, and then its image is transferred to a USB flash drive. I use Debian 6, but this method should work on most Linux distributions. It works for me, but I do not give any guarantees about its performance on your machine!
ATTENTION: Linux installed on a USB flash drive is usually used in some emergency cases, for example, if your main system fails. Intensive use of it in the future may lead to exceeding the maximum number of read / write cycles of the flash memory of the chip, which may lead to data loss. Therefore, it is necessary to periodically back up important files. 
WHAT CAN BE DONE:To extend the life of flash memory, you can use the following tricks: moving / var to RAM when the system boots; inclusion of the “noatime” option in / etc / fstab for the root file system; use of a file system optimized for flash memory devices.

QEMU / KVM or VirtualBox?

I tried QEMU / KVM and VirtualBox, so I’m giving instructions for both programs. They were used to install several variants of Debian and Mint, but this procedure should work with most Linux distributions, although I have not tested other distributions. 
When working in Linux Mint under QEMU / KVM, the responsiveness of the graphical desktop left much to be desired, and this problem was observed only in the virtual machine.

What we need

ISO image for linux live CD. 
An ISO image of the installation disk for installing the system (it can be the same disk). 
I used a virtual machine with a disk size slightly smaller than the size of a flash drive. I have a flash drive of 8 GB in size, if you have it different, set the size of the virtual machine disk in accordance with the size of the flash drive.

1a. Creating and running a virtual machine (QEMU / KVM)

Install packages for KVM:

$ apt-get install qemu-utils qemu-kvm

If you want to use QEMU, install packages for it:

$ apt-get install qemu-utils qemu

and in each command below replace “kvm” with “quemu”. 
Create a file filled with zeros “usb.img” of 7.5 GB for the virtual disk 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. Creating and running a virtual machine (VirtualBox)

Install the necessary packages:

$ apt-get install qemu-utils virtualbox-ose

Then: – run VirtualBox; 
– set up live CDs and installation CD (File> Virtual Media Manager) as virtual drives; 
– create a new virtual machine with 512 MB of RAM and a new virtual hard disk, called “usb.vdi”, slightly smaller than a flash drive 
– attach the live CD ISO image to the virtual machine drive; 
– start the virtual machine.

2. Breakdown of the virtual disk

In the virtual machine, run fdisk using the command:

$ fdisk / dev / sda -H 64 -S 32

Create two partitions (n ​​command): 
– FAT32 partition (code 0c) with a size of 1 Gb; 
– Linux partition (file system type – by default), using all remaining free space. 
To set the file system type, use the t command. 
To view the list of sections, use the p command. 
To record changes, use the w command.

3. Reboot

3a Reboot (QEMU / KVM)

If you are installing Linux from a live CD, skip this step.

Turn off the virtual machine.

Boot the virtual machine from the installation disk using the following command:

kvm -k en-gb -monitor stdio -m 512 -cdrom installcd.iso -boot d usb.img

3b. Reboot (VirtualBox)

If you are installing Linux from a live CD, skip this step.

Turn off the virtual machine. 
In the virtual machine settings, replace the live CD image with the installation CD image. 
Start the virtual machine.

4. Install Linux

Install your Linux distribution in a virtual machine. 
Select manual partitioning and set the following options: 
partition 1: mountpoint = / windows or / shared, format = yes 
partition 2: mountpoint = /, format = yes 
Ignore all warnings about the absence of a swap partition. 
After the installation process is complete, turn off the virtual machine.

5. Convert disk image (only for VirtualBox)

The file containing the virtual disk image must be converted from the .vdi format used by VirtualBox into the raw format required for the physical drive. The conversion is performed using the following command:

$ qemu-img convert -f vdi -O raw ~ / .VirtualBox / HardDisks / usb.vdi usb.img

6. Copying an image to a USB flash drive

Connect the USB flash drive to the computer. If the flash drive is automatically mounted, unmount it. 
Check the output of the dmesg command to find information about your device:

$ dmesg | tail -n 20

in my case, this is / dev / sdd

Copy the contents of the virtual disk to a USB flash drive:

$ dd if = usb.img of = / dev / sdd

This will take some time (in my case, 8 GB were copied within 30 minutes). After the installation is complete, view the partition structure of the flash drive using fdisk:

$ 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 with W95 FAT32 (LBA)
/ dev / sdd2 * 1026 7168 6290432 83 Linux

Now try booting your computer from a flash drive.