Imported Upstream version 3.1.0
[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 amgetconf() {
32     "${sbindir}/amgetconf" "${@}"
33 }
34 amdevcheck() {
35     "${sbindir}/amdevcheck" "${@}"
36 }
37
38 # This function tries to find a useable mt binary. If a fully-qualified path
39 # was provided at configure time or via a config file, we check that it
40 # exists and is executable. If an incomplete path was specified (e.g., "mt"),
41 # we ask the shell to search the path. Returns 0 on success, 1 on failure.
42
43 try_find_mt() {
44     # Only do this once.
45     if test -n $mt_found; then
46         return 0
47     fi
48
49     if test -z $MT; then
50         MT=mt
51     fi
52
53     if "`echo $MT | dd bs=1 count=1`" = "/"; then
54         if ! test -f "${MT}"; then
55             echo `_ "mt binary at '%s' not found" "$MTX"`
56             return 1
57         fi
58         if ! test -x "${MT}"; then
59             echo `_ "mt binary at '%s' is not executable" "$MTX"`
60             return 1
61         fi
62     else
63         # try running it to see if the shell can find it
64         "$MT" >/dev/null 2>/dev/null
65         if test $? -eq 127 -o $? -eq 126; then
66             echo `_ "Could not run mt binary at '%s'" "$MTX"`
67             return 1
68         fi
69     fi
70
71     mt_found=yes
72     return 0
73 }
74
75 # This function strips the tape: from the front of device names.
76 # Capture its output with ``.
77 tape_device_filename() {
78     if echo "$1"|grep '^tape:' >/dev/null; then
79         echo "$1" | sed 's/^tape://'
80     else
81         if echo "$1"|grep '^/' >/dev/null; then
82             echo "$1"
83         fi
84     fi
85 }
86
87 # Invoke amdevcheck to determine whether the device is ready for use.
88 #
89 # @return 0 if a tape is loaded or error
90 # @return 1 if a tape is tape offline or busy
91 #
92 # @side-effect: $amdevcheck_message is the contents of all MESSAGE lines from
93 #               amdevcheck, suitable for use in higher-level error messages
94 amdevcheck_status() {
95     amdevcheck_message=
96     local amdevcheck_output=`amdevcheck . $@`
97     local amdevcheck_status=$?
98
99     test "$amdevcheck_status" -ne 0 && return 0
100
101     # extract any messages
102     amdevcheck_message=`echo "$amdevcheck_output" | sed -n -e '/^MESSAGE /{' -e 's/^MESSAGE //' -e 'p' -e 'q' -e '}'`
103
104     # Return 1 if it's possible that the device is offline or busy; if the device cannot
105     # distinguish this state from an error condition, then our caller will just have to
106     # time out
107     if echo "$amdevcheck_output" | $EGREP "VOLUME_MISSING|DEVICE_BUSY" > /dev/null; then
108         return 1
109     else
110         return 0
111     fi
112 }
113
114 # This attempts to eject a device using whatever system tools are available.
115 # At the moment, that means mt for tapes, and nothing otherwise, but might
116 # be extended at some later time.
117
118 try_eject_device() {
119     if echo "$1" | grep '^tape:' > /dev/null; then
120         try_eject_device_tape="`echo \"$1\" | cut -b6-`"
121     elif echo "$1" | grep -v : > /dev/null; then
122         try_eject_device_tape="$1"
123     else
124         try_eject_device_tape=
125     fi
126
127     if test -n "$try_eject_device_tape"; then
128         if try_find_mt; then
129             $MT $MTF "$try_eject_device_tape" offline
130         fi
131     else
132         # Technically we failed to eject the device, but we presume that's
133         # because it doesn't require ejection.
134         return 0
135     fi
136 }