How to mount removable media in /media/ automatically when inserted, without any desktop environment?2019 Community Moderator ElectionAutomounting USB sticks on DebianAutomount USB drives with no GUI requirement (halevt replacement)How to force a device to be mounted as Mass Media Storage (MMS) rather than Media Transfer Protocol (MTP)?Register callback for newly inserted disk?Mounting USB disks automatically (How it works)What's the most “correct” mount point for a permanent NTFS partition?How to set default auto mount options for removable media?Mount USB flash drive to /media/<label>Automounts: All mounting and will not umount on its ownDifferent mount output with and without initramfsMounting weirdnessWhat are the FHS compliant mount points?amazon aws : dracut mount filesystems in random order?How to mount one of multiple disks to a specific location in fstab

Why does the frost depth increase when the surface temperature warms up?

How do you justify more code being written by following clean code practices?

Travelling in US for more than 90 days

Are hand made posters acceptable in Academia?

Derivative of an interpolated function

Should a narrator ever describe things based on a character's view instead of facts?

"Oh no!" in Latin

Why would five hundred and five same as one?

How to preserve electronics (computers, ipads, phones) for hundreds of years?

How can a new country break out from a developed country without war?

Should I warn a new PhD Student?

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Unfrosted light bulb

How to split IPA spelling into syllables

Mortal danger in mid-grade literature

C++ lambda syntax

Sort with assumptions

Put the phone down / Put down the phone

How do you say "Trust your struggle." in French?

Magnifying glass in hyperbolic space

Why is participating in the European Parliamentary elections used as a threat?

Center page as a whole without centering each element individually

Connection Between Knot Theory and Number Theory

Can you describe someone as luxurious? As in someone who likes luxurious things?



How to mount removable media in /media/



2019 Community Moderator ElectionAutomounting USB sticks on DebianAutomount USB drives with no GUI requirement (halevt replacement)How to force a device to be mounted as Mass Media Storage (MMS) rather than Media Transfer Protocol (MTP)?Register callback for newly inserted disk?Mounting USB disks automatically (How it works)What's the most “correct” mount point for a permanent NTFS partition?How to set default auto mount options for removable media?Mount USB flash drive to /media/<label>Automounts: All mounting and will not umount on its ownDifferent mount output with and without initramfsMounting weirdnessWhat are the FHS compliant mount points?amazon aws : dracut mount filesystems in random order?How to mount one of multiple disks to a specific location in fstab










17















I have a Debian sid system (Wheezy) (and same for arch), without any desktop environment (and no Xorg at all).



I can mount my SD-cards, USB sticks, external HDD by label into a specified directory in /media/ manually with mount / umount and the suitable entries in /etc/fstab, or automatically at boot time, but this is compelling, to restrictive and not dynamic enough for my needs: if I want them to be mounted in /media/<LABEL>, each device with a different <LABEL> needs its own entry, and each <LABEL> subdirectory needs to be created / removed manually in /media/ as well).



So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged no matter how risky it is for the data)?



The ideal solution would:



  1. detect when a removable media is plugged-in (i.e. when added in /dev/ as
    sdax, sdbx, ... by udev)

  2. create a directory in /media/ according to its label (label of the removable media)

  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem supports that) with flush option (for a vfat)

  4. detect if the media has been unplugged

  5. if then, unmount it from the filesystem

  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching edit: sync is way too much for vfat removable media so flush has been created as a compromise, less secure than sync, but less risky according to the life-cycles of flash memories)



I found some info about autofs, HAL, udisks, udisks2, usbmount etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...










share|improve this question
























  • First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

    – enzotib
    Aug 1 '12 at 9:06






  • 1





    Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

    – cedbeu
    Aug 1 '12 at 9:18












  • Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

    – enzotib
    Aug 1 '12 at 9:24











  • Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

    – cedbeu
    Aug 1 '12 at 9:30







  • 1





    I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

    – Emanuel Berg
    Aug 1 '12 at 10:40















17















I have a Debian sid system (Wheezy) (and same for arch), without any desktop environment (and no Xorg at all).



I can mount my SD-cards, USB sticks, external HDD by label into a specified directory in /media/ manually with mount / umount and the suitable entries in /etc/fstab, or automatically at boot time, but this is compelling, to restrictive and not dynamic enough for my needs: if I want them to be mounted in /media/<LABEL>, each device with a different <LABEL> needs its own entry, and each <LABEL> subdirectory needs to be created / removed manually in /media/ as well).



So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged no matter how risky it is for the data)?



The ideal solution would:



  1. detect when a removable media is plugged-in (i.e. when added in /dev/ as
    sdax, sdbx, ... by udev)

  2. create a directory in /media/ according to its label (label of the removable media)

  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem supports that) with flush option (for a vfat)

  4. detect if the media has been unplugged

  5. if then, unmount it from the filesystem

  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching edit: sync is way too much for vfat removable media so flush has been created as a compromise, less secure than sync, but less risky according to the life-cycles of flash memories)



I found some info about autofs, HAL, udisks, udisks2, usbmount etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...










share|improve this question
























  • First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

    – enzotib
    Aug 1 '12 at 9:06






  • 1





    Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

    – cedbeu
    Aug 1 '12 at 9:18












  • Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

    – enzotib
    Aug 1 '12 at 9:24











  • Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

    – cedbeu
    Aug 1 '12 at 9:30







  • 1





    I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

    – Emanuel Berg
    Aug 1 '12 at 10:40













17












17








17


7






I have a Debian sid system (Wheezy) (and same for arch), without any desktop environment (and no Xorg at all).



