]> git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/tests/functional/zvol/zvol_misc/zvol_misc_common.kshlib
New upstream version 0.7.9
[mirror_zfs-debian.git] / tests / zfs-tests / tests / functional / zvol / zvol_misc / zvol_misc_common.kshlib
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 #
38 function udev_wait
39 {
40 sleep 1
41 udevadm trigger --action=change
42 udevadm settle
43 for i in {1..3}; do
44 blkid="$(pgrep blkid | wc -l)"
45 zvol_id="$(pgrep zvol_id | wc -l)"
46 [[ "0" == "$zvol_id" && "0" == "$blkid" ]] && return
47 udevadm settle
48 done
49 log_fail "Wait timeout reached for udev_wait"
50 }
51
52 #
53 # Clean up udev status
54 # This is also a problem on "Amazon 2015.09 x86_64 Release (TEST)" where udev,
55 # sometimes, does not clean up /dev/zvol symlinks correctly for removed ZVOLs.
56 # Prune those links manually, then tell udev to forget them.
57 #
58 function udev_cleanup
59 {
60 log_note "Pruning broken ZVOL symlinks ..."
61 udevadm settle
62 # find all dangling links and delete them
63 find -L "${ZVOL_DEVDIR}" -type l -print -delete
64 # purge those links from udev database
65 udevadm info --cleanup-db
66 }
67
68 #
69 # Verify $device exists and is a block device
70 #
71 function blockdev_exists # device
72 {
73 typeset device="$1"
74
75 # we wait here instead of doing it in a wrapper around 'zfs set snapdev'
76 # because there are other commands (zfs snap, zfs inherit, zfs destroy)
77 # that can affect device nodes
78 for i in {1..3}; do
79 udev_wait
80 [[ -b "$device" ]] && return 0
81 done
82 log_fail "$device does not exist as a block device"
83 }
84
85 #
86 # Verify $device does not exist
87 #
88 function blockdev_missing # device
89 {
90 typeset device="$1"
91
92 # we wait here instead of doing it in a wrapper around 'zfs set snapdev'
93 # because there are other commands (zfs snap, zfs inherit, zfs destroy)
94 # that can affect device nodes
95 for i in {1..3}; do
96 udev_wait
97 [[ ! -e "$device" ]] && return 0
98 done
99 log_fail "$device exists when not expected"
100 }
101
102 #
103 # Verify $property on $dataset is inherited by $parent and is set to $value
104 #
105 function verify_inherited # property value dataset parent
106 {
107 typeset property="$1"
108 typeset value="$2"
109 typeset dataset="$3"
110 typeset parent="$4"
111
112 typeset val=$(get_prop "$property" "$dataset")
113 typeset src=$(get_source "$property" "$dataset")
114 if [[ "$val" != "$value" || "$src" != "inherited from $parent" ]]; then
115 log_fail "Dataset $dataset did not inherit $property properly:"\
116 "expected=$value, value=$val, source=$src."
117 fi
118 }
119
120 #
121 # Create a small partition on $device, then verify if we can access it
122 #
123 function verify_partition # device
124 {
125 typeset device="$1"
126
127 if [[ ! -b "$device" ]]; then
128 log_fail "$device is not a block device"
129 fi
130 # create a small dummy partition
131 set_partition 0 1 1m $device
132 # verify we can access the partition on the device
133 devname="$(readlink -f "$device")"
134 if is_linux; then
135 [[ -b "$devname""p1" ]]
136 else
137 [[ -b "$devname""s0" ]]
138 fi
139 return $?
140 }