xref: /aosp_15_r20/external/sg3_utils/examples/sg_persist_tst.sh (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1#!/bin/sh
2# This script is meant as an example of using the sg_persist utility
3# in the sg3_utils package. This script works as expected on the
4# author's Fujitsu MAM3184, Seagate ST373455 and ST9146803SS disks.
5#
6#  Version 2.0 20171104
7
8# N.B. make sure the device name is correct for your environment.
9
10key="123abc"
11key2="333aaa"
12kk=${key}
13rtype="1"
14verbose=""
15
16usage()
17{
18  echo "Usage: sg_persist_tst.sh [-e] [-h] [-s] [-v] <device>"
19  echo "  where:"
20  echo "    -e, --exclusive      exclusive access (def: write " \
21   "exclusive)"
22  echo "    -h, --help           print usage message"
23  echo "    -s, --second         use second key"
24  echo "    -v, --verbose        more verbose output"
25  echo "    -vv                  even more verbose output"
26  echo "    -vvv                 even more verbose output"
27  echo ""
28  echo "Test SCSI Persistent Reservations with sg_persist utility."
29  echo "Default key is ${key} and alternate, second key is ${key2} ."
30  echo "Should be harmless (unless one of those keys is already in use)."
31  echo "The APTPL bit is not set in the PR register so a power cycle"
32  echo "on the device will clear the reservation if this script stops"
33  echo "(or is stopped) before clearing it. Tape drives only seem to "
34  echo "support 'exclusive access' type (so use '-e')."
35}
36
37opt="$1"
38while test ! -z "$opt" -a -z "${opt##-*}"; do
39  opt=${opt#-}
40  case "$opt" in
41    e|-exclusive) rtype="3" ;;
42    h|-help) usage ; exit 0 ;;
43    s|-second) kk=${key2} ;;
44    vvv) verbose="-vvv" ;;
45    vv) verbose="-vv" ;;
46    v|-verbose) verbose="-v" ;;
47    *) echo "Unknown option: -$opt " ; exit 1 ;;
48  esac
49  shift
50  opt="$1"
51done
52
53if [ $# -lt 1 ]
54  then
55    usage
56    exit 1
57fi
58
59echo ">>> try to report capabilities:"
60sg_persist -c ${verbose} "$1"
61res=$?
62case "$res" in
63    0) ;;
64    1) echo "  syntax error" ;;
65    2) echo "  not ready" ;;
66    3) echo "  medium error" ;;
67    5) echo "  illegal request, report capabilities not supported?" ;;
68    6) echo "  unit attention" ;;
69    9) echo "  illegal request, Persistent Reserve (In) not supported" ;;
70    11) echo "  aborted command" ;;
71    15) echo "  file error with $1 " ;;
72    20) echo "  no sense" ;;
73    21) echo "  recovered error" ;;
74    33) echo "  timeout" ;;
75    97) echo "  response fails sanity" ;;
76    98) echo "  other SCSI error" ;;
77    99) echo "  other error" ;;
78    *) echo "  unknown exit status for sg_persist: $res" ;;
79esac
80echo ""
81sleep 1
82
83echo ">>> check if any keys are registered:"
84sg_persist --no-inquiry --read-keys ${verbose} "$1"
85sleep 1
86
87echo
88echo ">>> register a key:"
89sg_persist -n --out --register --param-sark=${kk} ${verbose} "$1"
90sleep 1
91
92echo
93echo ">>> now key ${kk} should be registered:"
94sg_persist -n --read-keys ${verbose} "$1"
95sleep 1
96
97echo
98echo ">>> reserve the device (based on key ${kk}):"
99sg_persist -n --out --reserve --param-rk=${kk} --prout-type=${rtype} ${verbose} "$1"
100sleep 1
101
102echo
103echo ">>> check if the device is reserved (it should be now):"
104sg_persist -n --read-reservation ${verbose} "$1"
105sleep 1
106
107echo
108echo ">>> try to 'read full status' (may not be supported):"
109sg_persist -n --read-full-status ${verbose} "$1"
110sleep 1
111
112echo
113echo ">>> now release reservation:"
114sg_persist -n --out --release --param-rk=${kk} --prout-type=${rtype} ${verbose} "$1"
115sleep 1
116
117echo
118echo ">>> check if the device is reserved (it should _not_ be now):"
119sg_persist -n --read-reservation ${verbose} "$1"
120sleep 1
121
122echo
123echo ">>> unregister key ${kk}:"
124sg_persist -n --out --register --param-rk=${kk} ${verbose} "$1"
125sleep 1
126
127echo
128echo ">>> now key ${kk} should not be registered:"
129sg_persist -n -k ${verbose} "$1"
130sleep 1
131