File: inject-smart.sh

package info (click to toggle)
ndctl 81-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,436 kB
  • sloc: ansic: 41,432; sh: 3,931; makefile: 28
file content (179 lines) | stat: -rwxr-xr-x 3,025 bytes parent folder | download | duplicates (3)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash -Ex
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018-2020 Intel Corporation. All rights reserved.

rc=77
. $(dirname $0)/common
bus="$NFIT_TEST_BUS0"
inj_val="42"

trap 'err $LINENO' ERR

# sample json:
# {
#  "dev":"nmem0",
#  "id":"cdab-0a-07e0-ffffffff",
#  "handle":0,
#  "phys_id":0,
#  "health":{
#    "health_state":"non-critical",
#    "temperature_celsius":23,
#    "spares_percentage":75,
#    "alarm_temperature":true,
#    "alarm_spares":true,
#    "temperature_threshold":40,
#    "spares_threshold":5,
#    "life_used_percentage":5,
#    "shutdown_state":"clean"
#  }
#}

translate_field()
{
	local in="$1"

	case $in in
	media-temperature)
		echo "temperature_celsius"
		;;
	ctrl-temperature)
		echo "controller_temperature_celsius"
		;;
	spares)
		echo "spares_percentage"
		;;
	media-temperature-alarm)
		echo "alarm_temperature"
		;;
	ctrl-temperature-alarm)
		echo "alarm_controller_temperature"
		;;
	spares-alarm)
		echo "alarm_spares"
		;;
	media-temperature-threshold)
		echo "temperature_threshold"
		;;
	spares-threshold)
		echo "spares_threshold"
		;;
	unsafe-shutdown)
		echo "shutdown_state"
		;;
	fatal)
		echo "health_state"
		;;
	*)
		# passthrough
		echo "$in"
		return
		;;
	esac
}

translate_val()
{
	local in="$1"

	case $in in
	dirty)
		;&
	fatal)
		;&
	true)
		echo "1"
		;;
	non-critical)
		;&
	clean)
		;&
	false)
		echo "0"
		;;
	*)
		# passthrough
		echo "$in"
		;;
	esac
}

get_field()
{
	local field="$1"
	local smart_listing="$(translate_field $field)"

	json="$($NDCTL list -b $bus -d $dimm -H)"
	val="$(jq -r ".[].dimms[].health.$smart_listing" <<< $json)"
	val="$(translate_val $val)"
	printf "%0.0f\n" "$val"
}

verify()
{
	local field="$1"
	local val="$(printf "%0.0f\n" "$2")"

	[[ "$val" == "$(get_field $field)" ]]
}

test_field()
{
	local field="$1"
	local val="$2"
	local op="$3"
	local old_val=""

	if [ -n "$val" ]; then
		inj_opt="--${field}=${val}"
	else
		inj_opt="--${field}"
	fi

	old_val=$(get_field $field)
	if [[ "$old_val" == "0" || "$old_val" == "1" ]]; then
		val=$(((old_val + 1) % 2))
	fi
	$NDCTL inject-smart -b $bus $dimm $inj_opt
	verify $field $val

	if [[ "$op" != "thresh" ]]; then
		$NDCTL inject-smart -b $bus --${field}-uninject $dimm
		verify $field $old_val
	fi
}

do_tests()
{
	local fields_val=(media-temperature spares)
	local fields_bool=(unsafe-shutdown fatal)
	local fields_thresh=(media-temperature-threshold spares-threshold)
	local field=""

	$NDCTL inject-smart -b $bus --uninject-all $dimm

	# start tests
	for field in "${fields_val[@]}"; do
		test_field $field $inj_val
	done

	for field in "${fields_bool[@]}"; do
		test_field $field
	done

	for field in "${fields_thresh[@]}"; do
		test_field $field $inj_val "thresh"
	done
}

check_min_kver "4.19" || do_skip "kernel $KVER may not support smart (un)injection"
check_prereq "jq"
modprobe nfit_test
rc=1

jlist=$($TEST_PATH/list-smart-dimm -b $bus)
dimm="$(jq '.[]."dev"?, ."dev"?' <<< $jlist | sort | head -1 | xargs)"
test -n "$dimm"

do_tests
_cleanup
exit 0