Welcome! Log In Create A New Profile

Advanced

Automount USB drives with udev rules using disk label

Posted by bodhi 
Automount USB drives with udev rules using disk label
October 18, 2015 10:11PM
The following udev rules are for automounting (hotplug) USB drives with the disk partition label. If you know what udev rules are and wish to use udev to automount USB drives, then this is for you. If you are not sure about udev rules, then I'd suggest looking into usbmount, autofs, ... to install one of these Debian packages to handle the automounting.

1. udev rules

Create the automount rules file (or edit your current rules) /etc/udev/rules.d/70-automount.rules. Copy and paste the following rules below.

# NOTE: 
#       1. use pmount --sync if drive label ends with Sync/SYNC/sync in add action to allow removal without corruption
#       2. avoid mounting EFI partition for a HFS+ drive

KERNEL!="sd*", GOTO="media_label_end"
ENV{ID_TYPE}!="disk", GOTO="media_label_end"

IMPORT{program}="/sbin/blkid -o udev -p %N", ENV{name}="%E{ID_FS_LABEL}"
ENV{ID_FS_TYPE}=="", GOTO="media_label_end"

ENV{ID_FS_LABEL}!="EFI", GOTO="continue"
ENV{ID_PART_TABLE_TYPE}=="gpt", GOTO="media_label_end"

LABEL="continue"

ENV{name}=="", ENV{name}="%k"

KERNEL=="sd*", SYMLINK+="usb-storage/%E{name}"

ENV{name}=="*SYNC", ENV{mode}="--sync"
ENV{name}=="*Sync", ENV{mode}="--sync"
ENV{name}=="*sync", ENV{mode}="--sync"
ENV{name}!="*SYNC", ENV{mode}=""

ACTION=="add", RUN+="/usr/bin/pmount -t %E{ID_FS_TYPE} %E{mode} %E{mode2} --noatime --umask 000 %k %E{name}"
ACTION=="add", RUN+="%E{mode3}"
ACTION=="remove", RUN+="/usr/bin/pumount --yes-I-really-want-lazy-unmount %E{name}"

LABEL="media_label_end"


2. Aumounting USB drives during system startup

A long time ago, we've found that udevs rules for automounting USB drives were not enough. During system startup, the rules were not triggered, so the USB drives were not aumounted (except for the rootfs, which is always mounted by the kernel). Perhaps with recent udev versions there might be a better way than using a script.

The script below is to help automounting all USB drives that were not mounted by udev rules during system startup. This script should be executed in /etc/rc.local (preferably at the beginning).

usb-storage.pmount
#!/bin/bash
 
logger -s -i "Pmounting all unmounted USB drives"

PMOUNT=`which pmount`
if [ -z "$PMOUNT" ]
then
  logger -s -i  "Error: $0 cant find pmount!" 1>&2
 exit 1
fi

ROOT="/dev/usb-storage"
DEV_LIST=`ls -1 $ROOT`

logger -s -i "Mounting all unmounted USB disks: $DEV_LIST (skip rootfs)"

for DEV in $DEV_LIST; do
   
   if [ "$DEV" != "rootfs" ]; then
   
      if  ! /bin/mountpoint /media/$DEV -q ; then
         $PMOUNT --noatime --umask 000 --read-write $ROOT/$DEV $DEV
         
      fi
   fi

done

logger -s -i "Done Pmounting USB drives"

exit 0

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner



Edited 4 time(s). Last edit at 06/05/2016 04:35PM by bodhi.
Re: Automount USB drives with udev rules using disk label
October 28, 2015 05:03AM
Bodhi,
I noticed that one of the added capabilities of the 4.0.0 kernel for the Oxnas OX820 devises shows "1. Automount (AUTOFS), pNFS, XFS". Does this kernel automatically locate and associate the rootfs, rendering this rule unnecessary for kernal 4.0.0 and above? Or am I just not quite understanding the Automount capability of 4.0.0?
Re: Automount USB drives with udev rules using disk label
October 28, 2015 11:16AM
cbalde04,

Autofs is a convenient utility to automount NFS, USB, HDD, ... file systems, where you will set it up to automount certain file systems that you want. udev rules is a lower level kind of device handling. If you dont want to deal with udev, then use autofs.

