Udev Automount Problem

This forum is for discussion about general software issues.

Udev Automount Problem

Postby dustovich » Fri Feb 05, 2016 7:26 pm

Hello,
I know that this isn't exactly an archlinuxarm specific issue, but if someone could give me some pointers. I am trying to do get USB automounting to work on my rpi and I am getting some very wierd occurences, and everything looks like it should be. When I plug a usb hard drive/stick in, it will create the directory under /media by label and it will mount the disk, but it for some reason seems to immediately unmount the disk and it does not show up in /etc/mtab, but the directory under /media still exists until I unplug the drive. I dont know where to get some kind of log to see when a drive is unmounted since it does not list in dmesg. My hard drive that I am using has a 1gb swap partition and a ~630GB ext4 partiion, the swap partition mounts and shows as being active in swapon without issue, but the ext4 partition acts as above. Right now I need to manually mount the partition myself to the folder in /media and it works fine after that. I am wondering if there is something missing in my script, or perhaps there is something that is interfering with the auto mounting of the partition.

This is the code I have in my /etc/udev/rules.d/11-media-by-label-auto-mount.rules:
$this->bbcode_second_pass_code('', 'KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add", ENV{ID_FS_TYPE}!="swap", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/usr/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
ACTION=="add", ENV{ID_FS_TYPE}=="swap", RUN+="/usr/bin/swapon /dev/%k"
# Clean up after removal
ACTION=="remove", ENV{ID_FS_TYPE}!="swap", ENV{dir_name}!="", RUN+="/usr/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
ACTION=="remove", ENV{ID_FS_TYPE}=="swap", RUN+="/usr/bin/swapoff /dev/%k"
# Exit
LABEL="media_by_label_auto_mount_end"
')

The only other udev rules that exist are in the raspberrypi.rules as follows:
$this->bbcode_second_pass_code('', 'SUBSYSTEM=="vchiq|input", MODE="0777"
KERNEL=="mouse*|mice|event*", MODE="0777"

# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"

# kodi keyboard use
SUBSYSTEM=="tty", KERNEL=="tty0", GROUP="tty", MODE="0666"
')

Thanks for your assistance again.
dustovich
 
Posts: 10
Joined: Fri Feb 05, 2016 7:10 pm

Re: Udev Automount Problem

Postby moonman » Sat Feb 06, 2016 4:25 am

$this->bbcode_second_pass_code('', 'pacman -S udevil
systemctl enable devmon@root.service
systemctl start devmon@root.service')
Pogoplug V4 | GoFlex Home | Raspberry Pi 4 4GB | CuBox-i4 Pro | ClearFog | BeagleBone Black | Odroid U2 | Odroid C1 | Odroid XU4
-----------------------------------------------------------------------------------------------------------------------
[armv5] Updated U-Boot | [armv5] NAND Rescue System
moonman
Developer
 
Posts: 3387
Joined: Sat Jan 15, 2011 3:36 am

Re: Udev Automount Problem

Postby sambul13 » Sat Feb 06, 2016 1:49 pm

$this->bbcode_second_pass_quote('dustovich', 'R')ight now I need to manually mount the partition myself to the folder in /media and it works fine after that.


Using Udev rules is the most compact way to automount drives, but not always the most convenient. Often we need to mount just one volume out of several existing on a drive. At times we need to remove a thumb drive and forget to unmount it before removal, which may lead to filesystem corruption. Autofs is another automount utility that auto unmounts the mounted volumes not used for some time, so its worse to try. ;)

Another way to unmount all unused disk volumes except root. If can't unmount, run fuser or lsof to see what processes or apps use the drive.

$this->bbcode_second_pass_code('', 'umount /dev/sd**')
Last edited by sambul13 on Sun Feb 07, 2016 5:40 pm, edited 1 time in total.
sambul13
 
Posts: 258
Joined: Sat Aug 18, 2012 10:32 pm

Re: Udev Automount Problem

Postby dustovich » Sat Feb 06, 2016 6:06 pm

Thanks for the suggestions. I could not get the udev rule to work for the life of me, so I ended up trying 'ldm' and 'devmon' which do similar things. I think the issue lies in the fact that the partition needs to be mounted as a specific user, and udev wasn't able to do that for some reason (and I wasn't catching on until I seen the service needing to be run as a specific user '@root'). They both work as advertised mounting the drive to the /media directory and adding/removing the label directory inside, except for the added side-affect of auto-mounting the rootfs in /media also.

I am guessing this may be a special case in the configuration of the rpi-2 image, the rootfs is mounted rw from the kernel command-line and it does not appear in the fstab (only /boot does). So either of these programs will not know that /dev/mmcblk0p2 is already mounted at / (as mtab just lists this as /dev/root). So to solve this double mounting issue I had to add an entry to the fstab for the root partition. Then all is well.

I will now just adjust the above rule to only auto activate the swap partition on the usb drive as that works without issue. No more meddling with fstab.

Thanks again.
dustovich
 
Posts: 10
Joined: Fri Feb 05, 2016 7:10 pm

Automount swap.

Postby dustovich » Sat Feb 06, 2016 6:18 pm

Here is the script if anybody wants it.
/etc/udev/rules.d/60-autoactivate-swap.rules:
$this->bbcode_second_pass_code('', 'KERNEL!="sd[a-z][0-9]", GOTO="swap_auto_activate_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Mount swap partitions
ACTION=="add", ENV{ID_FS_TYPE}=="swap", RUN+="/usr/bin/swapon /dev/%k"
# Clean up after removal
ACTION=="remove", ENV{ID_FS_TYPE}=="swap", RUN+="/usr/bin/swapoff /dev/%k"
# Exit
LABEL="swap_auto_activate_end"
')

I'm guessing unplugging active swap is a pretty bad idea also, as some data can be lost, so the cleanup shouldn't ever be used, but you never know.
The hard drive being used is a permanent fixture, well as permanent as a usb-hdd can be.
dustovich
 
Posts: 10
Joined: Fri Feb 05, 2016 7:10 pm

Re: Udev Automount Problem

Postby sambul13 » Sun Feb 07, 2016 5:37 pm

If you insist on using Udev rules in favor of other methods, bodhi posted some good examples here. May be you want to try those in Arch and let us know how they work for you unless you already used that?
sambul13
 
Posts: 258
Joined: Sat Aug 18, 2012 10:32 pm

Re: Udev Automount Problem

Postby dustovich » Sun Feb 07, 2016 5:50 pm

No I have switched away from udev for mounting the drives to udevil/devmon now and it is done. Those scripts use pmount which would be similar to udevil. I don't know if anybody can elaborate but I am thinking that the original problem was using 'mount' in the udev rules instead of one of the wrappers for it. Cant tell ya, but it did not work.

I based my original udev script on the one at this site:
http://www.axllent.org/docs/view/auto-mounting-usb-storage/
dustovich
 
Posts: 10
Joined: Fri Feb 05, 2016 7:10 pm

Re: Udev Automount Problem

Postby sambul13 » Sun Feb 07, 2016 6:32 pm

Would be nice to have some comparison btw udevil and autofs functionality, if you're interested in the topic. Can't comment on the Udev rules you posted, I guess trial and error is always part of Linux routing. I found generic Arch questions may be faster answered on Archlinux BBS forum, since they have a lot larger audience. Unless you believe Udev rules are specific to ARM.
sambul13
 
Posts: 258
Joined: Sat Aug 18, 2012 10:32 pm

Re: Udev Automount Problem

Postby dustovich » Sun Feb 07, 2016 6:52 pm

You and I both know that udev is not specific to either alarm or even arch in general, and I agree that it would get more views over at the other forum, but now that I have it working good enough I am going to leave it at that. In linux it seems that there are many different ways of doing things that is sure with none being 'wrong'.

I did a quick look at autofs, and from what I have seen the largest difference between it and using an automount daemon like udevil/devmon, ldm or other analogues is that in autofs you need to preconfigure the drives you want in its config files (and will not mount 'mystery' drives) while in the udevil/devmon type it will accept and mount any drive (by label) without configuration. This gives you some flexibility as you could mount specific drives in specific places (via config) rather then just everything going to /media/disk-label.

https://wiki.archlinux.org/index.php/autofs
dustovich
 
Posts: 10
Joined: Fri Feb 05, 2016 7:10 pm

Re: Udev Automount Problem

Postby sambul13 » Sun Feb 07, 2016 7:18 pm

I had a similar issue lately, hence the interest in this topic. Is it possible to bypass certain drives being mounted by udevil? I'm always amazed by a number of volumes popping up at attaching a drive, despite some won't be accessed and better keep unmounted. ;)
Last edited by sambul13 on Sun Feb 07, 2016 7:24 pm, edited 1 time in total.
sambul13
 
Posts: 258
Joined: Sat Aug 18, 2012 10:32 pm

Next

Return to General

Who is online

Users browsing this forum: No registered users and 10 guests