The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


TapeChanger::MTX - use 'mtx' to manipulate a tape library


  use TapeChanger::MTX;

  my $loaded = TapeChanger::MTX->loadedtape;
  print "Currently loaded: $loaded\n" if ($loaded);

  my $nowloaded = TapeChanger::MTX->loadedtape; 
  print "Currently loaded: $nowloaded\n" if ($nowloaded);

See below for more available functions.


TapeChanger::MTX is a module to manipulate a tape library using the 'mtx' tape library program. It is meant to work with a simple shell/perl script to load and unload tapes as appropriate, and to provide a interface for more complicated programs to do the same. The below functions and variables should do as good a job as explaining this as anything.


$TapeChanger::MTX::MT =item $TapeChanger::MTX::MTX

What is the location of the 'mt' and 'mtx' binaries? Can be set with '$MT' and '$MTX' in ~/.mtxrc, or defaults to '/usr/sbin/mt' and '/usr/local/sbin/mtx'.


What are the names of the tape (DRIVE) and changer (CONTROL) device nodes? Can be set with $DRIVE or $CONTROL in ~/.mtxrc, or default to '/dev/rmt/0' and '/dev/changer' respectively.


Does the tape drive have to eject the tape before the changer retrieves it? It's okay to say 'yes' if it's not necessary, in most cases. Can be set with $EJECT in ~/.mtxrc, or defaults to '1'.


How long should we wait to see if the drive is ready, in seconds, after mounting a volume? Can be set with $READY_TIME in ~/.mtxrc, or defaults to 60.


Print debugging information? Set to '0' for normal verbosity, '1' for debugging information, or '-1' for 'quiet mode' (be as quiet as possible).


This module uses the following functions:

tape_cmd ( COMMAND )
mt_cmd ( COMMAND )

Runs 'mtx' and 'mt' as appropriate. COMMAND is the command you're trying to send to them. Uses 'warn()' to print the commands to the screen if $TapeChanger::MTX::DEBUG is set.

numdrives ()
numslots ()
loadedtape ()
numloaded ()
nummailslots ()

Returns the number of drives, number of slots, currently loaded tape, number of loaded tapes, and number of Import/Export slots, respectively, by parsing tape_cmd('status'). Not all of these will apply to all tape drives.

slothash ()

Returns a hash table (not hashref) of information about each slot. The keys of the hash are the slot numbers, and the values are arrayrefs that contain three fields:

  SlotType      "Import/Export" or empty string
  Full          "Full" or "Empty"
  VolumeTag     Tape barcode, if it exists
drivehash ()

As with slothash, but looks at the drives instead of the slots.

loadtape ( SLOT [, DRIVE] )

Loads a tape into the tape changer, and waits until the drive is again ready to be written to. SLOT can be any of the following (with the relevant function indicated):

  current       C<loadedtape()>
  prev          C<loadprevtape()>
  next          C<loadnexttape()>
  first         C<loadfirsttape()>
  last          C<loadlasttape()>
  0             C<_ejectdrive()>
  1..99         Loads the specified tape number, ejecting whatever is
                currently in the drive.

DRIVE is the drive to load, and defaults to 0. Returns 0 if successful, an error string otherwise.

loadnexttape ()
loadprevtape ()
loadfirsttape ()
loadlasttape ()

Loads the next, previous, first, and last tapes in the changer respectively. Use tape_cmd('next'), tape_cmd('previous'), tape_cmd('first'), and tape_cmd('last'), respectively.

transfertape ( FROM, TO )

Transfers a tape from slot FROM to slot TO. Returns 0 on success. Makes sure the necessary slots are empty/full as appropriate.

tagtoslot ( TAG )

Returns the slot that the tape with volume tag TAG is in, or '0' if it's not in the tape changer.

slottotag ( SLOT )

Returns the volume tag of the tape in slot SLOT, or '' if there is no tag or tape.

tagtodrive ( TAG )

Returns the drive that the tape with volume tag TAG is in, or '-1' if it's not in a drive.

drivetotag ( DRIVE )

Returns the volume tag of the tape in drive DRIVE, or '' if there is no tag or tape.

ejecttape ()

Ejects the tape, by first ejecting the tape from the drive (mt_cmd(rewind) then mt_cmd(offline)) and then returning it to its slot (tape_cmd(unload)). Returns 1 if successful, 0 otherwise.

resetchanger ()

Resets the changer, ejecting the tape and loading the first one from the changer.

checkdrive ()

Checks to see if the drive is ready or not, by waiting for up to $TapeChanger::MTX::READY_TIME seconds to see if it can get status information using mt_cmd(status). Returns 1 if so, 0 otherwise.


Returns a string containing the loaded tape and the drive that it's mounted on.

inventory ()

Runs a tape inventroy, if supported by the tape changer. This works out volume tags and such.

cannot_run ()

Does some quick checks to see if you're actually capable of using this module, based on your user permissions. Returns a list of problems if there are any, 0 otherwise.


~/.mtxrc is automatically loaded when this module is used, if it exists, using do(). This could cause security problems if you're trying to use this with setuid() programs - so just don't do that. If you want someone to have permission to mess with the tape drive and/or changer, let them have that permission directly.


Perl 5.6.0 or better, an installed 'mtx' binary, and a tape changer and reader connected to the system.


Theoretically allows multiple drives per changer and I/E slots, but I haven't tested it, so I may have missed something. 'load previous' doesn't actually work, because mtx doesn't support it (though the help says it does).


mtx, mt, tapechanger. Inspired by stc-changer, which comes with the AMANDA tape backup package (, and MTX, available at


Tim Skirvin <>.


Code for multi-slot tape drives and volume tags from Hubert Mikulicz <>.


This code is distributed under the University of Illinois Open Source License. See for details.


Copyright 2001-2004 by the University of Illinois Board of Trustees and Tim Skirvin <>.