Too much free space on your USB key ? Install an Arch Linux on it !
Why waste the space when you could have a home in your pocket ?
I never got the use of “big” USB keys ; and by big I mean 32GB and upwards. There are really few files that need that kind of space, and when I actually have to transfer a lot of them at once I’ll use an USB hard drive disk instead, or something like that. And if the files are small, there are a number of ways to move them from a place to another : SMB, SSH, FTP, a simple mail attachment, …
Don’t get me wrong : the idea of carrying an empty container with me is interesting, ’cause it could prove to be useful at times, but I still feel like I’m wasting an opportunity. I mean, I’m basically walking around with an empty mayonnaise jar in my pocket just because, some day, I might stumble upon a few interesting-looking pebbles that i’d wish to keep a copy of — not that it gets along well with my analogy, but you get the point.
There is, though, something that I’d like to carry with me all the time, and that’s my computer. Not really my “computer”, but a system that’s mine because it has 3 things : a login/password I know, an operating system I’m familiar with, and a few softwares I like.
I know, I could buy a laptop, but they are pricey, fragile, and not really that easy to move around : that’s just one more suitcase to lug around. Plus, I usually go to places where they already have computers, I’m not trying to find a way to keep in touch with the internet when I’m going on hikes in the wilderness, I just want to “inject” my “homebase” into someone else’s computer.
And, yes, I know I could use X11 forwarding, or VNC, or another “stream my desktop to me by the internet” solution, but… no : I’d have to keep my home computer running at all times, and I’d have to rely on a stable internet connection to carry all that data…
Another thing : what if I need to access the files of a crashed computer ? I mean, really crashed, “won’t boot, won’t rescue mode” crashed. Well, with a laptop or X11 forwarding, I can’t. I’d have to use a live-CD, maybe, and then I’ll get a default Linux that’s not really “mine” and doesn’t have all the things I need to be… comfortable. Bottom line : it’ll do the trick, but that’s a CD burned, used, then thrown away; or a USB key that you’ll have to re-format afterwards.
So… what do we want, and what can we do ?
We want a USB key that can still be used as a container for files, but can store a Linux on which we can boot too. And we can easily partition (“divide”) our USB key to make it work, but there are some prerequisites.
Our first partition will have to be, without choice, our “storage” partition that will act as a normal USB does : when plugged, it is mounted and available to store or retrieve files. This is forced on us by Windows that only checks for the first partition of USB keys and doesn’t bother to see if there are others, which is fine.
Our second partition will have to be the /boot of our Linux distribution, which will be bootable and contain our bootloader. Not a lot of space necessary here.
Our third and last partition will be our /, the main Linux partition where everything will be installed and ran from.
Note : you can partition further, like having a separate /home for example, but think ahead about what you really need, what will have to be encrypted, and if that will not constrain you too much. Having a separate 2GB /var partition could, if not used properly, have you facing a lack of free space in your / partition when there is some on your /var that you don’t really have a use for.
A USB drive is “consumed” by its writes
One thing to consider before going further is that everytime you write on a USB key it gets closer to its expiration date. You can read as many time as you want but if you write too much your key will die quite fast, and a Linux system writes constantly. Litteraly everytime it does something, it writes it somewhere. And everytime it writes something, it writes on a journal what it is going to write, and where, in case you inadvertantly reboot and the system has to remember what it was doing at the time. That is a serious issue.
Fortunately, we can do some things to extend the life of our USB key : disabling the journal on our ext4 filesystem, and mounting some folder inside the RAM of our host (via tmpfs) will be some of them. Sure, our system will be less “stable”, but if you’re careful when your plug & unplug your USB key it shouldn’t be a problem. Just keep in mind we are trying to build a system on a support that hates when you write on it… so let’s try to avoid any unnecessary writes.
First of all, partition your USB key
I personnally just purchased a 64GB USB3 key by Corsair, since my last one died, and this is how I partitionned it :
Device Boot Start End Sectors Size Id Type
/dev/sdc1 2048 96471039 96468992 46G 7 NTFS/exFAT
/dev/sdc2 * 96471040 96675839 204800 100M b W95 FAT32
/dev/sdc3 96675840 123435007 26759168 12.8G 83 Linux
My storage partition has 46GB and will be in NTFS (FAT32 can’t have files over 4GB, which is a problem for system ISOs, plus almost everything supports NTFS these days); my /boot partition has 100MB, a boot flag and will be in FAT32 for compatibility reasons (ext2 would work but isn’t really needed); and my / partition has the rest, and will be in ext4 (without a journal !). I personnally used fdisk to partition my key but you’re welcome to use any partitionning tool you’d like.
Next, we’ll have to actually create the filesystem on our three partitions. Our first in NTFS (with the “fast” option to avoid some unnecessary writes), our second in FAT32, and our last in ext4 without a journal to avoid having too much read/write on our USB key. From a Linux, this is how it goes :
# mkfs.ntfs -f /dev/sdc1
Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.
# mkfs.fat -F32 /dev/sdc2
mkfs.fat 3.0.28 (2015–05–16)
# mkfs.ext4 -O “^has_journal” /dev/sdc3
Creating filesystem with 3344896 4k blocks and 837184 inodes
Filesystem UUID: b83844e9–52b9–446e-aebd-3f5fd73b3ab0
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
Our USB key is now ready for install.
Time to install a Linux on it !
Which Linux distribution you’ll pick is up to you. There are some pretty good tutorials on how to get it done with Debian, but I’ll be choosing Arch Linux personnaly since it is pretty easy : either you already have Arch Linux installed on your computer and you need to install the arch-install-scripts package, or you don’t and just need an Arch Linux ISO to burn, copy on another USB key, or install inside a VM (via VirtualBox for example).
Note : if you are booting on a Live CD/USB, either on your machine or on a VM, follow the first 3 steps from this page (set the keyboard layout, connect to the internet & update the system clock).
Open a terminal, boot on your Live CD/USB or launch your VM, and we can begin. First, we’ll have to locate our USB key :
# ls -1 /dev/sd*
Here, we can see the only block device that has 3 partitions is /dev/sdc. We’ll have to be sure, so let’s check further :
# blkid | grep sdc
/dev/sdc1: UUID=”33675BE17E29C23A” TYPE=”ntfs” PARTUUID=”6f20736b-01"
/dev/sdc2: UUID=”17B9–08AC” TYPE=”vfat” PARTUUID=”6f20736b-02"
/dev/sdc3: UUID=”b83844e9–52b9–446e-aebd-3f5fd73b3ab0" TYPE=”ext4" PARTUUID=”6f20736b-03"
This is it : the first partition is NTFS, the second is vFAT (so, FAT32), and the third is ext4. This is our USB key. And we’ll have to recreate it’s architecture inside our own system :
# mkdir /mnt/usb
# mount /dev/sdc3 /mnt/usb/
# mkdir /mnt/usb/boot
# mount /dev/sdc2 /mnt/usb/boot
# mkdir -p /mnt/usb/mnt/storage
# mount /dev/sdc1 /mnt/usb/mnt/storage
Our system partition is mounted inside /mnt/usb, the /boot partition from our key is in /mnt/usb/boot (as it should be), and I’ve put our storage partition inside /mnt/usb/mnt/storage but you’re welcome to customize that if you wish.
# pacstrap /mnt/usb base grub efibootmgr dosfstools ntfs-3g
Wait a few minutes, and you are done ! Well, your system is installed, but there is still some config files to edit and some tweaking to do…
Configuring our new system without overdoing it
We need to configure the mounting points of our new system by UUID (unique identifier, to allow mount from any system without confusion between drives), and there’s a command that can do that for us :
# genfstab -Up /mnt/usb >> /mnt/usb/etc/fstab
Note : better check afterwards if there is a line that mounts a swap partition and remove it, or this could cause some trouble !
# nano /mnt/usb/etc/fstab
UUID=e93505db-884b-435d-90c3-b02efed5e338 none swap defaults 0 0
Then we’ll “chroot” inside our key, which means we will be running our system as if we booted on it :
# arch-chroot /mnt/usb
We are inside our USB key ! Isn’t it warm and comfy ?
Time to name our system and set the time zone :
# echo lockpick > /etc/hostname
# ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
Note : I chose to name my key “lockpick”, and my timezone is the one from Paris… feel free to chose a different name and to adapt the timezone according to your country’s.
Next we will need to set the “locales”, which means the language used and the way characters are “coded”. A simple way to do that is edit the file /etc/locale.gen, locate the language you wish to use followed by UTF-8, and uncomment that line (remove the #). Then, launch locale-gen to generate them, and configure your locale as the one by default :
# nano /etc/locale.gen
# echo LANG=fr_FR.UTF-8 > /etc/locale.conf
I personnally chose french language & keyboard with UTF-8, since I use a french AZERTY keyboard. I’ll also have to create the /etc/vconsole.conf file and add a line :
# nano /etc/vconsole.conf
Our system is almost okay, we just need to quickly configure our bootloader to be able to boot on our USB key. For compatibility reasons we’ll install it with the i386 option, and since this isn’t a classical Linux installation we’ll have to “force” it. Then we’ll generate our configuration file :
# grub-install --target=i386-pc --debug --force /dev/sdc
Installation finished. No error reported.
# grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-linux
Found initrd image: /boot/initramfs-linux.img
Found fallback initramfs image: /boot/initramfs-linux-fallback.img
And we’ll edit the “HOOK” line of our /etc/mkinitcpio.conf file to allow allow some modules to load early. Then we’ll launch the building of our hooks.
# nano /etc/mkinitcpio.conf
HOOKS=”base udev block autodetect modconf filesystems keyboard fsck”
# mkinitcpio -p linux
==> Image generation successful
Note : put “block” RIGHT AFTER “udev”, or it won’t work !
Great ! Now we can finally move on to creating you, our user.
Setting up our root password, and creating our first user
First of all, we’ll have to set the root password, which is the most important password on your system. Type passwd, wait for the prompt, and make your root password complicated but easy to remember.
Quick tip : write a small sentence with punctuation and replace some letters with numbers (“L1nux rul3s.”, “4pple p13.”, “M4n, th3s3 ex4mples 4r3 g3tt1ng r1d1culou5 !”, …).
The next important step is to create a user. Its name could be your first name, a nickname, or anything you’d like. Since I’m not very creative, I’ll just use my first name :
# useradd -m -G users serge
Then we’ll set up the password for that user; once again, choose something a bit complicated but easy for you to remember. Type this command, then wait for the prompt :
# passwd serge
Right after creating your user, we can edit our /etc/fstab file, which contains our mounting points and what rights should be given to whom. We’ll give full rights to your user on your storage partition, and edit the way it is handled (let’s use that NTFS-3G we installed earlier) :
# nano /etc/fstab
UUID=33675BE17E29C23A /mnt/storage ntfs-3g uid=serge,gid=users 0 0
Installing our lightweight graphical interface, Xfce4
To be reaaaaally comfortable, we’ll need a graphical interface : I personnally recommend Xfce4 because it is fast, lightweight, and has all the functionalities one would want.
We’ll also install a few things, like the Network Manager, which will allow us to connect to the internet easily without having to modify a config file everytime we plug our USB key into a new computer ; LightDM, a “login” manager ; and Xorg, our display server :
# pacman -S xorg-server xorg-server-utils xorg-apps xfce4 xfce4-goodies networkmanager network-manager-applet xfce4-notifyd gnome-keyring lightdm lightdm-gtk-greeter
Note : pacman, Arch Linux’s package manager, will offer you some choices regarding what you would like to install or not. Just hit Enter as every default choice is what we want, and go get a cold drink or something ’cause it’s going to take a while !
Since I’m using a french AZERTY keyboard, LightDM will need me to create a file where I’ll specify which keyboard layout I’m using. If you don’t use an US QWERTY keyboard, you might have to tweak that config file too… This is mine :
# nano /etc/X11/xorg.conf.d/20-keyboard.conf
Option “XkbLayout” “fr”
Note : do not copy/paste this, or some weird invisible characters are going to sneak in your configuration file. I am sorry, but you’ll have to type this manually…
Finally we’ll “enable” LightDM and NetworkManager, which means we’ll ask them to run right after booting :
# systemctl enable lightdm
# systemctl enable NetworkManager
And we are done ! You are now the proud owner of an USB key that contains your own portable Linux. The only thing left to do is to restart your computer, boot on your USB key, and install your favourite softwares.
Enjoy, and if you have any issues or questions, let me know !