By default when you install any Linux system, the /tmp directory is created automatically under the root partition(‘/’). In some situations, you might want to separate out the /tmp 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 /tmp.
I have seen cases where users dump a lot of data under /tmp directory causing the root filesystem to become full and hampering many important functionalities of the system. When the /tmp is on a separate mount point, this issue may never arise at all. This post explains step by step procedure to move /tmp 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 tmp_vg /dev/sdc1
4. Verify the free space available in the newly created volume group tmp_vg:
# vgdisplay tmp_vg
5. Create a new logical volume (tmp_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 tmp_lv tmp_vg
6. Create the filesystem for /tmp.
# mkfs.ext4 /dev/vg_tmp/lv_tmp
7. Backup the /tmp/ directory contents to a backup directory.
# mkdir /tmp_bkp # rsync -avz /tmp/ /tmp_bkp
8. Mount the newly create /tmp filesystem:
# mount /dev/tmp_vg/tmp_lv /tmp/
At this point, you will not find any data present in /tmp mount point or directory.
9. Copy all the contents from backup directory to the newly mounted /tmp.
# rsync -avz /tmp_bkp/ /tmp/
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 tmp_lv logical volume with the below command:
2. Make and entry as shown below using the UUID from the above command.
# cat /etc/fstab UUID=[UUID-for-tmp_lv] /tmp ext4 defaults 0 0
replace the [UUID-for-tmp_lv] with actual UUID from blkid command we just fired above.
3. You can umount the /tmp now and try mounting it with “mount -a” command to verify if the entry we just made in /etc/fstab is correct.
# umount /tmp # mount -a ### (or mount /tmp)
4. Also make sure to set the permissions of new /tmp to 1777 if not already set. This is required to set sticky bit on the /tmp mount point.
# chmod 1777 /tmp