Setup automatic mounting of USB disks

In the thread Plug it in? - #5 by ilikelinux @barryascott wrote:

So long as you have not setup automatic mounting of USB disks

How do I get an external disk mounted automatically? I see this problem every evening when I try to make a backup of my data. I plug in the USB connector and forget to mount the disc, go downstairs to watch the news, come back up and nothing happened cause the disk was not mounted yet.
Can somebody please tel me how to do this?
Thank you.

Which desktop? Gnome?

No, I use KDE. Sorry, should have added that.

It looks like with the KDE Partition Manager you can achieve that.

Here is explained how to do that for a normal KDE installation:

I believe on kinoite it should be possible too. It will try to edit the etc/fstab file. You might just use an other path for that, as kinoite is an atomic desktop. For the paths you might have to check the manual.

There seems to be a misunderstanding here. The link in your post shows how to permanently mount a disk which is connected at boot already.
What I want is to mount it on the fly, each time as soon as I connect the cable from the disk to the laptop. I don’t like the disk to be permanently connected (and mounted) because it is a backup disk. Should something (through software) happen to the laptop, it should not happen on the backup disk as well. I only want to connect the disk when it is time to make a new backup.
The story in the link you sent I figured out myself already some time ago, also thinking it was the way to go, but then I found out it only works when the disk is mounted at boot.
Isn’t it possible to mount a new disk on the fly?

I can use a simple line like this: sudo mount /dev/sda1 /mnt/USB but that requires sudo and my backup script doesn’t use that. It’s a simple script in which rsync copies new and changed files to the backup disk with normal user rights.

I already wrote here there is no difference between Kinoite and the regular KDE edition, but I did find some while searching the net:
Some programs (like autofs and udev which should be able to do what I want) are not included in Kinoite. I tried it in a toolbox but then I got an error saying they have no permission to do mounting operations.
Regarding the location of the fstab file, there is no difference with the regulars.

Why not adding a call to sudo in your script ?

Eventually write first a sudorule allowing anybody or your acount to do this mount

Also IIRC declaring it in /etc/fstab with user and noauto may work (without using sudo thus).

2 Likes

As an example I do for some apps create a file in

#substitute username, foo & bar.
/etc/sudoers.d/username
username  ALL=(ALL) NOPASSWD: /usr/bin/foo, /usr/bin/bar, 

This will work.

@jandemus you want the auto mount in /run/media/username/volume, right?

Have a look in the partition manager if your external hard-drive still has the boot, esp flag. This also prevents the disk to auto mount.

Hello Francis,
thank you for your reply, it really helped me, although I’m still a bit baffled about why it helped me.

Maybe it works, I don’t know, but I can’t use it. The backup script starts at 19.25 with a few notify send instructions for me, so I have time to prepare everything for the script to do its job. Then at 1930, when the news on TV starts I am downstairs and the script starts the backup. Having to type my password then is not possible as you can imagine.
I did test it out and yes, it works.

This is better because no sudo and therefore no password, plus it works.
The line which I use in /etc/fstab is this one:
UUID=xxx-xxx-xxxx-xxxxxxxx /mnt/USB/ ext4 user,noauto,x-systemd.automount 0 2
The disk gets mounted with the first operation on the disk so I guess I will add an ls command before the rsync command just to make sure the disk is mounted when rsync starts.
Thank you very much Francis, you made my day.

Well, I use /mnt/USB as mountpoint, I think /media is one of the read-only directories. Does it make any difference which mountpoint I use?
I looked in the partition-manager but the USB disk has no flags at all.

What I mentioned is what my Gnome desktop makes automatically (/run/media).
And it is read write. You have to check that that it is available for your user at least (rwx).

Edit:
sudo systemctl status udisks2 # this service creates the automount.
Automatically Mount USB External Drives: A Comprehensive Guide
udisks - ArchWiki

Maybe you’re aware of this @jandemus, but just in case… As I’m the only user on this machine, immensely lazy, and the only other person here is my wife, I tweak /etc/sudoers to never prompt for my password.

## Allows people in group wheel to run all commands
# %wheel    ALL=(ALL)   ALL

## Same thing without a password
%wheel  ALL=(ALL)   NOPASSWD: ALL

I still have to run some commands with sudo, but I don’t have to type my password 80 times per day.

Better do not tuch the sudoers file. We on Fedora use the drop in directory:

# create for each user the own rules file and backup them with your .dot files.
/etc/sudoers.d/
1 Like

On the first operation on the mountpoint yes, because you added x-systemd.automount,
but then the noauto option is not needed and may perhaps prevent systemd to start the
automount on boot.

As a non root user, and with x-systemd.automount and user, you still can umount the
disk, but not umount the (initial) autofs mount (must be superuser to unmount).

