19

I'd like to store /var on a separate partition from /. What is the correct way to set this up?

2 Answers 2

21

First prepare a new partition (e.g. with parted and mkfs).

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

Reboot.

If you happen to need to go back you your old /var just comment out the entry in fstab.

10
  • 1
    I would add some information to recover the space on the old /var: start from a livecd, mount the partition on the installed system / and rm -rf /var/*.
    – enzotib
    May 3, 2011 at 5:06
  • 1
    Good point. Or you can mount / to some other location on the current system while it's running (but after rebooting). Then you can get at the old /var and delete it if you like May 3, 2011 at 23:32
  • 1
    I used to wonder about those too. The first 1 is something really archaic and effect-less, the second one controls the order of fsck's when it's time to scan your ext during boot. I don't pay attention to them anymore. Just put 1 1 because the syntax requires it. May 4, 2011 at 3:04
  • 2
    You should put 2 2 to let things like / get fsck scan before /var when you reboot the 30th time. Otherwise if /var fails and asks you to fix things manually in a single user shell - then you will not have any filesystem. Fun stuff. May 4, 2011 at 3:27
  • 2
    Isn't the recommendation to use UUIDs in fstab? I got the appropriate one using blkid -o list -s UUID, but I think you need to run with sudo
    – steevc
    May 27, 2012 at 11:46
4

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 var and 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 /var.

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

The old /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/
2
  • Nice explanation of the options involved. However, you use lvcreate and mkfs.ext4 without explaining what are or why are used. Maybe you can simply tell that is for making the new partition as they aren't the main topic of the answer.
    – PhoneixS
    Dec 16, 2019 at 10:49
  • Thanks for the feedback @PhoneixS I've edited the answer to make it clearer and provide additional contextual information. Dec 16, 2019 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .