Attached is a "Bacula And Removable Disk HOWTO" that describes a method and script I am using for backing up to USB disk drives on Fedora Core 4. USB disk drives are used as virtual magazines for emulating a mult-drive magazine-based tape library with barcode reader. The "magazines" and the volumes they contain are handled from within bconsole exactly as one would handle tape magazines for a tape device.

I believe it should work on most *nix systems that have udev and autofs capability. Likewise, though USB drives are used as an example, it should work with Firewire, eSATA, etc. hot-swappable drives so long as the OS supports them.

The script and method were developed using Bacula version 1.39.24, but should work on the 1.38.* and above releases. The script relies on the OS (via udev and autofs) to mount and unmount removable drives, so does not use any of the new 1.39.* directives.

Enjoy!

Josh Fisher

Bacula And Removable Disk HOWTO

Josh Fisher

<[EMAIL PROTECTED]>


Revision History

Revision 0.7 2006-11-13

Initial version.

This document describes how to utilize removable disk drives as backup media for a backup solution using Bacula.



Table of Contents

  1. Introduction

    1. Copyright And License

    2. Disclaimer

    3. Credits / Contributors

    4. Feedback

  2. Definitions

  3. Requirements

  4. How It Works

  5. Mounting USB Disk Drives

    1. udev And Hot-swappable Drives

    2. Using autofs To Mount Magazine Partitions

  6. Preparing USB Disk Drives

    1. Partitioning

    2. Formatting / Labeling Magazine Partitions

  7. Installing vchanger

  8. Configuring Bacula To Use The Autochanger

    1. Configuring The Storage Daemon

    2. Configuring The Director

  9. Using The Autochanger

    1. Labeling Volumes In New Magazines

    2. Unloading Magazines

    3. Loading Magazines

    4. Adding New Magazines

  10. The vchanger Script

1. Introduction

This document describes how to utilize removable disk drives as backup media for a backup solution using Bacula. Having seen many people ask about the use of USB disk drives as a backup media on the Bacula User's e-mail list, I felt that producing this document based on my experience with using USB disk drives as backup media might be beneficial to some bacula users.

What follows is the description of a method to use several USB disk drives as the backup media for a backup solution using Bacula and is based on my experience with setting up a backup system for a small company with three servers and around 10 workstations. This method involves emulating a traditional magazine-based tape library device by using partitions on the USB disk drives as the “magazines”. As such, the method would work equally well for disk drives in Firewire, eSATA, or any other hot-swappable enclosures supported by the OS.

1.1 Copyright And License

This document, Bacula And Removable Disk HOWTO, is copyrighted (c) 2006 by Josh Fisher. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is available at http://www.gnu.org/copyleft/fdl.html.

1.2 Disclaimer

No liability for the contents of this document can be accepted. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies which could damage to your system. Though this is highly unlikely, proceed with caution. The author(s) do not accept responsibility for your actions.

All copyrights are held by their respective owners, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Naming of particular products or brands should not be seen as endorsements.

1.3 Credits / Contributors

Thanks to all those who frequent the Bacula User's e-mail list, and of course to Kern Sibbald and the other Bacula developers.

1.4 Feedback

Send your additions, comments and criticisms to <[EMAIL PROTECTED]>

2. Definitions

Bacula is an open source network backup solution. Much more information about Bacula is available at the Bacula website http://www.bacula.org.

A removable disk storage device is a random access disk storage device that is external to, or easily removable from, a computer system. Examples are external drive enclosures that connect via USB, Firewire, eSATA, or other hot-swappable interfaces.

vchanger is a shell script that emulates a traditional tape library device and acts as an interface to Bacula.

An USB disk drive is an external enclosure that connects to a computer system via a USB interface and usually houses a hard disk drive designed for either desktop or laptop computers. Those that use laptop hard drives are more shock tolerant and better suited to transporting.

The term hot-swappable refers to hardware devices that may be attached to and removed from a running computer system.

3. Requirements

Briefly, the requirements are:

  • Two or more removable disk storage devices

  • A server running bacula-dir, bacula-sd, udev, and autofs

  • A network connecting the servers and workstations to be backed up

  • The vchanger script and a vchanger configuration file for each virtual autochanger

