Score:1

Mount specific removable media with mount options by default

mx flag

I run Ubuntu Mate 22.04 and I have a USB mass storage device (aka USB-Stick) that is formatted with btrfs. I know it's partition UUID and label and I want it specifically to be mounted with specific mount options. In particular I want to enable file system compression every time I plug it into my computer without having to unmount it and then manually remount it with the terminal.

When I plug it in, I would usually have to do

sudo umount /dev/sdb/
sudo mkdir /media/username/label
sudo mount -t btrfs -o rw,ssd,nosuid,nodev,noatime,space_cache=v2,subvolid=5,subvol=/,uhelper=udisks2,compress=zstd:15,discard=async /dev/disk/by-uuid/<uuid> /media/username/label
sudo chown username:usergroup /media/username/label

which I would like to automate somehow.

How do I do that?

Edit

I tried adding this line to my /etc/fstab as suggested in the comments.

UUID=<uuid> /mnt/<label> btrfs rw,ssd,nosuid,nodev,noatime,space_cache=v2,subvolid=5,subvol=/,uhelper=udisks2,compress=zstd:15,discard=async,nofail        0       2

However this causes the device to not auto-mount anymore when I plug it in. When I mount it, I have to be root. Then it doesn't show up in the devices anymore in my file manager (caja). It also is not user writable by default.

I would like the same behavior as plugging it in normally, just with the options

  • ssd, because the device in question is somewhat special and announces itself as rotational when it is not),
  • compress=zstd:15, because the device is very slow anyway and also somewhat small and
  • noatime to preserve the flash drive's lifespan

I want it to mount automatically with those settings, but also is user writable and show up in the file manager.

mook765 avatar
cn flag
Then create a fstab entry, add `nofail` to your other mount-options for the case the drive is not connected during boot.
FalcoGer avatar
mx flag
@mook765 i tried that, now it doesn't auto-mount at all anymore and doesn't show up in caja as a device at all. furthermore it requires root to mount and default permissions are root writable only.
mook765 avatar
cn flag
Your fstab entry uses `/mnt/label`, does this folder exist? Devices are only shown in the file manager when they are mounted under `/media`. I suggest to `mkdir /media/label` and use this folder in fstab. `chown` to your user will be needed as well.
FalcoGer avatar
mx flag
@mook765 that fixed it for the first mount only, after that i get an error "Error mounting system-managed device /dev/sdc1: mount(2) system call failed: File exists"
FalcoGer avatar
mx flag
@mook765 I rebooted, and that seems to have fixed the issue. Plugging in the device now works every time and it has the right permissions and mount options. Feel free to answer.
vanadium avatar
cn flag
Try the utiity "Disks" to set mount options. I am not sure mount options entered will persist the next time the drive is automatically mounted, but it is worth trying.
FalcoGer avatar
mx flag
@vanadium the disks utility also edits /etc/fstab. Feel free to answer.
vanadium avatar
cn flag
@FalcoGer, not, I believe, if you do not check "Mount at system checkup". Still, it is possible that the options you set are saved (using another mechanism related to the udev automatic mounting), but I am not sure.
Score:0
mx flag

One can add an entry to fstab with the desired mount options. The mount point must be in /media/username/, so a directory for that should be created. The name of the directory in /media/username/ will be the drive name as shown in the file manager. The directory won't be automatically deleted if this procedure is followed, as it would be with other removable media.

The options that need to be added for this to work are

  • nofail to prevent an error during boot when the drive is not connected
  • user to allow a user to mount the drive
  • uhelper=udisks2 to allow users to unmount the drive without root privileges
  • nodev to disallow device files on the drive
  • nosuid disallow set userid files on the drive

dump and filesystem check order should both be 0.

The complete line would be something like this

UUID=<UUID> /media/<user>/<label> btrfs rw,nosuid,nodev,user,uhelper=udisks2,nofail,ssd,noatime,space_cache=v2,subvolid=5,subvol=/,compress=zstd:15,discard=async        0       0
I sit in a Tesla and translated this thread with Ai:

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.