Relocate /var

From Leo's Notes
Last edited on 15 June 2022, at 20:09.

Relocating the contents of /var to another storage device or partition might be useful when you are running low on storage on the root partition.

To relocate /var from the root partition to another partition or storage device:

  1. Add the new storage device or create a new partition or LVM volume.
  2. Format the new storage
  3. Mount the new storage to /mnt/var
  4. Ensure no programs have /var opened. Run lsof /var to verify. Alternatively, boot into single user mode before proceeding.
  5. Copy the /var contents to the new location. Run cp -apx /var/* /mnt/var
  6. Move the original /var to /var.original and recreate the /var directory
  7. Edit /etc/fstab so that /var is mounted appropriately on startup

In summary, on a system using LVM, I ran the following:

## Expand the LVM partition if the disk was grown.
# /usr/bin/growpart /dev/sda 2

## Expand the LVM volume group
# /usr/sbin/pvresize -y -q /dev/sda2

## Create a new LVM volume for /var
# lvcreate -n var -L 20G rl

## Create a filesystem
# mkfs.xfs /dev/mapper/rl-var

## Copy data to the new partition
# mkdir /mnt/var
# mount /dev/mapper/rl-var /mnt/var
# cp -apx /var/* /mnt/var

## Ensure that fstab has an entry for this volume.
# cat <<EOF >> /etc/fstab
/dev/mapper/rl-var      /var                    xfs    defaults        0 0
EOF

## Cleanup
# mv /var /var.original
# mkdir /var
# mount -a