6

In particular, how to catch it for the purposes of automounting the DVD filesystem?


Update in response to Oli's answer:

udev seems to be able to detect the insertion of DVD, as demonstrated by changes in udevadm's output before and after the event:

% udevadm info -q env -n /dev/cdrom

which results in additional environment variables as follows:

ID_CDROM_MEDIA=1
ID_CDROM_MEDIA_DVD=1
ID_CDROM_MEDIA_SESSION_COUNT=1
ID_CDROM_MEDIA_STATE=complete
ID_CDROM_MEDIA_TRACK_COUNT=1
ID_CDROM_MEDIA_TRACK_COUNT_DATA=1
ID_FS_LABEL=20130926_Backup
ID_FS_LABEL_ENC=20130926_Backup
ID_FS_TYPE=udf
ID_FS_USAGE=filesystem

However, when I tried the following udev rule (which tries to detect ID_FS_TYPE=="udf"),

SUBSYSTEM=="block", ENV{ID_CDROM}=="?*", ENV{ID_FS_TYPE}=="udf", ENV{ID_PATH}=="pci-0000:00:1f.2-scsi-0:0:0:0", ACTION=="add", RUN+="/bin/mount -t udf -o ro /dev/cdrom /var/run/usbmount/dvdrom"

it doesn't work. What could be the problem?


Solution

Was finally able to solve this thanks to Oli's suggestions:

/etc/udev/rules.d/autodvd.rules:

SUBSYSTEM=="block", ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="pci-0000:00:1f.2-scsi-0:0:0:0", ACTION=="change", RUN+="/usr/local/bin/autodvd"

/usr/local/bin/autodvd:

#!/bin/bash
{
  if [ $ID_CDROM_MEDIA -eq 1 ]; then
    mkdir -p /var/run/usbmount/dvdrom
    mount -t $ID_FS_TYPE -o ro /dev/cdrom /var/run/usbmount/dvdrom
  else
    umount -l /var/run/usbmount/dvdrom
    rm -rf /var/run/usbmount/dvdrom
  fi
} &>> "/var/log/autodvd.log" &

Apparently ACTION=="add" isn't triggered when DVD disc is inserted. So instead, we use ACTION=="change" and then detect the insert or eject event via scripting.

5

UDEV sends out events for CD/DVD drives (I've just tested it with udevadm) so you should be able to either write a UDEV script or write an upstart script like so:

start on block-device-added

task

script
   if [ `$DEV` -eq "/dev/sr0" ]; then
      # do something
   fi
end script

You might have to be careful about checking its mount status. I'm pillaging this from a similar answer of mine which is a bit more explanatory.


When running udevadm monitor --property --udev, here is the output I got when putting in a DVD (--property makes this quite verbose but it lets you know exactly what you're dealing with):

UDEV  [2251414.166711] change   /devices/pci0000:00/0000:00:1c.1/0000:07:00.0/ata17/host16/target16:0:0/16:0:0:0/block/sr0 (block)
ACTION=change
DEVLINKS=/dev/cdrom /dev/cdrw /dev/disk/by-id/ata-Optiarc_DVD_RW_AD-7240S /dev/disk/by-label/UT2004_DVD /dev/disk/by-path/pci-0000:07:00.0-scsi-0:0:0:0 /dev/dvd /dev/dvdrw
DEVNAME=/dev/sr0
DEVPATH=/devices/pci0000:00/0000:00:1c.1/0000:07:00.0/ata17/host16/target16:0:0/16:0:0:0/block/sr0
DEVTYPE=disk
DISK_MEDIA_CHANGE=1
GENERATED=1
ID_ATA=1
ID_ATA_SATA=1
ID_ATA_SATA_SIGNAL_RATE_GEN1=1
ID_BUS=ata
ID_CDROM=1
ID_CDROM_CD=1
ID_CDROM_CD_R=1
ID_CDROM_CD_RW=1
ID_CDROM_DVD=1
ID_CDROM_DVD_PLUS_R=1
ID_CDROM_DVD_PLUS_RW=1
ID_CDROM_DVD_PLUS_R_DL=1
ID_CDROM_DVD_R=1
ID_CDROM_DVD_RAM=1
ID_CDROM_DVD_RW=1
ID_CDROM_MEDIA=1
ID_CDROM_MEDIA_DVD=1
ID_CDROM_MEDIA_SESSION_COUNT=1
ID_CDROM_MEDIA_STATE=complete
ID_CDROM_MEDIA_TRACK_COUNT=1
ID_CDROM_MEDIA_TRACK_COUNT_DATA=1
ID_CDROM_MRW=1
ID_CDROM_MRW_W=1
ID_FS_LABEL=UT2004_DVD
ID_FS_LABEL_ENC=UT2004_DVD
ID_FS_TYPE=iso9660
ID_FS_USAGE=filesystem
ID_FS_VERSION=Joliet\x20Extension
ID_MODEL=Optiarc_DVD_RW_AD-7240S
ID_MODEL_ENC=Optiarc\x20DVD\x20RW\x20AD-7240S\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
ID_PATH=pci-0000:07:00.0-scsi-0:0:0:0
ID_PATH_TAG=pci-0000_07_00_0-scsi-0_0_0_0
ID_REVISION=1.00
ID_SERIAL=Optiarc_DVD_RW_AD-7240S
ID_TYPE=cd
MAJOR=11
MINOR=0
SEQNUM=4400
SUBSYSTEM=block
TAGS=:udev-acl:
UDEV_LOG=3
UDISKS_PRESENTATION_NOPOLICY=0
USEC_INITIALIZED=10393360
  • thanks, though I'd prefer to use a udev rule to launch the script. May I ask how you inferred from udevadm's output that a DVD disc was inserted? – silvernightstar Oct 16 '13 at 14:54
  • @silvernightstar output added. It should be fairly simple to write a little UDEV rule that did something similar. – Oli Oct 16 '13 at 14:59
  • I've updated my question again in response to your update. Could you check it again? Thanks – silvernightstar Oct 16 '13 at 15:22
  • 1
    @silvernightstar at a glance, the action looks incorrect. It was always "change" on my system. I'd also be as least specific as possible at first just to make sure the event is getting through, then wump the restrictions on. – Oli Oct 16 '13 at 16:01
  • That did the trick! Apparently, ACTION=="add" is meaningless for DVD's (in contrast to USB devices). Or maybe it's associated with SATA hotplugging, but anyway that's beyond my use case already. Thanks again! – silvernightstar Oct 16 '13 at 17:53
1

This rule seems to work for me, and should be more generic than one that refers to the device by ID. Also, it should only trigger once per insertion, and not trigger when you eject the disk.

ACTION=="change", KERNEL=="sr[0-9]*", ENV{ID_CDROM_DVD}=="1", ENV{ID_CDROM_MEDIA_STATE}=="complete", ENV{ID_FS_TYPE}=="udf", RUN+="/usr/local/bin/dvd-automount"
ACTION=="change", KERNEL=="sr[0-9]*", ENV{ID_CDROM_DVD}=="1", ENV{ID_CDROM_MEDIA_STATE}=="complete", ENV{ID_FS_TYPE}=="iso9660", RUN+="/usr/local/bin/dvd-automount"

The mount script I use looks like this:

#!/bin/bash

sleep 5

if ! mount | grep -q /dev/sr0; then
  mount /dev/sr0 /media/cdrom0
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.