3 # Bacula interface to virtual autoloader using disk storage
5 # Written by Kern Sibbald
7 # Bacula(R) - The Network Backup Solution
9 # Copyright (C) 2000-2016 Kern Sibbald
11 # The original author of Bacula is Kern Sibbald, with contributions
12 # from many others, a complete list can be found in the file AUTHORS.
14 # You may use this file and others of this release according to the
15 # license defined in the LICENSE file, which includes the Affero General
16 # Public License, v3.0 ("AGPLv3") and some additional permissions and
17 # terms pursuant to its AGPLv3 Section 7.
19 # This notice must be preserved when any source code is
20 # conveyed and/or propagated.
22 # Bacula(R) is a registered trademark of Kern Sibbald.
23 # If you set in your Device resource
25 # Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
26 # you will have the following input to this script:
28 # So Bacula will always call with all the following arguments, even though
29 # in come cases, not all are used. Note, the Volume name is not always
32 # disk-changer "changer-device" "command" "slot" "archive-device" "drive-index" "volume"
35 # By default the autochanger has 10 Volumes and 1 Drive.
37 # Note: For this script to work, you *must" specify
39 # in each of the Devices associated with your AutoChanger resource.
41 # changer-device is the name of a file that overrides the default
42 # volumes and drives. It may have:
43 # maxslot=n where n is one based (default 10)
44 # maxdrive=m where m is zero based (default 1 -- i.e. 2 drives)
46 # This code can also simulate barcodes. You simply put
47 # a list of the slots and barcodes in the "base" directory/barcodes.
48 # See below for the base directory definition. Example of a
50 # /var/bacula/barcodes
55 # archive-device is the name of the base directory where you want the
56 # Volumes stored appended with /drive0 for the first drive; /drive1
57 # for the second drive, ... For example, you might use
58 # /var/bacula/drive0 Note: you must not have a trailing slash, and
59 # the string (e.g. /drive0) must be unique, and it must not match
60 # any other part of the directory name. These restrictions could be
61 # easily removed by any clever script jockey.
63 # Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0 TestVol001
65 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
66 # base directory. In the above example the base directory is /var/bacula.
67 # However, as with tapes, their Bacula Volume names will be stored inside the
68 # Volume label. In addition to the Volumes (e.g. /var/bacula/slot1,
69 # /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
70 # file to keep track of what Slot is loaded. You should not change this file.
72 # Modified 8 June 2010 to accept Volume names from the calling program as arg 6.
73 # In this case, rather than storing the data in slotn, it is stored in the
74 # Volume name. Note: for this to work, Volume names may not include spaces.
82 # to turn on logging, uncomment the following line
83 #touch $wd/disk-changer.log
85 dbgfile="$wd/disk-changer.log"
87 if test -f $dbgfile; then
88 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
94 # Create a temporary file
97 TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
98 if test x${TMPFILE} = x; then
99 TMPFILE="$wd/disk-changer.$$"
100 if test -f ${TMPFILE}; then
101 echo "Temp file security problem on: ${TMPFILE}"
107 # check parameter count on commandline
112 if test $pCount -lt $pCountNeed; then
113 echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
114 echo " Insufficient number of arguments arguments given."
115 if test $pCount -lt 2; then
116 echo " Mimimum usage is first two arguments ..."
118 echo " Command expected $pCountNeed arguments"
125 # Strip off the final name in order to get the Directory ($dir)
126 # that we are dealing with.
129 bn=`basename $device`
130 dir=`echo "$device" | sed -e s%/$bn%%g`
131 if [ ! -d $dir ]; then
132 echo "ERROR: Autochanger directory \"$dir\" does not exist."
133 echo " You must create it."
139 # Get the Volume name from the call line, or directly from
140 # the volslotn information.
145 if test "x$volume" != x && test "x$volume" != "x*NONE*" ; then
146 debug "touching $dir/$volume"
148 echo "$volume" >$dir/volslot${slot}
150 elif [ -f $dir/volslot${slot} ]; then
151 volume=`cat $dir/volslot${slot}`
175 # Check for special cases where only 2 arguments are needed,
176 # all others are a minimum of 5
180 check_parm_count $# 2
183 check_parm_count $# 2
186 check_parm_count $# 4
187 if [ $slot -gt $maxslot ]; then
188 echo "Slot ($slot) out of range (1-$maxslot)"
189 debug "Error: Slot ($slot) out of range (1-$maxslot)"
194 check_parm_count $# 5
195 if [ $drive -gt $maxdrive ]; then
196 echo "Drive ($drive) out of range (0-$maxdrive)"
197 debug "Error: Drive ($drive) out of range (0-$maxdrive)"
200 if [ $slot -gt $maxslot ]; then
201 echo "Slot ($slot) out of range (1-$maxslot)"
202 debug "Error: Slot ($slot) out of range (1-$maxslot)"
209 debug "Parms: $ctl $cmd $slot $device $drive $volume $havevol"
213 debug "Doing disk -f $ctl unload $slot $device $drive $volume"
215 if [ -f $dir/loaded${drive} ]; then
216 ld=`cat $dir/loaded${drive}`
218 echo "Storage Element $slot is Already Full"
219 debug "Unload error: $dir/loaded${drive} is already unloaded"
222 if [ $slot -eq $ld ]; then
223 echo "0" >$dir/loaded${drive}
224 unlink $device 2>/dev/null >/dev/null
225 unlink ${device}.add 2>/dev/null >/dev/null
226 rm -f ${device} ${device}.add
228 echo "Storage Element $slot is Already Full"
229 debug "Unload error: $dir/loaded${drive} slot=$ld is already unloaded"
235 debug "Doing disk $ctl load $slot $device $drive $volume"
238 # Check if slot already in a drive
239 while [ $i -le $maxdrive ]; do
240 if [ -f $dir/loaded${i} ]; then
241 ld=`cat $dir/loaded${i}`
245 if [ $ld -eq $slot ]; then
246 echo "Drive ${i} Full (Storage element ${ld} loaded)"
247 debug "Load error: Cannot load Slot=${ld} in drive=$drive. Already in drive=${i}"
252 # Check if we have a Volume name
254 if [ $havevol -eq 0 ]; then
255 # check if slot exists
256 if [ ! -f $dir/slot${slot} ] ; then
257 echo "source Element Address $slot is Empty"
258 debug "Load error: source Element Address $slot is Empty"
262 if [ -f $dir/loaded${drive} ]; then
263 ld=`cat $dir/loaded${drive}`
267 if [ $ld -ne 0 ]; then
268 echo "Drive ${drive} Full (Storage element ${ld} loaded)"
269 echo "Load error: Drive ${drive} Full (Storage element ${ld} loaded)"
272 echo "0" >$dir/loaded${drive}
273 unlink $device 2>/dev/null >/dev/null
274 unlink ${device}.add 2>/dev/null >/dev/null
275 rm -f ${device} ${device}.add
276 if [ $havevol -ne 0 ]; then
277 ln -s $dir/$volume $device
278 ln -s $dir/${volume}.add ${device}.add
281 ln -s $dir/slot${slot} $device
282 ln -s $dir/slot${slot}.add ${device}.add
285 if [ $rtn -eq 0 ]; then
286 echo $slot >$dir/loaded${drive}
292 debug "Doing disk -f $ctl -- to list volumes"
294 if [ -f $dir/barcodes ]; then
298 while [ $i -le $maxslot ]; do
302 if [ $havevol -eq 0 ]; then
314 # ***FIXME*** must add new Volume stuff
316 debug "Doing disk -f $ctl -- to list volumes"
318 if [ ! -f $dir/barcodes ]; then
322 # we print drive content seen by autochanger
323 # and we also remove loaded media from the barcode list
325 while [ $i -le $maxdrive ]; do
326 if [ -f $dir/loaded${i} ]; then
327 ld=`cat $dir/loaded${i}`
328 v=`awk -F: "/^$ld:/"' { print $2 }' $dir/barcodes`
330 echo "^$ld:" >> $TMPFILE
335 # Empty slots are not in barcodes file
336 # When we detect a gap, we print missing rows as empty
337 # At the end, we fill the gap between the last entry and maxslot
338 grep -v -f $TMPFILE $dir/barcodes | sort -n | \
339 perl -ne 'BEGIN { $cur=1 }
351 END { while ($cur < '"$maxslot"') { print "S:$cur:E\n"; $cur++; } } '
357 # ***FIXME*** must add new Volume stuff
361 if [ -f $dir/slot{$slotdest} ]; then
362 echo "destination Element Address $slot is Full"
365 if [ ! -f $dir/slot${slot} ] ; then
366 echo "source Element Address $slot is Empty"
370 echo "Transfering $slot to $slotdest"
371 mv $dir/slot${slot} $dir/slot{$slotdest}
372 mv $dir/slot${slot}.add $dir/slot{$slotdest}.add
374 if [ -f $dir/barcodes ]; then
375 sed "s/^$slot:/$slotdest:/" > $TMPFILE
376 sort -n $TMPFILE > $dir/barcodes
381 debug "Doing disk -f $ctl $drive -- to find what is loaded"
383 if [ -f $dir/loaded${drive} ]; then
384 a=`cat $dir/loaded${drive}`
388 debug "Loaded: drive=$drive is $a"
394 debug "Doing disk -f $ctl -- to get count of slots"