I have written a script that tries to analyse the tape drive / changer / library connected to the SME server as part of an auto-magical set-up of Bacula (i.e. working with the existing Bacula contrib).
It assumes that if there is a tape library / changer it is attached to /dev/sgX where X is a..z or numeric, and all SCSI tape drives are attached to that device.
Could people try the script on their setup and post the results and comments so I can get it working for as many scenarios as possible (It is my very first PERL script so please forgive the coding...).
Cheers
Douglas.
Please cut the code below, save as find_tape.pl
make sure that any lines are not wrapped and therefore break!
then "chmod a+x find_tape.pl"
then run the script, post the output and comments as to whether it got it correct and if not, what it should have done.
#!/usr/bin/env perl
#use strict;
$|++;
use File::Find;
use Cwd 'abs_path';
my @tape_changers = ();
my @tape_drives = ();
print "Analysing tape drives on this machine\n";
my $dir = "/dev/tape";
find(\&do_something_with_file, $dir);
# and now print the results out for debug
if ($#tape_changers >=0) { #remember an array is -1 if it is empty and starts at the zeroth position
print "number of tape changers or libraries found is ".($#tape_changers+1)." and are:\n";
foreach $tape_changer (@tape_changers) {
print $tape_changer."\n";
}
}
if ($#tape_drives >=0) { #remember an array is -1 if it is empty and starts at the zeroth position
print "number of tape drives found is ".($#tape_drives+1)." and are:\n";
foreach $tape_drive (@tape_drives) {
print $tape_drive."\n";
}
}
#end of mainline
sub do_something_with_file
{
my $file = $_;
# scan the tape devices
if (-l $file) { # file is a link
my $realfile = abs_path($file);
my $is_it_a_changer = index $realfile, "/sg"; # we assume that a changer is on the generic scsi interface
if ($is_it_a_changer > 0) {
# we have found a generic scsi device, try to use mtx to retrieve its details, if this fails then
# it is not a changer
my @results = `mtx -f ${realfile} inquiry`;
my $vendor ="";
my $product="";
foreach $result (@results) {
# lets figure out what type of tape changer or library it is
# we assume that we should get its name in something like:
# Vendor ID: 'COMPAQ '
# Product ID: 'MSL5000 Series '
if ((index $result, "Vendor ID")>=0) {
my $left_delim = (index $result, "'")+1;
my $right_delim = (index $result, "'", $left_delim) - $left_delim;
$vendor = substr($result, $left_delim, $right_delim);
$vendor =~ s/^\s+//; # remove leading spaces
$vendor =~ s/\s+$//; # remove trailing spaces
}
if ((index $result, "Product ID")>=0) {
my $left_delim = (index $result, "'")+1;
my $right_delim = (index $result, "'", $left_delim) - $left_delim;
$product = substr($result, $left_delim, $right_delim);
$product =~ s/^\s+//; # remove leading spaces
$product =~ s/\s+$//; # remove trailing spaces
}
}
#build a unique tape changer name in case there are more than one by adding the dev path as a suffix
my $tape_changer = $vendor."_".$product."_".$realfile;
$tape_changer =~ s/ /_/; # change any spaces to underscores
push (@tape_changers , $tape_changer);
} else { # must just be a tape drive
my @results = `mt -f ${realfile} status`;
foreach $result (@results) {
# lets figure out what type of tape drive it is
# we assume that we should get its name in something like:
# Tape block size 0 bytes. Density code 0x48 (Quantum SDLT220).
if ((index $result, "Density code")>0) {
my $left_delim = (index $result, "(")+1;
my $right_delim = (index $result, ")") - $left_delim;
$vendor = substr($result, $left_delim, $right_delim);
$vendor =~ s/^\s+//; # remove leading spaces
$vendor =~ s/\s+$//; # remove trailing spaces
$vendor =~ s/ /_/; # change spaces to underscores
my $tape_drive = $vendor."_".$realfile;
push (@tape_drives , $tape_drive);
}
}
}
}
}