If you ever have an emergency and need a rescue disk to recover your Linux install, or maybe you just want to brag to your friends there’s some good LiveCD/USB’s out there and many distro’s now make LiveUSB install images, but it is also possible to create your own customizable LiveUSB. Hey, if you’re willing to put the time in, you can have a portable Linux in your pocket.
There’s alot of articles about creating your own custom CD/LiveUSB but many of them seemed dramatic involving messing with things like syslinux… Plus many of these create a fixed image, meaning that once it’s on your USB it can’t be changed. But having a customizable Linux on a USB flashdrive isn’t that difficult – just install Linux to the USB drive.
Partition the USB Drive
The first thing you’ll need is at least a 2GB flash drive. Anything less and you better plan a real basic install. First thing you might like to do is partition the flash drive. This isn’t necessary but I was pretty sure I wouldn’t need the 4GB for what I needed so I partitioned the flash drive to have a 1GB FAT32 partition first (so that Vista can see any files I put on it) then I partitioned the remaining 3GB as ext4 with parted.
Install via VirtualBox
No need to burn an ISO and reboot, use VirtualBox and do it from your desktop. You can follow my Testdrive a LiveCD with VirtualBox post to getting VirtualBox setup. I personally used Arch Linux for this install because it’s easy to configure.
Make sure your user is part of the VirtualBox group to enable usb recognition:
sudo gpasswd -a <username> vboxusers
Boot the LiveCD/USB iso/img in VirtualBox then in Devices > USB devices select your flash drive. Now the installer will recognize your flash drive. Proceed to install the distro on the flash drive. If you partitioned beforehand you can skip partitioning and go to setting Filesystem Mountpoints. When you reach GRUB setup be sure to install GRUB on the flash drive itself, for me it was
/dev/sdb. Be sure NOT to install GRUB to a partition, it should be at the beginning of the drive.
Because your BIOS is likely setup to recognize your hard drive before your USB drive you get drive denominations like
/dev/sda for your hard disk and
/dev/sdb for your flash drive on regular bootup. If booting from a flash drive, many BIOS’s have you enter a key (mine is F10) to get to a Boot Menu. So when you select your flash drive in your BIOS Boot Menu your flash drive now becomes
/dev/sda, hard drive
/dev/sdb. In grub terminology this is
hd1. Most BIOS’s are like this (though there a few exceptions). To know for sure you won’t be able to detect this until you try and boot your flash drive (more below).
Close VirtualBox and open your GRUB menu list and change to the first recognized drive:
sudo mount /dev/sdb2 /mnt/usb sudo vim /mnt/usb/boot/grub/menu.lst
or however you edit your system files. Then change:
# (0) Arch Linux title Arch Linux root (hd1,1) kernel /boot/vmlinuz26 root=/dev/disk/by-uuid/34393cdf-9f39-431e-88c8-ea89a2518c83 ro initrd /boot/kernel26.img
# (0) Arch Linux title Arch Linux root (hd0,1) kernel /boot/vmlinuz26 root=/dev/disk/by-uuid/34393cdf-9f39-431e-88c8-ea89a2518c83 ro initrd /boot/kernel26.img
The (hd0,1) value denotes the partition number, again starting with 0. So this denotation tells GRUB the root filesystem is on the first drive, second partition.
Arch-specific Details (Mostly)
If you already did the configuration for your hard disk, you should be able to copy most the configuation files over to the flash drive (rc.conf, mirrorlist, modprobe.conf, local.conf…) and then install xorg, xfce4… by chrooting in. This is my chroot script:
#!/bin/bash # chrootmount - change root to current directory cp /etc/resolv.conf etc/resolv.conf mount -t proc none proc mount -o bind /dev dev mount -t sysfs none sys chroot . /bin/bash
source /etc/profile grep -v rootfs /proc/mounts > /etc/mtab source ~/.bashrc
This will allow you to just cd to the mounted directory and enter command to chroot to the new environment. From there you can install a desktop environment (I choose XFCE because I wanted a lightweight environment and limited disk space):
pacman -Syu pacman -S xorg xfce4 gdm <few-fonts> nvidia
And a couple other things following the Beginner’s Guide.
The kernel initramfs image will need to be rebuilt too to have usb driver support. In the chrooted environment edit
/etc/mkinitcpio.conf and add usb to HOOKS:
HOOKS="base udev autodetect pata scsi sata filesystems usb"
Then find the the kernel version name and version:
and build a new initramfs image:
mkinitcpio -g /boot/kernel26.img -k <your-kernel-name-version>
-k option needs to be specified to use the chrooted kernel and not runtime kernel that is being used by chroot.
When done, exit chroot:
exit && umount proc sys dev
Reboot and Test
Now reboot and get to the BIOS Boot Menu. As I said, all BIOS’s are different so keep an eye for a key to get to it. Once in the Boot Menu select your USB drive.
Try and boot the flash drive. If you get a GRUB 17 error or boot into hard drive OS, you’ll have to edit your
menu.lst. You can find the devices Grub sees by starting the flash drive again and in the Grub menu press e to edit. On the root line press e again and delete to:
now press tab and it will show you the availble drive and partitions. Enter the correct one, hit escape and then b to boot. That’s it, you should now have your own customizable Linux USB drive.
If you get errors loading the kernel, it may be because USB device detection may need a delay before loading root. Try to add this to the end of your kernel line in your
I was a bit surprised. I didn’t think a USB drive would be much different that a CD/DVD but actually it was alot faster. And I just discovered that I’m using a USB 1.1 flash drive. :) Not quite as quick as my hard drive but definitely not bad. This is also the first time I ran without an xorg.conf and my desktop runs great. Definitely worth a try if you ever need a rescue os to fix problems with.