I can mount my SD-cards, USB sticks, external HDD by label into a specified directory in /media/ manually with mount / umount and the suitable entries in /etc/fstab, or automatically at boot time, but this is compelling, to restrictive and not dynamic enough for my needs: if I want them to be mounted in /media/<LABEL>, each device with a different <LABEL> needs its own entry, and each <LABEL> subdirectory needs to be created / removed manually in /media/ as well).



So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged no matter how risky it is for the data)?



The ideal solution would:



  1. detect when a removable media is plugged-in (i.e. when added in /dev/ as
    sdax, sdbx, ... by udev)

  2. create a directory in /media/ according to its label (label of the removable media)

  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem supports that) with flush option (for a vfat)

  4. detect if the media has been unplugged

  5. if then, unmount it from the filesystem

  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching edit: sync is way too much for vfat removable media so flush has been created as a compromise, less secure than sync, but less risky according to the life-cycles of flash memories)



I found some info about autofs, HAL, udisks, udisks2, usbmount etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...










share|improve this question
















I have a Debian sid system (Wheezy) (and same for arch), without any desktop environment (and no Xorg at all).



I can mount my SD-cards, USB sticks, external HDD by label into a specified directory in /media/ manually with mount / umount and the suitable entries in /etc/fstab, or automatically at boot time, but this is compelling, to restrictive and not dynamic enough for my needs: if I want them to be mounted in /media/<LABEL>, each device with a different <LABEL> needs its own entry, and each <LABEL> subdirectory needs to be created / removed manually in /media/ as well).



So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged no matter how risky it is for the data)?



The ideal solution would:



  1. detect when a removable media is plugged-in (i.e. when added in /dev/ as
    sdax, sdbx, ... by udev)

  2. create a directory in /media/ according to its label (label of the removable media)

  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem supports that) with flush option (for a vfat)

  4. detect if the media has been unplugged

  5. if then, unmount it from the filesystem

  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching edit: sync is way too much for vfat removable media so flush has been created as a compromise, less secure than sync, but less risky according to the life-cycles of flash memories)



I found some info about autofs, HAL, udisks, udisks2, usbmount etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...







mount automounting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 '17 at 13:23









Faheem Mitha

23.1k1884137




23.1k1884137










asked Aug 1 '12 at 8:40









cedbeucedbeu

3022312




3022312












  • First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

    – enzotib
    Aug 1 '12 at 9:06






  • 1





    Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

    – cedbeu
    Aug 1 '12 at 9:18












  • Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

    – enzotib
    Aug 1 '12 at 9:24











  • Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

    – cedbeu
    Aug 1 '12 at 9:30







  • 1





    I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

    – Emanuel Berg
    Aug 1 '12 at 10:40

















  • First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

    – enzotib
    Aug 1 '12 at 9:06






  • 1





    Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

    – cedbeu
    Aug 1 '12 at 9:18












  • Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

    – enzotib
    Aug 1 '12 at 9:24











  • Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

    – cedbeu
    Aug 1 '12 at 9:30







  • 1





    I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

    – Emanuel Berg
    Aug 1 '12 at 10:40
















First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

– enzotib
Aug 1 '12 at 9:06





First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging.

– enzotib
Aug 1 '12 at 9:06




1




1





Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

– cedbeu
Aug 1 '12 at 9:18






Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal.

– cedbeu
Aug 1 '12 at 9:18














Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

– enzotib
Aug 1 '12 at 9:24





Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it.

– enzotib
Aug 1 '12 at 9:24













Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

– cedbeu
Aug 1 '12 at 9:30






Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that.

– cedbeu
Aug 1 '12 at 9:30





1




1





I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

– Emanuel Berg
Aug 1 '12 at 10:40





I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this.

– Emanuel Berg
Aug 1 '12 at 10:40










6 Answers
6






active

oldest

votes


















9














I think you're looking for pmount.



If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.



If your system uses udisks2, that's the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.



Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn't work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.



You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.






share|improve this answer

























  • Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

    – cedbeu
    Aug 2 '12 at 8:08






  • 1





    @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

    – Gilles
    Aug 2 '12 at 9:08











  • Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

    – cedbeu
    Aug 6 '12 at 16:58











  • anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

    – cedbeu
    Aug 6 '12 at 17:03







  • 1





    Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

    – malat
    May 19 '15 at 15:31


















2














Here is how I recently did it, and I am quite happy with this approach now.
This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.



Prerequisites: You have to have installed udev + autofs.



Step 1)



Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :



# Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
# if partition has a label
# Add symlink /media/usb/<label> to /media/autousb/<label>
# for automounter support
ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660",
RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC",
RUN+="/bin/ln -sf /media/autousb/$envID_FS_LABEL_ENC /media/usb/$envID_FS_LABEL_ENC"

# Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
SYMLINK+="usbdisks/%k", MODE:="0660",
RUN+="/bin/rm /media/usb/%k",
RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

# Some FileSystems emit a "change" event when they are unmounted.
# UDEV seems to delete the device symlink in this case :-(
# So we need to re-create it here
ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660"

# Fallback: If partition has NO label, use kernel name
ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
SYMLINK+="usbdisks/%k", MODE:="0660"


# When device is removed, also remove /media/usb/<label>
ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC"

# Fallback: If partition has no label, remove /media/usb/%k
ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
RUN+="/bin/rm /media/usb/%k"


What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label.
The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).



Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?



But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.



To make sure udev reads these rules use



sudo udevadm control --reload-rules


With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with



Step 2)



Setup autofs to automount /media/autousb/<...> :
I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):



/media/autousb /etc/auto.usb --timeout=60


This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.



For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use



