I'd like to store
/var on a separate partition from
/. What is the correct way to set this up?
First prepare a new partition (e.g. with
Say the partition is /dev/sda5
Mount the new partition:
mkdir /var2 mount /dev/sda5 /var2
Sync your current var:
rsync -a /var/ /var2
Add the entry to /etc/fstab
/dev/sda5 /var ext4 defaults 2 2
If you happen to need to go back you your old /var just comment out the entry in fstab.
Configuring a new /var partition on a virtual server
When I took over a new virtual server that had been provisioned by my employer’s hosting company, there wasn’t enough free space in the root filesystem. Luckily, they had used Logical Volume Manager (LVM) to sub-divide the virtual disk and there was sufficient free space available to create new volumes. I created extra logical volumes for
home which had been regular directories in the root filesystem. Since the virtual server provider didn’t provide a KVM-like interface by which I could access the server in single-user mode, I used a very similar method to the one outlined by Aleksander (this answer includes extra details for recovering disk space in addition to LVM-specific commands).
Create a new /var filesystem with LVM
Create a logical volume for the new
var filesystem, mount it (using a temporary directory) and copy files from the current
/var to the new filesystem:
# Create a new 60GB logical volume in the `VolGroup00` group called `var`. sudo lvcreate -L 60GB -n var VolGroup00 # Create an ext4 filesystem on this new `var` volume. sudo mkfs.ext4 /dev/VolGroup00/var # Mount this filesystem at a temporary mount-point. sudo mkdir /var.new sudo mount /dev/VolGroup00/var /var.new
Since running processes will have files in
/var kept open and in use, the directory tree can’t simply be moved to the new filesystem. Recursively (
-r) copy files from the current
/var partition to the new filesystem while preserving file attributes and extended attributes (
-a, --archive option). A cautious user might first create an LVM snapshot of the current volume before copying but that’s too much off-topic detail for this question.
sudo cp -ra /var/ /var.new/
Alternatively, the files can be copied with
rsync, with 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 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.
/dev/mapper/VolGroup00-var /var ext4 defaults 0 0
Since changing into single-user mode is not possible, reboot the computer to use this new volume as
Remove temporary mount-point
After the machine has restarted, the new filesystem will be mounted on
/var so the temporary mount-point can be safely removed:
sudo rmdir /var.new
Recover disk space from the root filesystem
/var files will still be taking up space on the root partition but they are not easily accessible while another filesystem is mounted at
/var (they are “masked” by the new filesystem using the
/var directory as its mount-point). Use a temporary mount-point to mount the root filesystem so that contents of the original
/var directory are available by an alternative path.
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/