Or you can also use both. For example, if you use these rules here, then don't use autofs to automount USB drives.

For automount NFS, autofs is pretty convenient. I use it so that the shares are mounted when I start typing the names at the command line. And they time out and disappear after a period of time.

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner
Bodhi,
It seems the latest 4.1 kernel breaks the udev rules. Would you please confirm? It works great for 3.17 kernel. After upgraded to 4.1 kernel automount script with udev no longer work.
I forgot to mention I am using Pogo Pro with Debian.
Re: Automount USB drives with udev rules using disk label
December 19, 2015 02:05PM
Thang,

Upgrade should not break any of the custom rules that you have in the system. With the exception that sometime the new version could change the rules, e.g. persistent network rules are usually affected by kernel and package upgrade.

Is /etc/udev/rules.d/70-automount.rules still there, the content is the same? is there any other automount rules file that were created in this directory after the upgrade ?

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner
The 70-automount.rules still remain the same. I have tried to recopy and paste it. 70-persistent-net.rules is the auto generated one after each reboot. A total of two files.

This is the content of the 70-persistent-net.rules:
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# Unknown net device (/devices/platform/40400000.ethernet/net/eth0) (stmmaceth)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:25:31:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x1814:/sys/devices/platform/47c00000.pcie-controller/pci0000:00/0000:00:00.0 (rt2800pci)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="48:5d:60:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

I ran udevadm monitor and see that the trigger command is working but it just won't mount. I am mounting FAT32 at the moment. FAT32 did not work with kernel 3.17. I think someone forgot to include the module. It didnot even mount manually. I can manually mount with 4.1 but not working with udev and udev script.

lsblk -o name,size,type,fstype,label,mountpoint
NAME SIZE TYPE FSTYPE LABEL MOUNTPOINT
sda 1.9G disk
`-sda1 1.9G part vfat USBFAT32
sdb 14.5G disk
`-sdb1 14.5G part ext3 rootfs /
mtdblock0 14M disk
mtdblock1 114M disk
Re: Automount USB drives with udev rules using disk label
December 19, 2015 02:39PM
Ah, I remember discussing a similar problem here with another forum member, and it might be it. We'll need to mount the USB drives using label in rc.local in some case. It's been a long time so I can't remember the details. The solution was to create a shell script and mount all USB drives with label when the system is running rc.local.

But first, slightly off topic, your persistent network rules should be commented out (everything). This is to avoid future problem in using the rootfs on anther box.

# Unknown net device (/devices/platform/40400000.ethernet/net/eth0) (stmmaceth) 
###SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:25:31:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" 

# PCI device 0x1814:/sys/devices/platform/47c00000.pcie-controller/pci0000:00/0000:00:00.0 (rt2800pci) 
###SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="48:5d:60:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner
Bodhi,
This is the thread you are referring to?
http://forum.doozan.com/read.php?2,6410,6417#msg-6417
It is quite old. Not sure if it is still valid.

The persistent network rule regenerate after each reboot even when I comment it out. I will make note to delete this file when backing up rootfs.
Nope, that is not the thread. I applied the script and no boot. :-P Good thing for usb rootfs!!!
Re: Automount USB drives with udev rules using disk label
December 19, 2015 04:29PM
Bodhi,
The new code work perfectly. Thank you so much!!! At one point, I decided to go back to 3.17 and reload everything from the 3.17 rootfs. I couldn't not get udev working again as work prior to kernel upgrade. I consider the issue is an intermittent. I am very appreciate your continuing support for the community!
Re: Automount USB drives with udev rules using disk label
June 05, 2016 12:32PM
I placed the first script where it should be. The results:
I logged into xfce but did not find any mounted media.
I clicked on "media" and "mnt;" nothing.

I placed the second script where it should be by simply coping and pasting the entire thing. The results:
The pogo started but without LED.
I removed it and the Pogo starts with LED.

Issuing "mount" gives:
root@debian:/media# mount  /media
mount: can't find /media in /etc/fstab

After removing the second script, and issued mount, I get:

root@debian:/media# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,relatime,size=10240k,nr_inodes=30447,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=25092k,mode=755)
/dev/sda1 on / type ext2 (rw,noatime,errors=remount-ro,user_xattr,acl)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=50160k)
tmpfs on /tmp type tmpfs (rw,relatime)
rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw,relatime)

