Bind Linux Directories

I wanted to move the /var and /home directories from the MicroSD to a USB flash drive, this because those are the two directories that see much more movement and that could benefit from being moved away from the MicroSD.

It’s really easy to have the directories in a different partition but I didn’t want to partition a single USB thumb drive or have two thumb drives for each directory so I looked into having the directories in one partition, this can be achieved with binding.

First, create the partition on the desired thumb drive

sudo mkfs.ext4 -O ^has_journal /dev/sdxn

This creates the ext4 partition in the thumb drive without journaling which lowers the amount of reads and writes that is done to the drive.

Once you have the partition created and mounted, create the directories you want to move to the drive, in my case I created home and var so I ran the following commands

sudo mkdir /mnt/usb/var

sudo mkdir /mnt/usb/home

Because I’m creating this on a system that hasn’t been fully setup yet, I don’t have anything on home thus no need to copy over anything from there, but var does have some files that need to be copied over, so I ran this command

sudo cp -ax /var/\* /mnt/usb/var

After the copying process is completed you can check the directory to see everything that was copied over. As an extra step I created an empty file in each directory and named it notusb this with the sole purpose of knowing when the directory isn’t mounted.

You could either leave the directories as they are or completely remove them, which is something that would be advised in case you want to have that space available. I decided to leave them.

Once you’ve copied everything over you can edit fstab to have everything mount during boot time, so use your favorite file editor and add the following line

/mnt/usb/var /var none bind

I also added another line for the home but you get the idea. It will be necessary to first mount the drive before trying to mount as it wouldn’t make sense otherwise, so be sure to add the drive to the fstab as well.

You want to obtain the UUID of the partition as it makes it easier to mount that specific partition and not have another device get mounted because of some issue. To do this run the following command

sudo blkid | grep /dev/sdxn

Where sdxn is the same one used on the command above to create a partition. This should return one line with the UUID of the partition, if it doesn’t then just run the _blkid _command and look at all of the output given.

You should see a line like

/dev/sda1: UUID=”f6e6oe58–467c-ae79-9cf4–000199562b81" TYPE=”ext4" PARTUUID=”fe096750–02"

You would grab the UUID value without the quotes and place it in the fstab in the line where you establish the mount point for the drive

UUID=f6e6oe58–467c-ae79–9cf4–000199562b81 /mnt/usb ext4 defaults,noatime 0 0

At this point you should be set and would be just a matter of mounting everything manually, but if you can reboot then I would recommend going that route as you will verify that the mounting will be carried out accordingly during boot up of the device.

I check if the file notusb exists in either directory and if it doesn’t then it means that everything worked accordingly and I can continue working on setting up the system.