aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/common.sh.in
blob: 2583efcb209bb9b2e109fc481caba0629180a918 (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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/bin/bash
#
# Common support functions for testing scripts.  If a script-config
# files is available it will be sourced so in-tree kernel modules and
# utilities will be used.  If no script-config can be found then the
# installed kernel modules and utilities will be used.

basedir="$(dirname $0)"

SCRIPT_CONFIG=zfs-script-config.sh
if [ -f "${basedir}/../${SCRIPT_CONFIG}" ]; then
. "${basedir}/../${SCRIPT_CONFIG}"
else
KERNEL_MODULES=(zlib_deflate zlib_inflate)
MODULES=(spl splat zavl znvpair zunicode zcommon zfs)
fi

PROG="<define PROG>"
CLEANUP=
VERBOSE=
VERBOSE_FLAG=
FORCE=
FORCE_FLAG=
DUMP_LOG=
ERROR=
RAID0S=()
RAID10S=()
RAIDZS=()
RAIDZ2S=()
TESTS_RUN=${TESTS_RUN:-'*'}
TESTS_SKIP=${TESTS_SKIP:-}

prefix=@prefix@
exec_prefix=@exec_prefix@
libexecdir=@libexecdir@
pkglibexecdir=${libexecdir}/@PACKAGE@
bindir=@bindir@
sbindir=@sbindir@

ETCDIR=${ETCDIR:-/etc}
DEVDIR=${DEVDIR:-/dev/disk/zpool}
ZPOOLDIR=${ZPOOLDIR:-${pkglibexecdir}/zpool-config}
ZPIOSDIR=${ZPIOSDIR:-${pkglibexecdir}/zpios-test}
ZPIOSPROFILEDIR=${ZPIOSPROFILEDIR:-${pkglibexecdir}/zpios-profile}

ZDB=${ZDB:-${sbindir}/zdb}
ZFS=${ZFS:-${sbindir}/zfs}
ZINJECT=${ZINJECT:-${sbindir}/zinject}
ZPOOL=${ZPOOL:-${sbindir}/zpool}
ZPOOL_ID=${ZPOOL_ID:-${bindir}/zpool_id}
ZTEST=${ZTEST:-${sbindir}/ztest}
ZPIOS=${ZPIOS:-${sbindir}/zpios}

COMMON_SH=${COMMON_SH:-${pkglibexecdir}/common.sh}
ZFS_SH=${ZFS_SH:-${pkglibexecdir}/zfs.sh}
ZPOOL_CREATE_SH=${ZPOOL_CREATE_SH:-${pkglibexecdir}/zpool-create.sh}
ZPIOS_SH=${ZPIOS_SH:-${pkglibexecdir}/zpios.sh}
ZPIOS_SURVEY_SH=${ZPIOS_SURVEY_SH:-${pkglibexecdir}/zpios-survey.sh}

LDMOD=${LDMOD:-/sbin/modprobe}
LSMOD=${LSMOD:-/sbin/lsmod}
RMMOD=${RMMOD:-/sbin/rmmod}
INFOMOD=${INFOMOD:-/sbin/modinfo}
LOSETUP=${LOSETUP:-/sbin/losetup}
MDADM=${MDADM:-/sbin/mdadm}
PARTED=${PARTED:-/sbin/parted}
BLOCKDEV=${BLOCKDEV:-/sbin/blockdev}
LSSCSI=${LSSCSI:-/usr/bin/lsscsi}
SCSIRESCAN=${SCSIRESCAN:-/usr/bin/scsi-rescan}
SYSCTL=${SYSCTL:-/sbin/sysctl}
UDEVADM=${UDEVADM:-/sbin/udevadm}
AWK=${AWK:-/usr/bin/awk}

COLOR_BLACK="\033[0;30m"
COLOR_DK_GRAY="\033[1;30m"
COLOR_BLUE="\033[0;34m"
COLOR_LT_BLUE="\033[1;34m" 
COLOR_GREEN="\033[0;32m"
COLOR_LT_GREEN="\033[1;32m"
COLOR_CYAN="\033[0;36m"
COLOR_LT_CYAN="\033[1;36m"
COLOR_RED="\033[0;31m"
COLOR_LT_RED="\033[1;31m"
COLOR_PURPLE="\033[0;35m"
COLOR_LT_PURPLE="\033[1;35m"
COLOR_BROWN="\033[0;33m"
COLOR_YELLOW="\033[1;33m"
COLOR_LT_GRAY="\033[0;37m"
COLOR_WHITE="\033[1;37m"
COLOR_RESET="\033[0m"

die() {
	echo -e "${PROG}: $1" >&2
	exit 1
}

msg() {
	if [ ${VERBOSE} ]; then
		echo "$@"
	fi
}

pass() {
	echo -e "${COLOR_GREEN}Pass${COLOR_RESET}"
}

fail() {
	echo -e "${COLOR_RED}Fail${COLOR_RESET} ($1)"
	exit $1
}

skip() {
	echo -e "${COLOR_BROWN}Skip${COLOR_RESET}"
}

spl_dump_log() {
	${SYSCTL} -w kernel.spl.debug.dump=1 &>/dev/null
	local NAME=`dmesg | tail -n 1 | cut -f5 -d' '`
	${SPLBUILD}/cmd/spl ${NAME} >${NAME}.log
	echo
	echo "Dumped debug log: ${NAME}.log"
	tail -n1 ${NAME}.log
	echo
	return 0
}

check_modules() {
	local LOADED_MODULES=()
	local MISSING_MODULES=()

	for MOD in ${MODULES[*]}; do
		local NAME=`basename $MOD .ko`

		if ${LSMOD} | egrep -q "^${NAME}"; then
			LOADED_MODULES=(${NAME} ${LOADED_MODULES[*]})
		fi

		if [ ${INFOMOD} ${MOD} 2>/dev/null ]; then
			MISSING_MODULES=("\t${MOD}\n" ${MISSING_MODULES[*]})
		fi
	done

	if [ ${#LOADED_MODULES[*]} -gt 0 ]; then
		ERROR="Unload these modules with '${PROG} -u':\n"
		ERROR="${ERROR}${LOADED_MODULES[*]}"
		return 1
	fi

	if [ ${#MISSING_MODULES[*]} -gt 0 ]; then
		ERROR="The following modules can not be found,"
		ERROR="${ERROR} ensure your source trees are built:\n"
		ERROR="${ERROR}${MISSING_MODULES[*]}"
		return 1
	fi

	return 0
}

load_module() {
	local NAME=`basename $1 .ko`

	if [ ${VERBOSE} ]; then
		echo "Loading ${NAME} ($@)"
	fi

	${LDMOD} $* &>/dev/null || ERROR="Failed to load $1" return 1

	return 0
}

load_modules() {
	mkdir -p /etc/zfs

	for MOD in ${KERNEL_MODULES[*]}; do
		load_module ${MOD}
	done

	for MOD in ${MODULES[*]}; do
		local NAME=`basename ${MOD} .ko`
		local VALUE=

		for OPT in "$@"; do
			OPT_NAME=`echo ${OPT} | cut -f1 -d'='`

			if [ ${NAME} = "${OPT_NAME}" ]; then
				VALUE=`echo ${OPT} | cut -f2- -d'='`
			fi
		done

		load_module ${MOD} ${VALUE} || return 1
	done

	if [ ${VERBOSE} ]; then
		echo "Successfully loaded ZFS module stack"
	fi

	return 0
}

unload_module() {
	local NAME=`basename $1 .ko`

	if [ ${VERBOSE} ]; then
		echo "Unloading ${NAME} ($@)"
	fi

	${RMMOD} ${NAME} || ERROR="Failed to unload ${NAME}" return 1

	return 0
}

unload_modules() {
	local MODULES_REVERSE=( $(echo ${MODULES[@]} |
		${AWK} '{for (i=NF;i>=1;i--) printf $i" "} END{print ""}') )

	for MOD in ${MODULES_REVERSE[*]}; do
		local NAME=`basename ${MOD} .ko`
		local USE_COUNT=`${LSMOD} |
				egrep "^${NAME} "| ${AWK} '{print $3}'`

		if [ "${USE_COUNT}" = 0 ] ; then

			if [ "${DUMP_LOG}" -a ${NAME} = "spl" ]; then
				spl_dump_log
			fi

			unload_module ${MOD} || return 1
		fi
	done

	if [ ${VERBOSE} ]; then
		echo "Successfully unloaded ZFS module stack"
	fi

	return 0
}

#
# Check that the mdadm utilities are installed.
#
check_loop_utils() {
        test -f ${LOSETUP} || die "${LOSETUP} utility must be installed"
}


#
# Find and return an unused loopback device.
#
unused_loop_device() {
	for DEVICE in `ls -1 /dev/loop* 2>/dev/null`; do
		${LOSETUP} ${DEVICE} &>/dev/null
		if [ $? -ne 0 ]; then
			echo ${DEVICE}
			return
		fi
	done

	die "Error: Unable to find unused loopback device"
}

#
# This can be slightly dangerous because the loop devices we are
# cleaning up may not be ours.  However, if the devices are currently
# in use we will not be able to remove them, and we only remove
# devices which include 'zpool' in the name.  So any damage we might
# do should be limited to other zfs related testing.
#
cleanup_loop_devices() {
	local TMP_FILE=`mktemp`

	${LOSETUP} -a | tr -d '()' >${TMP_FILE}
	${AWK} -F":" -v losetup="$LOSETUP" \
	    '/zpool/ { system("losetup -d "$1) }' ${TMP_FILE}
	${AWK} -F" " '/zpool/ { system("rm -f "$3) }' ${TMP_FILE}

	rm -f ${TMP_FILE}
}

#
# Destroy the passed loopback devices, this is used when you know
# the names of the loopback devices.
#
destroy_loop_devices() {
	local LODEVICES="$1"

	msg "Destroying ${LODEVICES}"
	 ${LOSETUP} -d ${LODEVICES} || \
		die "Error $? destroying ${FILE} -> ${DEVICE} loopback"

	rm -f ${FILES}
	return 0
}

#
# Check that the mdadm utilities are installed.
#
check_md_utils() {
        test -f ${MDADM} || die "${MDADM} utility must be installed"
	test -f ${PARTED} || die "${PARTED} utility must be installed"
}

check_md_partitionable() {
	local LOFILE=`mktemp -p /tmp zpool-lo.XXXXXXXX`
	local LODEVICE=`unused_loop_device`
	local MDDEVICE=`unused_md_device`
	local RESULT=1

	check_md_utils

	rm -f ${LOFILE}
	dd if=/dev/zero of=${LOFILE} bs=1M count=0 seek=16 \
		&>/dev/null || return ${RESULT}

	msg "Creating ${LODEVICE} using ${LOFILE}"
	${LOSETUP} ${LODEVICE} ${LOFILE}
	if [ $? -ne 0 ]; then
		rm -f ${LOFILE}
		return ${RESULT}
	fi

	msg "Creating ${MDDEVICE} using ${LODEVICE}"
	${MDADM} --build ${MDDEVICE} --level=faulty \
		--raid-devices=1 ${LODEVICE} &>/dev/null
	if [ $? -ne 0 ]; then
		destroy_loop_devices ${LODEVICE}
		rm -f ${LOFILE}
		return ${RESULT}
	fi
	wait_udev ${MDDEVICE} 30

	${BLOCKDEV} --rereadpt ${MDDEVICE} 2>/dev/null
	RESULT=$?

	destroy_md_devices ${MDDEVICE}
	destroy_loop_devices ${LODEVICE}
	rm -f ${LOFILE}

	return ${RESULT}
}

#
# Find and return an unused md device.
#
unused_md_device() {
	for (( i=0; i<32; i++ )); do
		MDDEVICE=md${i}

		# Skip active devicesudo in /proc/mdstat.
		grep -q "${MDDEVICE} " /proc/mdstat && continue

		# Device doesn't exist, use it.
		if [ ! -e $/dev/{MDDEVICE} ]; then
			echo /dev/${MDDEVICE}
			return
		fi

		# Device exists but may not be in use.
		if [ -b /dev/${MDDEVICE} ]; then
			${MDADM} --detail /dev/${MDDEVICE} &>/dev/null
			if [ $? -eq 1 ]; then
				echo /dev/${MDDEVICE}
				return
			fi
		fi
        done

        die "Error: Unable to find unused md device"
}

#
# This can be slightly dangerous because it is possible the md devices
# we are cleaning up may not be ours.  However, if the devices are
# currently in use we will not be able to remove them, and even if
# we remove devices which were not out we do not zero the super block
# so you should be able to reconstruct them.
#
cleanup_md_devices() {
	destroy_md_devices "`ls /dev/md* 2>/dev/null | grep -v p`"
	udev_trigger
}

#
# Destroy the passed md devices, this is used when you know
# the names of the md devices.
#
destroy_md_devices() {
	local MDDEVICES="$1"

	msg "Destroying ${MDDEVICES}"
	for MDDEVICE in ${MDDEVICES}; do
		${MDADM} --stop ${MDDEVICE} &>/dev/null
		${MDADM} --remove ${MDDEVICE} &>/dev/null
		${MDADM} --detail ${MDDEVICE} &>/dev/null
	done

	return 0
}

#
# Check that the scsi utilities are installed.
#
check_sd_utils() {
	${INFOMOD} scsi_debug &>/dev/null || die "scsi_debug module required"
	test -f ${LSSCSI} || die "${LSSCSI} utility must be installed"
}

#
# Rescan the scsi bus for scsi_debug devices.  It is preferable to use the
# scsi-rescan tool if it is installed, but if it's not we can fall back to
# removing and readding the device manually.  This rescan will only effect
# the first scsi_debug device if scsi-rescan is missing.
#
scsi_rescan() {
	local AWK_SCRIPT="/scsi_debug/ { print \$1; exit }"

	if [ -f ${SCSIRESCAN} ]; then
		${SCSIRESCAN} --forcerescan --remove &>/dev/null
	else
		local SCSIID=`${LSSCSI} | ${AWK} "${AWK_SCRIPT}" | tr -d '[]'`
		local SCSIHOST=`echo ${SCSIID} | cut -f1 -d':'`
		echo 1 >"/sys/class/scsi_device/${SCSIID}/device/delete"
		udev_trigger
		echo "- - -" >/sys/class/scsi_host/host${SCSIHOST}/scan
		udev_trigger
	fi
}

#
# Trigger udev and wait for it to settle.
#
udev_trigger() {
	if [ -f ${UDEVADM} ]; then
		${UDEVADM} trigger
		${UDEVADM} settle
	else
		/sbin/udevtrigger
		/sbin/udevsettle
	fi
}

#
# The following udev helper functions assume that the provided
# udev rules file will create a /dev/disk/zpool/<CHANNEL><RANK>
# disk mapping.  In this mapping each CHANNEL is represented by
# the letters a-z, and the RANK is represented by the numbers
# 1-n.  A CHANNEL should identify a group of RANKS which are all
# attached to a single controller, each RANK represents a disk.
# This provides a simply mechanism to locate a specific drive
# given a known hardware configuration.
#
udev_setup() {
	local SRC_PATH=$1

	# When running in tree manually contruct symlinks in tree to
	# the proper devices.  Symlinks are installed for all entires
	# in the config file regardless of if that device actually
	# exists.  When installed as a package udev can be relied on for
	# this and it will only create links for devices which exist.
	if [ ${INTREE} ]; then
		PWD=`pwd`
		mkdir -p ${DEVDIR}/
		cd ${DEVDIR}/
		${AWK} '!/^#/ && /./ { system( \
			"ln -f -s /dev/disk/by-path/"$2" "$1";" \
			"ln -f -s /dev/disk/by-path/"$2"-part1 "$1"p1;" \
			"ln -f -s /dev/disk/by-path/"$2"-part9 "$1"p9;" \
			) }' $SRC_PATH
		cd ${PWD}
	else
		DST_FILE=`basename ${SRC_PATH} | cut -f1-2 -d'.'`
		DST_PATH=/etc/zfs/${DST_FILE}

		if [ -e ${DST_PATH} ]; then
			die "Error: Config ${DST_PATH} already exists"
		fi

		cp ${SRC_PATH} ${DST_PATH}
		udev_trigger
	fi

	return 0
}

udev_cleanup() {
	local SRC_PATH=$1

	if [ ${INTREE} ]; then
		PWD=`pwd`
		cd ${DEVDIR}/
		${AWK} '!/^#/ && /./ { system( \
			"rm -f "$1" "$1"p1 "$1"p9") }' $SRC_PATH
		cd ${PWD}
	fi

	return 0
}

udev_cr2d() {
	local CHANNEL=`echo "obase=16; $1+96" | bc`
	local RANK=$2

	printf "\x${CHANNEL}${RANK}"
}

udev_raid0_setup() {
	local RANKS=$1
	local CHANNELS=$2
	local IDX=0

	RAID0S=()
	for RANK in `seq 1 ${RANKS}`; do
		for CHANNEL in `seq 1 ${CHANNELS}`; do
			DISK=`udev_cr2d ${CHANNEL} ${RANK}`
			RAID0S[${IDX}]="${DEVDIR}/${DISK}"
			let IDX=IDX+1
		done
	done

	return 0
}

udev_raid10_setup() {
	local RANKS=$1
	local CHANNELS=$2
	local IDX=0

	RAID10S=()
	for RANK in `seq 1 ${RANKS}`; do
		for CHANNEL1 in `seq 1 2 ${CHANNELS}`; do
			let CHANNEL2=CHANNEL1+1
			DISK1=`udev_cr2d ${CHANNEL1} ${RANK}`
			DISK2=`udev_cr2d ${CHANNEL2} ${RANK}`
			GROUP="${DEVDIR}/${DISK1} ${DEVDIR}/${DISK2}"
			RAID10S[${IDX}]="mirror ${GROUP}"
			let IDX=IDX+1
		done
	done

	return 0
}

udev_raidz_setup() {
	local RANKS=$1
	local CHANNELS=$2

	RAIDZS=()
	for RANK in `seq 1 ${RANKS}`; do
		RAIDZ=("raidz")

		for CHANNEL in `seq 1 ${CHANNELS}`; do
			DISK=`udev_cr2d ${CHANNEL} ${RANK}`
			RAIDZ[${CHANNEL}]="${DEVDIR}/${DISK}"
		done

		RAIDZS[${RANK}]="${RAIDZ[*]}"
	done

	return 0
}

udev_raidz2_setup() {
	local RANKS=$1
	local CHANNELS=$2

	RAIDZ2S=()
	for RANK in `seq 1 ${RANKS}`; do
		RAIDZ2=("raidz2")

		for CHANNEL in `seq 1 ${CHANNELS}`; do
			DISK=`udev_cr2d ${CHANNEL} ${RANK}`
			RAIDZ2[${CHANNEL}]="${DEVDIR}/${DISK}"
		done

		RAIDZ2S[${RANK}]="${RAIDZ2[*]}"
	done

	return 0
}

run_one_test() {
	local TEST_NUM=$1
	local TEST_NAME=$2

	printf "%-4d %-34s " ${TEST_NUM} "${TEST_NAME}"
	test_${TEST_NUM}
}

skip_one_test() {
	local TEST_NUM=$1
	local TEST_NAME=$2

	printf "%-4d %-34s " ${TEST_NUM} "${TEST_NAME}"
	skip
}

run_test() {
	local TEST_NUM=$1
	local TEST_NAME=$2

	for i in ${TESTS_SKIP[@]}; do
		if [[ $i == ${TEST_NUM} ]] ; then
			skip_one_test ${TEST_NUM} "${TEST_NAME}"
			return 0
		fi
	done

	if [ "${TESTS_RUN[0]}" = "*" ]; then
		run_one_test ${TEST_NUM} "${TEST_NAME}"
	else
		for i in ${TESTS_RUN[@]}; do
			if [[ $i == ${TEST_NUM} ]] ; then
				run_one_test ${TEST_NUM} "${TEST_NAME}"
				return 0
			fi
		done

		skip_one_test ${TEST_NUM} "${TEST_NAME}"
	fi
}

wait_udev() {
	local DEVICE=$1
	local DELAY=$2
	local COUNT=0

	udev_trigger
	while [ ! -e ${DEVICE} ]; do
		if [ ${COUNT} -gt ${DELAY} ]; then
			return 1
		fi

		let COUNT=${COUNT}+1
	        sleep 1
	done

	return 0
}