How can I determine how much space is remaining on an LTO tape?
If I seek to the end and check the status, I can see I am on file number 17, but what I do not know is how large each file was.
$ mt -f /dev/nst1 eod
$ mt -f /dev/nst1 status
SCSI 2 tape drive:
File number=17, block number=0, partition=0.
Tape block size 0 bytes. Density code 0x44 (LTO-3).
Soft error count since last status=0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
Short of reading the entire strip, how can I determine the used / free capacity of the tape?
You can find the remaining tape capacity in the SCSI logs, along with many other interesting tidbits such as compression ratio, read/write failures, drive/tape history, etc.
sg_logs -a /dev/nst1
This utility is available in distro packages commonly named
sg3-utils
orsg3_utils
.You're looking for lines such as:
Main partition remaining capacity (in MiB)
Megabytes written to tape
(subtract from uncompressed capacity)Data bytes written to media by WRITE commands
(subtract from uncompressed capacity)Failing that, you can try vendor-specific tools. For example HP has HPE Library & Tape Tools, which works on all HP-compatible* drives. Buried in its menus you can find the ability to create and view a report of a drive which offers all the info found in
sg_logs
and more.And failing all that, you can just write incompressible data until the end of tape and do the maths on the blocks written to figure what was remaining (goes without saying this is a bit nasty):
dd if=/dev/urandom of=/dev/nst1 bs=1M status=progress iflag=fullblock
or
openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | dd of=/dev/nst1 bs=1M status=progress iflag=fullblock
I've used
nst1
throughout this answer as that is the device in the original question, update as necessary.First of all: this is not going to be easy.
There is a chip in the tape cartridge that offers a wide variety of information. That chip is called MAM (media auxiliary memory). The information can be read using SCSI command "READ ATTRIBUTE" on the tape drive while the tape is loaded. One of the attributes is "REMAINING CAPACITY IN PARTITION". This contains the amount of bytes that can be written to the current tape partition. Which leads to the question: what is a tape partition? Recent versions of LTO (LTO 5 and newer) tapes can be partitioned (LTFS uses two partitions).
If your tape was partitioned, each of your partitions will have an amount of free space and you will need to loop over all partitions to get each partitions free space information.
If you want to know more about MAM, take a look at IBM's LTO-8 SCSI Reference that explains this in detail (section 6.5 starting on page 355 contains the interesting stuff). If you need a starting point on how to implement this, maybe you should take a look at maminfo or this issue in sg3_utils issue tracker.
I'm not sure this information is updated immediately after a write to the tape. Maybe it is only update on unmount which would make retrieving accurate information even harder. I'm also not sure how this behaves when you overwrite the tape. You'll probably have to figure this out on your own.
I am not sure but maybe i can offer you to use ibm's software ITDT , it has a lot of abilities to do on tape libraries and lto tapes , maybe you can have a look around that software , it has such ability as Retrieve and display cartridge usage and health information.
Here is one more software solution: https://github.com/Kevin-Nakamoto/LTO-CM-Read
This is an open-source Linux bash script which allows us to read MAM information via SCSI command. This is a kind of software wrapper for "READ ATTRIBUTE" command that Andreas has already suggested in the previous post. All you have to do is to install
sg3-utils
orsg3_utils
, and then run the script. You will see "Remaining Capacity In Partition" as well as "Maximum Capacity In Partition" as a result.On Linux all you need is sg3-utils or sg3_utils (the name depends on the distro).
sg_read_attr gives you everything you want, namely attribute:
You can get all available attributes using
To read the attributes: