Excluding a USB-stick from automounting?

Hi all, :wave:

I´ve got a question regarding how USB-sticks behave when being inserted.

I noticed the following: whenever I insert a usb-stick into my Linux Lite PC it gets automatically mounted.

But for some particular reason I just want to exclude this one: /media/rosika/74C1-30C7.
This is the path to it.
Any other usb-sticks should still be automatically mounted. :wink:

I did some research on the topic and came up with this possible solution:

I might set up a rule to prevent a specific USB stick from automatically mounting while still allowing others to mount normally.

I could achieve this by using udev rules.
With a custom udev rule, I should be able to specify exactly which devices should have automatic mounting disabled based on their unique properties, like their UUID.

  1. Identify Unique Properties of the USB Stick:
lsblk -o NAME,UUID,MOUNTPOINT
NAME   UUID                                 MOUNTPOINT
[...]
sdf    74C1-30C7                            /media/rosika/74C1-30C7
[...]

So the relevant UUID is 74C1-30C7.

  1. create the file: sudo nano /etc/udev/rules.d/99-usb-no-automount.rules

Add the following rule to the file:

SUBSYSTEM=="block", ENV{ID_FS_UUID}=="74C1-30C7", ENV{UDISKS_AUTO}="0"

Here’s what each part does:

SUBSYSTEM==“block”: Targets block devices, like USB sticks.
ENV{ID_FS_UUID}==“74C1-30C7”: Matches the UUID of my specific USB stick.
ENV{UDISKS_AUTO}=“0”: Tells the system not to auto-mount this device.

  1. sudo udevadm control --reload-rules # reload udev-rules

That should (hopefully) do the trick.

For manually mounting the stick it´s most convenient to do it with thunar, as it provides a GUI.
Otherwise:
sudo mount /dev/sdf /mnt/my-usb-stick

… provided that /dev/sdf denotes the relevant stick and that /mnt/my-usb-stick is the mount point.

What do you think :question:
Is the setting up of the udev rule the correct way of achieving the task?

Thanks a lot and many greetings from Rosika :slightly_smiling_face:

3 Likes

Hi Rosika,
I think so. udev is the tool to handle plugin devices.
The only other way is to let it mount , and put an unmount in rc.local… that might fail if the stick was not present.
Regards
Neville

4 Likes

Hi Neville, :wave:

thanks a lot for the feedback.

Good point. That made me think:

What about my udev rule in this case?
If the stick is not present the udev rule cannot be applied either. How will the system react then :question:

Cheers from Rosika :slightly_smiling_face:

2 Likes

No problem. Udev is designed to handle pluggable devices. You can put the stick in and out while the system is running ( with precautions) and udev will handle it.
Precautions are you want to unpower it before pulling it out while running… if you pull a live stick, the pins can short electrically and damage it. I usually wait till I power off the system , it is safer.

3 Likes

@nevj :

Thanks Neville.

Well, I get the scenario of putting the stick in and out while the system is running (unmounting and ejecting it first, of course).

I was rather thinking of this:

I have 3 usb-sticks attached at all times.
One of them is the “74C1-30C7” one.
The rule to it is now defined in /etc/udev/rules.d/99-usb-no-automount.rules.

Suppose I power on the PC and this stick isn´t attached: So the rule wouldn´t have a hardware conterpart to react on.
Basically it´d be like this: The rule says: “don´t automount this particular stick”.
But the stick isn´t there in the first place… :thinking: .
No confusion for the PC?

After some thinking:

Oh, I get it now. When powering on the PC none of the sticks would me mounted by default.
They don´t have an ftsab entry.

It´s just when I insert the stick into the running PC that the udev-rule applies.

Everything´s clear now. :blush:

Thanks a lot, Neville.

Many greetings from Rosika :slightly_smiling_face:

2 Likes

Yes… and if the stick is there at boot time that is ok, it applies the rule as soon as udevd is started and detects the stick.

4 Likes

Hi Neville, :wave:

O.K., but as the stick (and the other 2 sticks as well) don´t have an entry in fstab they´re not automatically mounted anyway. I have to mount them manually (I do it with thunar) whenever I need them.

