#!/usr/bin/env bash set -euo pipefail DEVICE=$(readlink -f -- "${DEVICE}") REGEXP='^/dev/n?st([0-9]+)[alm]?$' if [[ ! $DEVICE =~ $REGEXP ]]; then echo "'$DEVICE' doesn't match" >&2 exit 1 fi NUM="${BASH_REMATCH[1]}" # sg_map opens devices and omits the Tape mapping while LTFS holds the SG device. # The kernel mapping remains available throughout mount and unmount. DEVICE_PATH=$(readlink -f -- "/sys/class/scsi_tape/st${NUM}/device") shopt -s nullglob SG_DEVICES=("${DEVICE_PATH}"/scsi_generic/sg*) if [[ ${#SG_DEVICES[@]} -ne 1 || ! -d ${SG_DEVICES[0]} ]]; then echo "'$DEVICE' has no unique SCSI generic device" >&2 exit 1 fi printf '/dev/%s\n' "${SG_DEVICES[0]##*/}"