Wednesday, July 25, 2007

Moving /var and /tmp in Ubuntu

Note added 16 April 2008: Since I wrote the below, I subsequently discovered some problems - only once in a while, but they were hard to diagnose - with certain programs that were resolved when /var was left as Ubuntu expected it to be. To be honest, I think it was an exercise of dubious value anyway to the home user.


Why would you want to move /var and /tmp to a partition of their own? Well, since frequently changed files cause disk fragmentation, putting the frequently changed directories of /var and /tmp on their own partition limits the effects of the fragmentation.

But when you first installed ubuntu, unless you'd read up on ideal partitioning structures beforehand, or you're freakishly prescient, you didn't know that did you? So now you're faced with the problem of how to move live directories around without breaking your system.

Now, messing around with partitions on a live system is a bit of a tightrope act, but it is possible to do it without hosing your setup if you're methodical, careful, or extraordinarily lucky. However, there is a chance you could lose everything, so make sure you've backed up anything irreplacable before you do anything else. (Obviously, if you're like me, you'll ignore this advice and hope for the best; however, you have been warned, however futile the gesture.)

Anyway, there's an excellent guide on how to move partitions here at Daniel Robbins' Funtoo site. Before you charge headlong into doing it, though, there's an important difference between Daniel's instructions and their use in Ubuntu. If you simply replace Daniel's sections on copying /var and setting up symlinks with those below, you'll be fine (hopefully :0).

Copying /var in Ubuntu

This section is slightly, but importantly, different for Ubuntu users.

Unfortunately, Ubuntu -- at least up to and including Feisty Fawn -- requires certain subdirectories of /var, particularly /var/run and (I think) /var/lock to remain under the root filesystem, and therefore /var cannot be moved wholesale to the new partition. Grateful thanks are due to Chris Seibenmann for uncovering this.

Moving /var completely will result in networking wierdness: although the loopback interface will come up after boot, it will be without its 127.0.0.1 IP address. Symptoms include very slow DNS lookups, certain applications taking an age to load (e.g. the Gnome Network configuration app.), and (of course) inability to use anything that references localhost or 127.0.0.1.

However, there are /var subdirectories that are still worth moving, for all the reasons given previously. Having created the /mnt/rwstorage/var directory, I would advise you to rename it something like /mnt/rwstorage/varstore, just to prevent confusion since we will leave /var on the root filesystem. The rest of this section assumes that name is used.

First, create the directories you are going to move. The list below has worked well for me, but I don't run anything taxing like web-servers accessible to the public, so you may need to experiment a little based on your own setup.

Code Listing: Creating the new subdirectories
# cd /mnt/rwstorage/varstore
# mkdir cache lib log spool www
You then need to make sure they have the right permissions.

Code Listing: Setting permissions
# chmod 777 cache lib log spool www
Now we copy the contents of the relevant /var directories to their new home.

Code Listing: Copying the data
# cd /var/cache 
# cp -ax * /mnt/rwstorage/varstore/cache
# cd /var/cache
# cp -ax * /mnt/rwstorage/varstore/lib
# cd /var/lib
# cp -ax * /mnt/rwstorage/varstore/log
# cd /var/log
# cp -ax * /mnt/rwstorage/varstore/spool
# cd /var/www
# cp -ax * /mnt/rwstorage/varstore/www

Back up and create symlinks

After the above, you'll have an exact copy of the relevant directories in /mnt/rwstorage/varstore. So now we need to get Linux to use these /mnt/rwstorage/varstore directories and /mnt/rwstorage/tmp instead of their defaults in the root directory.

This is easily done using symbolic links -- we'll create the new symbolic links, /tmp, /var/cache, /var/lib, /var/log, /var/spool, and /var/www, which point to the correct directories in /mnt/rwstorage/varstore. First, let's back up the original directories:

Code Listing: Backing up the directories
# cd /var
# mv cache cache.old && mv lib lib.old && mv log log.old &&
mv spool spool.old && mv www www.old

# cd /
# mv tmp tmp.old
The last line is probably not necessary, since it's very likely that you don'thave anything important in /tmp, but we're playing it safe. Now,let's create the symlinks:

Code Listing: Creating the symlinks
# cd /var
# ln -s /mnt/rwstorage/varstore/cache cache && ln -s /mnt/rwstorage/varstore/lib lib &&
ln -s /mnt/rwstorage/varstore/log log && ln -s /mnt/rwstorage/varstore/spool spool &&
ln -s /mnt/rwstorage/varstore/www www

# cd /
# ln -s /mnt/rwstorage/tmp /tmp
Well, that's the end of any specific instructions for Ubuntu, so back to the main guide to complete the move.