4. How It Works

USB disk drives are used to emulate a multi-drive magazine-based tape library. The heart of the emulation is the vchanger shell script. This script understands the Bacula Autochanger Interface, accepting commands from the bacula storage daemon, performing the command, and then returning a result. When vchanger is used as the Autochanger Command in an Autochanger resource in Bacula Storage Daemon configuration file, (bacula-sd.conf), the Storage Daemon will invoke vchanger to load and unload virtual tapes, list the barcodes of the “tapes”, etc.

Vchanger treats a partition on a USB disk drive as a virtual magazine. Each “magazine” filesystem is given a filesystem label that links it to a particular virtual autochanger. In other words, all magazines belonging to a particular autochanger are given the same filesystem label. An autochanger's magazines are distinguished by a magazine index number in the range 1-99. The magazine index number is stored as a line of text in a file named 'index' in the magazine partition's root directory.

Each autochanger is configured for a fixed number of virtual slots per magazine, and of course, all magazines belonging to an autochanger have the same number of slots. Each slot is “occupied” by a regular file which acts as a virtual tape that will contain a bacula volume. These files are named using the pattern 'mIIsJJJ', where II is replaced by the (one-based) two digit magazine index number and JJJ is replaced by the (one-based) three digit slot number. This name is used as the “barcode” of the “tape”, and Bacula will also use this as the volume label name when the volumes in the magazine are labeled using Bacula's 'label barcodes' command.

Bacula is configured to invoke vchanger with the following parameters:

vchanger “config_file” “command” “slot_number” “archive_device” “drive_index”

The first parameter gives the location of the vchanger configuration file to use. Every virtual autochanger has its own configuration file, (defined in 7. below).

The second parameter is the command to be performed and is one of:

load Load a “tape” from a magazine “slot” into a “drive”. Vchanger creates a symlink with the name passed in parameter 4 (archive_device) pointing to the file mIIsJJJ, where II is the two digit magazine index and JJJ is the slot number passed to Vchanger as parameter 3.

unload Unload a “tape” from the “drive” back into the magazine “slot”. Vchanger deletes the symlink that was created by a previous load command for the drive.

loaded Return the “slot” currently loaded into the “drive”.

list List the barcodes of the “tapes” in each of the “slots” in this magazine.

slots Return the number of slots per magazine.

The third parameter is the slot number to use for the current command.

The fourth parameter, the Archive Device, will be the path where vchanger should create a symlink when loading a drive.

The fifth parameter is the drive index of the drive this command affects.

5. Mounting USB Disk Drives

5.1 udev And Hot-swappable Drives

Udev is the subsystem that dynamically assigns device nodes to hot-swappable hardware devices whenever they are attached to a running system and frees those device nodes whenever they are detached. There is no guarantee that a particular piece of hardware will be assigned a known device node when it is plugged in. The device node that udev assigns will depend on how many other devices are already attached and could change every time the hot-swappable device is plugged in.

Fortunately, udev provides a way to create aliases to the device nodes it assigns so that the alias is always at a known path. On most modern *nix systems, aliases to hot-swappable disk storage devices are placed under /dev/disk. Of particular interest for the virtual autochanger we are creating from USB disk drives, udev uses the filesystem label of any mountable partitions on the USB disk drive to create a symlink under /dev/disk/by-label that points to the real device node that udev assigned to the disk partition. By labeling the filesystems on the disk partitions that will be used as our backup media, we then gain access to the dynamically assigned device node via the alias (symlink). For example, if we set the filesystem label to 'vchanger1' on all of the partitions we will be using for the virtual autochanger, then we can access the device node through the alias at /dev/disk/by-label/vchanger1, where /dev/disk/by-label/vchanger1 is simply a symlink pointing to the real device node that udev assigned to the partition when the USB drive was plugged in.

Note that while it is possible to write udev rules that create an alias at some other path, writing udev rules is beyond the scope of this document. If you want (or need) to do this, then a good place to start might be http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html.

5.2 Using autofs To Mount Magazine Partitions

Udev provides the means to access device nodes for the USB drive's partitions, but we still must somehow get them mounted so that Bacula can read and write volumes on them. There are many ways to do this, and in fact newer releases of Bacula provide configuration variables for just this purpose. It is also possible to create entries in /etc/fstab using the alias described above. The method described here, however, makes use of autofs to mount the USB drive's partition(s).

The autofs daemon provides a way to mount and unmount the partitions on-the-fly as they are accessed. Since we know the device node (alias) will be at /dev/disk/by-label/vchanger1 for our 'vchanger1' autochanger, we can select a mountpoint of /mnt/vchanger1/magazine and create a file named /etc/auto.vchanger1 as follows:

# /etc/auto.vchanger1

magazine -fstype=auto,rw :/dev/disk/by-label/vchanger1

# eof

Add an entry to /etc/auto.master to tell autofs to pull in the new auto.vchanger1 configuration:

# /etc/auto.master

# ..

/mnt/vchanger1 /etc/auto.vchanger1 --timeout=30

# eof

Now restart the autofs daemon and when a drive that has a partition with a filesystem label of 'vchanger1' is plugged in, it can be accessed at /mnt/vchanger1/magazine without any need for an explicit mount command. Autofs will mount the partition on-the-fly as needed.

6. Preparing USB Disk Drives

Preparing the USB drives for use with the virtual autochanger is fairly simple. All that is needed is a partition on the drive with a filesystem that has a filesystem label named appropriately for the virtual autochanger it will be used with. Most USB drives come from the factory with a single large FAT32 partition. While that could be used by simply giving it a filesystem label and adjusting the autofs configuration from 5.2 above appropriately, in this example we will be using a ext3 filesystem on a single partition encompassing the entire disk. The preparation procedure detailed in 6.1 and 6.2 below is used to prepare each drive to be used.

6.1 Partitioning

To partition a drive we need to know the drive's device node. This can usually be determined from the output of dmesg a few seconds after the drive is plugged in. After finding the drive's device node, say for example /dev/sdc, use fdisk /dev/sdc (or some other partitioning tool) to create a single Linux partition as the drive's primary partition number 1.

6.2 Formatting / Labeling Magazine Partitions

Assuming the drive was assigned node /dev/sdc, the partition can now be formatted and labeled using:

mke2fs -j -T largefile -L “vchanger1” /dev/sdc1

This will create a new ext3 filesystem on the partition with filesystem label 'vchanger1'. Now unplug the USB drive, wait a few seconds, then plug it back in. A few seconds after plugging it in you should see a symlink /dev/disk/by-label/vchanger1 pointing to whatever device node udev decided to assign to the partition. Now try :

ls /mnt/vchanger1/magazine

This should cause autofs to mount the partition, and the output from ls should show an empty filesystem (or possibly a single 'lost+found' directory). The output from the 'df' command should show that the partition is mounted at /mnt/vchanger1/magazine.

Now there is one thing left to do to prepare the drive. Unless you will be running the Bacula Storage Daemon as root, (not recommended), you will need to set the owner, group, and permissions for the filesystem on the new partition using:

chown -R bacula.disk /mnt/vchanger1/magazine

chmod 0770 /mnt/vchanger1/magazine

7. Installing vchanger

Place a copy of the vchanger shell script in the /etc/bacula directory, than change its owner and permissions as foolows:

chown root.disk /etc/bacula/vchanger

chmod 750 /etc/bacula/vchanger

The first positional parameter passed to vchanger from Bacula specifies the vchanger configuration file. Each virtual autochanger will have its own configuration file. A vchanger configuration file defines the following values:

magslots The number of slots in a magazine
Default: 10

max drive The maximum zero-based drive index number to use. This defines the number of virtual drives the autochanger uses. Multiple drives allow Bacula to concurrently access multiple volumes. The number of virtual drives used must be less than or equal to the number of slots in a magazine.
Default: 0 ( 1 virtual drive)

statedir [Required] Directory where state information for this autochanger is stored
Default: none

mountpoint [Required] Path to where the magazine partition is mounted
Default: none

For example, if we want the 'vchanger1' virtual autochanger, (the autochanger that will use as its virtual magazines partitions containing a filesystem with label 'vchanger1'), to have 50 slots in a magazine, two virtual drives, its magazines mounted at /mnt/vchanger1/magazine, and its state information stored in /var/bacula/vchanger1, then we create the /etc/bacula/vchanger1.conf file to be:

