Determine optical media type (Audio CD, DVD, blu-ray) by using UDEV and scripts Determine optical media type (Audio CD, DVD, blu-ray) by using UDEV and scripts linux linux

Determine optical media type (Audio CD, DVD, blu-ray) by using UDEV and scripts


The variables are not set anywhere.

Usually this is asetting in udev startup rule ( in /usr/lib/udev/rules.d/ ), and looks somewhat like

# ID_CDROM_MEDIA_BD = Bluray# ID_CDROM_MEDIA_DVD = DVD# ID_CDROM_MEDIA_CD = CDSUBSYSTEM=="scsi", KERNEL=="sr0", ENV{ID_CDROM_MEDIA_BD}=="1", RUN+="/home/user/ripping_script.sh"

Since udev doesn't know about the media-type in advance , this is manually set as a environment variable. But since you want to automatically start a different script on certain conditions, this is not useful.

However, you can determine and set the mediatype variables in the ripping script also:

first install cdtool , it can give you some info on audio CD's (with cdir). sudo apt-get install cdtool

Add this to the beginning of your script:

#!/bin/bash# ripping_script.shCDDVD=`cdir -vd /dev/sr0  2>&1 |grep -q "no_disc" || echo "cd"`if [ $CDDVD ]; then ID_CDROM_MEDIA_CD=1 echo "CD detected" >> $HOME/myscripts/scriptlogs/rip.log  else ID_CDROM_MEDIA_DVD=1          echo "DVD detected" >> $HOME/myscripts/scriptlogs/rip.log fi# ... your rippingscript here 

/dev/sr0 is most likely your cd/dvd.

this only differentiates between audioCD and DVD. I don't have any blu-ray stuff for tests.


Solved!

Udev rule looks like this.

# ID_CDROM_MEDIA_BD = Bluray# ID_CDROM_MEDIA_DVD = DVD# ID_CDROM_MEDIA_CD = CDACTION=="change", SUBSYSTEMS=="scsi", KERNEL=="s[rg][0-9]*",  ATTRS{vendor}=="TSSTcorp", ENV{ID_CDROM}=="?*", MODE="0660", GROUP="optical", RUN+="/usr/local/bin/DiscTypeTest3.sh"

and script to trigger ripping scripts looks like this:

#!/bin/bash# ID_CDROM_MEDIA_BD = Bluray# ID_CDROM_MEDIA_DVD = DVD# ID_CDROM_MEDIA_CD = CDMEDIA=if [ $ID_CDROM_MEDIA_DVD = "1" ]   then   MEDIA=dvd   (   echo "$MEDIA" >> "/var/log/DiscTypeTest.log"   ) &elif [ $ID_CDROM_MEDIA_CD = "1" ]    then    MEDIA=cdrom    (    echo "$MEDIA" >> "/var/log/DiscTypeTest.log"    ) &fi(set -o posix ; set) > "/var/log/DiscTypeTestVariables.log"

This results in the following output to log file when a audio cd is inserted followed by a dvd

cdromcdromdvddvd

Replacing the echo lines with the path to ripping scripts should result in automated headless system as desired

Credit goes to Jim Vanns for code, Keith_Helms and blm_ubunet on ubuntu forums for posix code and corrections to if statements and Ixer here for the variable pointers

Hope this helps

James