Perhaps I should explain my setup for better understanding:

There´s a hub to which I have connected the following two devices:

  • the usb-stick in question
  • my Huawei 3372h web-stick (dongle) for providing my internet connection.

This hub sports an on/off-switch.
After recent events (power outage discussed in another thread) I don´t want to have the usb-stick automounted any loger as soon as I switch on the hub (for providing internet connection).

That´s the background to the topic here.
So the udev-rule would take care of that. :wink:

Thanks a lot and many greetings from Rosika :slightly_smiling_face:

4 Likes

Hi Rosika,
Firstly, congrats on the research you’ve done to disable just one external USB device.

Here’s just a hint of how we can see the changes made with your script, for anyone who’s interested.

Here’s the command:

udisksctl dump

Example:
I changed the mount of an USB stick with your script:

Thanks again for the script, Rosika :+1:

Jorge

4 Likes

Superb images Jorge, helpful for everyone trying this, pictures says a 1000 words.

Thanks for the contribution.

3 Likes

Thanks @Tech_JA ,
That is a really useful command
We should all read the man page for udiskctl

https://man.archlinux.org/man/udisksctl.1.en

It has lots other uses.
I am going to try it on my pluggable sata disks. They are under udev too.

3 Likes

Hi Jorge, :wave:

thanks a lot for your introduction to udisksctl. :heart:

I have to admit I´ve never known this command before. :blush:
You never stop learning, do you?

I´ll try it out myself. Seems intriguing.

Thanks again and many greetings from Rosika :slightly_smiling_face:

2 Likes

Hi again to all, :wave:

for anyone who might be interested:

Update:

After a while I found the time to try out the exclusion of automounting the stick as outlined in my post #1.

Actually it turned out it wouldn´t work as expected. :slightly_frowning_face:

The part ENV{UDISKS_AUTO}="0" doesn´t work as it should have. The stick got automounted anyway.

My research led me to the insight that udev can sometimes be finicky with rules, especially for auto-mounting.

So I tried a different approach:

I re-edited the file /etc/udev/rules.d/99-usb-no-automount.rules thus:

SUBSYSTEM=="block", ENV{ID_FS_UUID}=="74C1-30C7", ENV{UDISKS_IGNORE}="1"

Here, ENV{UDISKS_IGNORE}="1" should signal udisks2 to completely ignore this device for auto-mounting.

After that: sudo udevadm control --reload-rules again.

This time it worked. :smiley:

The stick didn´t get automounted, which is great.
But: it also didn´t get listed as available in thunar anymore. Neither is there a respective icon on the desktop anymore.

But gnome-disks does list it and I could mount the partition from here. :wink:

So the downside is that the device is now hidden from thunar and the desktop because UDISKS_IGNORE="1" tells udisks2 to ignore the device entirely, rather than just disabling the automounting behavior.
gnome-disks still lists it because it doesn’t rely on udisks2 settings in quite the same way.

Also:

thunar´s entry changed to 74C1-30C7 when mounting with gnome-disks.
It used to be 31 GB Volume.

Well, as long as it uses the same mountpoint as before it might not matter that much.
So, it´s a workaround - for time being at least.

Many greetings from Rosika :slightly_smiling_face:

2 Likes

Going to be a bit dumb and ask why you would not want to mount a usb, if you had put it in, as i would guess it was in to use, why not just ignore the icon.

Secondly if the system does not mount it, how do you get it to be seen so you can mount it.

I have a kindle which sometimes will not mount, then other times does. Would love to be able to see it so I can mount it, but its not on disks when i look in that.

1 Like

Hi Rosika,
Can lsblk see it?
There has to be a better way.
Did you try a umount in rc.local
Regards
Neville

2 Likes

deleted by author

Hi all, :wave:

thanks for your new replies. :heart:

@callpaul.eu :

No dumb question at all, Paul.
Your question is certainly justified.

As I pointed out in my post #7 :

