Of Symlinks, Home Folders and Multiple Drives

I recently found myself with two drives, an SSD and an HDD to devote to a single Linux install. This led to much deliberation regarding drive partitioning schemes. The simple solution would have been to put a /home/ partition on the HDD. However, I didn’t want my config files, scripts, documents, games, etc. on the slower HDD. I wanted to relegate the HDD to storing files that didn’t necessarily need the speed. I decided my pictures, videos, music and VMs could all live on the HDD. I partitioned the HDD, created folders with the relevant names, and put my files on the drive.

I had a problem though. When I opened the Pictures folder in my home directory, it was empty. Even though my pictures, etc. were on a separate drive from my home folder, I still wanted my pictures, etc. to show up there in Nautilus. Read on to learn how you too can link folders in your home directory to another location.

What is a symlink? Symlink is short for symbolic link, and it is a link from one place to another. If you need a metaphor, think wormholes as represented in a Sci-Fi show. You go in a wormhole in one part of the universe and come out light years away. Symlinks are like that, but not quite as cool. Essentially, you click on a file (or folder) in one place and run a file (or open a folder) from another place.

The basic syntax is as follows:

ln -s <target> <link_name>

Prior to creating symlinks from your home directory to directories on a second drive, you need to make sure that you delete the relevant folders in your home directory (after moving any files in them that you want to keep, of course). Do not try to create a symlink from a folder with files in it to another folder. It won’t work. For example, I created symlinks for the Pictures, Videos, Music and VM folders in my home directory. Prior to doing so, I deleted each of those folders. After deleting, the appropriate folders, you can create the symlinks. The following are examples based on what I did for myself.

ln -s /media/data/Pictures ~/Pictures
ln -s /media/data/Videos ~/Videos
ln -s /media/data/Music ~/Music
ln -s /media/data/virtMachines ~/virtMachines

That’s it. Now when you open your file manager, you can click on Pictures on the left sidebar, or on the folder in your home directory and see all your pictures. They just aren’t actually stored on that drive in that folder. They are in another folder on another drive!

Limitations

You may run into some limitations with this setup. Some utilities may not work as you would assume. For example, if you use rsync to backup your home folder, it will not follow symlinks by default. This means that it will backup the Pictures symlink itself, but it won’t back up the actual pictures in the linked directory. If you want to backup your entire “home” directory (actual home plus linked files stored on the second drive) with one call to rsync, you can use the –copy-links (-L) and –keep-dirs (-K) options in your rsync command. When trying to restore from the backup, this may mess with your symlink setup though, so it may be best to just backup each directory separately. To save time, you can create a quick bash script with each of the necessary rsync calls in it. That way you can run a single script instead of entering multiple rsync commands.

The other issue I have had is a somewhat special case that may not be a worry for most. If the second drive does not mount when you log in, it not only messes up the symlinks, it messes up the sidebar in Nautilus. By “messes up the sidebar,” I mean that if the symlink for your Pictures folder breaks, because the second drive doesn’t mount, the Pictures shortcut in the left sidebar of Nautilus disappears. Fixing the symlink does not make it automatically come back either. You will have to edit the config file found at ~/.config/user-dirs.dirs. The necessary changes are quick to make and fairly self-explanatory when you open the file, but it’s still a pain. I have had to do this a few times for my Pictures, Videos, and Music folders.

Before you start to rethink this symlink idea because of what I said in the prior paragraph, let me remind you that I noted that the issue is the result of a somewhat special case. I encrypted my home folder (on SSD) at the time of install then used dm-crypt to encrypt the HDD. As I didn’t want to have to type in a password to decrypt the HDD each time the computer booted, I used PAM_mount to automatically decrypt the drive at login. Due to a bug w/ PAM_mount and systemd, the drive does not unmount properly if I log my user out. This is not an issue that you will likely face, but I thought you should be aware of the aforementioned config file just in case you run into problems of your own.

If you have any questions about symlinks, or any tips for me regarding auto-mounting encrypted drives, please leave them in the comments below!

comments powered by Disqus