But even by this time, the drive, sda1 was not mounted in the file manager, even after issuing this:

root@debian:/media# mount /dev/sda1
mount: /dev/sda1 is already mounted or / busy
       /dev/sda1 is already mounted on /

All this was done using a USB stick, formatted in ext4 and plugged into the front usb port of the pogo.

I'm hoping to simply be able to plug in drives and have them automount and ready to serve files.

Thanks, as always, for your kind help.
Re: Automount USB drives with udev rules using disk label
June 05, 2016 03:09PM
notoneofmyseeds,

Note that I said in the other thread that there are many ways to set up USB automounting in Linux.

The hardest way to set up is using udev rules (sometime you want to control the automounting at a lower level). That's why I posted the instruction here. However, the easier way to set up is using usbmount or autofs.

Quote

~# apt-cache show usbmount
Package: usbmount
Version: 0.0.22
Installed-Size: 116
Maintainer: Rogério Brito <rbrito@ime.usp.br>
Architecture: all
Depends: lockfile-progs, udev, util-linux (>> 2.16)
Recommends: pmount
Description-en: automatically mount and unmount USB mass storage devices
This package automatically mounts USB mass storage devices (typically
USB pens) when they are plugged in, and unmounts them when they are
removed. The mountpoints (/media/usb[0-7] by default), filesystem types
to consider, and mount options are configurable. When multiple devices
are plugged in, the first available mountpoint is automatically
selected. If the device provides a model name, a symbolic link
/var/run/usbmount/MODELNAME pointing to the mountpoint is automatically
created.
.
The script that does the mounting is called by the udev daemon.
Therefore, USBmount requires a 2.6 (or newer) Linux kernel.
.
Firewire devices are also supported by USBmount.
.
USBmount is intended as a lightweight solution which is independent of
a desktop environment. Users which would like an icon to appear when an
USB device is plugged in should use the pmount and hal packages
instead.
Description-md5: 665bf065788bff1ada4b796e26321438
Homepage: http://usbmount.alioth.debian.org/
Tag: admin::filesystem, hardware::storage, hardware::usb, interface::daemon,
role::program, scope::utility, use::configuring
Section: admin
Priority: extra
Filename: pool/main/u/usbmount/usbmount_0.0.22_all.deb
Size: 17666
MD5sum: 1d62c98011ac6452d1e9b3be7b56b272
SHA1: 9a4d4d88ac7f609a27db15235bed605f3f6dadc1
SHA256: 764462e65a544fc6a03ca4f90157898a4f289c3f3bea866c5b739ff9184d6c93

Package: usbmount
Status: deinstall ok config-files
Priority: extra
Section: admin
Installed-Size: 112
Maintainer: Rogério Brito <rbrito@ime.usp.br>
Architecture: all
Version: 0.0.21
Config-Version: 0.0.21
Depends: udev, lockfile-progs, util-linux (>> 2.16)
Recommends: pmount
Conffiles:
/etc/usbmount/umount.d/00_remove_model_symlink 06304855418cfe29f6cec46042bdd857
/etc/usbmount/mount.d/00_create_model_symlink 13ef81167c202627ca43b1fe6c7cd0bf
/etc/usbmount/usbmount.conf a78d1be4f28c20bdb0b88b23cc01cf5d
Description-en: automatically mount and unmount USB mass storage devices
This package automatically mounts USB mass storage devices (typically
USB pens) when they are plugged in, and unmounts them when they are
removed. The mountpoints (/media/usb[0-7] by default), filesystem types
to consider, and mount options are configurable. When multiple devices
are plugged in, the first available mountpoint is automatically
selected. If the device provides a model name, a symbolic link
/var/run/usbmount/MODELNAME pointing to the mountpoint is automatically
created.
.
The script that does the mounting is called by the udev daemon.
Therefore, USBmount requires a 2.6 (or newer) Linux kernel.
.
Firewire devices are also supported by USBmount.
.
USBmount is intended as a lightweight solution which is independent of
a desktop environment. Users which would like an icon to appear when an
USB device is plugged in should use the pmount and hal packages
instead.
Description-md5: 665bf065788bff1ada4b796e26321438
Homepage: http://usbmount.alioth.debian.org/