/media/autousb /etc/autofs/auto.usb --timeout=60


Now I created "auto.usb" with the following content:



#!/bin/bash

key=$1
fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/$key)
if [ "$fstype" = "vfat" ] ; then
echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
exit 0
fi
if [ "$fstype" = "ntfs" ] ; then
echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
exit 0
fi
if [ "$fstype" = "ext4" ] ; then
echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/$key"
exit 0
fi

exit 1


This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example



sudo chmod 0755 /etc/auto.usb


What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.



The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.



The script will then provide the right mount options depending on the device partition.



Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.



Step 3)
Optional...
To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :



#!/bin/bash
killall -s SIGUSR1 /usr/sbin/automount


With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).



Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...



The real trick here is to use symlinks from "/media/usb" to "/media/autousb":



  • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there

  • AutoFS will then automount the partition on demand if you use it via /media/usb

  • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.

  • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.





share|improve this answer






























    2














    Ok, it's been a long time, but I'll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.



    1 - Creating add / remove scripts



    Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-automount.sh):



    #!/bin/sh

    # set the mountpoint name according to partition or device name
    mount_point=$ID_FS_LABEL
    if [ -z $mount_point ]; then
    mount_point=$DEVNAME##*/
    fi

    # if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
    plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
    if [ -z $plugdev_gid ]; then
    gid=''
    else
    chown root:plugdev $mount_point
    gid=",gid=$plugdev_gid"
    fi

    # create the mountpoint directory in /media/ (if not empty)
    if [ -n $mount_point ]; then
    mkdir -p /media/$mount_point
    # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
    mount -t $ID_FS_TYPE
    -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002
    $DEVNAME /media/$mount_point
    fi


    Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-autounmount.sh):



    #!/bin/sh

    # set the mountpoint name according to partition or device name
    mount_point=$ID_FS_LABEL
    if [ -z $mount_point ]; then
    mount_point=$DEVNAME##*/
    fi

    # remove the mountpoint directory from /media/ (if not empty)
    if [ -n $mount_point ]; then
    umount -l /media/$mount_point
    rm -R /media/$mount_point
    fi


    2 - Creating the udev rule to attach those scripts to events



    And finally, add a udev rule in /etc/udev/rules.d/, for instance 85-storage-automount.rules:



    ENVDEVTYPE=="partition", RUN+="/lib/udev/storage-automount.sh", ENVREMOVE_CMD="/lib/udev/storage-autounmount.sh"


    and make it have the same permissions as the other rules in that dir/folder



    Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.



    When the device is removed, it's unmounted and the directory is removed from /media



    Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:



    $ udevadm monitor --env


    This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.






    share|improve this answer

























    • I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

      – Jeremy
      Dec 27 '14 at 14:42











    • I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

      – TomRoche
      Jul 12 '15 at 23:54











    • [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

      – TomRoche
      Jul 12 '15 at 23:54












    • @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

      – cedbeu
      Jul 25 '15 at 6:04



















    1














    I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)



    After checking for the solution in many places, I found a simple solution in Arch Wiki:



    Link to Arch Wiki.



    We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.



    Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:



    ENVID_FS_USAGE=="filesystem|other|crypto", ENVUDISKS_FILESYSTEM_SHARED="1"



    Then we can just install a simple tool like Udiskie for automount.






    share|improve this answer























    • Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

      – cedbeu
      Jan 24 at 3:30


















    0














    I found a way in doing this by editing /etc/fstab file. A simple entry looks like:



    /dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0



    <file system> <mount point> <type> <options> <dump> <pass>






    share|improve this answer

























    • Mounts in /etc/fstab are only automatically mounted at boot.

      – Graeme
      Jan 4 '15 at 9:52











    • @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

      – Gino
      Jul 21 '17 at 3:05












    • @Gino, Ubuntu has done auto mounting by default for some time now.

      – Graeme
      Jul 21 '17 at 7:53











    • @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

      – Gino
      Jul 21 '17 at 21:49












    • @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

      – Graeme
      Jul 21 '17 at 22:00


















    0














    Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.



    However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or "user session" to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.






    share|improve this answer






















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f44454%2fhow-to-mount-removable-media-in-media-label-automatically-when-inserted-with%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      I think you're looking for pmount.



      If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.



      If your system uses udisks2, that's the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.



      Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn't work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.



      You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.






      share|improve this answer

























      • Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

        – cedbeu
        Aug 2 '12 at 8:08






      • 1





        @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

        – Gilles
        Aug 2 '12 at 9:08











      • Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

        – cedbeu
        Aug 6 '12 at 16:58











      • anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

        – cedbeu
        Aug 6 '12 at 17:03







      • 1





        Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

        – malat
        May 19 '15 at 15:31















      9














      I think you're looking for pmount.



      If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.



      If your system uses udisks2, that's the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.



      Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn't work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.



      You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.






      share|improve this answer

























      • Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

        – cedbeu
        Aug 2 '12 at 8:08






      • 1





        @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

        – Gilles
        Aug 2 '12 at 9:08











      • Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

        – cedbeu
        Aug 6 '12 at 16:58











      • anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

        – cedbeu
        Aug 6 '12 at 17:03







      • 1





        Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

        – malat
        May 19 '15 at 15:31













      9












      9








      9







      I think you're looking for pmount.



      If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.



      If your system uses udisks2, that's the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.



      Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn't work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.



      You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.






      share|improve this answer















      I think you're looking for pmount.



      If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.



      If your system uses udisks2, that's the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.



      Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn't work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.



      You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 13 '17 at 12:36









      Community

      1




      1










      answered Aug 2 '12 at 0:37









      GillesGilles

      543k12811001617




      543k12811001617












      • Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

        – cedbeu
        Aug 2 '12 at 8:08






      • 1





        @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

        – Gilles
        Aug 2 '12 at 9:08











      • Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

        – cedbeu
        Aug 6 '12 at 16:58











      • anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

        – cedbeu
        Aug 6 '12 at 17:03







      • 1





        Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

        – malat
        May 19 '15 at 15:31

















      • Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

        – cedbeu
        Aug 2 '12 at 8:08






      • 1





        @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

        – Gilles
        Aug 2 '12 at 9:08











      • Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

        – cedbeu
        Aug 6 '12 at 16:58











      • anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

        – cedbeu
        Aug 6 '12 at 17:03







      • 1





        Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

        – malat
        May 19 '15 at 15:31
















      Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

      – cedbeu
      Aug 2 '12 at 8:08





      Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore.

      – cedbeu
      Aug 2 '12 at 8:08




      1




      1





      @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

      – Gilles
      Aug 2 '12 at 9:08





      @cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html

      – Gilles
      Aug 2 '12 at 9:08













      Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

      – cedbeu
      Aug 6 '12 at 16:58





      Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise.

      – cedbeu
      Aug 6 '12 at 16:58













      anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

      – cedbeu
      Aug 6 '12 at 17:03






      anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me.

      – cedbeu
      Aug 6 '12 at 17:03





      1




      1





      Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

      – malat
      May 19 '15 at 15:31





      Pay attention that usbmount do not support NTFS since jessie anymore: bugs.debian.org/774149

      – malat
      May 19 '15 at 15:31













      2














      Here is how I recently did it, and I am quite happy with this approach now.
      This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.



      Prerequisites: You have to have installed udev + autofs.



      Step 1)



      Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :



      # Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
      # if partition has a label
      # Add symlink /media/usb/<label> to /media/autousb/<label>
      # for automounter support
      ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
      SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660",
      RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC",
      RUN+="/bin/ln -sf /media/autousb/$envID_FS_LABEL_ENC /media/usb/$envID_FS_LABEL_ENC"

      # Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
      ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
      SYMLINK+="usbdisks/%k", MODE:="0660",
      RUN+="/bin/rm /media/usb/%k",
      RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

      # Some FileSystems emit a "change" event when they are unmounted.
      # UDEV seems to delete the device symlink in this case :-(
      # So we need to re-create it here
      ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
      SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660"

      # Fallback: If partition has NO label, use kernel name
      ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
      SYMLINK+="usbdisks/%k", MODE:="0660"


      # When device is removed, also remove /media/usb/<label>
      ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
      RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC"

      # Fallback: If partition has no label, remove /media/usb/%k
      ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
      ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
      RUN+="/bin/rm /media/usb/%k"


      What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label.
      The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).



      Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?



      But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.



      To make sure udev reads these rules use



      sudo udevadm control --reload-rules


      With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with



      Step 2)



      Setup autofs to automount /media/autousb/<...> :
      I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):



      /media/autousb /etc/auto.usb --timeout=60


      This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.



      For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use



      /media/autousb /etc/autofs/auto.usb --timeout=60


      Now I created "auto.usb" with the following content:



      #!/bin/bash

      key=$1
      fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/$key)
      if [ "$fstype" = "vfat" ] ; then
      echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
      exit 0
      fi
      if [ "$fstype" = "ntfs" ] ; then
      echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
      exit 0
      fi
      if [ "$fstype" = "ext4" ] ; then
      echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/$key"
      exit 0
      fi

      exit 1


      This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example



      sudo chmod 0755 /etc/auto.usb


      What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.



      The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.



      The script will then provide the right mount options depending on the device partition.



      Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.



      Step 3)
      Optional...
      To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :



      #!/bin/bash
      killall -s SIGUSR1 /usr/sbin/automount


      With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).



      Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...



      The real trick here is to use symlinks from "/media/usb" to "/media/autousb":



      • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there

      • AutoFS will then automount the partition on demand if you use it via /media/usb

      • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.

      • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.





      share|improve this answer



























        2














        Here is how I recently did it, and I am quite happy with this approach now.
        This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.



        Prerequisites: You have to have installed udev + autofs.



        Step 1)



        Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :



        # Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
        # if partition has a label
        # Add symlink /media/usb/<label> to /media/autousb/<label>
        # for automounter support
        ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
        SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660",
        RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC",
        RUN+="/bin/ln -sf /media/autousb/$envID_FS_LABEL_ENC /media/usb/$envID_FS_LABEL_ENC"

        # Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
        ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
        SYMLINK+="usbdisks/%k", MODE:="0660",
        RUN+="/bin/rm /media/usb/%k",
        RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

        # Some FileSystems emit a "change" event when they are unmounted.
        # UDEV seems to delete the device symlink in this case :-(
        # So we need to re-create it here
        ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
        SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660"

        # Fallback: If partition has NO label, use kernel name
        ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
        SYMLINK+="usbdisks/%k", MODE:="0660"


        # When device is removed, also remove /media/usb/<label>
        ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
        RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC"

        # Fallback: If partition has no label, remove /media/usb/%k
        ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
        ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
        RUN+="/bin/rm /media/usb/%k"


        What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label.
        The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).



        Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?



        But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.



        To make sure udev reads these rules use



        sudo udevadm control --reload-rules


        With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with



        Step 2)



        Setup autofs to automount /media/autousb/<...> :
        I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):



        /media/autousb /etc/auto.usb --timeout=60


        This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.



        For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use



        /media/autousb /etc/autofs/auto.usb --timeout=60


        Now I created "auto.usb" with the following content:



        #!/bin/bash

        key=$1
        fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/$key)
        if [ "$fstype" = "vfat" ] ; then
        echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
        exit 0
        fi
        if [ "$fstype" = "ntfs" ] ; then
        echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
        exit 0
        fi
        if [ "$fstype" = "ext4" ] ; then
        echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/$key"
        exit 0
        fi

        exit 1


        This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example



        sudo chmod 0755 /etc/auto.usb


        What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.



        The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.



        The script will then provide the right mount options depending on the device partition.



        Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.



        Step 3)
        Optional...
        To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :



        #!/bin/bash
        killall -s SIGUSR1 /usr/sbin/automount


        With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).



        Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...



        The real trick here is to use symlinks from "/media/usb" to "/media/autousb":



        • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there

        • AutoFS will then automount the partition on demand if you use it via /media/usb

        • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.

        • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.





        share|improve this answer

























          2












          2








          2







          Here is how I recently did it, and I am quite happy with this approach now.
          This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.



          Prerequisites: You have to have installed udev + autofs.



          Step 1)



          Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :



          # Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
          # if partition has a label
          # Add symlink /media/usb/<label> to /media/autousb/<label>
          # for automounter support
          ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660",
          RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC",
          RUN+="/bin/ln -sf /media/autousb/$envID_FS_LABEL_ENC /media/usb/$envID_FS_LABEL_ENC"

          # Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
          ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          SYMLINK+="usbdisks/%k", MODE:="0660",
          RUN+="/bin/rm /media/usb/%k",
          RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

          # Some FileSystems emit a "change" event when they are unmounted.
          # UDEV seems to delete the device symlink in this case :-(
          # So we need to re-create it here
          ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660"

          # Fallback: If partition has NO label, use kernel name
          ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          SYMLINK+="usbdisks/%k", MODE:="0660"


          # When device is removed, also remove /media/usb/<label>
          ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC"

          # Fallback: If partition has no label, remove /media/usb/%k
          ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          RUN+="/bin/rm /media/usb/%k"


          What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label.
          The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).



          Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?



          But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.



          To make sure udev reads these rules use



          sudo udevadm control --reload-rules


          With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with



          Step 2)



          Setup autofs to automount /media/autousb/<...> :
          I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):



          /media/autousb /etc/auto.usb --timeout=60


          This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.



          For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use



          /media/autousb /etc/autofs/auto.usb --timeout=60


          Now I created "auto.usb" with the following content:



          #!/bin/bash

          key=$1
          fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/$key)
          if [ "$fstype" = "vfat" ] ; then
          echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
          exit 0
          fi
          if [ "$fstype" = "ntfs" ] ; then
          echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
          exit 0
          fi
          if [ "$fstype" = "ext4" ] ; then
          echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/$key"
          exit 0
          fi

          exit 1


          This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example



          sudo chmod 0755 /etc/auto.usb


          What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.



          The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.



          The script will then provide the right mount options depending on the device partition.



          Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.



          Step 3)
          Optional...
          To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :



          #!/bin/bash
          killall -s SIGUSR1 /usr/sbin/automount


          With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).



          Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...



          The real trick here is to use symlinks from "/media/usb" to "/media/autousb":



          • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there

          • AutoFS will then automount the partition on demand if you use it via /media/usb

          • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.

          • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.





          share|improve this answer













          Here is how I recently did it, and I am quite happy with this approach now.
          This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.



          Prerequisites: You have to have installed udev + autofs.



          Step 1)



          Create the following "/etc/udev/rules.d/90-usbsd-auto.rules" file (of course you might use any name as long as it ends with ".rules"). :



          # Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9] 
          # if partition has a label
          # Add symlink /media/usb/<label> to /media/autousb/<label>
          # for automounter support
          ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660",
          RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC",
          RUN+="/bin/ln -sf /media/autousb/$envID_FS_LABEL_ENC /media/usb/$envID_FS_LABEL_ENC"

          # Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
          ACTION=="add", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          SYMLINK+="usbdisks/%k", MODE:="0660",
          RUN+="/bin/rm /media/usb/%k",
          RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"

          # Some FileSystems emit a "change" event when they are unmounted.
          # UDEV seems to delete the device symlink in this case :-(
          # So we need to re-create it here
          ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          SYMLINK+="usbdisks/$envID_FS_LABEL_ENC", MODE:="0660"

          # Fallback: If partition has NO label, use kernel name
          ACTION=="change", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          SYMLINK+="usbdisks/%k", MODE:="0660"


          # When device is removed, also remove /media/usb/<label>
          ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC=="?*",
          RUN+="/bin/rm /media/usb/$envID_FS_LABEL_ENC"

          # Fallback: If partition has no label, remove /media/usb/%k
          ACTION=="remove", KERNEL=="sd*", ENVDEVTYPE=="partition",
          ENVID_BUS=="usb", ENVID_FS_LABEL_ENC!="?*",
          RUN+="/bin/rm /media/usb/%k"


          What does this do: It creates two symlinks, one for the USB storage device partition under "/dev/usbdisks/<...>". It will either use the label of the USB storage partition or the kernel name, if there is no label.
          The second symlink will link from "/media/usb/<...>" to "/media/autousb/<...>" this is done for automounter support (see step 2).



          Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?



          But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.



          To make sure udev reads these rules use



          sudo udevadm control --reload-rules


          With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with



          Step 2)



          Setup autofs to automount /media/autousb/<...> :
          I added the following line to my "/etc/auto.master" file (for Ubuntu 12.04):



          /media/autousb /etc/auto.usb --timeout=60


          This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.



          For Gentoo you have to modify "/etc/autofs/auto.master" so it makes sense to use



          /media/autousb /etc/autofs/auto.usb --timeout=60


          Now I created "auto.usb" with the following content:



          #!/bin/bash

          key=$1
          fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/$key)
          if [ "$fstype" = "vfat" ] ; then
          echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
          exit 0
          fi
          if [ "$fstype" = "ntfs" ] ; then
          echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/$key"
          exit 0
          fi
          if [ "$fstype" = "ext4" ] ; then
          echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/$key"
          exit 0
          fi

          exit 1


          This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example



          sudo chmod 0755 /etc/auto.usb


          What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.



          The script will first use "/sbin/blkid" to find out what kind of file system is on the partition.



          The script will then provide the right mount options depending on the device partition.



          Note: I included sample code for "vfat" (probably most common for usb sticks), "ntfs" and "xfs" file systems. Of course it is quite easy to extend this to support more file systems.



          Step 3)
          Optional...
          To "eject" == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :



          #!/bin/bash
          killall -s SIGUSR1 /usr/sbin/automount


          With this script you might use "sudo usbeject" to unmount all mounted USB device partitions (by telling automount to unmount them).



          Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout...



          The real trick here is to use symlinks from "/media/usb" to "/media/autousb":



          • The "/media/usb" symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there

          • AutoFS will then automount the partition on demand if you use it via /media/usb

          • With the scripted "auto.usb" file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.

          • This approach supports USB sticks with multiple partition, even if these partitions use different file system types.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 23 '15 at 23:24









          Ingo BlackmanIngo Blackman

          1412




          1412





















              2














              Ok, it's been a long time, but I'll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.



              1 - Creating add / remove scripts



              Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-automount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
              plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
              if [ -z $plugdev_gid ]; then
              gid=''
              else
              chown root:plugdev $mount_point
              gid=",gid=$plugdev_gid"
              fi

              # create the mountpoint directory in /media/ (if not empty)
              if [ -n $mount_point ]; then
              mkdir -p /media/$mount_point
              # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
              mount -t $ID_FS_TYPE
              -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002
              $DEVNAME /media/$mount_point
              fi


              Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-autounmount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # remove the mountpoint directory from /media/ (if not empty)
              if [ -n $mount_point ]; then
              umount -l /media/$mount_point
              rm -R /media/$mount_point
              fi


              2 - Creating the udev rule to attach those scripts to events



              And finally, add a udev rule in /etc/udev/rules.d/, for instance 85-storage-automount.rules:



              ENVDEVTYPE=="partition", RUN+="/lib/udev/storage-automount.sh", ENVREMOVE_CMD="/lib/udev/storage-autounmount.sh"


              and make it have the same permissions as the other rules in that dir/folder



              Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.



              When the device is removed, it's unmounted and the directory is removed from /media



              Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:



              $ udevadm monitor --env


              This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.






              share|improve this answer

























              • I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

                – Jeremy
                Dec 27 '14 at 14:42











              • I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

                – TomRoche
                Jul 12 '15 at 23:54











              • [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

                – TomRoche
                Jul 12 '15 at 23:54












              • @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

                – cedbeu
                Jul 25 '15 at 6:04
















              2














              Ok, it's been a long time, but I'll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.



              1 - Creating add / remove scripts



              Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-automount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
              plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
              if [ -z $plugdev_gid ]; then
              gid=''
              else
              chown root:plugdev $mount_point
              gid=",gid=$plugdev_gid"
              fi

              # create the mountpoint directory in /media/ (if not empty)
              if [ -n $mount_point ]; then
              mkdir -p /media/$mount_point
              # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
              mount -t $ID_FS_TYPE
              -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002
              $DEVNAME /media/$mount_point
              fi


              Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-autounmount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # remove the mountpoint directory from /media/ (if not empty)
              if [ -n $mount_point ]; then
              umount -l /media/$mount_point
              rm -R /media/$mount_point
              fi


              2 - Creating the udev rule to attach those scripts to events



              And finally, add a udev rule in /etc/udev/rules.d/, for instance 85-storage-automount.rules:



              ENVDEVTYPE=="partition", RUN+="/lib/udev/storage-automount.sh", ENVREMOVE_CMD="/lib/udev/storage-autounmount.sh"


              and make it have the same permissions as the other rules in that dir/folder



              Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.



              When the device is removed, it's unmounted and the directory is removed from /media



              Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:



              $ udevadm monitor --env


              This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.






              share|improve this answer

























              • I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

                – Jeremy
                Dec 27 '14 at 14:42











              • I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

                – TomRoche
                Jul 12 '15 at 23:54











              • [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

                – TomRoche
                Jul 12 '15 at 23:54












              • @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

                – cedbeu
                Jul 25 '15 at 6:04














              2












              2








              2







              Ok, it's been a long time, but I'll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.



              1 - Creating add / remove scripts



              Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-automount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
              plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
              if [ -z $plugdev_gid ]; then
              gid=''
              else
              chown root:plugdev $mount_point
              gid=",gid=$plugdev_gid"
              fi

              # create the mountpoint directory in /media/ (if not empty)
              if [ -n $mount_point ]; then
              mkdir -p /media/$mount_point
              # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
              mount -t $ID_FS_TYPE
              -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002
              $DEVNAME /media/$mount_point
              fi


              Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-autounmount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # remove the mountpoint directory from /media/ (if not empty)
              if [ -n $mount_point ]; then
              umount -l /media/$mount_point
              rm -R /media/$mount_point
              fi


              2 - Creating the udev rule to attach those scripts to events



              And finally, add a udev rule in /etc/udev/rules.d/, for instance 85-storage-automount.rules:



              ENVDEVTYPE=="partition", RUN+="/lib/udev/storage-automount.sh", ENVREMOVE_CMD="/lib/udev/storage-autounmount.sh"


              and make it have the same permissions as the other rules in that dir/folder



              Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.



              When the device is removed, it's unmounted and the directory is removed from /media



              Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:



              $ udevadm monitor --env


              This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.






              share|improve this answer















              Ok, it's been a long time, but I'll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.



              1 - Creating add / remove scripts



              Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-automount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
              plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
              if [ -z $plugdev_gid ]; then
              gid=''
              else
              chown root:plugdev $mount_point
              gid=",gid=$plugdev_gid"
              fi

              # create the mountpoint directory in /media/ (if not empty)
              if [ -n $mount_point ]; then
              mkdir -p /media/$mount_point
              # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
              mount -t $ID_FS_TYPE
              -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002
              $DEVNAME /media/$mount_point
              fi


              Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /lib/udev/storage-autounmount.sh):



              #!/bin/sh

              # set the mountpoint name according to partition or device name
              mount_point=$ID_FS_LABEL
              if [ -z $mount_point ]; then
              mount_point=$DEVNAME##*/
              fi

              # remove the mountpoint directory from /media/ (if not empty)
              if [ -n $mount_point ]; then
              umount -l /media/$mount_point
              rm -R /media/$mount_point
              fi


              2 - Creating the udev rule to attach those scripts to events



              And finally, add a udev rule in /etc/udev/rules.d/, for instance 85-storage-automount.rules:



              ENVDEVTYPE=="partition", RUN+="/lib/udev/storage-automount.sh", ENVREMOVE_CMD="/lib/udev/storage-autounmount.sh"


              and make it have the same permissions as the other rules in that dir/folder



              Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.



              When the device is removed, it's unmounted and the directory is removed from /media



              Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:



              $ udevadm monitor --env


              This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 13 '15 at 0:38









              TomRoche

              5181620




              5181620










              answered Aug 12 '13 at 13:35









              cedbeucedbeu

              3022312




              3022312












              • I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

                – Jeremy
                Dec 27 '14 at 14:42











              • I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

                – TomRoche
                Jul 12 '15 at 23:54











              • [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

                – TomRoche
                Jul 12 '15 at 23:54












              • @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

                – cedbeu
                Jul 25 '15 at 6:04


















              • I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

                – Jeremy
                Dec 27 '14 at 14:42











              • I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

                – TomRoche
                Jul 12 '15 at 23:54











              • [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

                – TomRoche
                Jul 12 '15 at 23:54












              • @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

                – cedbeu
                Jul 25 '15 at 6:04

















              I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

              – Jeremy
              Dec 27 '14 at 14:42





              I think that +r should be replaced by +x in "set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh)"

              – Jeremy
              Dec 27 '14 at 14:42













              I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

              – TomRoche
              Jul 12 '15 at 23:54





              I have a new jessie install on which I followed your directions closely, but cannot make automount happen from tty1, even after restart (which is not in your instructions, but I also got no automount pre-restart). Inserting the USB drive is detected (I get console messages), but nothing is visible in /media/ (except cdrom). I can manually mount the USB drive, so I know that works as well. [continued in following comment]

              – TomRoche
              Jul 12 '15 at 23:54













              [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

              – TomRoche
              Jul 12 '15 at 23:54






              [continued from previous comment] I have package=udev installed, have dirs=/etc/udev/rules.d/ , /lib/udev/ pre-populated, and have executable=udevadm in $PATH. I'd like to know (1) in what Debian release did you make this work? (I'm assuming wheezy) (2) what Debian packages do you have installed besides udev?

              – TomRoche
              Jul 12 '15 at 23:54














              @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

              – cedbeu
              Jul 25 '15 at 6:04






              @TomRoche Was Wheezy, at the time, indeed, and I had no special package installed, I was trying to make a minimalistic setup and was trying to avoid as far as possible to use packages that were not provided by the minimal install. I did not need to restart, that it why this is not in my instructions. It's been quite a long time I haven't dived into it… I might try again soon.

              – cedbeu
              Jul 25 '15 at 6:04












              1














              I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)



              After checking for the solution in many places, I found a simple solution in Arch Wiki:



              Link to Arch Wiki.



              We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.



              Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:



              ENVID_FS_USAGE=="filesystem|other|crypto", ENVUDISKS_FILESYSTEM_SHARED="1"



              Then we can just install a simple tool like Udiskie for automount.






              share|improve this answer























              • Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

                – cedbeu
                Jan 24 at 3:30















              1














              I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)



              After checking for the solution in many places, I found a simple solution in Arch Wiki:



              Link to Arch Wiki.



              We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.



              Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:



              ENVID_FS_USAGE=="filesystem|other|crypto", ENVUDISKS_FILESYSTEM_SHARED="1"



              Then we can just install a simple tool like Udiskie for automount.






              share|improve this answer























              • Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

                – cedbeu
                Jan 24 at 3:30













              1












              1








              1







              I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)



              After checking for the solution in many places, I found a simple solution in Arch Wiki:



              Link to Arch Wiki.



              We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.



              Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:



              ENVID_FS_USAGE=="filesystem|other|crypto", ENVUDISKS_FILESYSTEM_SHARED="1"



              Then we can just install a simple tool like Udiskie for automount.






              share|improve this answer













              I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)



              After checking for the solution in many places, I found a simple solution in Arch Wiki:



              Link to Arch Wiki.



              We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.



              Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:



              ENVID_FS_USAGE=="filesystem|other|crypto", ENVUDISKS_FILESYSTEM_SHARED="1"



              Then we can just install a simple tool like Udiskie for automount.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 23 at 14:11









              Abhijeet BhattacharjeeAbhijeet Bhattacharjee

              111




              111












              • Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

                – cedbeu
                Jan 24 at 3:30

















              • Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

                – cedbeu
                Jan 24 at 3:30
















              Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

              – cedbeu
              Jan 24 at 3:30





              Hi, and welcome here. Thanks for your option. For Udiskie, I actually wanted a full command line option (no Xorg nor any graphical interface available on the system). And… Question is 6.5 years old (ow, gosh!), I guess things changed now :) But anyway, +1 for your first answer, thank you.

              – cedbeu
              Jan 24 at 3:30











              0














              I found a way in doing this by editing /etc/fstab file. A simple entry looks like:



              /dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0



              <file system> <mount point> <type> <options> <dump> <pass>






              share|improve this answer

























              • Mounts in /etc/fstab are only automatically mounted at boot.

                – Graeme
                Jan 4 '15 at 9:52











              • @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

                – Gino
                Jul 21 '17 at 3:05












              • @Gino, Ubuntu has done auto mounting by default for some time now.

                – Graeme
                Jul 21 '17 at 7:53











              • @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

                – Gino
                Jul 21 '17 at 21:49












              • @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

                – Graeme
                Jul 21 '17 at 22:00















              0














              I found a way in doing this by editing /etc/fstab file. A simple entry looks like:



              /dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0



              <file system> <mount point> <type> <options> <dump> <pass>






              share|improve this answer

























              • Mounts in /etc/fstab are only automatically mounted at boot.

                – Graeme
                Jan 4 '15 at 9:52











              • @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

                – Gino
                Jul 21 '17 at 3:05












              • @Gino, Ubuntu has done auto mounting by default for some time now.

                – Graeme
                Jul 21 '17 at 7:53











              • @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

                – Gino
                Jul 21 '17 at 21:49












              • @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

                – Graeme
                Jul 21 '17 at 22:00













              0












              0








              0







              I found a way in doing this by editing /etc/fstab file. A simple entry looks like:



              /dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0



              <file system> <mount point> <type> <options> <dump> <pass>






              share|improve this answer















              I found a way in doing this by editing /etc/fstab file. A simple entry looks like:



              /dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0



              <file system> <mount point> <type> <options> <dump> <pass>







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 2 '14 at 12:12

























              answered Oct 2 '14 at 12:04









              PeterPeter

              1093




              1093












              • Mounts in /etc/fstab are only automatically mounted at boot.

                – Graeme
                Jan 4 '15 at 9:52











              • @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

                – Gino
                Jul 21 '17 at 3:05












              • @Gino, Ubuntu has done auto mounting by default for some time now.

                – Graeme
                Jul 21 '17 at 7:53











              • @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

                – Gino
                Jul 21 '17 at 21:49












              • @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

                – Graeme
                Jul 21 '17 at 22:00

















              • Mounts in /etc/fstab are only automatically mounted at boot.

                – Graeme
                Jan 4 '15 at 9:52











              • @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

                – Gino
                Jul 21 '17 at 3:05












              • @Gino, Ubuntu has done auto mounting by default for some time now.

                – Graeme
                Jul 21 '17 at 7:53











              • @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

                – Gino
                Jul 21 '17 at 21:49












              • @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

                – Graeme
                Jul 21 '17 at 22:00
















              Mounts in /etc/fstab are only automatically mounted at boot.

              – Graeme
              Jan 4 '15 at 9:52





              Mounts in /etc/fstab are only automatically mounted at boot.

              – Graeme
              Jan 4 '15 at 9:52













              @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

              – Gino
              Jul 21 '17 at 3:05






              @Graeme, I have an sdcard that I mount via /etc/fstab. As a test, I tried unmounting and physically removing the sdcard from my computer. I verified that the mount no longer showed via 'df'. When I re-inserted my sdcard, the mount then re-appeared even though I had not explicitly remounted it. This is on Ubuntu 16.04.

              – Gino
              Jul 21 '17 at 3:05














              @Gino, Ubuntu has done auto mounting by default for some time now.

              – Graeme
              Jul 21 '17 at 7:53





              @Gino, Ubuntu has done auto mounting by default for some time now.

              – Graeme
              Jul 21 '17 at 7:53













              @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

              – Gino
              Jul 21 '17 at 21:49






              @Graeme, it looks like the Ubuntu automounter is picking the correct mount from /etc/fstab, if it exists there. So, at least on Ubuntu 16.04, the mount is automatically mounted at boot and after subsequent re-insertions into the usb port, if there are any..

              – Gino
              Jul 21 '17 at 21:49














              @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

              – Graeme
              Jul 21 '17 at 22:00





              @Gino I haven't used Ubuntu for a while, but I'm fairly sure it will automount your sd card regardless of what is in the fstab. Fstab is more about setting up core system elements than additional disks on modern systems.

              – Graeme
              Jul 21 '17 at 22:00











              0














              Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.



              However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or "user session" to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.






              share|improve this answer



























                0














                Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.



                However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or "user session" to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.






                share|improve this answer

























                  0












                  0








                  0







                  Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.



                  However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or "user session" to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.






                  share|improve this answer













                  Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.



                  However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or "user session" to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 15 hours ago









                  MetamorphicMetamorphic

                  356110




                  356110



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f44454%2fhow-to-mount-removable-media-in-media-label-automatically-when-inserted-with%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                      Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                      Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.