There are three painful things you can meet if you want to install a system: limited installer, unbootable systems and silent bugs making garbage out of your target system.
Installing linux on usb is meeting all three. The standard usb installer is too limited so you have either to install with the installer directly on your flashdrive (something I wouldn’t recommend) or to install in an virtual machine and put it on the drive afterwards (recommend because of a second try and an easy update method).
The first thing to do is setting up a virtual machine. For this I would recommend qemu-kvm and a frontend like virtualbricks. Virtualbricks is not really finished so it has is rough edges but it is usable. Important is that the virtual disk file has raw format. This will be important in the second step. Install a system you like in my case archlinux in the virtual machine and configure it like you want it in the end. Don’t forget to configure the usb support! Use uuids: this will make the system robust. Try to use just one partition and optional one swap partition. It will make things much easier. Remember: if you want the virtual machine as template think about folders which will later contain user files.
For the next part you need root permissions:
The next step is to “mount” the raw file via losetup.
This can be done via losetup -f -P <path to raw file>.
little explaination: -f is for using the next unused loop and -P for recognizing the partitions.
If you can’t create an loop device and restart your PC (this error happened mainly because of a kernel update).
The underlaying partitions are now visible as /dev/loopXpX. They are visible but not mounted. Mount them whereever you like but think about the flashdrive: its partitions have to be mounted too and doing this via the user account is not enough.
After mounting the source partition the target partition have to be mounted. It should be big enough for the files copied in the next step. And IMPORTANT: mount the target partition as root even you copy the files as root. I don’t know why exactly but some programs like sudo or gdm break elsewise. Also be careful with the partition type: It must be supported by the system you have configured in your virtual machine and it must support acl attributes. Fat32 is not enough.
The next step is transfering the files via a acl preserving copy program. In my case rsync
Rsync is a program to sync a target with a source. It can also be used for updates.
For transfering the data with preserved acls use:
rsnyc -a –progress /source /destination
Rsync can be extended almost like you want:
–progress for being verbose (important if you want to limit data breaches by inattention)
-a for preserving the important attributes
–exclude for excluding folders from being transfered. This will be important for updates but not for the installation. Excluding canditates are /etc/fstab /boot/grub/grub.cfg (or whatever).
Warning: rsync –exclude /xy/* … is your enemy. It will transfer in the worst case all your private data on the flashdrive. Have a happy day with shredding them all.
This step is just important in case if no bootloader is installed or should be updated:
Mount via: “mount -o bind” /sys, /dev and /proc in your target system
Example: mount -o bind /sys /target/sys
Afterwards use chroot:
chroot /target bash
You will see the command line changing. This means you are in your target system. Of course with your real kernel but the programs are already the ones of your target.
Now you have to install the bootloader on the flashdrive (in my case grub):
Now exit via ‘exit and unmount the binded directories via:
The installation of the bootloader has changed an important file: grub.cfg or whatever. Now it will think it isn’t on the first disk what is wrong in our case.
Replace it with the original file
The last thing to do is to adjust the file paths. You need the uuids of the new partitions (they aren’t them used in the virtual machine)
look them up with:
ls -l /dev/disk/by-uuid/
Files where the uuids need to be replaced are:
/etc/fstab (optional: update the uuid of the swap partition)
/etc/grub/grub.cfg (or whatever your bootloader uses)
for thinkpad users: have a look on my previous post