WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: A simple script for mounting cdrom disc automatically when insert disc  (Read 6010 times)

Offline littlebat

  • Newbie
  • *
  • Posts: 25
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?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
Re: A simple script for mounting cdrom disc automatically when insert disc
« Reply #1 on: January 07, 2012, 11: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.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: A simple script for mounting cdrom disc automatically when insert disc
« Reply #2 on: January 07, 2012, 12:31:32 PM »
Try 'blkid /dev/sr0'

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
Re: A simple script for mounting cdrom disc automatically when insert disc
« Reply #3 on: January 07, 2012, 12:49:08 PM »
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.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: A simple script for mounting cdrom disc automatically when insert disc
« Reply #4 on: January 07, 2012, 01:01:10 PM »
You can't mount audio CDs, and I am not aware of any CDfs for core.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
Re: A simple script for mounting cdrom disc automatically when insert disc
« Reply #5 on: January 07, 2012, 02:12:17 PM »
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.