Painlessly moving /var to a new partition

When I installed my current Linux system, I created a root partition of only 10GB (mainly because I already had another distribution installed that I wanted to keep as a backup). While this is enough for my purposes, Audacity regularly filled up the whole partition with data in /var/tmp/audacity-daniel. That is not only annoying (because Audacity will stop working), but also potentially dangerous for the stability of the whole system.
Now I could have just knocked off the other distribution and resized the root partition, but it occured to me that it would be a better way to just use its existing partition and move /var to it.

I found various different tutorials on how to do this around the web, so to make confusion complete I decided to add my own. 😀
I am currently running Ubuntu 13.10 (64-Bit), but this should work on any other recent Ubuntu/Debian release, and probably on most other current distributions, too.
I should also mention that I assume you have some basic knowledge about the file system structure on a linux system, that you know what a terminal is and you’re not afraid to use it. 😉

First of all, we need to format the partition we are going to move /var to. I did this with the graphical tool gparted. Some people use reiserfs, I simply stuck with ext4. In my case, the partition’s name is /dev/sda4. At this point, also create a new directory /var2 by entering sudo mkdir /var2. This is going to be our temporary mount point.
Before you edit your /etc/fstab file, make a backup of it! I am serious; if you do ANYTHING wrong, your whole system will be unable to boot!!! To do this, move to the directory and copy it:
cd /etc
sudo cp fstab fstab_backup

Now we can put our new partition into the fstab. Find out its UUID with the command blkid, that will return something like this:
/dev/sda1: UUID="e9325f37-4394-409f-bd9a-cb473ad0379f" TYPE="ext4"
/dev/sda2: UUID="75c07cfd-99ab-4a9f-a25c-d6db771c3565" TYPE="swap"
/dev/sda3: UUID="306b4fea-31c5-4eae-b631-c89374c28c12" TYPE="ext4"
/dev/sda4: UUID="20b0eecf-e80e-4d37-abe6-d2a2bb20078d" TYPE="ext4"
/dev/zram0: UUID="78caa6fc-d44e-4826-8e60-d0fc07e660cc" TYPE="swap"
/dev/zram1: UUID="323ad229-b8c4-44c5-8e56-006ca1fcaa9a" TYPE="swap"

Copy the value for /dev/sda4 (or whatever your partition is) without the quotation marks, and open the fstab with a text editor of your choice. I simply use nano: sudo nano /etc/fstab

Add the line for your new partition, so that it looks something like this:
UUID=20b0eecf-e80e-4d37-abe6-d2a2bb20078d /var2           ext4    defaults        0       2
(If you are curious what the other options mean, you might want to check the manpage for fstab.)

To mount everything according to new fstab, enter sudo mount -a. If no error occurs, you can also check if it has been correctly mounted with mount or gparted.

Now comes the tricky part. Enter sudo init 1, your system will drop down into single user mode, leaving you with only a root prompt. Now we need to copy everything from the „old“ /var to the new one. Enter sudo cp -a /var/* /var2/ – this may take a while. If you’re curious about the progress, add the option -v. You can check the correct execution by typing ls /var and ls /var2, which should look mostly the same.

If you want to empty the old /var, you need to do this now – you won’t be able to do it from within the system later on! But unless you urgently need space on the root partition, I wouldn’t touch anything more that necessary, just to be safe and able to go back, just in case.

Now, go back to the fstab, and just change the line for /var2 into /var. Again, you might want to check whether that worked by entering mount -a and mount.

Congratulations, we’re done! Type reboot and your system should come up like it has always been that way.

If you found any mistakes in my little tutorial, please drop me an e-mail!