Thoughts on Whatnot
A blog about .
Moving Dirs to Another Drive

I recently helped my brother move a number of directories to their own hard drives on a new Linux system, and he requested that I make a guide so that he can do it again if he needs to. So, here’s a guide.

Some Things to Note

There are lots of other guides out there for how to do this kind of thing, but none of them achieve quite what he wanted. In particular, he wasn’t sure if he would be putting other data on the drive later on down the line, but he didn’t want to waste disk space now by reserving some space for later use. While there are multiple ways to advice this affect, for this guide I’ll be using Btrfs subvolumes.

Another important thing to note is that any data currently on the drive in question will be wiped by the steps in this guide, so make sure you back up anything you don’t want to lose before you do anything else. Again, this wipes the whole drive, not just one partition.

Steps

The first thing you’ll need to do is figure out which drives are which. This is most easily achieved with the lsblk command. You may also find that the -f flag helps. For this guide, it will be assumed that the goal is to place the /home directory on /dev/sdb. It should be fairly straightforward to substitute your own information where necessary.

Once you’ve found the correct drive, you’ll need to format it as Btrfs. This is the step that deletes data, so make sure you’ve backed up and that you’ve identified the correct drive before running it. Once you’re ready, run the following, as root, substituting info where necessary:

$ mkfs.btrfs /dev/sdb

Once this is done, you’ll need to mount the new filesystem somewhere temporarily to fiddle with some stuff. Pick somewhere, such as /mnt/sdb, and run:

$ mount /dev/sdb /mnt/sdb
$ btrfs subvolume create /mnt/sdb/home
$ umount /mnt/sdb

The next step is replacing the existing /home directory with the new one. Before starting, make sure that nothing’s writing to the directory. For safety, it’s probably best to log out of any non-root accounts while dealing with the /home directory in particular before running the following commands:

$ mv /home /home.backup
$ mkdir /home
$ mount -o subvol=/home /dev/sdb /home
$ rsync -HAXr /home.backup/ /home/

Make sure you use the final slash on the source directory in the rsync command. It tells rsync to only copy the directory’s contents, not the directory itself. The subvol option in the mount command is the name of the subvolume given to the btrfs command in the previous set of commands.

The last step is to update /etc/fstab with the new mount. First, identify the UUID of the new Btrfs partition. This can, again, be done with lsblk -f, or by checking symlinks in /dev/disk/by-uuid for links to /dev/sdb. For this guide, I’ll just use totally-unique-id. Once you have done so, add the following line to /etc/fstab:

UUID=totally-unique-id  /home btrfs defaults,subvol=/home 0 0

And reboot. Congratulations, you’re done. Once you’ve verified that everything’s in working order and nothing’s missing, feel free to delete /home.backup. There’s some supplementary information below, but this concludes the main guide. Thanks for reading.

Adding More Directories

So, it’s now been a couple of months and you decide you want to stick another directory on the same drive that you used while following the above guide. Well, doing so is fairly simple. All you need to do is add another subvolume to the Btrfs instance on that drive and then follow the above steps starting from that point.

In this case, let’s say you’ve decided to move your /var directory over to the other drive. First, create the new subvolume by running the following:

$ mount /dev/sdb /mnt/sdb
$ btrfs subvolume create /mnt/sdb/var
$ umount /mnt/sdb

Once this is done, simply replace the /var directory with the new one, as above:

$ mv /var /var.backup
$ mkdir /var
$ mount -o subvol=/var /dev/sdb /var
$ rsync -HAXr /var.backup/ /var/

Then add the new mount to /etc/fstab:

UUID=totally-unique-id  /var  btrfs  defaults,subvol=/var  0 0

And you’re done.

Sans Explanation

Shell
$ mkfs.btrfs /dev/sdb
$ mount /dev/sdb /mnt/sdb
$ btrfs subvolume create /mnt/sdb/home
$ umount /mnt/sdb
$ mv /home /home.backup
$ mkdir /home
$ mount -o subvol=/home /dev/sdb /home
$ rsync -HAXr /home.backup/ /home/
/etc/fstab
UUID=totally-unique-id  /home btrfs defaults,subvol=/home 0 0
comments powered by Disqus