Adding new space to the / (root) partition via another partition

Q My / partition is nearly full. I need more space, and have a free partition where I could put, for example, /usr/lib. But how is that done?

A Linux allows you to mount a new filesystem anywhere under your / directory, making it quite easy to use a separate partition for part of the overall filesystem to increase the space available. The trickiest part of the process is moving the data from the original filesystem to the new one. If you do not already use a separate partition for /home, I would strongly suggest using this, because separating /home carries several advantages. Whatever you do, back up first. If you accidentally delete the wrong data, you'll be glad you made a backup.

Copying data, particularly system files, while a filesystem is in use is a risky business, so you should boot from a Live CD, such as Knoppix. This assumes that your current partition is on /dev/hda1 and you are moving home from there to /dev/hda2. Make the relevant adjustments if your system is different. The first step is to run QtParted to prepare and format the new partition. Now open a terminal and type the following:

su
mount /dev/hda1 /mnt/hda1
mount /dev/hda2 /mnt/hda2
rsync -avx /mnt/hda1/home/ /mnt/hda2/

The first line gives you root access, the next two mount your old and new partitions, and the third replicates everything from the old home directory to the new partition. You could use tar or cp to copy the files, but I find rsync to be the most reliable method of producing an exact copy, including all permissions and timestamps. Now you need to add a line to /etc/fstab so that the new partition will be used. Knoppix comes with the Nano text editor, among others, so do

nano /mnt/hda1/etc/fstab

and add a line like

/dev/hda2 /home ext3 defaults 0 0

This assumes you formatted the partition with the ext3 filesystem, the default in QtParted. If you used ReiserFS instead, change ext3 to reiserfs. If you reboot into your distro and type

df -h

in a terminal, you will see that /home (or whichever directory you decided to move) is on its own partition. "But," you are shouting at the monitor, "my / partition is still full!" That is because you copied the data to the new partition, so it is still in the old location too. This was deliberate, so you could go back if something went wrong. The data is there, but invisible because the new partition is mounted on /home, obscuring the original files. You could reboot Knoppix to remove these files, once you are sure you want to, but here is a little trick to save you having to reboot:

mkdir /mnt/tmp
mount --bind / /mnt/tmp
rm -fr /mnt/tmp/home/*

This lets you see and delete the files in the old home directory. Make sure you only delete the contents, not the directory itself. That is needed to mount to the new partition. You could do this with /usr/lib as you suggest, but /home is a better choice if not already mounted elsewhere (otherwise look at moving /usr/local). A lot depends on how much space you want to free up, so it helps to know how much space each directory is using. My favourite tool for this job is Filelight, available from www.methylblue.com/filelight and included in some distros' package repositories.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar