Linux, mount external drives at login!
I recently solved an issue that has plagued me for a while in using Linux and external drives. I have a 1TB drive partitioned in two with both using EXT4. Every time I plug in the drive, it is mounted and works fine. When I reboot, the drive and partitions are recognized but they are not mounted. I need them mounted so that my backup will function automatically.
I have recently switched to Elementary OS Luna, but I still had the same issue in Ubuntu 13.04 and 13.10. I found many different ways to mount the drives but they all required editing the FSTAB file and I prefer to leave that alone. Plus I prefer a solution that the user can do within the confines of their user account.
So a quick synopsis of what we are in the middle of. Let’s use an external USB drive as an example. When you plug in an external drive into a linux machine, that drive will be assigned a device name, for instance sda1. But let’s say you remove the drive then plug it in to another USB port, well the system may reassign the device name to sdb1 this time and anything you had mapped to that drive would not be able to find it. So, one of the many things UDISKS does is assign a “uuid” (universally unique identifier) to each drive. So that no matter how or where that drive is added to a system it will always be mapped to the same device name.
So let’s get started!
First thing we need to do is get the device name for the drive we want to have mounted at login and we do this in the command line.
Make a note for yourself of the device names or print out a screenshot.
Next, we need to know the uuid of our drive/drives and for that another terminal command;
ls -al /dev/disk/by-uuid/
As you can see from the screenshot above, the long, blue alpha/numeric code is the uuid and at the end of each line tells you which device it belongs to. Now, we have the device name and the uuid!
Next, create a folder in your home directory called “Scripts” and create a new file called “MountDrives.sh” and add the following to the file.
#!/bin/bash /usr/bin/udisks --mount /dev/disk/by-uuid/ /usr/bin/udisks --mount /dev/disk/by-uuid/ exit 0
Now, you can have one drive or as many as you want, just as long as they are listed after “#!/bin/bash” but before “exit 0“.
Next, make the file executable. From a terminal, navigate to your Home Scripts folder and type:
chmod 774 MountDrives.sh
Now, test the script…unmount your drives if they are mounted, from a terminal, navigate to your Scripts folder and type :
You should see your drives mounted, now just need to make the script execute at login.
Depending on your distribution, the next step may differ, so I will use the Elementary OS screenshots, but you will get the idea.
Next, click “Add”, to add an item that will start/execute when you login. Give it a name, then for the “command” use the “browse” button to find you script but then add “sh” with a space at the beginning and click save. It should now mount your drives at login.