Dec 18, 2012

Portable Fedora 17 on a USB key

Intro, AKA the "tl;dr" part you can skip 

 

A few weeks ago I have become very upset about the laptops provided by my employer (Dell on Win7) and my main client (Dell on WinXP). They all came with all sorts of checking, locking and logging software that resulted in crappy performances and low autonomy. Also, I don't carry both PC with me every day so I had to keep them synchronized or use a removable device. But the tools themselves were attached to Windows.
So I decided to install a personal portable Linux ("do or do not; there is no try" via @MasterYoda). With USB keys now being so ridiculously cheap, 16GB from a trustworthy vendor for €12, this is the way to go.
My first attempts were with Live USB creator tools that take an iso image from a live distribution, copy it to a USB key and add a persistent storage area. That was terrible. This persistent layer is far from solid and couldn't be trusted, whatever the distribution.
Then I went for normal install of Fedora 17 on USB, even if it lacks all the optimizations of a live distribution. And it works great. It's light, fast, responsive, battery-saving. I used it more than 2 hours straight, dev + music, and still had 50% left. I love my portable Fedora so much that it hurts when I think how I was doing without it "back in the days".

Now, the interesting part 

 

Here's my set up :
  • Fedora 17 x64, installed from Live CD
  • 16 GB SanDisk USB key
  • 500 MB partition for /boot
  • 2 logical volumes :
    • 5 GB for /  (go for 7GB if you need LibreOffice)
    • the rest LUKS encrypted for /home
  • no swap
I will not explain how to install, there are enough tutorials around. Just be careful which target device you select.

 

tmpfs

All the changes described below are focused on limiting the write accesses to the file system, to give your USB key or SSD a bigger life span, by moving the temporary files to tmpfs. tmpfs allows you to mount a filesystem in memory. All the items you usually don't need to be persistent (after a stop/start) like locks, some logs, cache, ... don't need to be written on a disk.
Some tips I found myself, some were gathered from various web sites, in particular :
  • http://wiki.debian.org/DebianAcerOne
  • http://forums.mozillazine.org/viewtopic.php?f=8&t=2115257

 

Moving /var/cache to tmpfs

Not everything in /var/cache can be stored in memory. Some cache needs to be written to disk if you use it frequently like yum and man :

move /var/cache/yum to /var/yum
edit /etc/yum.conf

move /var/cache/man to /var/man
edit /etc/man_db.conf

 

Editing /etc/fstab

Now we can edit /etc/fstab to use tmpfs for a few temporary directories. The "noatime" for ext4 saves a lot of i/o by not saving the last access time to a file.

/dev/mapper/your-lv-name /  ext4    defaults        1 1
UUID=your-boot-fs-uuid /boot  ext4    defaults,noatime        1 2
/dev/mapper/luks-your-luks-fs-id /home     ext4    defaults,noatime        1 2
tmpfs    /tmp    tmpfs    defaults,noatime    0 0
tmpfs    /var/lock    tmpfs    defaults,noatime    0 0
tmpfs    /var/tmp    tmpfs    defaults,noatime    0 0
tmpfs    /var/spool    tmpfs    defaults,noatime    0 0
tmpfs    /var/cache    tmpfs    defaults,noatime    0 0


/var/run can not be stored in tmpfs because systemd uses it very early in the boot process, before the file system is mounted into memory.

Restart.
If the system is not booting, then there is an error in your fstab and systemd was not able to mount one or more file systems. Enter the maintenance shell and correct the problem.
Some commands that can help you out : blkid, vgdisplay -v your-vg-name, mount, df, ...

 

Optimizing Firefox

You can configure Firefox to keep its cache in memory instead of writing it on the disk.
Open a new tab, then go to "about:config". Swear that you know what you're doing and that you'll be careful. (promise !)
Search for "browser.cache.disk.enable" and set it to "false".

Update (20130513) : It has been 6 months now and I have not had any problem yet. The upgrade process to Fedora 18 went like a breeze.