To install
apt-get install usbmount

I'd recommend trying the ready made solution usbmount first. udev rules takes a lot of time to troubleshoot when it does not work like you expect.

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner



Edited 1 time(s). Last edit at 06/05/2016 04:38PM by bodhi.
Re: Automount USB drives with udev rules using disk label
June 08, 2016 03:46PM
bodhi Wrote:
-------------------------------------------------------
> notoneofmyseeds,
>
> Note that I said in the other thread that there
> are many ways to set up USB automounting in Linux.
>
>
> The hardest way to set up is using udev rules
> (sometime you want to control the automounting at
> a lower level). That's why I posted the
> instruction here. However, the easier way to set
> up is using usbmount or autofs.
>
>
Quote

> ~# apt-cache show usbmount
> Package: usbmount
> Version: 0.0.22
> Installed-Size: 116
> Maintainer: Rogério Brito <rbrito@ime.usp.br>
> Architecture: all
> Depends: lockfile-progs, udev, util-linux (>>
> 2.16)
> Recommends: pmount
> Description-en: automatically mount and unmount
> USB mass storage devices
> This package automatically mounts USB mass
> storage devices (typically
> USB pens) when they are plugged in, and unmounts
> them when they are
> removed. The mountpoints (/media/usb[0-7] by
> default), filesystem types
> to consider, and mount options are configurable.
> When multiple devices
> are plugged in, the first available mountpoint is
> automatically
> selected. If the device provides a model name, a
> symbolic link
> /var/run/usbmount/MODELNAME pointing to the
> mountpoint is automatically
> created.
> .
> The script that does the mounting is called by
> the udev daemon.
> Therefore, USBmount requires a 2.6 (or newer)
> Linux kernel.
> .
> Firewire devices are also supported by USBmount.
> .
> USBmount is intended as a lightweight solution
> which is independent of
> a desktop environment. Users which would like an
> icon to appear when an
> USB device is plugged in should use the pmount
> and hal packages
> instead.
> Description-md5: 665bf065788bff1ada4b796e26321438
> Homepage: http://usbmount.alioth.debian.org/
> Tag: admin::filesystem, hardware::storage,
> hardware::usb, interface::daemon,
> role::program, scope::utility, use::configuring
> Section: admin
> Priority: extra
> Filename:
> pool/main/u/usbmount/usbmount_0.0.22_all.deb
> Size: 17666
> MD5sum: 1d62c98011ac6452d1e9b3be7b56b272
> SHA1: 9a4d4d88ac7f609a27db15235bed605f3f6dadc1
> SHA256:
> 764462e65a544fc6a03ca4f90157898a4f289c3f3bea866c5b
> 739ff9184d6c93
>
> Package: usbmount
> Status: deinstall ok config-files
> Priority: extra
> Section: admin
> Installed-Size: 112
> Maintainer: Rogério Brito <rbrito@ime.usp.br>
> Architecture: all
> Version: 0.0.21
> Config-Version: 0.0.21
> Depends: udev, lockfile-progs, util-linux (>>
> 2.16)
> Recommends: pmount
> Conffiles:
> /etc/usbmount/umount.d/00_remove_model_symlink
> 06304855418cfe29f6cec46042bdd857
> /etc/usbmount/mount.d/00_create_model_symlink
> 13ef81167c202627ca43b1fe6c7cd0bf
> /etc/usbmount/usbmount.conf
> a78d1be4f28c20bdb0b88b23cc01cf5d
> Description-en: automatically mount and unmount
> USB mass storage devices
> This package automatically mounts USB mass
> storage devices (typically
> USB pens) when they are plugged in, and unmounts
> them when they are
> removed. The mountpoints (/media/usb[0-7] by
> default), filesystem types
> to consider, and mount options are configurable.
> When multiple devices
> are plugged in, the first available mountpoint is
> automatically
> selected. If the device provides a model name, a
> symbolic link
> /var/run/usbmount/MODELNAME pointing to the
> mountpoint is automatically
> created.
> .
> The script that does the mounting is called by
> the udev daemon.
> Therefore, USBmount requires a 2.6 (or newer)
> Linux kernel.
> .
> Firewire devices are also supported by USBmount.
> .
> USBmount is intended as a lightweight solution
> which is independent of
> a desktop environment. Users which would like an
> icon to appear when an
> USB device is plugged in should use the pmount
> and hal packages
> instead.
> Description-md5: 665bf065788bff1ada4b796e26321438
> Homepage:
> http://usbmount.alioth.debian.org/
>
> To install
>
> apt-get install usbmount
>
>
> I'd recommend trying the ready made solution
> usbmount first. udev rules takes a lot of time to
> troubleshoot when it does not work like you
> expect.

