xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/device_access/device_access.kshlib (revision 2f10ffc003be396f3fc23cd2888023896560252b)
1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# https://opensource.org/license/CDDL-1.0.
12#
13
14#
15# Copyright (c) 2026, TrueNAS.
16#
17
18. $STF_SUITE/include/libtest.shlib
19
20typeset -a _disks=($DISKS)
21typeset -r DEV1=/dev/${_disks[0]}
22typeset -r DEV2=/dev/${_disks[1]}
23typeset -r DEV3=/dev/${_disks[2]}
24
25typeset -A _saved_perms
26
27function save_perms { # path
28	typeset path="$1"
29	_saved_perms[$path]=$(stat -c '%a' "$path")
30}
31
32function restore_perms { #path
33	typeset path="$1"
34	if [[ -z ${_saved_perms[$path]} ]] ; then
35		log_fail "restore_perms($path): must call save_perms first"
36	fi
37	log_must chmod ${_saved_perms[$path]} "$path"
38}
39
40function check_vdevs { #pool, vdevs...
41	typeset pool="$1"
42	typeset want=$(echo "${@:2}" | xargs -n 1 | sort | xargs)
43	typeset have=$(zpool list -HvLP -o name "$pool" | grep /dev | sort | xargs)
44	log_must test "$want" == "$have"
45}
46
47function without_cap { #capability name, cmd...
48	typeset capname="$1"
49	capsh --drop=$capname -- -c "${*:2}"
50}
51
52function has_cap { #capability name
53	typeset capname="$1"
54	typeset capeff=$(grep ^CapEff /proc/self/status)
55	capsh --decode="${capeff##*:}" | grep -q $capname
56}
57
58function check_cap_support {
59	if ! command -v capsh > /dev/null ; then
60		log_unsupported "capsh program required to test device access"
61	fi
62	if ! has_cap cap_sys_admin ; then
63		log_unsupported "test user requires CAP_SYS_ADMIN capability"
64	fi
65	if ! has_cap cap_dac_override ; then
66		log_unsupported "test user requires CAP_DAC_OVERRIDE capability"
67	fi
68}
69