By default when you install any Linux system, the /var directory is created automatically under the root partition(‘/’). In some situations, you might want to separate out the /var directory on a different mount point or partition altogether. Especially when you want to manage it independently and have a large amount of data to be stored under /var.
I have seen cases where users dump a lot of data under /var directory causing the root filesystem to become full and hampering many important functionalities of the system. When the /var is on a separate mount point, this issue may never arise at all. This post explains step by step procedure to move /var out of the root filesystem on a separate mount point.
1. View the available space in the existing VGs and disks. In case of space is not available on the existent volume group, add a new disk or new partition. You may use the below commands to view the available space and disks present on the system.
# vgdisplay # fdisk -l
2. Initiatilize a new disk or a partition ona disk to be used by LVM to create new mount point. In our example we are using partition on sdc disk.
# pvcreate /dev/sdc1
3. Create a new volume group using this partition:
# vgcreate var_vg /dev/sdc1
4. Verify the free space available in the newly created volume group var_vg:
# vgdisplay var_vg
5. Create a new logical volume (var_lv) on this volume group. In my case I have 20GB free space in the VG. You may adjust the size as per your VG free space availability.
# lvcreate -L 20G -n var_lv var_vg
6. Create the filesystem for /var.
# mkfs.ext4 /dev/var_vg/var_lv
7. Backup the /var/ directory contents to a backup directory.
# mkdir /var_bkp # rsync -avz /var/ /var_bkp
8. Mount the newly create /var filesystem:
# mount /dev/var_vg/var_lv /var/
At this point, you will not find any data present in /var mount point or directory.
9. Copy all the contents from backup directory to the newly mounted /var.
# rsync -avz /var_bkp/ /var/
Making Changes Persistent
Lets make the above changes to persist across reboots. For this we need to have a filesystem entry in the /etc/fstab file.
1. First, find the UUID for the var_lv logical volume with the below command:
# blkid
2. Make and entry as shown below using the UUID from the above command.
# cat /etc/fstab UUID=[UUID-for-var_lv] /var ext4 defaults 0 0
replace the [UUID-for-var_lv] with the actual UUID from blkid command we just fired above.
3. You can umount the /var now and try mounting it with “mount -a” command to verify if the entry we just made in /etc/fstab is correct.
# umount /var # mount -a ### (or mount /var)
4. Also make sure to set the permissions of new /var/tmp to 1777 if not already set. This is required to set sticky bit on the /var mount point.
# chmod 1777 /var/tmp