#!/bin/bash # # lstape - Tool to show information about tape devices # # Copyright IBM Corp. 2003, 2018 # # s390-tools is free software; you can redistribute it and/or modify # it under the terms of the MIT license. See LICENSE for details. # CMD=$(basename $0) SG_INQ=$(type -p sg_inq) function RequireArgument() { if [ $2 -eq 1 ]; then echo "The $1 option requires an argument" >&2 else echo "The $1 option requires $2 arguments" >&2 fi } function PrintUsage() { cat <<-EOD | cut -c2- :Usage: $(basename $0) [] : : : -h|--help : Print this help text. : --ccw-only|--scsi-only : Limit output to CCW or SCSI devices only. : --online|--offline : Show only devices that are either online or : offline (only one option is allowed). This : option only affects CCW devices. : -s|--shortid : Show only devices on channel subsytem 0 with : subchannel set 0 and remove the leading '0.0.' : from the displayed bus id (only affects the : output of CCW devices). : -V|--verbose : Print additional information that does not fit : into a single line (only affects the output for : SCSI devices). : -v|--version : Display the version of the tools package and : the lstape command. : :$(basename $0) without the --ccw-only option causes extra SAN traffic :for each SCSI tape or changer device by invoking the sg_inq command. EOD } function PrintVersion() { cat <<-EOD $CMD: version %S390_TOOLS_VERSION% Copyright IBM Corp. 2003, 2018 EOD } FLSEP="" SHORTID=false SHOWCCW=true VERBOSE=false SHOWSCSI=true FILTERONLINE=false FILTEROFFLINE=false while [ $# -ne 0 ]; do case $1 in -h|--help) PrintUsage exit 0 ;; --online) if $FILTEROFFLINE; then echo -n "Option --online and --offline " >&2 echo "are exclusive" >&2 exit 1 fi FILTERONLINE=true ;; --offline) if $FILTERONLINE; then echo -n "Option --online and --offline " >&2 echo "are exclusive" >&2 exit 1 fi FILTEROFFLINE=true ;; -s|--shortid) SHORTID=true ;; -v|--version) PrintVersion exit 0 ;; -V|--verbose) VERBOSE=true; ;; --ccw-only) if ! $SHOWCCW; then echo "ERROR: --ccw-only after --scsi-only!" >&2 exit 1 fi SHOWSCSI=false; ;; --scsi-only) if ! $SHOWSCSI; then echo "ERROR: --scsi-only after --ccw-only!" >&2 exit 1 fi SHOWCCW=false ;; -*|--*) echo "$CMD: Invalid option $1" >&2 echo "Try 'lstape --help' for more information." >&2 exit 1 ;; *) FILTERLIST="$FILTERLIST$FLSEP$1" FLSEP="," ;; esac shift done function SysfsCreateListCCW() { ( find $1/bus/ccw/drivers/tape_34xx -type l \ -printf '%f\n' 2>/dev/null ) | awk -v format="$CCWFORMAT" ' BEGIN{ medium_state_str[0] = "UNKNOWN " medium_state_str[1] = "LOADED " medium_state_str[2] = "UNLOADED" shortid = "'$SHORTID'"=="true" ? 1 : 0 filteronline = "'$FILTERONLINE'"=="true" ? 1 : 0 filteroffline = "'$FILTEROFFLINE'"=="true" ? 1 : 0 } function Read(file) { value = "" getline value &2 exit 1 ;; 2.3|2.4) SYSFS=false ;; *) SYSFS=true if [ "$(cat /proc/filesystems|grep sysfs)" = "" ]; then echo "WARNING: no sysfs support." >&2 SYSFS=false fi SYSFSDIR=$(cat /proc/mounts|awk '$3=="sysfs"{print $2; exit}') if [ "SYSFS" = "true" -a "$SYSFSDIR" = "" ]; then echo "WARNING: sysfs not mounted." >&2 SYSFS=false fi if [ "$SYSFS" = "false" -a "$SHOWCCW" = "true" ]; then echo -n "WARNING: proc interface will not find offline" echo " devices on kernel $(uname -r|cut -d. -f1-2)." echo fi ;; esac function ShowTapesCCW() { if $SYSFS; then SysfsCreateListCCW $SYSFSDIR else if [ ! -r /proc/tapedevices ]; then echo "ERROR: neither proc nor sysfs found!" >&2 return 1 fi cat /proc/tapedevices fi | awk -v LIST="$FILTERLIST" ' BEGIN{ if(LIST != "") split(LIST, A, ",") } LIST != ""{ for(i in A) { if(index($2, A[i]) > 0) { print $0 next } } next } { print $0 } ' } if $SHOWCCW; then CCWFORMAT="%-7s %-10s %-12s %-15s %-7s %-7s %-7s %-8s\n" LIST=$(ShowTapesCCW) if [ "$LIST" = "" ]; then NUMCCW=0 else NUMCCW=$(echo "$LIST"|wc -l) fi if $SHOWSCSI; then echo "FICON/ESCON tapes (found $NUMCCW):" fi printf "$CCWFORMAT" "TapeNo" "BusID" "CuType/Model" "DevType/Model" \ "BlkSize" "State" "Op" "MedState" if [ $NUMCCW -gt 0 ]; then echo "$LIST" fi fi if $SHOWSCSI; then SCSIFORMAT="%-7s %-13s %-12s %-8s %-16s %-8s %s\n" SCSIVFORMAT=" %-8s %-18s %s\n" LIST=$(SysfsCreateListSCSI $SYSFSDIR) if [ "$LIST" = "" ]; then NUMSCSI=0 else NUMSCSI=$(echo "$LIST"|wc -l) if $VERBOSE; then let "NUMSCSI/=2" fi fi if $SHOWCCW; then echo echo "SCSI tape devices (found $NUMSCSI):" fi printf "$SCSIFORMAT" "Generic" "Device" "Target" "Vendor" "Model" \ "Type" "State" if $VERBOSE; then printf "$SCSIVFORMAT" "HBA" "WWPN" "Serial" fi if [ $NUMSCSI -gt 0 ]; then echo "$LIST" fi fi exit 0