11c5ac21333f543680ce306d46f287d62c175d873mridge#!/bin/bash
21c5ac21333f543680ce306d46f287d62c175d873mridge
31c5ac21333f543680ce306d46f287d62c175d873mridge# Library to find CD devices.
437550cf5a86c7ca7424a2a318dd64e550f13f5dfChris Dearman#
51c5ac21333f543680ce306d46f287d62c175d873mridge# Copyright (C) 2003-2006 IBM
637550cf5a86c7ca7424a2a318dd64e550f13f5dfChris Dearman#
71c5ac21333f543680ce306d46f287d62c175d873mridge# This program is free software; you can redistribute it and/or
81c5ac21333f543680ce306d46f287d62c175d873mridge# modify it under the terms of the GNU General Public License as
91c5ac21333f543680ce306d46f287d62c175d873mridge# published by the Free Software Foundation; either version 2 of the
101c5ac21333f543680ce306d46f287d62c175d873mridge# License, or (at your option) any later version.
111c5ac21333f543680ce306d46f287d62c175d873mridge#
121c5ac21333f543680ce306d46f287d62c175d873mridge# This program is distributed in the hope that it will be useful, but
131c5ac21333f543680ce306d46f287d62c175d873mridge# WITHOUT ANY WARRANTY; without even the implied warranty of
141c5ac21333f543680ce306d46f287d62c175d873mridge# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
151c5ac21333f543680ce306d46f287d62c175d873mridge# General Public License for more details.
1637550cf5a86c7ca7424a2a318dd64e550f13f5dfChris Dearman#
171c5ac21333f543680ce306d46f287d62c175d873mridge# You should have received a copy of the GNU General Public License
181c5ac21333f543680ce306d46f287d62c175d873mridge# along with this program; if not, write to the Free Software
191c5ac21333f543680ce306d46f287d62c175d873mridge# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
201c5ac21333f543680ce306d46f287d62c175d873mridge# 02111-1307, USA.
211c5ac21333f543680ce306d46f287d62c175d873mridge
221c5ac21333f543680ce306d46f287d62c175d873mridge
231c5ac21333f543680ce306d46f287d62c175d873mridge# Create a list of potential devices.  Note that this may pick up some non-block
241c5ac21333f543680ce306d46f287d62c175d873mridge# devices; it is assumed that they will be filtered out by find_discs_with_media.
251c5ac21333f543680ce306d46f287d62c175d873mridgefunction find_disc_devices() {
261c5ac21333f543680ce306d46f287d62c175d873mridge	NUM_DEVICES=`/bin/ls $(egrep '(cdr|dvd)' /etc/fstab | awk -F " " '{print $1}') /dev/cdr* /dev/dvd* /dev/cdrom/* /dev/sr* 2> /dev/null | sort | uniq | wc -l`
271c5ac21333f543680ce306d46f287d62c175d873mridge	if [ $NUM_DEVICES -lt 1 ]; then
281c5ac21333f543680ce306d46f287d62c175d873mridge		# No CDs at all?
291c5ac21333f543680ce306d46f287d62c175d873mridge		echo NONE
301c5ac21333f543680ce306d46f287d62c175d873mridge	fi
311c5ac21333f543680ce306d46f287d62c175d873mridge	/bin/ls $(egrep '(cdr|dvd)' /etc/fstab | awk -F " " '{print $1}') /dev/cdr* /dev/dvd* /dev/cdrom/* /dev/sr* 2> /dev/null | sort | uniq
321c5ac21333f543680ce306d46f287d62c175d873mridge}
331c5ac21333f543680ce306d46f287d62c175d873mridge
341c5ac21333f543680ce306d46f287d62c175d873mridge# Try to find a disc with media in it.  Hopefully, $DEFAULT_MOUNT already exists.
351c5ac21333f543680ce306d46f287d62c175d873mridgefunction find_discs_with_media() {
361c5ac21333f543680ce306d46f287d62c175d873mridge	# If the caller doesn't specify a DEFAULT_MOUNT point, specify one.
371c5ac21333f543680ce306d46f287d62c175d873mridge	if [ -z "$DEFAULT_MOUNT" ]; then
381c5ac21333f543680ce306d46f287d62c175d873mridge		DEFAULT_MOUNT=/mnt
391c5ac21333f543680ce306d46f287d62c175d873mridge	fi
401c5ac21333f543680ce306d46f287d62c175d873mridge	POTENTIAL_DEVICES=`find_disc_devices`
411c5ac21333f543680ce306d46f287d62c175d873mridge	# Grab a list of all CD/DVD devices that we can find.
421c5ac21333f543680ce306d46f287d62c175d873mridge	for i in `echo "$POTENTIAL_DEVICES"`
431c5ac21333f543680ce306d46f287d62c175d873mridge	do
441c5ac21333f543680ce306d46f287d62c175d873mridge		# Did we get nothing at all?
451c5ac21333f543680ce306d46f287d62c175d873mridge		if [ "$i" == "NONE" ]; then
461c5ac21333f543680ce306d46f287d62c175d873mridge			echo NONE 0
471c5ac21333f543680ce306d46f287d62c175d873mridge			return
481c5ac21333f543680ce306d46f287d62c175d873mridge		fi
491c5ac21333f543680ce306d46f287d62c175d873mridge
501c5ac21333f543680ce306d46f287d62c175d873mridge		# Is this a link pointing to a device that's in the
511c5ac21333f543680ce306d46f287d62c175d873mridge		# list of potential discs AND isn't in fstab?
521c5ac21333f543680ce306d46f287d62c175d873mridge		# We want to avoid considering /dev entries that are symlinked
531c5ac21333f543680ce306d46f287d62c175d873mridge		# elsewhere ... but we also assume that anything in fstab was
541c5ac21333f543680ce306d46f287d62c175d873mridge		# put there for a reason and ought to be considered anyway.
551c5ac21333f543680ce306d46f287d62c175d873mridge		if [ -L "$i" ]; then
561c5ac21333f543680ce306d46f287d62c175d873mridge			IN_LIST=`echo "$POTENTIAL_DEVICES" | grep "$(readlink $i)" -c`
571c5ac21333f543680ce306d46f287d62c175d873mridge			if [ $IN_LIST -gt 0 ]; then
581c5ac21333f543680ce306d46f287d62c175d873mridge				IN_FSTAB=`grep "^$i[ 	]" /etc/fstab -c`
591c5ac21333f543680ce306d46f287d62c175d873mridge				if [ $IN_FSTAB -eq 0 ]; then
601c5ac21333f543680ce306d46f287d62c175d873mridge					continue;
611c5ac21333f543680ce306d46f287d62c175d873mridge				fi
621c5ac21333f543680ce306d46f287d62c175d873mridge			fi
631c5ac21333f543680ce306d46f287d62c175d873mridge		fi
641c5ac21333f543680ce306d46f287d62c175d873mridge
651c5ac21333f543680ce306d46f287d62c175d873mridge		# Block device?
661c5ac21333f543680ce306d46f287d62c175d873mridge		if [ -b "$i" ]; then
671c5ac21333f543680ce306d46f287d62c175d873mridge			IN_FSTAB=`grep -c "^$i[ 	]" /etc/fstab`
681c5ac21333f543680ce306d46f287d62c175d873mridge			FSTAB_TYPE=`grep "^$i[ 	]" /etc/fstab | awk -F " " '{print $3}'`
691c5ac21333f543680ce306d46f287d62c175d873mridge			if [ $IN_FSTAB -gt 0 -a "$FSTAB_TYPE" != "subfs" ]; then
701c5ac21333f543680ce306d46f287d62c175d873mridge				# This device is listed in fstab and is NOT of
711c5ac21333f543680ce306d46f287d62c175d873mridge				# type "subfs" (SLES9 weirdness); try to mount it.
721c5ac21333f543680ce306d46f287d62c175d873mridge				mount "$i" > /dev/null 2> /dev/null
731c5ac21333f543680ce306d46f287d62c175d873mridge				RESULT=$?
741c5ac21333f543680ce306d46f287d62c175d873mridge
751c5ac21333f543680ce306d46f287d62c175d873mridge				if [ $RESULT -eq 0 ]; then
761c5ac21333f543680ce306d46f287d62c175d873mridge					# Mounted ok!
771c5ac21333f543680ce306d46f287d62c175d873mridge					umount "$i"
781c5ac21333f543680ce306d46f287d62c175d873mridge					echo "$i" 1
791c5ac21333f543680ce306d46f287d62c175d873mridge					continue
801c5ac21333f543680ce306d46f287d62c175d873mridge				fi
811c5ac21333f543680ce306d46f287d62c175d873mridge			fi
821c5ac21333f543680ce306d46f287d62c175d873mridge
831c5ac21333f543680ce306d46f287d62c175d873mridge			# Not in fstab, or the mount failed.
841c5ac21333f543680ce306d46f287d62c175d873mridge			mount "$i" "$DEFAULT_MOUNT" -t auto > /dev/null 2> /dev/null
851c5ac21333f543680ce306d46f287d62c175d873mridge			RESULT=$?
861c5ac21333f543680ce306d46f287d62c175d873mridge
871c5ac21333f543680ce306d46f287d62c175d873mridge			if [ $RESULT -eq 0 ]; then
881c5ac21333f543680ce306d46f287d62c175d873mridge				# Mounted ok once we gave it options.
891c5ac21333f543680ce306d46f287d62c175d873mridge				umount "$i"
901c5ac21333f543680ce306d46f287d62c175d873mridge				echo "$i" 0
911c5ac21333f543680ce306d46f287d62c175d873mridge				continue
921c5ac21333f543680ce306d46f287d62c175d873mridge			fi
931c5ac21333f543680ce306d46f287d62c175d873mridge		fi
941c5ac21333f543680ce306d46f287d62c175d873mridge	done
951c5ac21333f543680ce306d46f287d62c175d873mridge}
96