3 # Bacula interface to virtual autoloader using disk storage
5 # Written by Kern Sibbald
7 # Copyright (C) 2000-2015, Kern Sibbald
8 # License: BSD 2-Clause
10 # Bacula® is a registered trademark of Kern Sibbald.
13 # If you set in your Device resource
15 # Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
16 # you will have the following input to this script:
18 # So Bacula will always call with all the following arguments, even though
19 # in come cases, not all are used. Note, the Volume name is not always
22 # disk-changer "changer-device" "command" "slot" "archive-device" "drive-index" "volume"
25 # By default the autochanger has 10 Volumes and 1 Drive.
27 # Note: For this script to work, you *must" specify
29 # in each of the Devices associated with your AutoChanger resource.
31 # changer-device is the name of a file that overrides the default
32 # volumes and drives. It may have:
33 # maxslot=n where n is one based (default 10)
34 # maxdrive=m where m is zero based (default 1 -- i.e. 2 drives)
36 # This code can also simulate barcodes. You simply put
37 # a list of the slots and barcodes in the "base" directory/barcodes.
38 # See below for the base directory definition. Example of a
40 # /var/bacula/barcodes
45 # archive-device is the name of the base directory where you want the
46 # Volumes stored appended with /drive0 for the first drive; /drive1
47 # for the second drive, ... For example, you might use
48 # /var/bacula/drive0 Note: you must not have a trailing slash, and
49 # the string (e.g. /drive0) must be unique, and it must not match
50 # any other part of the directory name. These restrictions could be
51 # easily removed by any clever script jockey.
53 # Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0 TestVol001
55 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
56 # base directory. In the above example the base directory is /var/bacula.
57 # However, as with tapes, their Bacula Volume names will be stored inside the
58 # Volume label. In addition to the Volumes (e.g. /var/bacula/slot1,
59 # /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
60 # file to keep track of what Slot is loaded. You should not change this file.
62 # Modified 8 June 2010 to accept Volume names from the calling program as arg 6.
63 # In this case, rather than storing the data in slotn, it is stored in the
64 # Volume name. Note: for this to work, Volume names may not include spaces.
72 # to turn on logging, uncomment the following line
73 #touch $wd/disk-changer.log
75 dbgfile="$wd/disk-changer.log"
77 if test -f $dbgfile; then
78 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
84 # Create a temporary file
87 TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
88 if test x${TMPFILE} = x; then
89 TMPFILE="$wd/disk-changer.$$"
90 if test -f ${TMPFILE}; then
91 echo "Temp file security problem on: ${TMPFILE}"
97 # check parameter count on commandline
102 if test $pCount -lt $pCountNeed; then
103 echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
104 echo " Insufficient number of arguments arguments given."
105 if test $pCount -lt 2; then
106 echo " Mimimum usage is first two arguments ..."
108 echo " Command expected $pCountNeed arguments"
115 # Strip off the final name in order to get the Directory ($dir)
116 # that we are dealing with.
119 bn=`basename $device`
120 dir=`echo "$device" | sed -e s%/$bn%%g`
121 if [ ! -d $dir ]; then
122 echo "ERROR: Autochanger directory \"$dir\" does not exist."
123 echo " You must create it."
129 # Get the Volume name from the call line, or directly from
130 # the volslotn information.
135 if test "x$volume" != x && test "x$volume" != "x*NONE*" ; then
136 debug "touching $dir/$volume"
138 echo "$volume" >$dir/volslot${slot}
140 elif [ -f $dir/volslot${slot} ]; then
141 volume=`cat $dir/volslot${slot}`
165 # Check for special cases where only 2 arguments are needed,
166 # all others are a minimum of 5
170 check_parm_count $# 2
173 check_parm_count $# 2
176 check_parm_count $# 4
177 if [ $slot -gt $maxslot ]; then
178 echo "Slot ($slot) out of range (1-$maxslot)"
179 debug "Error: Slot ($slot) out of range (1-$maxslot)"
184 check_parm_count $# 5
185 if [ $drive -gt $maxdrive ]; then
186 echo "Drive ($drive) out of range (0-$maxdrive)"
187 debug "Error: Drive ($drive) out of range (0-$maxdrive)"
190 if [ $slot -gt $maxslot ]; then
191 echo "Slot ($slot) out of range (1-$maxslot)"
192 debug "Error: Slot ($slot) out of range (1-$maxslot)"
199 debug "Parms: $ctl $cmd $slot $device $drive $volume $havevol"
203 debug "Doing disk -f $ctl unload $slot $device $drive $volume"
205 if [ -f $dir/loaded${drive} ]; then
206 ld=`cat $dir/loaded${drive}`
208 echo "Storage Element $slot is Already Full"
209 debug "Unload error: $dir/loaded${drive} is already unloaded"
212 if [ $slot -eq $ld ]; then
213 echo "0" >$dir/loaded${drive}
214 unlink $device 2>/dev/null >/dev/null
216 echo "Storage Element $slot is Already Full"
217 debug "Unload error: $dir/loaded${drive} slot=$ld is already unloaded"
223 debug "Doing disk $ctl load $slot $device $drive $volume"
226 # Check if slot already in a drive
227 while [ $i -le $maxdrive ]; do
228 if [ -f $dir/loaded${i} ]; then
229 ld=`cat $dir/loaded${i}`
233 if [ $ld -eq $slot ]; then
234 echo "Drive ${i} Full (Storage element ${ld} loaded)"
235 debug "Load error: Cannot load Slot=${ld} in drive=$drive. Already in drive=${i}"
240 # Check if we have a Volume name
242 if [ $havevol -eq 0 ]; then
243 # check if slot exists
244 if [ ! -f $dir/slot${slot} ] ; then
245 echo "source Element Address $slot is Empty"
246 debug "Load error: source Element Address $slot is Empty"
250 if [ -f $dir/loaded${drive} ]; then
251 ld=`cat $dir/loaded${drive}`
255 if [ $ld -ne 0 ]; then
256 echo "Drive ${drive} Full (Storage element ${ld} loaded)"
257 echo "Load error: Drive ${drive} Full (Storage element ${ld} loaded)"
260 echo "0" >$dir/loaded${drive}
261 unlink $device 2>/dev/null >/dev/null
262 if [ $havevol -ne 0 ]; then
263 ln -s $dir/$volume $device
266 ln -s $dir/slot${slot} $device
269 if [ $rtn -eq 0 ]; then
270 echo $slot >$dir/loaded${drive}
276 debug "Doing disk -f $ctl -- to list volumes"
278 if [ -f $dir/barcodes ]; then
282 while [ $i -le $maxslot ]; do
286 if [ $havevol -eq 0 ]; then
298 # ***FIXME*** must add new Volume stuff
300 debug "Doing disk -f $ctl -- to list volumes"
302 if [ ! -f $dir/barcodes ]; then
306 # we print drive content seen by autochanger
307 # and we also remove loaded media from the barcode list
309 while [ $i -le $maxdrive ]; do
310 if [ -f $dir/loaded${i} ]; then
311 ld=`cat $dir/loaded${i}`
312 v=`awk -F: "/^$ld:/"' { print $2 }' $dir/barcodes`
314 echo "^$ld:" >> $TMPFILE
319 # Empty slots are not in barcodes file
320 # When we detect a gap, we print missing rows as empty
321 # At the end, we fill the gap between the last entry and maxslot
322 grep -v -f $TMPFILE $dir/barcodes | sort -n | \
323 perl -ne 'BEGIN { $cur=1 }
335 END { while ($cur < '"$maxslot"') { print "S:$cur:E\n"; $cur++; } } '
341 # ***FIXME*** must add new Volume stuff
345 if [ -f $dir/slot{$slotdest} ]; then
346 echo "destination Element Address $slot is Full"
349 if [ ! -f $dir/slot${slot} ] ; then
350 echo "source Element Address $slot is Empty"
354 echo "Transfering $slot to $slotdest"
355 mv $dir/slot${slot} $dir/slot{$slotdest}
357 if [ -f $dir/barcodes ]; then
358 sed "s/^$slot:/$slotdest:/" > $TMPFILE
359 sort -n $TMPFILE > $dir/barcodes
364 debug "Doing disk -f $ctl $drive -- to find what is loaded"
366 if [ -f $dir/loaded${drive} ]; then
367 a=`cat $dir/loaded${drive}`
371 debug "Loaded: drive=$drive is $a"
377 debug "Doing disk -f $ctl -- to get count of slots"