# /etc/bacula/vchanger1.conf

magslots=50

maxdrive=1

mountpoint=/mnt/vchanger1/magazine

statedir=/var/bacula/vchanger1

# eof

The owner and permissions for /etc/bacula/vchanger1.conf should be set using:

chown root.disk /etc/bacula/vchanger1.conf

chmod 0640 /etc/bacula/vchanger1.conf

Note that the mountpoint is set to the the mountpoint we defined when setting up autofs in 5.2 above.

8. Configuring Bacula To Use The Autochanger

The virtual autochanger must be defined by adding Autochanger and Device resources to Bacula's configuration files as described in 8.1 and 8.2. After making changes to Bacula's configuration, the Storage Daemon and Director must be restarted for these changes to take effect.

8.1 Configuring The Storage Daemon

To configure the Bacula storage daemon (bacula-sd) we add an Autochanger resource and associated Device resource(es) to bacula-sd.conf. For example:

# /etc/bacula/bacula-sd.conf

# ..

#---- local virtual autochanger with USB drive "magazines"

Autochanger {

Name = usb-changer-1

Device = usb-changer-1-drive-0

Device = usb-changer-1-drive-1

Changer Command = "/etc/bacula/vchanger %c %o %S %a %d"

Changer Device = "/etc/bacula/vchanger1.conf"

}

#--- drive 0 of the usb-changer-1 autochanger

Device {

Name = usb-changer-1-drive-0

DriveIndex = 0

Autochanger = yes;

DeviceType = File

MediaType = File

ArchiveDevice = /var/bacula/vchanger1/drive0

RemovableMedia = no;

RandomAccess = yes;

}

#--- drive 1 of the usb-changer-1 autochanger

Device {

Name = usb-changer-1-drive-1

DriveIndex = 1

Autochanger = yes;

DeviceType = File

MediaType = File

ArchiveDevice = /var/bacula/vchanger1/drive1

RemovableMedia = no;

RandomAccess = yes;

}# ..

# eof

In the Autochanger resource, the Changer Device value is the path to the vchanger configuration file for this autochanger. The Changer Command value is the command Bacula will execute when it needs the autochanger to perform some function, (like loading a “tape”). Here, we have installed the vchanger script at /etc/bacula/vchanger, and bacula is going to pass the Changer Device value in parameter 1, the command (load, unload, etc) in parameter 2, the slot number for the command in parameter 3, the Archive Device value from the Device resource that has a Drive Index value equal to the drive index for the command in parameter 4, and the drive index for the command in parameter 5.

In the Device resource(es) it is important to set Device Type = File so that Bacula understands it is reading/writing a File Storage type volume. The ArchiveDevice value is set to a path where vchanger will create a symlink to the requested slot's file when a “tape” is loaded. Here we are telling vchanger to create symlink /var/bacula/vchanger1/drive0 for drive index 0 and symlink /var/bacula/vchanger1/drive1 for drive index 1.

8.2 Configuring The Director

The Bacula Director is configured to use the autochanger more or less as it would be configured for a traditional tape autochanger.

# /etc/bacula/bacula-dir.conf

# ..

# Local USB drive virtual autochanger

Storage {

Name = USBChanger1

Address = 192.168.1.3

SDPort = 9103

Password = "secret_password"

Device = usb-changer-1

Media Type = File

Autochanger = yes;

}

# ..

Pool {

Name = Scratch

Pool Type = Backup

}

# ..

# eof

Note that we have a Scratch pool defined in bacula-dir.conf. Bacula treats this pool differently than other pools in that if a job requires a new volume from a pool that has no available volumes, then Bacula will automatically move a volume from the Scratch pool into the pool that needs a new volume. When the volumes in new virtual magazines are labeled, (usually using the 'label barcodes' command from bconsole), we will initially place all of the new volumes into the Scratch pool so that they can be moved into other pools when and as needed.

9. Using The Autochanger

After restarting Bacula Storage Daemon and Director to load the new configuration, the virtual autochanger vchanger1 is ready to use.

