Linux: using bind mount to move a subset of root subdirectories to another partion or disk

I was in the situation dealing with a Linux box with two hard disks:

  • /dev/sda: fast hard drive (SSD), small size (~200 GB)
  • /dev/sdb: very big hard drive (HDD), large size (~4 TB)

The operating system was installed on /dev/sda, so I had /dev/sdb empty. I knew I could create a mount point (e.g. /storage) and mount it to /dev/sdb, but after reading Intelligent partitioning and the recommended Debian partitioning scheme I thought about moving:

  • /var
  • /home
  • /tmp

to the big hard drive /dev/sdb

The process described here is completely different from just putting a mount point to a partition in /etc/fstab: in our solution, we will use one disk (or one partition) to store multiple root subdirectories (/var, /home, /tmp). With the fstab “usual” method, you put one subdirectory in one disk or partition or volume.

The solution to this problem is a bind mount: the three original directories will exist in the root disk (/dev/sda) but they will be empty. Those directories will live into the second disk (/dev/sdb) and, upon mounting, a bind will be created between the root filesystem and the directories in the second disk.

The process is easy:

  1. Backup your data
  2. Boot from a live distribution (e.g. KNOPPIX)
  3. Mount your hard drives:mkdir /mnt/sd{a,b}1
    mount /dev/sda1 /mnt/sda1
    mount /dev/sdb1 /mnt/sdb1
  4. Copy the directories from sda to sdb:cp -ax /mnt/sda1/{home,tmp,var} /mnt/sdb1/
  5. Rename the old directories you just copied and create the new mount points:mv /mnt/sda1/home /mnt/sda1/home.old
    mv /mnt/sda1/tmp /mnt/sda1/tmp.old
    mv /mnt/sda1/var /mnt/sda1/var.old
    mkdir /mnt/sda1/{home,tmp,var}
  6. Update your fstab with the new locations:Mount the second hard drive:

    /dev/sdb1 /mnt/sdb1 ext4 defaults 0 2

    Then create the bind mounts for the 3 subdirectories you moved:

    /mnt/sdb1/home /home none defaults,bind 0 0
    /mnt/sdb1/tmp /tmp none defaults,bind 0 0
    /mnt/sdb1/var /var none defaults,bind 0 0

  7. umount your hard drives and reboot
  8. Check that everything under /home, /var and /tmp is working as expected. You may also want to clean up and delete /home.old, /var.old, and /tmp.old.

This process can be repeated for any subdirectory you want to move (except, obviously, /boot).

Closing notes: if you are brave enough:

  • you are not required to boot from a live distribution, just boot into single-user mode (adapt the paths of the following guide though!)
  • you can also skip booting into single user mode if you are using LVM: just create a new logical volume and copying subdirectories into it

Leave a Reply