Thanks a lot bohdi, that solved the problem after a restart and walking away from the project some few days.
Now a ext4 formatted USB auto, yes, automounted. I cd to it, made a directory, checked and it's all good.
I came to desktop installed, xfce, via Remmina, and did the same. All was good. It's mounted to USB0. That's fine.
I hope once I move to setting up drives permanently, no problems.
And so thanks a lot. The automounting issue is gone, for now.
Now I move to configuration. Samba is not behaving. But I will now post on the relevant thread.
Re: Automount USB drives with udev rules using disk label
August 31, 2017 09:45PM
bodhi Wrote:
-------------------------------------------------------
> The following udev rules are for automounting (hot
> plug) USB drives with the disk partition label. If
> you know what udev rules are and wish to use udev
> to automount USB drives, then this is for you. [co
> lor=#3333FF]If you are not sure about udev rules,
> then I'd suggest looking into usbmount,
> autofs
,[/color] ... to install one of these De
> bian packages to handle the automounting.

I found an alternative approach that uses shell scripts triggered from minimal udev rules. For someone who doesn't know much about udev, isn't this more manageable?

You can quickly find all the scripts mentioned in the above post here.


bodhi Wrote:
-------------------------------------------------------
> A long time ago, we've found that udevs rules for automounting USB drives were
> not enough. During system startup, the rules were not triggered, so the USB
> drives were not aumounted (except for the rootfs, which is always mounted by the
> kernel). Perhaps with recent udev versions there might be a better way than
> using a script.

Do you think this is still an issue?
Re: Automount USB drives with udev rules using disk label
August 31, 2017 11:01PM
haridsv ,

I actually discouraged people from using udev rules. It just that it has the least footprint on a system, so it might be useful to somebody. I think most people should use Debian packages.

Quote

If you are not sure about udev rules, then I'd suggest looking into usbmount, autofs, ... to install one of these Debian packages to handle the automounting.

> During system startup, the rules wer
> e not triggered, so the USB
> > drives were not aumounted (except for the rootfs
> , which is always mounted by the
> > kernel). Perhaps with recent udev versions there
> might be a better way than
> > using a script.
>
> Do you think this is still an issue?

I don't believe it is still an issue anymore in the latest kernel linux-4.12.8-kirkwood-tld-1. You could try the udev rules without the usb-storage.pmount script and let me know!

-bodhi
===========================
Wiki
latest Kirkwood kernel builds and rootfs
latest u-boot-kirkwood builds
latest Oxnas kernel builds and rootfs
latest u-boot-oxnas builds
latest MVEBU Armada kernel builds and rootfs
U-Boot & Kernel Booting process
bodhi's u-boot GitHub
bodhi's corner



Edited 1 time(s). Last edit at 08/31/2017 11:03PM by bodhi.
Re: Automount USB drives with udev rules using disk label
August 31, 2017 11:08PM
bodhi Wrote:
-------------------------------------------------------
> I actually discouraged people from using udev rule
> s. It just that it has the least footprint on a sy
> stem, so it might be useful to somebody.

Got it! I experimented with the scripts and it worked, except that I had to remove locale option or I was getting this error:

[137246.680981] ntfs: driver 2.1.32 [Flags: R/W MODULE].
[137246.687304] ntfs: (device sdb1): parse_options(): Unrecognized mount option locale

bodhi Wrote:
-------------------------------------------------------
> I don't believe it is still an issue anymore in th
> e latest kernel linux-4.12.8-kirkwood-tld-1. You c
> ould try the udev rules without the usb-storage.pm
> ount script and let me know!

I haven't rebooted yet, but I will check and report.
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: