You are not logged in.

#1 2016-04-23 00:01:36

secretreaction
Member
Registered: 2016-04-22
Posts: 1

mount throwing error when executed by udev?

Okay, I've been working on this for a few days, and I feel like I'm ready to tear my hair out, so I come to you on hands and knees, pleading for your assistance.

What's wrong?

I'm trying to do a two-factor authentication system for unlocking a LUKS partition is my home directory. There's two parts: a password and a keyfile. The password decrypts the keyfile, and the decrypted keyfile is stored in memory and used as the key for the LUKS partition. The encrypted keyfile is stored on a usb flash drive I own, which is only plugged in when I'm physically at the machine.

So, I've got all the other parts done except... well, automatically mounting and unmounting the usb flash drive when inserted/removed. Since I like doing things the hard way, I decided to write udev rules to detect the insertion and removal of the device. Problem is... mount insists it completed successfully, and yet the drive isn't mounted!

Did you do your homework?

I fully updated pacman, I haven't tried to partially upgrade ever, I tried to search both these forums and the bug tracker. I looked at wiki entries that might've given me warnings. I haven't found anything!

What are you working with?

I'm using a USB 3.0 flash drive formatted with ext4. (I was using vfat, but I got kernel messages about bad blocks... apparently USB 3.0 + vfat gives linux the willies.)

I was using this post I found as a skeleton for my own work.
http://superuser.com/questions/53978/au … user-logge

I've written a udev rule for when the usb device is inserted/removed. I already have confirmed that it successfully activates when I insert/remove the device.

ACTION=="add",KERNEL=="sd?1",ENV{ID_BUS}=="usb",ENV{ID_FS_TYPE}=="ext4",ENV{ID_FS_UUID}=="(The UUID of the ext4 partition.)",RUN+="/usr/local/bin/automounter mount-background %k usbdrive"
ACTION=="remove",KERNEL=="sd?1",ENV{ID_BUS}=="usb",ENV{ID_FS_TYPE}=="ext4",ENV{ID_FS_UUID}=="(The UUID of the ext4 partition.)",RUN+="/usr/local/bin/autoumounter umount-background usbdrive"

I wrote two scripts, automounter and autoumounter. All they do is mount the drive to /run/media/usbkey on insert and umount on removal.
(I've already tried doing mount/umount via udev, they don't work either.)

/usr/local/bin/automounter

#!/bin/sh
MODE=$1
DEVICE=$2
NAME=$3

if [ "$MODE" = "mount-background" ]; then

  # Create a background process. udev needs to return quickly from scripts.
  /usr/local/bin/automounter mount "$DEVICE" "$NAME" &

elif [ "$MODE" = "mount" ]; then

  # If /run/media doesn't exist, create it now.
  if [ ! -d "/run/media" ]; then
    /usr/bin/mkdir -m 0755 /run/media
  fi

  # If /run/media/$NAME doesn't exist, create it now and mount.
  if [ ! -d "/run/media/$NAME" ]; then
    /usr/bin/mkdir "/run/media/$NAME"

    # Mount the device read-only, and give it the bare minimum of permissions. Output what's done to our log.
    /usr/bin/mount -v -o ro,noatime,nodev,nosuid,noexec "/dev/$DEVICE" "/run/media/$NAME" >> /var/local/automounter

    # This stuff never gets done. After script execution, the directory is owned by root:root and has mode 0755.
    /usr/bin/chown root:usbkey "/run/media/$NAME"
    /usr/bin/chmod 0775 "/run/media/$NAME"
  else
    /usr/bin/echo "Name $NAME is already mounted."
    exit 1
  fi

else
  /usr/bin/echo "Unknown command."
  exit 1
fi
exit 0

/usr/local/bin/autoumounter

#!/bin/sh
MODE=$1
NAME=$2

if [ "$MODE" = "umount-background" ]; then

  # Create a background process. udev needs to return quickly from scripts.
  /usr/local/bin/autoumounter umount "$NAME" &

elif [ "$MODE" = "umount" ]; then

  # If /run/media/$NAME exists, unmount and remove the directory.
  if [ -d "/run/media" ] && [ -d "/run/media/$NAME" ]; then
    /usr/bin/umount -v "/run/media/$NAME" >> /var/local/automounter

    # After script execution, the $NAME directory is gone, so umount is working.
    /usr/bin/rmdir "/run/media/$NAME"
  else
    /usr/bin/echo "Name $NAME is not mounted."
    exit 1
  fi

else
  /usr/bin/echo "Unknown command."
  exit 1
fi
exit 0
What output are you getting?

State: Before connecting
/run/media/usbkey doesn't exist.
/var/local/automounter doesn't exist.
My dog hasn't eaten EXT4-fs, the usb hub, the kernel, the cpu, the power supply, the hard drive... maybe udev?

State: Connected
Device sdc and ext4 partition sdc1 appear.
/run/media/usbkey exists.
It has owner root:root and permissions 0755. (Automounter should have set it to root:usbkey with 0775.)
There are no files contained within, not even a lost and found. (If it was mounted, it would have one.)
/etc/mtab has no record of the mount.

dmesg

[ 9684.722555] usb 2-1.8: new high-speed USB device number 16 using ehci-pci
[ 9684.884204] usb-storage 2-1.8:1.0: USB Mass Storage device detected
[ 9684.888225] scsi host15: usb-storage 2-1.8:1.0
[ 9685.894369] scsi 15:0:0:0: Direct-Access     Kingston DataTraveler 3.0 PMAP PQ: 0 ANSI: 6
[ 9687.121941] sd 15:0:0:0: [sdc] 60632064 512-byte logical blocks: (31.0 GB/28.9 GiB)
[ 9687.122695] sd 15:0:0:0: [sdc] Write Protect is off
[ 9687.122701] sd 15:0:0:0: [sdc] Mode Sense: 23 00 00 00
[ 9687.123355] sd 15:0:0:0: [sdc] No Caching mode page found
[ 9687.123361] sd 15:0:0:0: [sdc] Assuming drive cache: write through
[ 9687.128550]  sdc: sdc1
[ 9687.131474] sd 15:0:0:0: [sdc] Attached SCSI removable disk
[ 9687.312618] EXT4-fs (sdc1): mounted filesystem with ordered data mode. Opts: (null)

/var/local/automounter (output from mount -v)

mount: /dev/sdc1 mounted on /run/media/usbkey.

State: Removed
/run/media/usbkey doesn't exist.
/var/local/automounter has a blank line appended to it. (output from umount -v)

dmesg

[10729.017080] usb 2-1.8: USB disconnect, device number 16
Anything else to mention?
  1. This is a relatively virgin install. I've been on Arch about 3 weeks, and I was gone for about a week due to family stuff.

  2. I have executed the scripts as sudo, and they work fine. So it's not the scripts that are messing up.

  3. Decryption occurs at user login, so a display manager cannot automatically mount the drive.

  4. Logging in to one user account just to mount a flash drive to log into another user account is crazy slow and annoying.

  5. I'm far more used to writing udev rules than relying on unknown software, especially when that software is just a wrapper around udev anyway.

  6. Throwing away all the work I've done is silly, especially when this information might help others in some form.

Last edited by secretreaction (2016-04-23 00:06:42)

Offline

#2 2016-04-23 01:04:17

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: mount throwing error when executed by udev?

RUN+= is the wrong approach; see man udev for the low down, but essentially the process is likely killed before your script is complete.

Use SYSTEMD_WANTS and a service file that Requires=media.YOURUSB.mount

man systemd.device has the details.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB