mt is a tape control program, it controls magnetic tape drive operation.

mt performs the given operations, which must be one of the tape operations(commands) listed by running the program with the -h option.

The path of the tape device on which to operate can be given with the -f or -t option. If neither of those options is given, and the environment variable TAPE is set, it is used. Otherwise, a default device defined in the file /usr/include/sys/mtio.h is used.

Some  operations optionally take an argument or repeat count, which can be given after the operation name and defaults to 1. The postfix k , M , or G can be used to give counts in units of 1024, 1024 * 1024, or 1024 * 1024 * 1024, respectively.

Usage, list commands

# mt -h
usage: mt [-v] [--version] [-h] [ -f device ] command [ count ]
commands: weof, wset, eof, fsf, fsfm, bsf, bsfm, fsr, bsr, fss, bss, rewind,
          offline, rewoffl, eject, retension, eod, seod, seek, tell, status,
          erase, setblk, lock, unlock, load, compression, setdensity,
          drvbuffer, stwrthreshold, stoptions, stsetoptions, stclearoptions,
          defblksize, defdensity, defdrvbuffer, defcompression, stsetcln,
          sttimeout, stlongtimeout, densities, setpartition, mkpartition,
          partseek, asf, stshowopt.

Introduction about tape devices

skip it if you know it.
Basically, there are two types of scsi tape devices created by SCSI tape driver, st and nst devices
       Devices are typically created by:

           mknod -m 666 /dev/st0 c 9 0
           mknod -m 666 /dev/st0l c 9 32
           mknod -m 666 /dev/st0m c 9 64
           mknod -m 666 /dev/st0a c 9 96
           mknod -m 666 /dev/nst0 c 9 128
           mknod -m 666 /dev/nst0l c 9 160
           mknod -m 666 /dev/nst0m c 9 192
           mknod -m 666 /dev/nst0a c 9 224

mt operats tape device that created by SCSI tape driver, which provides the interface to a variety of SCSI tape devices.  Currently, the driver takes control of all detected devices of type “sequential-access”.  The st driver uses major device number 9.

Each device uses eight minor device numbers.  The lowermost five bits in the minor numbers are assigned sequentially in the  order  of detection.  

In the 2.6 kernel, the bits above the eight lowermost bits are concatenated to the five lowermost bits to form the tape number. The minor numbers can be grouped into two sets of four numbers: the principal (auto-rewind) minor device numbers, n, and  the “no-rewind”  device  numbers, (n + 128). 

Devices opened using the principal device number will be sent a REWIND command when they are closed.  Devices opened using the “no-rewind” device number will not. 

(Note that using an auto-rewind device for positioning the tape with,  for instance, mt does not lead to the desired result: the tape is rewound after the mt command and the next command starts from the beginning of the tape).

Keep it simple:

/dev/st0 is an auto rewind tape device, tape will be rewinded when the device closed, thus, it's good for single write per tape volume or single file for multiple tape volumes.
/dev/nst0 is a non auto rewind tape device, single or multiple writes can be writeten to this tape.

Usage examples:

Rewind device

#mt -f /dev/fctd51 rewind

Write to device

#tar -cf /dev/fctd51 /usr/pgsql-9.1/lib
tar: Removing leading `/' from member names

Get current tape head position

#mt -f /dev/fctd51 tell
At block 823.

Get current tape status

#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=1, block number=0, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (81010000):
 EOF ONLINE IM_REP_EN

Rewind and list the files on the tape

#mt -f /dev/fctd51 rewind
#tar -tvf /dev/fctd51
drwxr-xr-x root/root         0 2013-05-10 16:29 usr/pgsql-9.1/lib/
-rwxr-xr-x root/root      7768 2013-04-02 09:26 usr/pgsql-9.1/lib/pg_upgrade_support.so
...
-rwxr-xr-x root/root      9616 2013-04-02 09:25 usr/pgsql-9.1/lib/utf8_and_cyrillic.so

Check the current status

#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=0, block number=822, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (1010000):
ONLINE IM_REP_EN

Check current status

#mt -f /dev/fctd51 tell
At block 822.

Move tape head to next file marker

#mt -f /dev/fctd51 fsf 1
#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=1, block number=0, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (81010000):
 EOF ONLINE IM_REP_EN
#mt -f /dev/fctd51 tell
At block 823.

As you can see seek/tell works with block addressing, while fsf/fsr/bsf etc.. work with file mark addressing. Nothing will go wrong on same machine, but if you are doing tape operation in a cluster environment. file marker addressing doesn't reflect tape real position, for example, if you move a tape to the second file, but the other node won't know by file marker addressing, so you'd need to enable global block addressing(I'll discuss this in stinit article).

How earse works

Another set ot commands, show you how earse works with positioning.

#mt -f /dev/fctd51 seek 500
#mt -f /dev/fctd51 erase
/dev/fctd51: Input/output error

Note: In the case above, erase doesn't work in the middle of a file, double check

#mt -f /dev/fctd51 rewind
#tar tvf /dev/fctd51
drwxr-xr-x root/root         0 2013-05-10 16:29 usr/pgsql-9.1/lib/
-rwxr-xr-x root/root      7768 2013-04-02 09:26 usr/pgsql-9.1/lib/pg_upgrade_support.so
drwxr-xr-x root/root         0 2013-05-10 16:29 usr/pgsql-9.1/lib/pgxs/
drwxr-xr-x root/root         0 2013-05-10 16:29 usr/pgsql-9.1/lib/pgxs/config/
-rwxr-xr-x root/root     13901 2013-04-02 09:23 usr/pgsql-9.1/lib/pgxs/config/install-sh
...

Try again

#mt -f /dev/fctd51 rewind
#mt -f /dev/fctd51 erase
#tar tvf /dev/fctd51
tar: /dev/fctd51: Cannot read: Input/output error
tar: At beginning of tape, quitting now
tar: Error is not recoverable: exiting now
#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=0, block number=-1, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (1010000):
 ONLINE IM_REP_EN
#mt -f /dev/fctd51 seek 500
/dev/fctd51: Input/output error

All content on the tape were erased.

Another one, erase files after the first one

#mt -f /dev/fctd51 rewind
#tar -cf /dev/fctd51 /usr/pgsql-9.1/lib
tar: Removing leading `/' from member names
#mt -f /dev/fctd51 rewind
#mt -f /dev/fctd51 eof
#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=1, block number=0, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (81010000):
 EOF ONLINE IM_REP_EN
#mt -f /dev/fctd51 status
SCSI 2 tape drive:
File number=1, block number=0, partition=0.
Tape block size 0 bytes. Density code 0x58 (no translation).
Soft error count since last status=0
General status bits on (81010000):
 EOF ONLINE IM_REP_EN
#mt -f /dev/fctd51 tell
At block 1.

Append files to tape

Move tape head to end of the tape

mt -f /dev/fctd51 eof

Write file to the tape

#tar -cf /dev/fctd51 /usr/pgsql-9.1/bin
tar: Removing leading `/' from member names
#mt -f /dev/fctd51 tell
At block 760.

Now, this is the new eof of the tape

mt -f /dev/fctd51 eof
#mt -f /dev/fctd51 tell
At block 761.

I'll discuss more about setoptions in another aritcle together with stinit, stay tunned.