November 21, 2013

How to Bulletproof Linux for Mad Experimentation

Everyone knows that keeping regular backups of our data is the No. 1 best insurance against mishaps. The No. 2 best insurance is smart partitioning on your Linux PC that puts your data on a different partition from the root filesystem. Having a single separate data partition is especially useful for distro-hoppers, and for multi-booting multiple distros; all your files are in one place, and protected from mad installation frenzies. And why not distro-hop and multi-boot random distros? Unlike certain inexplicably popular expensive fragile, low self-esteem proprietary operating systems it's easy and fun. No hoops to jump, no blurry eleventy-eight digit registration numbers, no mother-may-I, no phoning your activities home to the mother ship: just download and start playing.

Just to keep it simple let's start with a clean new empty hard disk. Thanks to SATA and USB adding new hard drives is dead-easy, which I know is totally obvious, but we should regularly take time out to be thankful for cool things like SATA and USB. Because adding new hard disks in the olden days was not easy, and we made do with megabytes. That's right, not giga- and terabytes. Oh, the hardships.

But I digress. So here we are with our new hard disk all ready to be populated with Linuxes and reams of data files. (Why not reams of data? We still dial our phones.) The first thing to do is to write a new partition table to your hard disk with GPT, the GUID partition table. This is the new replacement for the creaking and inadequate old MS-DOS partition table. So how do you install a new GUID partition table? Gparted provides a pleasant graphical interface, and command-line commandos might enjoy GPT fdisk. Use a nice bootable rescue distro to format your new drive, like SystemRescue, or use the partitioning tool in the installer of whatever Linux you are installing.

You may also elect to stick with the musty old MS-DOS partition table if you prefer; the point is to use a partitioning scheme that puts your data files off in their own little separate world.

Partitioning Scheme

You could do it this simply: root, data, and swap (figure 1).

 Simple partitioning scheme to separate root filesystem and data.

Using labels helps you know what's on each partition. This particular partitioning scheme is simple: just map your home directory to the data partition. Your home directory could even be on a separate hard disk. How do you do this? Do it with the installer's partitioning tool when you install a new distro. Or do it post-installation in /etc/fstab, which thankfully is the same as it's always been and has not been "improved" to the point that only a kernel hacker understands how to use it. Like this example:

# /home on sdb4
UUID=89bc6f52-fa07-45a9-b443-25bb65279d6a  /home  ext4  defaults

Now you can muck with the root filesystem all you want and it won't touch /home. This has one flaw, and that is dotfiles are stored in the same place as your data files. This has the potential to create a configuration mess when you have even slightly different versions of the same desktop environment, whether it's on a multi-boot setup or installing a different distro with the same DE. Another potential problem to look out for is your mail store-- some mail clients default to putting your messages in a dotfile. I recommend creating a normal, not-hidden directory for your mail store.

Clever Partitioning Scheme

So here is my clever tweak to avoid dotfile hassles, and that is to keep /home in the root filesystem. Then create a symlink from your homedir to your data partition, which contains only your data files and no dotfiles. This creates an extra level in your filepaths, which is a bit of an inconvenience, but then you get the best of all worlds: your personal dotfiles in /home, and the root filesystem cleanly separated from your data files.

But, you say, I want the same configs in multiple distros! No worries, just copy your dotfiles to your different homedirs. Though the reason for not sharing them in the first place is to avoid mis-configurations and conflicts, so don't say I didn't warn you.

More Clever Partitioning

You can share /boot/tmp and swap on a multiboot system (figure 2). Just remember, when you install a new Linux, to map it to these partitions. It's nice to have /tmp on its own partition in case some process goes nuts and fills it. /var is also a good candidate to have its own partition, but you can't share it-- each Linux installation must have its own /var.

 Sharing /boot and /tmp partitions

I give my /boot partition 500MB to a gigabyte on a multi-boot system. The Linux kernel and run around 3-5MB, but initrd on some of my systems hits 30MB and up. I'm not interested in finicky housekeeping and keeping old /boot files cleaned up, so it hits 150MB for a single distro easily.

But What If

What if you already have a separate /home partition, complete with dotfiles, and you want to overwrite your root partition with a different Linux, or install some new distros to multi-boot, and still share your homedir? Easy peasey, though a bit of work: first move all your dotfiles into a new directory in your homedir. Rename your original /home directory to something that doesn't conflict with the root filesystem like /data or /myfiles or whatever. Then install your new Linux or Linuxes and keep /home in the root directory, rather than putting it on a separate partition. Then symlink/data inside your new homedir, like this:

$ ln -s  /data /home/carla/data

You'll want to create an entry in /etc/fstab to make sure your /data partition is mounted at boot, like this example:

# /data on sdc3
UUID=3f84881f-507a-4676-8431-7771a6bc6d39 /data ext4 defaults

When you install a new Linux it automatically installs a set of default dotfiles, and also when you install new applications. If there is anything you need from your original set of dotfiles just copy them to wherever you need them.

What if everything is in your root filesystem and you don't have a separate /home partition? Again all you do is create new partitions, symlinks, and appropriate entries in /etc/fstab.

Be sure to consult du Know How Big Your Linux Files Are? and Linux Tips: The Misunderstood df Command for cool ways to manage filesystems and see what's going on in them, and GPT, the GUID partition table to learn more about GPT and UUIDs.