From b064d71a5cd1ac87cc1af710a8e5772377865064 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 7 Jun 2004 14:40:44 +0000 Subject: [PATCH] Add locking-mtx-changer + more doc git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1387 91ce42f0-d328-0410-95d8-f526ca767f89 --- .../examples/autochangers/locking-mtx-changer | 155 ++++++++++++++++++ bacula/scripts/mtx-changer.in | 2 + bacula/src/cats/drop_bacula_database.in | 3 - bacula/src/cats/drop_mysql_database.in | 6 +- bacula/src/cats/drop_sqlite_database.in | 1 + 5 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 bacula/examples/autochangers/locking-mtx-changer diff --git a/bacula/examples/autochangers/locking-mtx-changer b/bacula/examples/autochangers/locking-mtx-changer new file mode 100644 index 0000000000..451a36de98 --- /dev/null +++ b/bacula/examples/autochangers/locking-mtx-changer @@ -0,0 +1,155 @@ +#!/bin/ksh +# +# Bacula interface to mtx autoloader +# +# $Id$ +# +# If you set in your Device resource +# +# Changer Command = "path-to-this-script/mtx-changer" %c %o %S %a %d +# you will have the following input to this script: +# +# mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index" +# $1 $2 $3 $4 $5 +# +# for example: +# +# mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system) +# +# If you need to an offline, refer to the drive as $4 +# e.g. mt -f $4 offline +# +# Many changers need an offline after the unload. Also many +# changers need a sleep 60 after the mtx load. +# +# N.B. If you change the script, take care to return either +# the mtx exit code or a 0. If the script exits with a non-zero +# exit code, Bacula will assume the request failed. +# + +MTX=/lysator/bin/mtx +LOCKDIR=/tmp + +if test $# -lt 2 ; then + echo "usage: mtx-changer ctl-device command slot archive-device drive" + echo " Insufficient number of arguments arguments given." + echo " Mimimum usage is first two arguments ..." + exit 1 +fi + +# Setup arguments +ctl=$1 +cmd="$2" +slot=$3 +device=$4 +# If drive not given, default to 0 +if test $# = 5 ; then + drive=$5 +else + drive=0 +fi + +wait_for_drive() { + while ! mt -f $1 status >/dev/null 2>/dev/null; do +# echo "Device $1 - not ready, retrying..." + sleep 5 + done +} + +LOCKFILE="${LOCKDIR}/mtx-changer:`echo $ctl | tr / _'" + +changer_lock() { + echo "$$" >$LOCKFILE.$$ + + while ! ln -n $LOCKFILE.$$ $LOCKFILE 2>/dev/null; do + echo "$0: changer lock busy, retrying in 30 seconds..." + sleep 30 + done + + rm $LOCKFILE.$$ +} + +changer_unlock() { + LOCKPID="`cat $LOCKFILE 2>/dev/null`" + if [ "$LOCKPID" != $$ ]; then + echo "$0: Invalid lock file (${LOCKFILE}) - not owned by us!" + exit 1 + fi +} + + + +# +# Check for special cases where only 2 arguments are needed, +# all others are a minimum of 3 +case $cmd in + loaded) + ;; + unload) + ;; + list) + ;; + slots) + ;; + *) + if test $# -lt 3; then + echo "usage: mtx-changer ctl-device command slot archive-device drive" + echo " Insufficient number of arguments arguments given." + echo " Mimimum usage is first three arguments ..." + exit 1 + fi + ;; +esac + +changer_lock $ctl + +case $cmd in + unload) +# echo "Doing mtx -f $ctl unload $slot $drive" +# +# enable the following line if you need to eject the cartridge + mt -f $device offline + if test x$slot = x; then + ${MTX} -f $ctl unload + rtn=$? + else + ${MTX} -f $ctl unload $slot $drive + rtn=$? + fi + ;; + + load) +# echo "Doing mtx -f $ctl load $slot $drive" + ${MTX} -f $ctl load $slot $drive + rtn=$? + + wait_for_drive $device + changer_unlock $ctl + exit $rtn + ;; + + list) +# echo "Requested list" + ${MTX} -f $ctl status | tr ':=' ' ' | nawk '($1 == "Storage" && $2 == "Element" && $4 == "Full") { printf "%s:%s\n", $3, $6 }' + rtn=$? + ;; + + loaded) + ${MTX} -f $ctl status >/tmp/mtx.$$ + rtn=$? + cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}" + cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}" + rm -f /tmp/mtx.$$ + changer_unlock $ctl + exit $rtn + ;; + + slots) +# echo "Request slots" + ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}" + rtn=$? + ;; +esac + +changer_unlock $ctl +exit $rtn diff --git a/bacula/scripts/mtx-changer.in b/bacula/scripts/mtx-changer.in index 17e57ec3ae..3760cae748 100644 --- a/bacula/scripts/mtx-changer.in +++ b/bacula/scripts/mtx-changer.in @@ -97,6 +97,8 @@ case $cmd in list) # echo "Requested list" ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//" +# Comment out the previous line and add a line here +# to print "fake" barcodes. ;; loaded) diff --git a/bacula/src/cats/drop_bacula_database.in b/bacula/src/cats/drop_bacula_database.in index cf9adc360d..cdc02fa4db 100755 --- a/bacula/src/cats/drop_bacula_database.in +++ b/bacula/src/cats/drop_bacula_database.in @@ -5,14 +5,11 @@ # if test xsqlite = x@DB_NAME@ ; then @scriptdir@/drop_sqlite_database $* - echo "Dropped SQLite database" else if test xmysql = x@DB_NAME@ ; then echo "Making MySQL database" @scriptdir@/drop_mysql_database $* - echo "Dropped MySQL database" else @scriptdir@/drop_postgresql_database $* - echo "Dropped PostgreSQL database" fi fi diff --git a/bacula/src/cats/drop_mysql_database.in b/bacula/src/cats/drop_mysql_database.in index ff9ff481da..aa447bc3bf 100644 --- a/bacula/src/cats/drop_mysql_database.in +++ b/bacula/src/cats/drop_mysql_database.in @@ -6,10 +6,10 @@ bindir=@SQL_BINDIR@ if test xmysql = x@DB_NAME@ ; then - if $bindir/mysql $* -f <