• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Library to find CD devices.
4#
5# Copyright (C) 2003-2006 IBM
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20# 02111-1307, USA.
21
22
23# Create a list of potential devices.  Note that this may pick up some non-block
24# devices; it is assumed that they will be filtered out by find_discs_with_media.
25function find_disc_devices() {
26	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`
27	if [ $NUM_DEVICES -lt 1 ]; then
28		# No CDs at all?
29		echo NONE
30	fi
31	/bin/ls $(egrep '(cdr|dvd)' /etc/fstab | awk -F " " '{print $1}') /dev/cdr* /dev/dvd* /dev/cdrom/* /dev/sr* 2> /dev/null | sort | uniq
32}
33
34# Try to find a disc with media in it.  Hopefully, $DEFAULT_MOUNT already exists.
35function find_discs_with_media() {
36	# If the caller doesn't specify a DEFAULT_MOUNT point, specify one.
37	if [ -z "$DEFAULT_MOUNT" ]; then
38		DEFAULT_MOUNT=/mnt
39	fi
40	POTENTIAL_DEVICES=`find_disc_devices`
41	# Grab a list of all CD/DVD devices that we can find.
42	for i in `echo "$POTENTIAL_DEVICES"`
43	do
44		# Did we get nothing at all?
45		if [ "$i" == "NONE" ]; then
46			echo NONE 0
47			return
48		fi
49
50		# Is this a link pointing to a device that's in the
51		# list of potential discs AND isn't in fstab?
52		# We want to avoid considering /dev entries that are symlinked
53		# elsewhere ... but we also assume that anything in fstab was
54		# put there for a reason and ought to be considered anyway.
55		if [ -L "$i" ]; then
56			IN_LIST=`echo "$POTENTIAL_DEVICES" | grep "$(readlink $i)" -c`
57			if [ $IN_LIST -gt 0 ]; then
58				IN_FSTAB=`grep "^$i[ 	]" /etc/fstab -c`
59				if [ $IN_FSTAB -eq 0 ]; then
60					continue;
61				fi
62			fi
63		fi
64
65		# Block device?
66		if [ -b "$i" ]; then
67			IN_FSTAB=`grep -c "^$i[ 	]" /etc/fstab`
68			FSTAB_TYPE=`grep "^$i[ 	]" /etc/fstab | awk -F " " '{print $3}'`
69			if [ $IN_FSTAB -gt 0 -a "$FSTAB_TYPE" != "subfs" ]; then
70				# This device is listed in fstab and is NOT of
71				# type "subfs" (SLES9 weirdness); try to mount it.
72				mount "$i" > /dev/null 2> /dev/null
73				RESULT=$?
74
75				if [ $RESULT -eq 0 ]; then
76					# Mounted ok!
77					umount "$i"
78					echo "$i" 1
79					continue
80				fi
81			fi
82
83			# Not in fstab, or the mount failed.
84			mount "$i" "$DEFAULT_MOUNT" -t auto > /dev/null 2> /dev/null
85			RESULT=$?
86
87			if [ $RESULT -eq 0 ]; then
88				# Mounted ok once we gave it options.
89				umount "$i"
90				echo "$i" 0
91				continue
92			fi
93		fi
94	done
95}
96