13

If I edit the files on my pendrive, and then just remove it physically, the data will not be recorded. I have to click "eject", and so the led starts to blink, and the data starts to be recorded.

How can I change this, so that the data is recorded instantly when changed?

update: Ubuntu 11.10

In Ubuntu 11.10 I wrote the rules file (from @enzotib answer), and works, but gives the following message when I try to eject/unmount:

enter image description here

(should I start another question for Ubuntu 11.10?)

The Student
  • 11,926
  • 2
    Performance will be less as the write cache is not used anymore. Properly unmount partitions using the eject / unmount options or you'll lose data. – Lekensteyn Sep 14 '11 at 19:33
  • 4
    You can use the sync option when mounting the partition. This is not recommended though as it dramatically increases the number of writes to the thumbdrive. – arrange Sep 14 '11 at 20:04
  • 2
    @arrange as ubuntu do the automount, is there a way to put this sync as default, so I don't need to do this in a terminal every time? And I'm aware that it'll dramatically increase the number of writes, still is what I want. Thanks! – The Student Sep 15 '11 at 12:15
  • 1
    See http://askubuntu.com/questions/30762/is-setting-default-mount-options-for-udisks-really-not-possible – arrange Sep 15 '11 at 19:35
  • @arrange saw it, but it's still not a complete answer... – The Student Sep 21 '11 at 12:25

2 Answers2

9

Following the question (and answer) that @arrange shows in his comment, let's create a new file

/etc/udev/rules.d/11-media-by-label-auto-mount.rules

and write into it the following content (see udev::Mount under /media; use partition label if present)

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime,sync"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,uid=1000,gid=1000,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit
LABEL="media_by_label_auto_mount_end"

I have done some little modifications with respect to that reported on the website

  • added the sync option to ENV{mount_options}
  • modified the gid=100 option to uid=1000,gid=1000, where the number are relative to your used-id and group-id, as returned by id -u and id -g

The second one make you the owner of all files on the mounted partition, so no permission problem can arise. Obviously this is not the case if more than a single user should access the files, but this can be handled in other ways.

For non-windows filesystems, owner and group are metadata on the filesystem, and cannot be changed by a mount option.

The new rule is immediately active (udev uses inotify to detect new and modified rule files).

The mounted drive is not automatically opened in a file manager window, as you can see, and this is difficult to achieve, given that the mounting process is owned by root, and do not have access to your graphical login session. But the drive is on your desktop, so a simple double-click will open it.

Unfortunately I cannot get the desktop right-click unmount to work, but this is not a big problem: having the sync option in place, you can simply unplug the device, and the udev rule take care of removing the entry from /proc/mounts and removing the mount-point.

enzotib
  • 93,831
  • I couldn't get round to writing this how-to, thanks for doing it ;) And I also couldn't get the user unmount to work, it seems like the users option is only valid in the fstab file... This isn't a problem here ("sync"), but for a general how-to it would be. – arrange Sep 22 '11 at 09:24
  • Thanks for answering. Unfortunately, after doing the described, I couldn't create, edit or delete any files on the pendrive. Also, it's no more automatically opening the window when I plug it. Any idea? – The Student Sep 22 '11 at 16:14
  • By the way, I considered that the extension you typed ".rule" is wrong, and I used the same of the other files on that folder: ".rules". – The Student Sep 22 '11 at 16:14
  • The documentation do not talk about specific extensions to use. By the way I copied wrong from the site, so it is better to use .rules to be safe. For the rest, see the edited answer. – enzotib Sep 22 '11 at 16:41
  • Hey, I'm on Ubuntu 11.10 now, and there's a different behavior. Please, see my update. – The Student Mar 23 '12 at 13:37
  • @TomBrito: from the last paragraph of my answer you can understand that I think it should not work not event in 10.10. – enzotib Mar 23 '12 at 13:43
  • I had some trouble the last days with my "home" asking to mount when it was already mounted in the boot. I suspect this script may be related. As I don't understand it, maybe you want to review it? – The Student Apr 19 '12 at 21:01
0

For the newer Ubuntu (> 20.04) you can create/edit /etc/udisks2/mount_options.conf and set:

[defaults]
defaults=noatime,sync

And

sudo service udisks2 restart

the disconnect and reconnect your disk.

This make sync all filesystem automounted and ignore creation time (speed up navigation file system)

swipon
  • 1