9.1 Labeling Volumes In New Magazines

Plug in one of the newly prepared USB drives from 6. above, then from bconsole execute the command 'label barcodes'. When prompted for the pool to place the newly labeled volumes into, select the Scratch pool. Bacula will ask the vchanger script for the barcodes of the “tapes” in each of the magazine's “slots” and use that as the volume label name. The first magazine added to the autochanger will be given magazine index 1. The next will be given index 2, and so on.

9.2 Unloading Magazines

To unload a magazine, first check the status of the director with the command 'status dir' from within bconsole to make sure no currently running jobs are using a volume on the autochanger, then simply unplug the USB drive.

9.3 Loading Magazines

When Bacula requests a volume on another magazine, it will be necessary to load that magazine into the autochanger. Which magazine to load will be easily discovered from the volume label name that is being asked for. The two digit number following the 'm' at the beginning of the volume name is the magazine index number. To load the magazine, first unload any currently loaded magazine as described above, then plug in the requested magazine and from bconsole issue the command 'update slots'. If prompted for a drive index, just select zero. Bacula will ask the vchanger script for the barcodes of the volumes in the magazine and update the catalog database accordingly.

9.4 Adding New Magazines

You can add up to 99 magazines to an existing autochanger. Just prepare a new USB drive as described in 6. above and then label its volumes as in 9.1 above. A new magazine will be created using the next available magazine index number.

10. The vchanger Script

#!/bin/sh
#
#  Bacula interface to virtual autochanger using removable disk drives
#
#  Based (somewhat) on the "disk-changer" script from bacula 1.39.26
#
#  Vchanger is a Bacula autochanger script that emulates a conventional
#  magazine-based tape library device using removable disk drives.
#  Partitions on the removable drives are used as virtual magazines,
#  where each "magazine" contains the same number of virtual slots. Each
#  "slot" holds one virtual tape, where a "tape" is a regular file that
#  Bacula treats as a "Device Type = File" volume.
#
#  This script will be invoked by Bacula using the Bacula Autochanger
#  Interface and will be passed the following arguments:
#
#  vchanger "changer-device" "command" "slot" "archive-device" "drive-index"
#                 $1            $2       $3          #4             #5
#
#  See the Bacula documentation for Autochanger Interface details
#
#  Copyright (C) 2006 Josh Fisher
#
#  Permission to use, copy, modify, distribute, and sell this software
#  and its documentation for any purpose is hereby granted without fee,
#  provided that the above copyright notice appears in all copies.  This
#  software is provided "as is" without express or implied warranty.
#
#  This software is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# $Id: vchanger,v 0.7.3 2006/11/05 11:31:47 jfisher Exp $

#
# log whats done
#
dbgfile="/var/bacula/vchanger.log"
# to turn on logging, uncomment the following line
#touch $dbgfile
#

#
# Write to a log file
#    To log debugging info, create file /var/bacula/vchanger.log
#    with write permission for bacula-sd user. To stop logging,
#    delete file /var/bacula/vchanger.log
#
function debug()
{
    if test -e $dbgfile; then
	echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
    fi
}

#
# Return length of string $1
#
function strlen ()
{
   expr length $1
}

#
# Prepend zeros to $1 and return a string that is $2 characters long
#
function mklen ()
{
   o1=$1
   while [ `eval strlen ${o1}` -lt ${2} ]; do
      o1="0${o1}"
   done
   echo $o1
}

