6a83095028865f58b16439b1bac56f0d88c5a0f0
[debian/amanda] / changer-src / chg-lib.sh.in
1 ################################################################################
2 # You may want to customize these values
3 ################################################################################
4
5 # These are the defaults discovered by configure when Amanda was installed.
6 # They can be overridden here, or by by 'mt_binary' and 'mtx_binary', 
7 # respectively, in the changerfile (currently only for chg-zd-mtx.sh and
8 # chg-manual.sh).
9
10 MT=@MT@
11 MTX=@MTX@
12
13 # This is the flag used to specify a device filename to 'mt', usually '-f'
14 MTF=@MT_FILE_FLAG@
15
16 ################################################################################
17 # No user-level customization should be required beyond this point.
18 ################################################################################
19
20 prefix="@prefix@"
21 exec_prefix="@exec_prefix@"
22 sbindir="@sbindir@"
23 libexecdir="@libexecdir@"
24 amlibexecdir="@amlibexecdir@"
25 . "${amlibexecdir}/amanda-sh-lib.sh"
26
27 ####
28 # Eponymous functions to access various amanda apps
29 # TODO: move to amanda-sh-lib.sh
30
31 if test "@USE_VERSION_SUFFIXES@" = "yes"; then
32     amgetconf() {
33         "${sbindir}/amgetconf-@VERSION@" "${@}"
34     }
35     amdevcheck() {
36         "${sbindir}/amdevcheck-@VERSION@" "${@}"
37     }
38 else
39     amgetconf() {
40         "${sbindir}/amgetconf" "${@}"
41     }
42     amdevcheck() {
43         "${sbindir}/amdevcheck" "${@}"
44     }
45 fi
46
47 # This function tries to find a useable mt binary. If a fully-qualified path
48 # was provided at configure time or via a config file, we check that it
49 # exists and is executable. If an incomplete path was specified (e.g., "mt"),
50 # we ask the shell to search the path. Returns 0 on success, 1 on failure.
51
52 try_find_mt() {
53     # Only do this once.
54     if test -n $mt_found; then
55         return 0
56     fi
57
58     if test -z $MT; then
59         MT=mt
60     fi
61
62     if "`echo $MT | dd bs=1 count=1`" = "/"; then
63         if ! test -f "${MT}"; then
64             echo `_ "mt binary at '%s' not found" "$MTX"`
65             return 1
66         fi
67         if ! test -x "${MT}"; then
68             echo `_ "mt binary at '%s' is not executable" "$MTX"`
69             return 1
70         fi
71     else
72         # try running it to see if the shell can find it
73         "$MT" >/dev/null 2>/dev/null
74         if test $? -eq 127 -o $? -eq 126; then
75             echo `_ "Could not run mt binary at '%s'" "$MTX"`
76             return 1
77         fi
78     fi
79
80     mt_found=yes
81     return 0
82 }
83
84 # This function strips the tape: from the front of device names.
85 # Capture its output with ``.
86 tape_device_filename() {
87     if echo "$1"|grep '^tape:' >/dev/null; then
88         echo "$1" | sed 's/^tape://'
89     else
90         if echo "$1"|grep '^/' >/dev/null; then
91             echo "$1"
92         fi
93     fi
94 }
95
96 # Invoke amdevcheck to determine whether the device is ready for use.
97 #
98 # @return 0 if a tape is loaded or error
99 # @return 1 if a tape is tape offline or busy
100 #
101 # @side-effect: $amdevcheck_message is the contents of all MESSAGE lines from
102 #               amdevcheck, suitable for use in higher-level error messages
103 amdevcheck_status() {
104     amdevcheck_message=
105     local amdevcheck_config_dir=`pwd`
106     local amdevcheck_config_name=`basename $amdevcheck_config_dir`
107     local amdevcheck_output=`amdevcheck $amdevcheck_config_name $@`
108     local amdevcheck_status=$?
109
110     test "$amdevcheck_status" -ne 0 && return 0
111
112     # extract any messages
113     amdevcheck_message=`echo "$amdevcheck_output" | sed -n -e '/^MESSAGE /{' -e 's/^MESSAGE //' -e 'p' -e 'q' -e '}'`
114
115     # Return 1 if it's possible that the device is offline or busy; if the device cannot
116     # distinguish this state from an error condition, then our caller will just have to
117     # time out
118     if echo "$amdevcheck_output" | $EGREP "VOLUME_MISSING|DEVICE_BUSY" > /dev/null; then
119         return 1
120     else
121         return 0
122     fi
123 }
124
125 # This attempts to eject a device using whatever system tools are available.
126 # At the moment, that means mt for tapes, and nothing otherwise, but might
127 # be extended at some later time.
128
129 try_eject_device() {
130     if echo "$1" | grep '^tape:' > /dev/null; then
131         try_eject_device_tape="`echo \"$1\" | cut -b6-`"
132     elif echo "$1" | grep -v : > /dev/null; then
133         try_eject_device_tape="$1"
134     else
135         try_eject_device_tape=
136     fi
137
138     if test -n "$try_eject_device_tape"; then
139         if try_find_mt; then
140             $MT $MTF "$try_eject_device_tape" offline
141         fi
142     else
143         # Technically we failed to eject the device, but we presume that's
144         # because it doesn't require ejection.
145         return 0
146     fi
147 }