]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/tests/functional/zvol/zvol_misc/zvol_misc_common.kshlib
Update ZTS to work on FreeBSD
[mirror_zfs.git] / tests / zfs-tests / tests / functional / zvol / zvol_misc / zvol_misc_common.kshlib
CommitLineData
cf8738d8 1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
24#
25
26. $STF_SUITE/include/libtest.shlib
27. $STF_SUITE/tests/functional/cli_root/zfs_set/zfs_set_common.kshlib
28. $STF_SUITE/tests/functional/zvol/zvol_common.shlib
29
30#
31# Wait for udev to settle, completely.
32# This is quite discomforting, but there's a race condition here
33# (Amazon 2015.09 x86_64 Release (TEST) is good at triggering this) where the
34# kernel tries to remove zvol device nodes while they're open by [blkid],
35# [zvol_id] or other udev related processes.
36# Calling 'udevadm settle' is not enough: wait for those processes "manually".
37#
38function udev_wait
39{
40 sleep 1
7839c4b5
MM
41 if is_freebsd; then
42 return 0
43 fi
cf8738d8 44 udevadm trigger --action=change
45 udevadm settle
46 for i in {1..3}; do
47 blkid="$(pgrep blkid | wc -l)"
48 zvol_id="$(pgrep zvol_id | wc -l)"
49 [[ "0" == "$zvol_id" && "0" == "$blkid" ]] && return
50 udevadm settle
51 done
52 log_fail "Wait timeout reached for udev_wait"
53}
54
55#
56# Clean up udev status
57# This is also a problem on "Amazon 2015.09 x86_64 Release (TEST)" where udev,
58# sometimes, does not clean up /dev/zvol symlinks correctly for removed ZVOLs.
59# Prune those links manually, then tell udev to forget them.
60#
61function udev_cleanup
62{
63 log_note "Pruning broken ZVOL symlinks ..."
64 udevadm settle
65 # find all dangling links and delete them
66 find -L "${ZVOL_DEVDIR}" -type l -print -delete
67 # purge those links from udev database
68 udevadm info --cleanup-db
69}
70
71#
72# Verify $device exists and is a block device
73#
74function blockdev_exists # device
75{
76 typeset device="$1"
77
78 # we wait here instead of doing it in a wrapper around 'zfs set snapdev'
79 # because there are other commands (zfs snap, zfs inherit, zfs destroy)
80 # that can affect device nodes
81 for i in {1..3}; do
82 udev_wait
83 [[ -b "$device" ]] && return 0
84 done
85 log_fail "$device does not exist as a block device"
86}
87
88#
89# Verify $device does not exist
90#
91function blockdev_missing # device
92{
93 typeset device="$1"
94
95 # we wait here instead of doing it in a wrapper around 'zfs set snapdev'
96 # because there are other commands (zfs snap, zfs inherit, zfs destroy)
97 # that can affect device nodes
98 for i in {1..3}; do
99 udev_wait
100 [[ ! -e "$device" ]] && return 0
101 done
102 log_fail "$device exists when not expected"
103}
104
105#
106# Verify $property on $dataset is inherited by $parent and is set to $value
107#
108function verify_inherited # property value dataset parent
109{
110 typeset property="$1"
111 typeset value="$2"
112 typeset dataset="$3"
113 typeset parent="$4"
114
115 typeset val=$(get_prop "$property" "$dataset")
116 typeset src=$(get_source "$property" "$dataset")
117 if [[ "$val" != "$value" || "$src" != "inherited from $parent" ]]; then
118 log_fail "Dataset $dataset did not inherit $property properly:"\
119 "expected=$value, value=$val, source=$src."
120 fi
121}
122
123#
124# Create a small partition on $device, then verify if we can access it
125#
126function verify_partition # device
127{
128 typeset device="$1"
129
130 if [[ ! -b "$device" ]]; then
131 log_fail "$device is not a block device"
132 fi
133 # create a small dummy partition
134 set_partition 0 1 1m $device
135 # verify we can access the partition on the device
136 devname="$(readlink -f "$device")"
137 if is_linux; then
138 [[ -b "$devname""p1" ]]
139 else
140 [[ -b "$devname""s0" ]]
141 fi
142 return $?
143}