You may prefer to use:
UUID=xxx-xxx-xxxx-xxxxxxxx /mnt/USB ext4 user,noauto 0 2
then mount /mnt/USB at the beginning of your script, and umount /mnt/USB at its end.

This shows that Kinoite provides systemd.automount (but not autofs as you said)

I think that it is possible to write a systemd.path unit to start your script as soon
as you plug in your disk, by watching /dev/disk/by-uuid/YOUR_DISK_UUID (assuming that
udevd manages /dev/disk/by-uuid/ on Kinoite), but one may find that undesirable.

The short version of my script is this:

notify-send     # connect the USB disk

/usr/bin/rsync  # create a backup on my NAS, which is always connected

mount /mnt/USB  # mount the disk, which works 

/usr/bin/rsync  # create a backup on the USB disk, which is now mounted

umount /mnt/USB # unmount the USB disk which also works

notify-send     # disconnect the USB disk

In between I have more notify-send lines and also some lines with sleep instructions to give me time to do some things which need to be done before the backup can start.

Something I did not write until now is the script is automatically started by a systemd timer and service file. I have set it at 19.25, 5 minutes before the news starts and I want to be away from the PC because the USB disk, when mounted makes quite some noise.

The USB line in /etc/fstab looks now like this:
UUID=xxx-xxx-xxxx-xxxxxxxx /mnt/USB/ ext4 user,noauto 0 2
The disk is not automatically mounted when connected, but with the instruction in my script it does.

I tested it just now and it works great. Thank you very much for all your time and help.

2 Likes

Hello Uwe,
I see you deleted your post, but the KDE notification system still had a copy of it so I was able to read it.
You are a point & click type of person and I must say, normally I am too. Sometimes however I want to dig into the matter a little deeper and create a simple script to get something done. I want and need to learn much more about this, and I will learn more about this, to be able to create real scripts.
I’ve looked at your suggestion and it just works. Where were you 2 days ago? I could have used your help so much. :grinning_face:

To explain this a bit more to others reading this thread, Uwe wrote about the automatic mounting system in KDE, normally used for camera’s, but also for external disks.
It can be found in the System-settings in Disks & Camera's. When you click on that, a second column opens with 3 items, Uwe wrote to chose the 2nd one: Device Auto-Mount.
When I do that I see in the most right hand column 3 items where I first used number 1: Attached devices.
I ticked the box on that line which says On attach.
At the bottom of the screen you must tick the box which says:
Automatically mount removable media that have never been mounted before. Then click the Apply button.
Now, very important for KDE to be able to recognize your device, attach your device. Open it and for example create a new file, open that one, close it again and delete it as if nothing has happened. KDE now knows the device because you did some file handling on it.
Remove your device from the computer, so you can attach it again later. First do a reboot.

Now it’s time to get to item 2 in the right hand column: Attached devices.
Open also Dolphin, the file manager and look in the left most column.
Attach your device and in Dolphin a new item will appear in the section Removable devices.
At the same time in System-settings, below Attached Devices you will see that your external disk is attached.
At first it is not mounted yet, but that takes just a few seconds and then it is mounted: automagically. This can be seen in Dolphin where the orange symbol which indicates an unmounted device disappeared.
When I did it I could enter the disk, create a new file of which the properties showed me I, as normal user, was the owner with read and write permissions.

So once KDE knows your device, it remembers it and mounts it automatically when it is connected to your computer.
To be sure all you need is this, I removed the mount line for the disk in /etc/fstab before the reboot. And yes, it works.

One thing I could not find in the KDE help system (which is probably outdated because the texts don’t always match the app) is how to unmount the disk. One way is in Dolphin by clicking the very little up arrow just right of the name of the mounted disk, after which you can remove the disk.

Thank you very much Uwe for this completely other approach to automatically mount a disk when it is connected.

So sorry, I deleted it because I didn’t want it to look stupid, and the top line was missing the abbreviation for code. And there was no preview, so I didn’t want to be admonished (like I had been several times, in other discussions) for ā€œclutteringā€ a reply; a reply that already was marked as ā€˜solved’ anyway.

So sad, that ā€˜we’ never could agree on tags for stuff like that, and use different items. And no visible preview here, neither.

Thanks for adding the complete description; so that someone else looking for the same problem may find it.

[Having worked hard for 25+ years to evangelize *nix for use on the end users’ desktops, I still shed tears over ā€˜our’ failure. The potential was there. 25 years ago ā€˜we’ were clearly ahead of redmond, but blew it. Your question is a good showcase for this failure.
As former sysadmin, my hair would be standing on end, if anybody could do this on my server. For the end user, it ought to have been possible, via the GUI, across all distros, 20 years ago.]

Click the ā€œAā€ button on in the top left corner to get a preview panel (or hit Ctrl-M)… like so:

1 Like

This rich text editor is a pure mess. I do not know why discourse is forcing us to use that by default?

1 Like