Score:6

Avoid adding numbers to USB disks partition label/UUID when mount point exists under /media/user

lr flag

I'm using Ubuntu 23.04 and I'm assuming that Ubuntu is using autofs to auto mount. I'm unfamiliar with autofs configuration.

When I connect my labeled external drive(s) via usb, they automatically mount based on label in /media/user/label. However, if /media/user/label already exists (nothing mounted - empty directory), it mounts on /media/user/label1.

This is not what I want. If these old mount points exist, I want to use them as is.

Is this a possible configuration? If not, I would prefer it to fail and not mount anything.

Score:6
jp flag

There is enough explanation in @vanadium's answer as well as in this answer ... So, I'm here to offer a workaround ... A UDEV rule can automatically check and delete the mount point if it exists under /media/user.

You, basically, create a file under /etc/udev/rules.d/ like so:

sudo nano /etc/udev/rules.d/70-label.rules

and to make it effective for a certain user, add these two lines in it changing user in both lines to the desired username e.g. your username:

ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rmdir /media/user/%E{ID_FS_LABEL}"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rmdir /media/user/%E{ID_FS_UUID}"

or to make it effective for all users, add these two lines instead:

ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/sh -c '/bin/rmdir /media/*/%E{ID_FS_LABEL}'"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/sh -c '/bin/rmdir /media/*/%E{ID_FS_UUID}'"

then save the file and reload UDEV like so:

sudo systemctl restart udev

That should work with both labeled and unlabeled partitions/filesystems.

Notice: the above rule will be triggered by inserting a USB disk that has partition(s) on it and will run regardless of the currently logged-in user and will run commands as root so, you might want to keep that in mind and I used /bin/rmdir as a safety measure as it will remove only empty directories as well as the specified real path so it should only remove directories under /media/user ... Also sd[b-z][0-9] assumes that when you insert the first USB disk, it's given sdb as a name by the kernel ... If however that is not the case and naming on your system starts from e.g. sdc because you have more than one hard disk in your machine, then change it accordingly to e.g. sd[c-z][0-9] for performance reasons.

lr flag
Thanks to all for the explanations and the workaround!
Score:4
cn flag

No, this is not a possible configuration. An old mount point in principle should not exist: whenever an automounted removable drive is removed, the old mount point is automatically removed.

So in first instance, remove the USB drive, then delete the still existing old mount point. Then reconnect the USB drive: the mount point will now be recreated with the name you expect.

If you make sure to carefully disconnect the drive, or otherwise have the system correctly shut down before removing the drive, the mount point each time should be automatically deleted.

If you take these precautions, but still see the issue, something else may be ongoing. If the cause cannot be found, a workaround then could be to have that mount point deleted automatically during startup.

user10489 avatar
in flag
Note that if files on the device are still open when you try to eject, the eject will fail. If you yank the drive out without noticing the eject failed, you will end up with these left over mount points. This is one of the more common causes of this problem.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.