#
# Initialize autochanger's state directory if not already initialized
#
function init_statedir() {
   debug "Initializing $statedir"
   # Create state dir if needed
   if [ ! -d "${statedir}" ]; then
      mkdir "${statedir}"
      if [ $? -ne 0 ]; then
         echo "Could not create ${statedir}"
         exit 1
      fi
   fi
   chmod 770 "${statedir}"
   if [ $? -ne 0 ]; then
      echo "Could not chmod ${statedir}"
      exit 1
   fi
   # Create nextmag file to hold max magazine index used
   if [ ! -f "${statedir}/nextmag" ]; then
      echo 0 >"${statedir}/nextmag"
      if [ $? -ne 0 ]; then
         echo "Could not create ${statedir}/nextmag"
         exit 1
      fi
   fi
   chmod 660 "${statedir}/nextmag"
   if [ $? -ne 0 ]; then
      echo "Could not chmod ${statedir}/nextmag"
      exit 1
   fi
   # Check nextmag value
   nextmag=`cat "${statedir}/nextmag"`
   if [ $? -ne 0 -o "${nextmag}" == "" -o $nextmag -lt 1 -o $nextmag -gt 99 ]; then
      echo "${statedir}/nextmag has invalid value"
      return 1
   fi
   # Create 'loaded' files for each virtual drive that hold the slot
   # number currently loaded in that 'drive'
   i=0
   while [ $i -le $maxdrive ]; do
      if [ ! -f "${statedir}/loaded${i}" ]; then
         echo "0" 2>/dev/null >"${statedir}/loaded${i}"
         if [ $? -ne 0 ]; then
            echo "Could not create ${statedir}/loaded${i}"
            exit 1
         fi
         chmod 660 "${statedir}/loaded${i}"
         if [ $? -ne 0 ]; then
            echo "Could not chmod ${statedir}/loaded${i}"
            exit 1
         fi
      fi
      i=`expr ${i} + 1`
   done
}


#
# Initialize magazine if not already initialized
#
function init_magazine() {
   debug "Initializing magazine"
   # Get max magazine index that has been used
   nextmag=`cat "${statedir}/nextmag"`
   if [ $? -ne 0 -o "${nextmag}" == "" ]; then
      echo "Failed to read ${statedir}/nextmag"
      exit 1
   fi
   # Check magazine for existing index
   if [ -f "${mountpoint}/index" ]; then
      # retrieve existing magazine index
      mi=`cat "${mountpoint}/index"`
      if [ $? -ne 0 ]; then
         echo "Failed to read ${mountpoint}/index"
         exit 1
      fi
      # must be 1-99
      if [ $mi -lt 1 -o $mi -gt 99 ]; then
         echo "Magazine has invalid index ${mi}"
         exit 1
      fi
   else
      # new magazine, so assign it the next avail index
      mi=`expr ${nextmag} + 1`
      if [ $mi -lt 0 -o $mi -gt 99 ]; then
         echo "Max magazines exceeded"
         exit 1
      fi
      echo $mi 2>/dev/null >"${mountpoint}/index"
      if [ $? -ne 0 ]; then
         echo "Failed to write ${mountpoint}/index"
         exit 1
      fi
   fi
   # make sure max index used is up to date
   if [ $mi -gt $nextmag ]; then
      echo $mi 2>/dev/null >"${statedir}/nextmag"
      if [ $? -ne 0 ]; then
         echo "Failed to update ${statedir}/nextmag"
         exit 1
      fi
   fi
   # make magazine index 2 digits
   magindex=`eval mklen ${mi} 2`
   # setup slot files (ie. virtual tapes)
   i=1
   while [ $i -le $magslots ]; do
      s=`eval mklen ${i} 3`
      f="${mountpoint}/m${magindex}s${s}"
      if [ ! -f "${f}" ]; then
         touch "${f}" 2>/dev/null >/dev/null
         if [ $? -ne 0 ]; then
            echo "Failed to create ${f}"
            exit 1
         fi
      fi
      i=`expr ${i} + 1`
   done
   return 0
}


#
# check parameter count on commandline
#
function check_parm_count() {
    pCount=$1
    pCountNeed=$2
    if test $pCount -lt $pCountNeed; then
	echo "usage: vchanger ctl-device command [slot archive-device drive-index]"
	echo "	Insufficient number of arguments arguments given."
	if test $pCount -lt 2; then
	    echo "  Mimimum usage is first two arguments ..."
	else
	    echo "  Command expected $pCountNeed arguments"
	fi
	exit 1
    fi
}


# Setup arguments
ctl=$1
cmd="$2"
slot=$3
device=$4
drive=$5

# Setup default config values
magslots=10
maxdrive=0
statedir="/var/bacula/vchanger"
mountpoint=

# Pull in conf file
if [ -f $ctl ]; then
   . $ctl
else
   echo "Config file ${ctl} not found"
   exit 1
fi

