Manage tape autoloaders, drives and libraries from the Unix/Linux CLI
The first step in managing tapes in Linux is using the mt and mtx commands.
Tools used in this article:
- lsscsi (list SCSI devices (or hosts), list NVMe devices)
- mt (control magnetic tape drive operation, from the mt-st package): controls the tape drive, such as tape movement, ejection and drive settings.
- mtx (control SCSI media changer devices): controls tape libraries (moving cartridges from slots to drives, etc.)
- tar (an archiving utility)
- ltfs (Linear Tape File System)
- sg3-utils (The sg3_utils package contains utilities that send SCSI commands to devices)
- dd (convert and copy a file)
Warning! The first time you load an LTO-9 tape, it starts a calibration that can take up to 2 hours
When LTO-9 media is first loaded into an LTO-9 drive, the drive automatically initiates tape calibration, which includes initialization and characterization of the cartridge.
IMPORTANT: IBM has stated that LTO-9 calibration may require 20 minutes to 2 hours to complete.
During calibration, the drive state will indicate “Calibrating” and no other actions, including host access, can be performed on the drive. When calibration is complete, the drive will automatically transition to the “Loaded” state and the media will be available for host access. If an uninitialized LTO-9 tape is moved to a drive to perform any type of media validation, any attempt to cancel the media validation while in the “Calibrating” state will fail with “Drive is busy calibrating”.
Identify the autoloader with lsscsi: look for the device marked as mediumx
lsscsi -g
[1:0:0:0] tape IBM ULT3580-HH9 Q3F5 /dev/st0 /dev/sg3
[1:0:0:1] mediumx IBM 3572-TL 0131 /dev/sch0 /dev/sg4
# In this case the autoloader is /dev/sch0 / /dev/sg4, because it is marked as mediumx.
# The tape drive itself is /dev/st0.
How to control the tape autoloader in Linux
A tape autoloader consists of two devices:
- the autoloader (media changer)
- the tape drive
The loader is controlled with the mtx command, for example:
# Load tape 1
sudo mtx -f /dev/sg4 load 1
# Get status from loader
sudo mtx -f /dev/sg4 status
# Unload the autoloader
sudo mtx -f /dev/sg4 unload
With LTO tapes you might run into an error during unload. In that case, take the tape in the drive offline first:
sudo mt -f /dev/st0 offline
The tape drive is controlled with the mt command, for example:
sudo mt -f /dev/st0 rewind
Writing data to tape
Rewinding or not: st0 and nst0
# /dev/st0 rewinds after every command
# /dev/nst0 does NOT rewind: use it to put several archives on one tape
sudo mt -f /dev/nst0 status # where are we, is a tape loaded?
sudo mt -f /dev/nst0 setblk 0 # variable block size (works best with tar)
tar: the simplest backup to tape
tar writes a stream of files straight onto the tape. With nst0 you can put several archives after each other.
sudo mt -f /dev/nst0 rewind
sudo tar -cvf /dev/nst0 -b 512 /data/projects # archive 1 (256 KiB blocks: faster)
sudo tar -cvf /dev/nst0 -b 512 /data/photos # archive 2, right after it
sudo mt -f /dev/nst0 rewind
sudo tar -tvf /dev/nst0 -b 512 # list archive 1
sudo mt -f /dev/nst0 fsf 1 # skip forward to archive 2
sudo tar -xvf /dev/nst0 -b 512 -C /restore # restore archive 2 into /restore
sudo mt -f /dev/nst0 eod # go to the end, to add a new archive
💡 Use the same block size (
-b 512) for reading as for writing.
dd: copy a tape file to disk
# copy archive 1 from tape to a file on disk (raw, block by block)
sudo mt -f /dev/nst0 rewind
sudo dd if=/dev/nst0 of=archive1.tar bs=256k status=progress
LTFS: use the tape like a USB disk
With the Linear Tape File System a tape gets a file system: you mount it and copy files with cp or a file manager. Handy for exchanging data or archiving. The device name depends on the LTFS version; IBM’s LTFS uses the SCSI generic device (/dev/sg3 here).
# format the tape for LTFS (this ERASES the tape)
sudo mkltfs -d /dev/sg3
# mount it, use it, unmount it
sudo mkdir -p /mnt/ltfs
sudo ltfs -o devname=/dev/sg3 /mnt/ltfs
cp -r ~/photos /mnt/ltfs/
sudo umount /mnt/ltfs # wait: this writes the index to the tape
sg3_utils: ask the drive itself
sudo sg_inq /dev/sg3 # what is this device? (vendor, model, firmware)
sudo sg_logs -a /dev/st0 | less # drive statistics, errors, tape usage
This article is partly copied from here.