Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: littlebat on January 07, 2012, 05:15:59 AM

Title: A simple script for mounting cdrom disc automatically when insert disc
Post by: littlebat on January 07, 2012, 05:15:59 AM
Code: (bash) [Select]
#!/bin/sh
# amountcd.sh
# A simple script for mounting cdrom disc automatically when insert disc.
# It poll cdrom every 2 seconds just like "HAL" and "Udisks" do.
# But it can't take advantage of newer SATA ATAPI hardware which
# supports Asynchronous Notification(AN), see: "man hal-disable-polling".
# Usage: copy it as "/usr/local/bin/amountcd.sh", and
# "chmod +x /usr/local/bin/amountcd.sh", add it into "/opt/bootlocal.sh"
# or some where else, for example: "/usr/local/bin/amountcd.sh &"
# I have tested it on tinycore-3.7.1 and tinycore-4.1 ok.

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

# Read a list of CDROM/DVD Drives
CDROMS=""
CDROMSF=/etc/sysconfig/cdroms
[ -s "$CDROMSF" ] &&  read CDROMS < "$CDROMSF"

for cdrom in $CDROMS
do
  FSTYPE="$(fstype $cdrom)"
  devname=`echo $cdrom | sed "s@/dev/@@"`
  if [ -z $FSTYPE ]
  then
    eval sta_$devname="0"
  else
    eval sta_$devname="1"
  fi
  #eval echo "first sta_$devname=\${sta_$devname}"
done

while [ 1 ]
do
  sleep 2s
  #echo "******************"
  for cdrom in $CDROMS
  do
    FSTYPE="$(fstype $cdrom)"
    devname=`echo $cdrom | sed "s@/dev/@@"`
    if [ -z $FSTYPE ]
    then
      eval stb_$devname="0"
    else
      eval stb_$devname="1"
    fi
    #eval echo sta_$devname=\${sta_$devname}
    #eval echo stb_$devname=\${stb_$devname}
    eval sta=\${sta_$devname}
    eval stb=\${stb_$devname}
    if [[ "$sta" == "0" -a "$stb" == "1" ]]
    then
      mount $cdrom
      #echo "mount $cdrom ok"
    fi
    eval sta_$devname=\${stb_$devname}
  done
done

Todo: It can't detect audio CD inserting and mount it with "-t cdfs", any idea?
Title: Re: A simple script for mounting cdrom disc automatically when insert disc
Post by: Rich on January 07, 2012, 08:21:44 AM
Hi littlebat
Quote
It can't detect audio CD inserting and mount it with "-t cdfs", any idea?
Code: [Select]
/bin/dd if=/dev/cdrom of=cdpresent count=1If there is no disc in the drive, no file is created.
If there is an audio disc in the drive, an empty file is created.
If there is a data disc in the drive, a 512 byte file is created.
Or you can set of=/dev/null and parse the output of /bin/dd.
Title: Re: A simple script for mounting cdrom disc automatically when insert disc
Post by: gerald_clark on January 07, 2012, 09:31:32 AM
Try 'blkid /dev/sr0'
Title: Re: A simple script for mounting cdrom disc automatically when insert disc
Post by: Rich on January 07, 2012, 09:49:08 AM
Hi gerald_clark
He is also trying to detect if the disc is an audio CD. I tried that command and it doesn't return
anything for an audio CD.
Title: Re: A simple script for mounting cdrom disc automatically when insert disc
Post by: gerald_clark on January 07, 2012, 10:01:10 AM
You can't mount audio CDs, and I am not aware of any CDfs for core.
Title: Re: A simple script for mounting cdrom disc automatically when insert disc
Post by: Rich on January 07, 2012, 11:12:17 AM
Hi gerald_clark
Quite correct. I was just offering a possible solution to detecting an audio CD, which he ask about.
In order for him to make use of it, he will of course need to create a  cdfs  extension.

@littlebat: Although I have not tried it, my solution will probably flag a blank disc as being an audio CD.