# check for required config values
if [ "${mountpoint}" == "" ]; then
   echo "Required variable 'mountpoint' not defined in ${ctl}"
   exit 1
fi
if [ "${magslots}" == "" -o $magslots -lt 1 -o $magslots -gt 999 ]; then
   echo "Ivalid value for 'magslots' in ${ctl}"
   exit 1
fi
if [ "${maxdrive}" == "" -o $maxdrive -lt 0 -o $maxdrive -ge $magslots ]; then
   echo "Invalid value for 'maxdrive' in ${ctl}"
   exit 1
fi
if [ "${statedir}" == "" ]; then
   echo "Invalid value for 'statedir' in ${ctl}"
   exit 1
fi

# Initialize state directory for this autochanger
init_statedir

# Check for special cases where only 2 arguments are needed,
#  all others are a minimum of 5
#
case $2 in
    list)
	check_parm_count $# 2
	;;
    slots)
	check_parm_count $# 2
	;;
    *)
	check_parm_count $# 5
	if [ $drive -gt $maxdrive ]; then
	   echo "Drive ($drive) out of range (0-${maxdrive})"
	   exit 1
	fi
	if [ $slot -gt $magslots ]; then
	   echo "Slot ($slot) out of range (1-$magslots)"
	   exit 1
	fi
	;;
esac

debug "Parms: $ctl $cmd $slot $device $drive"

case $cmd in
   unload)
      debug "Doing vchanger -f $ctl unload $slot $device $drive"
      ld=`cat "${statedir}/loaded${drive}"`
      if [ $? -ne 0 ]; then
         echo "Failed to read ${statedir}/loaded${drive}"
         exit 1
      fi
      if [ $slot -eq $ld ]; then
         echo "0" >"${statedir}/loaded${drive}"
         if [ $? -ne 0 ]; then
            echo "Failed to write ${statedir}/loaded${drive}"
            exit 1
         fi
         unlink "${device}" 2>/dev/null >/dev/null
         exit 0
      fi
      if [ $ld -eq 0 ]; then
         echo "Drive ${drive} Is Empty"
      else
         echo "Storage Element ${slot} is Already Full"
      fi
      exit 1
      ;;

   load)
      debug "Doing vchanger $ctl load $slot $device $drive"
      ld=`cat "${statedir}/loaded${drive}"`
      if [ $? -ne 0 ]; then
         echo "Failed to read ${statedir}/loaded${drive}"
         exit 1
      fi
      if [ $ld -eq 0 ]; then
         unlink "${device}" 2>/dev/null >/dev/null
         # make sure slot is not loaded in another drive
         i=0
         while [ $i -le $maxdrive ]; do
            if [ $i -ne $drive ]; then
               ldi=`cat "${statedir}/loaded${i}"`
               if [ $ldi -eq $slot ]; then
                  echo "Storage Element ${slot} Empty (loaded in drive ${i})"
                  exit 1
               fi
            fi
            i=`expr ${i} + 1`
         done
         init_magazine
         if [ $? -ne 0 ]; then
            echo "Magazine Not Loaded"
            exit 1
         fi
         s=`eval mklen ${slot} 3`
         ln -s "${mountpoint}/m${magindex}s${s}" "${device}"
         echo $slot >"${statedir}/loaded${drive}"
         exit 0
      else
         echo "Drive ${drive} Full (Storage element ${ld} loaded)"
         exit 1
      fi
      ;;

   list)
      debug "Doing vchanger -f $ctl -- to list volumes"
      init_magazine
      if [ $? -ne 0 ]; then
         echo "Magazine Not Loaded"
         exit 1
      fi
      i=1
      while [ $i -le $magslots ]; do
         s=`eval mklen ${i} 3`
         echo "${i}:m${magindex}s${s}"
         i=`expr ${i} + 1`
      done
      exit 0
      ;;

   loaded)
      debug "Doing vchanger -f $ctl $drive -- to find what is loaded"
      cat "${statedir}/loaded${drive}"
      exit 0
      ;;

   slots)
      debug "Doing vchanger -f $ctl -- to get count of slots"
      echo $magslots
      exit 0
      ;;
esac
# eof



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bacula-users mailing list
Bacula-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-users

Reply via email to