Linux Tape Drive Management with mt Command
Magnetic tape drives remain a standard solution for high-capacity,
cold-storage data archiving. In Linux, interacting with these
sequential-access storage devices relies on the kernel's SCSI tape
subsystem and user-space management through the mt
(magnetic tape) utility. This guide explains how the Linux operating
system interacts with physical tape hardware, how device nodes handle
sequential operations, how the mt command controls drive
mechanics via system calls, and the primary commands used for tape
navigation.
Linux Tape Device Nodes
Linux interacts with tape hardware primarily through the
st (SCSI Tape) kernel driver. When a tape drive is
connected, the kernel exposes it as special character device files
located in /dev/:
- Rewinding devices (
/dev/st0,/dev/st1, etc.): When a process completes writing or reading to this device file and closes the file descriptor, the kernel automatically rewinds the tape back to the beginning (BOT). - Non-rewinding devices (
/dev/nst0,/dev/nst1, etc.): When an operation completes, the tape head remains at its current position. This is essential for appending multiple backup archives or sessions to a single tape.
Because tape drives are sequential rather than block-based (like HDDs or SSDs), data cannot be addressed randomly via standard filesystem mounts. Instead, data is written in sequential blocks separated by logical markers called filemarks.
How the mt Command
Operates
The mt command does not read or write payload data
itself; instead, it sends management and transport instructions directly
to the drive mechanism.
Under the hood, mt opens the designated tape device file
and issues ioctl (input/output control) system calls using
standard tape operations defined in the <sys/mtio.h>
header. These ioctl commands are translated by the Linux
st driver into SCSI Stream Commands (SSC) sent across the
bus (SAS, Fibre Channel, or USB) directly to the tape drive
controller.
Essential mt
Operations
By default, mt looks for a device designated by the
TAPE environment variable. If this variable is not set, you
specify the device using the -f flag. Always use
non-rewinding nodes (/dev/nst*) with mt to
prevent unintended rewinds after command execution.
1. Checking Drive Status
To check hardware readiness, error conditions, compression status, and tape head location:
mt -f /dev/nst0 statusThe output shows the current file number, block number, density code,
and tape flags (such as BOT for Beginning of Tape or
ONLINE).
2. Positioning the Tape
Because tapes are linear, locating a specific backup archive requires positioning the head relative to filemarks:
- Forward Space File (
fsf): Moves the tape forward by a specified number of filemarks.mt -f /dev/nst0 fsf 1 - Backward Space File (
bsf): Moves the tape backward across filemarks.mt -f /dev/nst0 bsf 1 - Rewind: Returns the tape immediately to the
physical beginning.
mt -f /dev/nst0 rewind
3. Writing Filemarks
When appending data using streaming utilities like tar
or dd, Linux writes an End-of-File (EOF) marker when the
stream closes. You can also manually write filemarks to segment
archives:
mt -f /dev/nst0 weof 14. Ejecting and Unloading Media
To safely unload tape tension and eject the cartridge from the drive:
mt -f /dev/nst0 offlineWorkflow Integration
In practice, mt works alongside archiving tools. A
standard archival workflow involves:
- Checking drive status with
mt -f /dev/nst0 status. - Writing an archive using
tar -czf /dev/nst0 /path/to/data. - Verifying the tape head advanced past the new filemark using
mt status. - Writing a second archive to the same tape using
tar -czf /dev/nst0 /path/to/second_dataset. - Rewinding the tape using
mt -f /dev/nst0 rewind.