How to Move /var to Another Directory on a Full Partition

When the var partition full problem occurs, it can disrupt system operations and applications due to lack of space for logging, caching, and other dynamic data handling tasks that occur in the /var directory. This article provides a comprehensive guide on how to relocate the /var directory to another partition to mitigate issues with space constraints. This procedure is crucial for maintaining the integrity and performance of your systems.

In this tutorial you will learn:

  • How to identify and prepare a new disk space for the /var partition.
  • Steps to move the /var directory to a new location securely.
Relocating a Full /var Directory in Linux
Relocating a Full /var Directory in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System A Linux system with a new, formatted disk partition available.
Software Standard Linux command-line tools, rsync, mkfs, and blkid.
Other Root access is required for the operations.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Steps to Move and Relocate /var

  1. Identify a New Disk Space: Start by identifying a new disk space to be used for the new /var partition.
    $ sudo fdisk -l

    Assuming you have already partitioned the new disk, in this example, we will use /dev/sdX1.

    Identify a New Disk Space
    Identify a New Disk Space
  2. Create a New Filesystem: Format the new partition with an ext4 filesystem.
    $ sudo mkfs.ext4 /dev/sdX1

    This command sets up /dev/sdX1 to be ready to store the relocated /var data.

    Create a New Filesystem
    Create a New Filesystem
  3. Mount the New Partition: Create a mount point and mount the new partition.
    $ sudo mkdir /mnt/newvar
    $ sudo mount /dev/sdX1 /mnt/newvar

    This step prepares the system to transfer data to the new partition.

    Mount the New Partition
    Mount the New Partition
  4. Copy Existing /var Directory Data: Use rsync to copy all existing data from /var to the new partition.
    $ sudo rsync -avxHAX --progress /var/* /mnt/newvar/

    This step ensures that all current data in /var is copied accurately to /mnt/newvar.

  5. Obtain UUID of the New Partition: Retrieve the UUID of the new partition which will be used in the fstab file.
    $ sudo blkid /dev/sdX1

    Note the UUID as it will be required in the next step.

    Obtain UUID of the New /var/ Partition
    Obtain UUID of the New /var/ Partition
  6. Update fstab: Update the fstab file to mount the new partition as /var on boot.
    $ echo 'UUID="UUID HERE" /var ext4 defaults 0 2' | sudo tee -a /etc/fstab

    Alternatively, use the device file:

    echo '/dev/sdX1 /var ext4 defaults 0 2' | sudo tee -a /etc/fstab

    Update fstab
    Update fstab


  7. Unmount Temporary Mount and Reboot:
    $ sudo umount /mnt/newvar
    $ sudo reboot

    These commands finalize the migration by unmounting the temporary mount point and rebooting the system.

  8. Confirm New /var Directory Mounted: Verify that the new /var directory is mounted correctly.
    $ df -h /var/

    This command will show the new disk space allocation for /var, confirming the successful relocation.

    Confirm New /var Directory Mounted
    Confirm New /var Directory Mounted

Conclusion

By following these steps, you can effectively move and relocate the /var directory to a new partition, alleviating issues related to a full var partition and ensuring continued system stability and performance. Remember to back up important data before attempting this procedure to avoid any data loss.



Comments and Discussions
Linux Forum