It´s FAT-formatted and in case of a possible power outage I don´t want to have it mounted by default.

  • Sure, I can manually unmount it as part of my daily routine
  • I´m not sure whether I could convert the filesystem to ext3 or to ext4?

But I wanted to dig a bit deeper in order to find out if a certain USB-stick could be excluded from automounting.

In my post #12 I was successful in achieving this task, but only partly, as thunar and the desktop don´t show it anymore.
Workaround: using gnome-disks for manually mounting the stick.

Well, gnome-disks lists the stick anyway (see above).

@nevj :

If it´s mounted, then yes:

NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
[...]
sdf    8:80   1  29,1G  0 disk /media/rosika/74C1-30C7

But it doesn´t see it if unmounted.

No, I haven´t.
Not sure how to tackle this approach. I have to investigate.
Thanks for the hint, Neville.

Well, further research led me to another possible approach.
I don´t know if I´m on the right track though:

I was thinking of using systemd.

Using systemd to manage the mount behavior should let me define a specific rule for the USB stick that will only mount it manually.

  • Get the Device’s UUID:
    I already have this (74C1-30C7), which I can use to create a systemd mount unit.

  • Create a Mount Unit File:
    … with the following command:

sudo nano /etc/systemd/system/media-rosika-74C1-30C7.mount

  • Add the Following Configuration:
[Unit]
Description=Manual mount for USB stick

[Mount]
What=/dev/disk/by-uuid/74C1-30C7
Where=/media/rosika/74C1-30C7
Options=noauto

[Install]
WantedBy=multi-user.target

What specifies the UUID of my USB stick.
Where is the directory where I want it mounted (/media/rosika/74C1-30C7).
That´s the path it´s been using all the time.

  • Reload systemd to recognize the new mount unit:
    sudo systemctl daemon-reload

  • Enable the mount unit so it’s available without auto-mounting:

sudo systemctl enable media-rosika-74C1-30C7.mount

I might need a reboot, but I´m not totally sure of that.

After rebooting, attach the USB stick. It should not automount, but I can mount it manually by running:

sudo systemctl start media-rosika-74C1-30C7.mount

I should also be able to mount it via thunar if it appears as an unmounted device.

Well, that´s the theory.

But I´d like to get a second opinion before attempting to go down such an adventurous path. :wink:

Many greetings from Rosika :slightly_smiling_face:

3 Likes

Thanks for the clarification, yes i had read the thread just missed or forgot that part in 7

1 Like

Hi Rosika,
This may help

See the third answer.

Also, another trick… try putting an entry in fstab for the usb stick, and give it the ‘noauto’ flag

Regards
Neville

2 Likes

Hi again, :wave:

@callpaul.eu :

No problem at all, Paul.
Thanks for the feedback :heart:.

@nevj :

thank you very much for providing the link, Neville.

The 3rd answer you were refering to would be this, I gather:

add a rule with UDISKS_IGNORE into /etc/udev/rules.d/12-prevent-automount.rules

[…]

ACTION=="add|change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ATTRS{model}=="File-CD Gadget ", ENV{UDISKS_IGNORE}="1"

The author mentioned as a preliminary action:

At first, run udisksctl status to get the device path.
Well, I actually don´t get the device path. My output rather looks like this:

udisksctl status
MODEL                     REVISION  SERIAL               DEVICE
--------------------------------------------------------------------------
[...]
    
05e3 0727                 0250      131115882408         sdf

The actual path would look like this:

/media/rosika/74C1-30C7/ :thinking: .

As far as putting an entry in fstab is concerned, I think I remember you advising against fiddling with fstab in some earlier thread.
Perhaps I´m mistaken in my assumption. My memory isn´t as good as it it used to be. :blush:

Thanks for the help.

Many greetings from Rosika :slightly_smiling_face:

2 Likes

Well, pluggable devices are not supposed to be in fstab… but I think noauto flag might actually work.
You may be able to test it by doing a mount statement with noauto instead of putting it in fstab… after all fstab just does mounts the same as a mount statement.
So
mount UUID=74C1-30C7 /mnt noauto 0 0

3 Likes