aboutsummaryrefslogtreecommitdiff
path: root/examples/sg_persist_tst.sh
blob: a75f9973eb60eb886cfdcc7d203fb2c4d623f40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
# This script is meant as an example of using the sg_persist utility
# in the sg3_utils package. This script works as expected on the
# author's Fujitsu MAM3184, Seagate ST373455 and ST9146803SS disks.
#
#  Version 2.0 20171104

# N.B. make sure the device name is correct for your environment.

key="123abc"
key2="333aaa"
kk=${key}
rtype="1"
verbose=""

usage()
{
  echo "Usage: sg_persist_tst.sh [-e] [-h] [-s] [-v] <device>"
  echo "  where:"
  echo "    -e, --exclusive      exclusive access (def: write " \
   "exclusive)"
  echo "    -h, --help           print usage message"
  echo "    -s, --second         use second key"
  echo "    -v, --verbose        more verbose output"
  echo "    -vv                  even more verbose output"
  echo "    -vvv                 even more verbose output"
  echo ""
  echo "Test SCSI Persistent Reservations with sg_persist utility."
  echo "Default key is ${key} and alternate, second key is ${key2} ."
  echo "Should be harmless (unless one of those keys is already in use)."
  echo "The APTPL bit is not set in the PR register so a power cycle"
  echo "on the device will clear the reservation if this script stops"
  echo "(or is stopped) before clearing it. Tape drives only seem to "
  echo "support 'exclusive access' type (so use '-e')."
}

opt="$1"
while test ! -z "$opt" -a -z "${opt##-*}"; do
  opt=${opt#-}
  case "$opt" in
    e|-exclusive) rtype="3" ;;
    h|-help) usage ; exit 0 ;;
    s|-second) kk=${key2} ;;
    vvv) verbose="-vvv" ;;
    vv) verbose="-vv" ;;
    v|-verbose) verbose="-v" ;;
    *) echo "Unknown option: -$opt " ; exit 1 ;;
  esac
  shift
  opt="$1"
done

if [ $# -lt 1 ]
  then
    usage
    exit 1
fi

echo ">>> try to report capabilities:"
sg_persist -c ${verbose} "$1"
res=$?
case "$res" in
    0) ;;
    1) echo "  syntax error" ;;
    2) echo "  not ready" ;;
    3) echo "  medium error" ;;
    5) echo "  illegal request, report capabilities not supported?" ;;
    6) echo "  unit attention" ;;
    9) echo "  illegal request, Persistent Reserve (In) not supported" ;;
    11) echo "  aborted command" ;;
    15) echo "  file error with $1 " ;;
    20) echo "  no sense" ;;
    21) echo "  recovered error" ;;
    33) echo "  timeout" ;;
    97) echo "  response fails sanity" ;;
    98) echo "  other SCSI error" ;;
    99) echo "  other error" ;;
    *) echo "  unknown exit status for sg_persist: $res" ;;
esac
echo ""
sleep 1

echo ">>> check if any keys are registered:"
sg_persist --no-inquiry --read-keys ${verbose} "$1"
sleep 1

echo
echo ">>> register a key:"
sg_persist -n --out --register --param-sark=${kk} ${verbose} "$1"
sleep 1

echo
echo ">>> now key ${kk} should be registered:"
sg_persist -n --read-keys ${verbose} "$1"
sleep 1

echo
echo ">>> reserve the device (based on key ${kk}):"
sg_persist -n --out --reserve --param-rk=${kk} --prout-type=${rtype} ${verbose} "$1"
sleep 1

echo
echo ">>> check if the device is reserved (it should be now):"
sg_persist -n --read-reservation ${verbose} "$1"
sleep 1

echo
echo ">>> try to 'read full status' (may not be supported):"
sg_persist -n --read-full-status ${verbose} "$1"
sleep 1

echo
echo ">>> now release reservation:"
sg_persist -n --out --release --param-rk=${kk} --prout-type=${rtype} ${verbose} "$1"
sleep 1

echo
echo ">>> check if the device is reserved (it should _not_ be now):"
sg_persist -n --read-reservation ${verbose} "$1"
sleep 1

echo
echo ">>> unregister key ${kk}:"
sg_persist -n --out --register --param-rk=${kk} ${verbose} "$1"
sleep 1

echo
echo ">>> now key ${kk} should not be registered:"
sg_persist -n -k ${verbose} "$1"
sleep 1