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

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.

Asked By: YMOZ

||

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.

Answered By: vanadium

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.

Answered By: Raffa
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.