How to move /var to another existing partition?

Solution 1:

Go into single user mode, and make sure any process writing to /var is stopped. (Check with lsof | grep /var)

  • mkdir -p /home/var
  • rsync -va /var /home/var
  • mv /var /var.old # you can remove /var.old when you are done to reclaim the space
  • mkdir -p /var
  • mount -o bind /home/var /var
  • update your /etc/fstab to make the bind-mount permanent.

/etc/fstab

 /home/var /var        none    bind

Solution 2:

You can also use:

 mkdir /home/var
 <move contents of /var to /home/var -- however you want; EX: mv /var/* /home/var>
 mv /var /var.old
 ln -s /home/var /var

This seems a lot easier than messing around with the fstab and mount stuff.


Solution 3:

Move /var without changing into single-user mode

When I took over a new virtual server that had been provisioned for me by my employer’s hosting company, I created extra logical volumes for var and home which had been regular directories in the root partition. Since the virtual server provider didn’t provide a KVM-like interface by which I could access the server in single-user mode, the above answers were not applicable to my setup. I hope this answer is useful for others in a similar situation (I've kept the LVM details but these can be skipped as it’s not particularly relevant whether the new filesystem is created on a logical volume or a disk partition).

Create and use a new /var filesystem with LVM

Create the filesystem for the new var volume, mount it (using a temporary directory) and copy files from the current /var to the new filesystem. When copying files with rsync, use its -a, --archive option to preserve time-stamps, ownership, modes, etc. and its -X, --xattrs option to preserve the extended attributes such as the security labels used by AppArmor and SELinux.

sudo lvcreate -L 60GB -n var VolGroup00
sudo mkfs.ext4 /dev/VolGroup00/var
sudo mkdir /var.new
sudo mount /dev/VolGroup00/var /var.new
sudo rsync -raX /var/ /var.new/

Update the filesystem table

Configure the new filesystem to be used as a new mount-point for /var by adding the following line to /etc/fstab. Note that 0 is used as the pass number (last field) so that the filesystem won’t be automatically checked (fsck) after a certain number of reboots (I’ve no access to log in to the server in single-user mode).

/dev/mapper/VolGroup00-var    /var    ext4  defaults  0 0

Since I can’t change into single-user mode, reboot the computer to use this new volume as /var.

Recover disk space from the root filesystem

After the machine has restarted, carry out the following steps to clean up the temporary directory and remove the old /var files from the root filesystem:

  1. Remove the temporary mount point:

    sudo rmdir /var.new
    
  2. Create a new mount point to create an alternative path to the files on the old /var directory on the root filesystem (it’s currently “masked” by the new /var filesystem mounted on the directory):

    sudo mkdir /old-root
    sudo mount /dev/mapper/VolGroup00-root /old-root/
    sudo rm -rf /old-root/var/*
    sudo umount /old-root/
    sudo rmdir /old-root/