]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/include/libtest.shlib
Fix typos in dbuf.c
[mirror_zfs.git] / tests / zfs-tests / include / libtest.shlib
CommitLineData
6bb24f4d
BB
1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2015 by Delphix. All rights reserved.
30#
31
32. ${STF_TOOLS}/include/logapi.shlib
33
6bb24f4d
BB
34# Determine if this is a Linux test system
35#
36# Return 0 if platform Linux, 1 if otherwise
37
38function is_linux
39{
40 if [[ $($UNAME -o) == "GNU/Linux" ]]; then
41 return 0
42 else
43 return 1
44 fi
45}
46
e676a196
BB
47# Determine if this is a 32-bit system
48#
49# Return 0 if platform is 32-bit, 1 if otherwise
50
51function is_32bit
52{
53 if [[ $(getconf LONG_BIT) == "32" ]]; then
54 return 0
55 else
56 return 1
57 fi
58}
59
6bb24f4d
BB
60# Determine whether a dataset is mounted
61#
62# $1 dataset name
63# $2 filesystem type; optional - defaulted to zfs
64#
65# Return 0 if dataset is mounted; 1 if unmounted; 2 on error
66
67function ismounted
68{
69 typeset fstype=$2
70 [[ -z $fstype ]] && fstype=zfs
71 typeset out dir name ret
72
73 case $fstype in
74 zfs)
75 if [[ "$1" == "/"* ]] ; then
76 for out in $($ZFS mount | $AWK '{print $2}'); do
77 [[ $1 == $out ]] && return 0
78 done
79 else
80 for out in $($ZFS mount | $AWK '{print $1}'); do
81 [[ $1 == $out ]] && return 0
82 done
83 fi
84 ;;
85 ufs|nfs)
86 out=$($DF -F $fstype $1 2>/dev/null)
87 ret=$?
88 (($ret != 0)) && return $ret
89
90 dir=${out%%\(*}
91 dir=${dir%% *}
92 name=${out##*\(}
93 name=${name%%\)*}
94 name=${name%% *}
95
96 [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
97 ;;
98 ext2)
99 out=$($DF -t $fstype $1 2>/dev/null)
100 return $?
101 ;;
102 zvol)
103 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
104 link=$(readlink -f $ZVOL_DEVDIR/$1)
105 [[ -n "$link" ]] && \
106 $MOUNT | $GREP -q "^$link" && \
107 return 0
108 fi
109 ;;
110 esac
111
112 return 1
113}
114
115# Return 0 if a dataset is mounted; 1 otherwise
116#
117# $1 dataset name
118# $2 filesystem type; optional - defaulted to zfs
119
120function mounted
121{
122 ismounted $1 $2
123 (($? == 0)) && return 0
124 return 1
125}
126
127# Return 0 if a dataset is unmounted; 1 otherwise
128#
129# $1 dataset name
130# $2 filesystem type; optional - defaulted to zfs
131
132function unmounted
133{
134 ismounted $1 $2
135 (($? == 1)) && return 0
136 return 1
137}
138
139# split line on ","
140#
141# $1 - line to split
142
143function splitline
144{
145 $ECHO $1 | $SED "s/,/ /g"
146}
147
148function default_setup
149{
150 default_setup_noexit "$@"
151
152 log_pass
153}
154
155#
156# Given a list of disks, setup storage pools and datasets.
157#
158function default_setup_noexit
159{
160 typeset disklist=$1
161 typeset container=$2
162 typeset volume=$3
3c67d83a 163 log_note begin default_setup_noexit
6bb24f4d
BB
164
165 if is_global_zone; then
166 if poolexists $TESTPOOL ; then
167 destroy_pool $TESTPOOL
168 fi
169 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
3c67d83a 170 log_note creating pool $TESTPOOL $disklist
6bb24f4d
BB
171 log_must $ZPOOL create -f $TESTPOOL $disklist
172 else
173 reexport_pool
174 fi
175
176 $RM -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
177 $MKDIR -p $TESTDIR || log_unresolved Could not create $TESTDIR
178
179 log_must $ZFS create $TESTPOOL/$TESTFS
180 log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
181
182 if [[ -n $container ]]; then
183 $RM -rf $TESTDIR1 || \
184 log_unresolved Could not remove $TESTDIR1
185 $MKDIR -p $TESTDIR1 || \
186 log_unresolved Could not create $TESTDIR1
187
188 log_must $ZFS create $TESTPOOL/$TESTCTR
189 log_must $ZFS set canmount=off $TESTPOOL/$TESTCTR
190 log_must $ZFS create $TESTPOOL/$TESTCTR/$TESTFS1
191 log_must $ZFS set mountpoint=$TESTDIR1 \
192 $TESTPOOL/$TESTCTR/$TESTFS1
193 fi
194
195 if [[ -n $volume ]]; then
196 if is_global_zone ; then
197 log_must $ZFS create -V $VOLSIZE $TESTPOOL/$TESTVOL
198 block_device_wait
199 else
200 log_must $ZFS create $TESTPOOL/$TESTVOL
201 fi
202 fi
203}
204
205#
206# Given a list of disks, setup a storage pool, file system and
207# a container.
208#
209function default_container_setup
210{
211 typeset disklist=$1
212
213 default_setup "$disklist" "true"
214}
215
216#
217# Given a list of disks, setup a storage pool,file system
218# and a volume.
219#
220function default_volume_setup
221{
222 typeset disklist=$1
223
224 default_setup "$disklist" "" "true"
225}
226
227#
228# Given a list of disks, setup a storage pool,file system,
229# a container and a volume.
230#
231function default_container_volume_setup
232{
233 typeset disklist=$1
234
235 default_setup "$disklist" "true" "true"
236}
237
238#
239# Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
240# filesystem
241#
242# $1 Existing filesystem or volume name. Default, $TESTFS
243# $2 snapshot name. Default, $TESTSNAP
244#
245function create_snapshot
246{
247 typeset fs_vol=${1:-$TESTFS}
248 typeset snap=${2:-$TESTSNAP}
249
250 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
251 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
252
253 if snapexists $fs_vol@$snap; then
254 log_fail "$fs_vol@$snap already exists."
255 fi
256 datasetexists $fs_vol || \
257 log_fail "$fs_vol must exist."
258
259 log_must $ZFS snapshot $fs_vol@$snap
260}
261
262#
263# Create a clone from a snapshot, default clone name is $TESTCLONE.
264#
265# $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
266# $2 Clone name, $TESTPOOL/$TESTCLONE is default.
267#
268function create_clone # snapshot clone
269{
270 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
271 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
272
273 [[ -z $snap ]] && \
274 log_fail "Snapshot name is undefined."
275 [[ -z $clone ]] && \
276 log_fail "Clone name is undefined."
277
278 log_must $ZFS clone $snap $clone
279}
280
281function default_mirror_setup
282{
283 default_mirror_setup_noexit $1 $2 $3
284
285 log_pass
286}
287
288#
289# Given a pair of disks, set up a storage pool and dataset for the mirror
290# @parameters: $1 the primary side of the mirror
291# $2 the secondary side of the mirror
292# @uses: ZPOOL ZFS TESTPOOL TESTFS
293function default_mirror_setup_noexit
294{
295 readonly func="default_mirror_setup_noexit"
296 typeset primary=$1
297 typeset secondary=$2
298
299 [[ -z $primary ]] && \
300 log_fail "$func: No parameters passed"
301 [[ -z $secondary ]] && \
302 log_fail "$func: No secondary partition passed"
303 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
304 log_must $ZPOOL create -f $TESTPOOL mirror $@
305 log_must $ZFS create $TESTPOOL/$TESTFS
306 log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
307}
308
309#
310# create a number of mirrors.
311# We create a number($1) of 2 way mirrors using the pairs of disks named
312# on the command line. These mirrors are *not* mounted
313# @parameters: $1 the number of mirrors to create
314# $... the devices to use to create the mirrors on
315# @uses: ZPOOL ZFS TESTPOOL
316function setup_mirrors
317{
318 typeset -i nmirrors=$1
319
320 shift
321 while ((nmirrors > 0)); do
322 log_must test -n "$1" -a -n "$2"
323 [[ -d /$TESTPOOL$nmirrors ]] && $RM -rf /$TESTPOOL$nmirrors
324 log_must $ZPOOL create -f $TESTPOOL$nmirrors mirror $1 $2
325 shift 2
326 ((nmirrors = nmirrors - 1))
327 done
328}
329
330#
331# create a number of raidz pools.
332# We create a number($1) of 2 raidz pools using the pairs of disks named
333# on the command line. These pools are *not* mounted
334# @parameters: $1 the number of pools to create
335# $... the devices to use to create the pools on
336# @uses: ZPOOL ZFS TESTPOOL
337function setup_raidzs
338{
339 typeset -i nraidzs=$1
340
341 shift
342 while ((nraidzs > 0)); do
343 log_must test -n "$1" -a -n "$2"
344 [[ -d /$TESTPOOL$nraidzs ]] && $RM -rf /$TESTPOOL$nraidzs
345 log_must $ZPOOL create -f $TESTPOOL$nraidzs raidz $1 $2
346 shift 2
347 ((nraidzs = nraidzs - 1))
348 done
349}
350
351#
352# Destroy the configured testpool mirrors.
353# the mirrors are of the form ${TESTPOOL}{number}
354# @uses: ZPOOL ZFS TESTPOOL
355function destroy_mirrors
356{
357 default_cleanup_noexit
358
359 log_pass
360}
361
362#
363# Given a minimum of two disks, set up a storage pool and dataset for the raid-z
364# $1 the list of disks
365#
366function default_raidz_setup
367{
368 typeset disklist="$*"
369 disks=(${disklist[*]})
370
371 if [[ ${#disks[*]} -lt 2 ]]; then
372 log_fail "A raid-z requires a minimum of two disks."
373 fi
374
375 [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
376 log_must $ZPOOL create -f $TESTPOOL raidz $1 $2 $3
377 log_must $ZFS create $TESTPOOL/$TESTFS
378 log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
379
380 log_pass
381}
382
383#
384# Common function used to cleanup storage pools and datasets.
385#
386# Invoked at the start of the test suite to ensure the system
387# is in a known state, and also at the end of each set of
388# sub-tests to ensure errors from one set of tests doesn't
389# impact the execution of the next set.
390
391function default_cleanup
392{
393 default_cleanup_noexit
394
395 log_pass
396}
397
398function default_cleanup_noexit
399{
400 typeset exclude=""
401 typeset pool=""
402 #
403 # Destroying the pool will also destroy any
404 # filesystems it contains.
405 #
406 if is_global_zone; then
407 $ZFS unmount -a > /dev/null 2>&1
408 [[ -z "$KEEP" ]] && KEEP="rpool"
409 exclude=`eval $ECHO \"'(${KEEP})'\"`
410 ALL_POOLS=$($ZPOOL list -H -o name \
411 | $GREP -v "$NO_POOLS" | $EGREP -v "$exclude")
412 # Here, we loop through the pools we're allowed to
413 # destroy, only destroying them if it's safe to do
414 # so.
415 while [ ! -z ${ALL_POOLS} ]
416 do
417 for pool in ${ALL_POOLS}
418 do
419 if safe_to_destroy_pool $pool ;
420 then
421 destroy_pool $pool
422 fi
423 ALL_POOLS=$($ZPOOL list -H -o name \
424 | $GREP -v "$NO_POOLS" \
425 | $EGREP -v "$exclude")
426 done
427 done
428
429 $ZFS mount -a
430 else
431 typeset fs=""
432 for fs in $($ZFS list -H -o name \
433 | $GREP "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
434 datasetexists $fs && \
435 log_must $ZFS destroy -Rf $fs
436 done
437
438 # Need cleanup here to avoid garbage dir left.
439 for fs in $($ZFS list -H -o name); do
440 [[ $fs == /$ZONE_POOL ]] && continue
441 [[ -d $fs ]] && log_must $RM -rf $fs/*
442 done
443
444 #
445 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
446 # the default value
447 #
448 for fs in $($ZFS list -H -o name); do
449 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
450 log_must $ZFS set reservation=none $fs
451 log_must $ZFS set recordsize=128K $fs
452 log_must $ZFS set mountpoint=/$fs $fs
453 typeset enc=""
454 enc=$(get_prop encryption $fs)
455 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
456 [[ "$enc" == "off" ]]; then
457 log_must $ZFS set checksum=on $fs
458 fi
459 log_must $ZFS set compression=off $fs
460 log_must $ZFS set atime=on $fs
461 log_must $ZFS set devices=off $fs
462 log_must $ZFS set exec=on $fs
463 log_must $ZFS set setuid=on $fs
464 log_must $ZFS set readonly=off $fs
465 log_must $ZFS set snapdir=hidden $fs
466 log_must $ZFS set aclmode=groupmask $fs
467 log_must $ZFS set aclinherit=secure $fs
468 fi
469 done
470 fi
471
472 [[ -d $TESTDIR ]] && \
473 log_must $RM -rf $TESTDIR
7050a65d
SV
474
475 disk1=${DISKS%% *}
476 if is_mpath_device $disk1; then
477 delete_partitions
478 fi
6bb24f4d
BB
479}
480
481
482#
483# Common function used to cleanup storage pools, file systems
484# and containers.
485#
486function default_container_cleanup
487{
488 if ! is_global_zone; then
489 reexport_pool
490 fi
491
492 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
493 [[ $? -eq 0 ]] && \
494 log_must $ZFS unmount $TESTPOOL/$TESTCTR/$TESTFS1
495
496 datasetexists $TESTPOOL/$TESTCTR/$TESTFS1 && \
497 log_must $ZFS destroy -R $TESTPOOL/$TESTCTR/$TESTFS1
498
499 datasetexists $TESTPOOL/$TESTCTR && \
500 log_must $ZFS destroy -Rf $TESTPOOL/$TESTCTR
501
502 [[ -e $TESTDIR1 ]] && \
503 log_must $RM -rf $TESTDIR1 > /dev/null 2>&1
504
505 default_cleanup
506}
507
508#
509# Common function used to cleanup snapshot of file system or volume. Default to
510# delete the file system's snapshot
511#
512# $1 snapshot name
513#
514function destroy_snapshot
515{
516 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
517
518 if ! snapexists $snap; then
519 log_fail "'$snap' does not existed."
520 fi
521
522 #
523 # For the sake of the value which come from 'get_prop' is not equal
524 # to the really mountpoint when the snapshot is unmounted. So, firstly
525 # check and make sure this snapshot's been mounted in current system.
526 #
527 typeset mtpt=""
528 if ismounted $snap; then
529 mtpt=$(get_prop mountpoint $snap)
530 (($? != 0)) && \
531 log_fail "get_prop mountpoint $snap failed."
532 fi
533
534 log_must $ZFS destroy $snap
535 [[ $mtpt != "" && -d $mtpt ]] && \
536 log_must $RM -rf $mtpt
537}
538
539#
540# Common function used to cleanup clone.
541#
542# $1 clone name
543#
544function destroy_clone
545{
546 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
547
548 if ! datasetexists $clone; then
549 log_fail "'$clone' does not existed."
550 fi
551
552 # With the same reason in destroy_snapshot
553 typeset mtpt=""
554 if ismounted $clone; then
555 mtpt=$(get_prop mountpoint $clone)
556 (($? != 0)) && \
557 log_fail "get_prop mountpoint $clone failed."
558 fi
559
560 log_must $ZFS destroy $clone
561 [[ $mtpt != "" && -d $mtpt ]] && \
562 log_must $RM -rf $mtpt
563}
564
565# Return 0 if a snapshot exists; $? otherwise
566#
567# $1 - snapshot name
568
569function snapexists
570{
571 $ZFS list -H -t snapshot "$1" > /dev/null 2>&1
572 return $?
573}
574
575#
576# Set a property to a certain value on a dataset.
577# Sets a property of the dataset to the value as passed in.
578# @param:
579# $1 dataset who's property is being set
580# $2 property to set
581# $3 value to set property to
582# @return:
583# 0 if the property could be set.
584# non-zero otherwise.
585# @use: ZFS
586#
587function dataset_setprop
588{
589 typeset fn=dataset_setprop
590
591 if (($# < 3)); then
592 log_note "$fn: Insufficient parameters (need 3, had $#)"
593 return 1
594 fi
595 typeset output=
596 output=$($ZFS set $2=$3 $1 2>&1)
597 typeset rv=$?
598 if ((rv != 0)); then
599 log_note "Setting property on $1 failed."
600 log_note "property $2=$3"
601 log_note "Return Code: $rv"
602 log_note "Output: $output"
603 return $rv
604 fi
605 return 0
606}
607
608#
609# Assign suite defined dataset properties.
610# This function is used to apply the suite's defined default set of
611# properties to a dataset.
612# @parameters: $1 dataset to use
613# @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
614# @returns:
615# 0 if the dataset has been altered.
616# 1 if no pool name was passed in.
617# 2 if the dataset could not be found.
618# 3 if the dataset could not have it's properties set.
619#
620function dataset_set_defaultproperties
621{
622 typeset dataset="$1"
623
624 [[ -z $dataset ]] && return 1
625
626 typeset confset=
627 typeset -i found=0
628 for confset in $($ZFS list); do
629 if [[ $dataset = $confset ]]; then
630 found=1
631 break
632 fi
633 done
634 [[ $found -eq 0 ]] && return 2
635 if [[ -n $COMPRESSION_PROP ]]; then
636 dataset_setprop $dataset compression $COMPRESSION_PROP || \
637 return 3
638 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
639 fi
640 if [[ -n $CHECKSUM_PROP ]]; then
641 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
642 return 3
643 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
644 fi
645 return 0
646}
647
648#
649# Check a numeric assertion
650# @parameter: $@ the assertion to check
651# @output: big loud notice if assertion failed
652# @use: log_fail
653#
654function assert
655{
656 (($@)) || log_fail "$@"
657}
658
659#
660# Function to format partition size of a disk
661# Given a disk cxtxdx reduces all partitions
662# to 0 size
663#
664function zero_partitions #<whole_disk_name>
665{
666 typeset diskname=$1
667 typeset i
668
669 if is_linux; then
670 log_must $FORMAT $DEV_DSKDIR/$diskname -s -- mklabel gpt
671 else
672 for i in 0 1 3 4 5 6 7
673 do
674 set_partition $i "" 0mb $diskname
675 done
676 fi
677}
678
679#
680# Given a slice, size and disk, this function
681# formats the slice to the specified size.
682# Size should be specified with units as per
683# the `format` command requirements eg. 100mb 3gb
684#
685# NOTE: This entire interface is problematic for the Linux parted utilty
686# which requires the end of the partition to be specified. It would be
687# best to retire this interface and replace it with something more flexible.
688# At the moment a best effort is made.
689#
690function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
691{
692 typeset -i slicenum=$1
693 typeset start=$2
694 typeset size=$3
695 typeset disk=$4
696 [[ -z $slicenum || -z $size || -z $disk ]] && \
697 log_fail "The slice, size or disk name is unspecified."
698
699 if is_linux; then
700 typeset size_mb=${size%%[mMgG]}
701
702 size_mb=${size_mb%%[mMgG][bB]}
703 if [[ ${size:1:1} == 'g' ]]; then
704 ((size_mb = size_mb * 1024))
705 fi
706
707 # Create GPT partition table when setting slice 0 or
708 # when the device doesn't already contain a GPT label.
709 $FORMAT $DEV_DSKDIR/$disk -s -- print 1 >/dev/null
710 typeset ret_val=$?
711 if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then
712 log_must $FORMAT $DEV_DSKDIR/$disk -s -- mklabel gpt
713 fi
714
715 # When no start is given align on the first cylinder.
716 if [[ -z "$start" ]]; then
717 start=1
718 fi
719
720 # Determine the cylinder size for the device and using
721 # that calculate the end offset in cylinders.
722 typeset -i cly_size_kb=0
723 cly_size_kb=$($FORMAT -m $DEV_DSKDIR/$disk -s -- \
724 unit cyl print | $HEAD -3 | $TAIL -1 | \
725 $AWK -F '[:k.]' '{print $4}')
726 ((end = (size_mb * 1024 / cly_size_kb) + start))
727
728 log_must $FORMAT $DEV_DSKDIR/$disk -s -- \
729 mkpart part$slicenum ${start}cyl ${end}cyl
730
731 $BLOCKDEV --rereadpt $DEV_DSKDIR/$disk 2>/dev/null
732 block_device_wait
733 else
734 typeset format_file=/var/tmp/format_in.$$
735
736 $ECHO "partition" >$format_file
737 $ECHO "$slicenum" >> $format_file
738 $ECHO "" >> $format_file
739 $ECHO "" >> $format_file
740 $ECHO "$start" >> $format_file
741 $ECHO "$size" >> $format_file
742 $ECHO "label" >> $format_file
743 $ECHO "" >> $format_file
744 $ECHO "q" >> $format_file
745 $ECHO "q" >> $format_file
746
747 $FORMAT -e -s -d $disk -f $format_file
748 fi
749 typeset ret_val=$?
750 $RM -f $format_file
751 [[ $ret_val -ne 0 ]] && \
752 log_fail "Unable to format $disk slice $slicenum to $size"
753 return 0
754}
755
7050a65d
SV
756#
757# Delete all partitions on all disks - this is specifically for the use of multipath
758# devices which currently can only be used in the test suite as raw/un-partitioned
759# devices (ie a zpool cannot be created on a whole mpath device that has partitions)
760#
761function delete_partitions
762{
763 typeset -i j=1
764
765 if [[ -z $DISK_ARRAY_NUM ]]; then
766 DISK_ARRAY_NUM=$($ECHO ${DISKS} | $NAWK '{print NF}')
767 fi
768 if [[ -z $DISKSARRAY ]]; then
769 DISKSARRAY=$DISKS
770 fi
771
772 if is_linux; then
773 if (( $DISK_ARRAY_NUM == 1 )); then
774 while ((j < MAX_PARTITIONS)); do
775 $FORMAT $DEV_DSKDIR/$DISK -s rm $j > /dev/null 2>&1
776 if (( $? == 1 )); then
777 $LSBLK | $EGREP ${DISK}${SLICE_PREFIX}${j} > /dev/null
778 if (( $? == 1 )); then
779 log_note "Partitions for $DISK should be deleted"
780 else
781 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
782 fi
783 return 0
784 else
785 $LSBLK | $EGREP ${DISK}${SLICE_PREFIX}${j} > /dev/null
786 if (( $? == 0 )); then
787 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
788 fi
789 fi
790 ((j = j+1))
791 done
792 else
793 for disk in `$ECHO $DISKSARRAY`; do
794 while ((j < MAX_PARTITIONS)); do
795 $FORMAT $DEV_DSKDIR/$disk -s rm $j > /dev/null 2>&1
796 if (( $? == 1 )); then
797 $LSBLK | $EGREP ${disk}${SLICE_PREFIX}${j} > /dev/null
798 if (( $? == 1 )); then
799 log_note "Partitions for $disk should be deleted"
800 else
801 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
802 fi
803 j=7
804 else
805 $LSBLK | $EGREP ${disk}${SLICE_PREFIX}${j} > /dev/null
806 if (( $? == 0 )); then
807 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
808 fi
809 fi
810 ((j = j+1))
811 done
812 j=1
813 done
814 fi
815 fi
816 return 0
817}
818
6bb24f4d
BB
819#
820# Get the end cyl of the given slice
821#
822function get_endslice #<disk> <slice>
823{
824 typeset disk=$1
825 typeset slice=$2
826 if [[ -z $disk || -z $slice ]] ; then
827 log_fail "The disk name or slice number is unspecified."
828 fi
829
830 if is_linux; then
831 endcyl=$($FORMAT -s $DEV_DSKDIR/$disk -- unit cyl print | \
832 $GREP "part${slice}" | \
833 $AWK '{print $3}' | \
834 $SED 's,cyl,,')
835 ((endcyl = (endcyl + 1)))
836 else
837 disk=${disk#/dev/dsk/}
838 disk=${disk#/dev/rdsk/}
839 disk=${disk%s*}
840
841 typeset -i ratio=0
842 ratio=$($PRTVTOC /dev/rdsk/${disk}s2 | \
843 $GREP "sectors\/cylinder" | \
844 $AWK '{print $2}')
845
846 if ((ratio == 0)); then
847 return
848 fi
849
850 typeset -i endcyl=$($PRTVTOC -h /dev/rdsk/${disk}s2 |
851 $NAWK -v token="$slice" '{if ($1==token) print $6}')
852
853 ((endcyl = (endcyl + 1) / ratio))
854 fi
855
856 echo $endcyl
857}
858
859
860#
861# Given a size,disk and total slice number, this function formats the
862# disk slices from 0 to the total slice number with the same specified
863# size.
864#
865function partition_disk #<slice_size> <whole_disk_name> <total_slices>
866{
867 typeset -i i=0
868 typeset slice_size=$1
869 typeset disk_name=$2
870 typeset total_slices=$3
871 typeset cyl
872
873 zero_partitions $disk_name
874 while ((i < $total_slices)); do
875 if ! is_linux; then
876 if ((i == 2)); then
877 ((i = i + 1))
878 continue
879 fi
880 fi
881 set_partition $i "$cyl" $slice_size $disk_name
882 cyl=$(get_endslice $disk_name $i)
883 ((i = i+1))
884 done
885}
886
887#
888# This function continues to write to a filenum number of files into dirnum
889# number of directories until either $FILE_WRITE returns an error or the
890# maximum number of files per directory have been written.
891#
892# Usage:
893# fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
894#
895# Return value: 0 on success
896# non 0 on error
897#
898# Where :
899# destdir: is the directory where everything is to be created under
900# dirnum: the maximum number of subdirectories to use, -1 no limit
901# filenum: the maximum number of files per subdirectory
902# bytes: number of bytes to write
903# num_writes: numer of types to write out bytes
904# data: the data that will be writen
905#
906# E.g.
907# file_fs /testdir 20 25 1024 256 0
908#
909# Note: bytes * num_writes equals the size of the testfile
910#
911function fill_fs # destdir dirnum filenum bytes num_writes data
912{
913 typeset destdir=${1:-$TESTDIR}
914 typeset -i dirnum=${2:-50}
915 typeset -i filenum=${3:-50}
916 typeset -i bytes=${4:-8192}
917 typeset -i num_writes=${5:-10240}
918 typeset -i data=${6:-0}
919
920 typeset -i odirnum=1
921 typeset -i idirnum=0
922 typeset -i fn=0
923 typeset -i retval=0
924
925 log_must $MKDIR -p $destdir/$idirnum
926 while (($odirnum > 0)); do
927 if ((dirnum >= 0 && idirnum >= dirnum)); then
928 odirnum=0
929 break
930 fi
931 $FILE_WRITE -o create -f $destdir/$idirnum/$TESTFILE.$fn \
932 -b $bytes -c $num_writes -d $data
933 retval=$?
934 if (($retval != 0)); then
935 odirnum=0
936 break
937 fi
938 if (($fn >= $filenum)); then
939 fn=0
940 ((idirnum = idirnum + 1))
941 log_must $MKDIR -p $destdir/$idirnum
942 else
943 ((fn = fn + 1))
944 fi
945 done
946 return $retval
947}
948
949#
950# Simple function to get the specified property. If unable to
951# get the property then exits.
952#
953# Note property is in 'parsable' format (-p)
954#
955function get_prop # property dataset
956{
957 typeset prop_val
958 typeset prop=$1
959 typeset dataset=$2
960
961 prop_val=$($ZFS get -pH -o value $prop $dataset 2>/dev/null)
962 if [[ $? -ne 0 ]]; then
963 log_note "Unable to get $prop property for dataset " \
964 "$dataset"
965 return 1
966 fi
967
968 $ECHO $prop_val
969 return 0
970}
971
972#
973# Simple function to get the specified property of pool. If unable to
974# get the property then exits.
975#
976function get_pool_prop # property pool
977{
978 typeset prop_val
979 typeset prop=$1
980 typeset pool=$2
981
982 if poolexists $pool ; then
983 prop_val=$($ZPOOL get $prop $pool 2>/dev/null | $TAIL -1 | \
984 $AWK '{print $3}')
985 if [[ $? -ne 0 ]]; then
986 log_note "Unable to get $prop property for pool " \
987 "$pool"
988 return 1
989 fi
990 else
991 log_note "Pool $pool not exists."
992 return 1
993 fi
994
995 $ECHO $prop_val
996 return 0
997}
998
999# Return 0 if a pool exists; $? otherwise
1000#
1001# $1 - pool name
1002
1003function poolexists
1004{
1005 typeset pool=$1
1006
1007 if [[ -z $pool ]]; then
1008 log_note "No pool name given."
1009 return 1
1010 fi
1011
1012 $ZPOOL get name "$pool" > /dev/null 2>&1
1013 return $?
1014}
1015
1016# Return 0 if all the specified datasets exist; $? otherwise
1017#
1018# $1-n dataset name
1019function datasetexists
1020{
1021 if (($# == 0)); then
1022 log_note "No dataset name given."
1023 return 1
1024 fi
1025
1026 while (($# > 0)); do
1027 $ZFS get name $1 > /dev/null 2>&1 || \
1028 return $?
1029 shift
1030 done
1031
1032 return 0
1033}
1034
1035# return 0 if none of the specified datasets exists, otherwise return 1.
1036#
1037# $1-n dataset name
1038function datasetnonexists
1039{
1040 if (($# == 0)); then
1041 log_note "No dataset name given."
1042 return 1
1043 fi
1044
1045 while (($# > 0)); do
1046 $ZFS list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
1047 && return 1
1048 shift
1049 done
1050
1051 return 0
1052}
1053
1054#
2f71caf2 1055# Given a mountpoint, or a dataset name, determine if it is shared via NFS.
6bb24f4d
BB
1056#
1057# Returns 0 if shared, 1 otherwise.
1058#
1059function is_shared
1060{
1061 typeset fs=$1
1062 typeset mtpt
1063
6bb24f4d
BB
1064 if [[ $fs != "/"* ]] ; then
1065 if datasetnonexists "$fs" ; then
1066 return 1
1067 else
1068 mtpt=$(get_prop mountpoint "$fs")
1069 case $mtpt in
1070 none|legacy|-) return 1
1071 ;;
1072 *) fs=$mtpt
1073 ;;
1074 esac
1075 fi
1076 fi
1077
2f71caf2 1078 if is_linux; then
1079 for mtpt in `$SHARE | $AWK '{print $1}'` ; do
1080 if [[ $mtpt == $fs ]] ; then
1081 return 0
1082 fi
1083 done
1084 return 1
1085 fi
1086
6bb24f4d
BB
1087 for mtpt in `$SHARE | $AWK '{print $2}'` ; do
1088 if [[ $mtpt == $fs ]] ; then
1089 return 0
1090 fi
1091 done
1092
1093 typeset stat=$($SVCS -H -o STA nfs/server:default)
1094 if [[ $stat != "ON" ]]; then
1095 log_note "Current nfs/server status: $stat"
1096 fi
1097
1098 return 1
1099}
1100
1101#
2f71caf2 1102# Given a dataset name determine if it is shared via SMB.
6bb24f4d 1103#
2f71caf2 1104# Returns 0 if shared, 1 otherwise.
6bb24f4d 1105#
2f71caf2 1106function is_shared_smb
6bb24f4d
BB
1107{
1108 typeset fs=$1
2f71caf2 1109 typeset mtpt
1110
1111 if datasetnonexists "$fs" ; then
1112 return 1
1113 else
1114 fs=$(echo $fs | sed 's@/@_@g')
1115 fi
6bb24f4d
BB
1116
1117 if is_linux; then
2f71caf2 1118 for mtpt in `$NET usershare list | $AWK '{print $1}'` ; do
1119 if [[ $mtpt == $fs ]] ; then
1120 return 0
1121 fi
1122 done
1123 return 1
1124 else
6bb24f4d
BB
1125 log_unsupported "Currently unsupported by the test framework"
1126 return 1
1127 fi
2f71caf2 1128}
1129
1130#
1131# Given a mountpoint, determine if it is not shared via NFS.
1132#
1133# Returns 0 if not shared, 1 otherwise.
1134#
1135function not_shared
1136{
1137 typeset fs=$1
6bb24f4d
BB
1138
1139 is_shared $fs
1140 if (($? == 0)); then
1141 return 1
1142 fi
1143
1144 return 0
1145}
1146
1147#
2f71caf2 1148# Given a dataset determine if it is not shared via SMB.
6bb24f4d 1149#
2f71caf2 1150# Returns 0 if not shared, 1 otherwise.
1151#
1152function not_shared_smb
6bb24f4d
BB
1153{
1154 typeset fs=$1
1155
2f71caf2 1156 is_shared_smb $fs
1157 if (($? == 0)); then
6bb24f4d
BB
1158 return 1
1159 fi
1160
2f71caf2 1161 return 0
1162}
1163
1164#
1165# Helper function to unshare a mountpoint.
1166#
1167function unshare_fs #fs
1168{
1169 typeset fs=$1
1170
1171 is_shared $fs || is_shared_smb $fs
6bb24f4d
BB
1172 if (($? == 0)); then
1173 log_must $ZFS unshare $fs
1174 fi
1175
1176 return 0
1177}
1178
2f71caf2 1179#
1180# Helper function to share a NFS mountpoint.
1181#
1182function share_nfs #fs
1183{
1184 typeset fs=$1
1185
1186 if is_linux; then
1187 is_shared $fs
1188 if (($? != 0)); then
1189 log_must $SHARE "*:$fs"
1190 fi
1191 else
1192 is_shared $fs
1193 if (($? != 0)); then
1194 log_must $SHARE -F nfs $fs
1195 fi
1196 fi
1197
1198 return 0
1199}
1200
1201#
1202# Helper function to unshare a NFS mountpoint.
1203#
1204function unshare_nfs #fs
1205{
1206 typeset fs=$1
1207
1208 if is_linux; then
1209 is_shared $fs
1210 if (($? == 0)); then
1211 log_must $UNSHARE -u "*:$fs"
1212 fi
1213 else
1214 is_shared $fs
1215 if (($? == 0)); then
1216 log_must $UNSHARE -F nfs $fs
1217 fi
1218 fi
1219
1220 return 0
1221}
1222
1223#
1224# Helper function to show NFS shares.
1225#
1226function showshares_nfs
1227{
1228 if is_linux; then
1229 $SHARE -v
1230 else
1231 $SHARE -F nfs
1232 fi
1233
1234 return 0
1235}
1236
1237#
1238# Helper function to show SMB shares.
1239#
1240function showshares_smb
1241{
1242 if is_linux; then
1243 $NET usershare list
1244 else
1245 $SHARE -F smb
1246 fi
1247
1248 return 0
1249}
1250
6bb24f4d
BB
1251#
1252# Check NFS server status and trigger it online.
1253#
1254function setup_nfs_server
1255{
1256 # Cannot share directory in non-global zone.
1257 #
1258 if ! is_global_zone; then
1259 log_note "Cannot trigger NFS server by sharing in LZ."
1260 return
1261 fi
1262
1263 if is_linux; then
2f71caf2 1264 log_note "NFS server must started prior to running test framework."
6bb24f4d
BB
1265 return
1266 fi
1267
1268 typeset nfs_fmri="svc:/network/nfs/server:default"
1269 if [[ $($SVCS -Ho STA $nfs_fmri) != "ON" ]]; then
1270 #
1271 # Only really sharing operation can enable NFS server
1272 # to online permanently.
1273 #
1274 typeset dummy=/tmp/dummy
1275
1276 if [[ -d $dummy ]]; then
1277 log_must $RM -rf $dummy
1278 fi
1279
1280 log_must $MKDIR $dummy
1281 log_must $SHARE $dummy
1282
1283 #
1284 # Waiting for fmri's status to be the final status.
1285 # Otherwise, in transition, an asterisk (*) is appended for
1286 # instances, unshare will reverse status to 'DIS' again.
1287 #
1288 # Waiting for 1's at least.
1289 #
1290 log_must $SLEEP 1
1291 timeout=10
1292 while [[ timeout -ne 0 && $($SVCS -Ho STA $nfs_fmri) == *'*' ]]
1293 do
1294 log_must $SLEEP 1
1295
1296 ((timeout -= 1))
1297 done
1298
1299 log_must $UNSHARE $dummy
1300 log_must $RM -rf $dummy
1301 fi
1302
1303 log_note "Current NFS status: '$($SVCS -Ho STA,FMRI $nfs_fmri)'"
1304}
1305
1306#
1307# To verify whether calling process is in global zone
1308#
1309# Return 0 if in global zone, 1 in non-global zone
1310#
1311function is_global_zone
1312{
1313 typeset cur_zone=$($ZONENAME 2>/dev/null)
1314 if [[ $cur_zone != "global" ]]; then
1315 return 1
1316 fi
1317 return 0
1318}
1319
1320#
1321# Verify whether test is permitted to run from
1322# global zone, local zone, or both
1323#
1324# $1 zone limit, could be "global", "local", or "both"(no limit)
1325#
1326# Return 0 if permitted, otherwise exit with log_unsupported
1327#
1328function verify_runnable # zone limit
1329{
1330 typeset limit=$1
1331
1332 [[ -z $limit ]] && return 0
1333
1334 if is_global_zone ; then
1335 case $limit in
1336 global|both)
1337 ;;
1338 local) log_unsupported "Test is unable to run from "\
1339 "global zone."
1340 ;;
1341 *) log_note "Warning: unknown limit $limit - " \
1342 "use both."
1343 ;;
1344 esac
1345 else
1346 case $limit in
1347 local|both)
1348 ;;
1349 global) log_unsupported "Test is unable to run from "\
1350 "local zone."
1351 ;;
1352 *) log_note "Warning: unknown limit $limit - " \
1353 "use both."
1354 ;;
1355 esac
1356
1357 reexport_pool
1358 fi
1359
1360 return 0
1361}
1362
1363# Return 0 if create successfully or the pool exists; $? otherwise
1364# Note: In local zones, this function should return 0 silently.
1365#
1366# $1 - pool name
1367# $2-n - [keyword] devs_list
1368
1369function create_pool #pool devs_list
1370{
1371 typeset pool=${1%%/*}
1372
1373 shift
1374
1375 if [[ -z $pool ]]; then
1376 log_note "Missing pool name."
1377 return 1
1378 fi
1379
1380 if poolexists $pool ; then
1381 destroy_pool $pool
1382 fi
1383
1384 if is_global_zone ; then
1385 [[ -d /$pool ]] && $RM -rf /$pool
1386 log_must $ZPOOL create -f $pool $@
1387 fi
1388
1389 return 0
1390}
1391
1392# Return 0 if destroy successfully or the pool exists; $? otherwise
1393# Note: In local zones, this function should return 0 silently.
1394#
1395# $1 - pool name
1396# Destroy pool with the given parameters.
1397
1398function destroy_pool #pool
1399{
1400 typeset pool=${1%%/*}
1401 typeset mtpt
1402
1403 if [[ -z $pool ]]; then
1404 log_note "No pool name given."
1405 return 1
1406 fi
1407
1408 if is_global_zone ; then
1409 if poolexists "$pool" ; then
1410 mtpt=$(get_prop mountpoint "$pool")
1411
1412 # At times, syseventd activity can cause attempts to
1413 # destroy a pool to fail with EBUSY. We retry a few
1414 # times allowing failures before requiring the destroy
1415 # to succeed.
1416 typeset -i wait_time=10 ret=1 count=0
1417 must=""
1418 while [[ $ret -ne 0 ]]; do
1419 $must $ZPOOL destroy -f $pool
1420 ret=$?
1421 [[ $ret -eq 0 ]] && break
1422 log_note "zpool destroy failed with $ret"
1423 [[ count++ -ge 7 ]] && must=log_must
1424 $SLEEP $wait_time
1425 done
1426
1427 [[ -d $mtpt ]] && \
1428 log_must $RM -rf $mtpt
1429 else
1430 log_note "Pool does not exist. ($pool)"
1431 return 1
1432 fi
1433 fi
1434
1435 return 0
1436}
1437
1438#
1439# Firstly, create a pool with 5 datasets. Then, create a single zone and
1440# export the 5 datasets to it. In addition, we also add a ZFS filesystem
1441# and a zvol device to the zone.
1442#
1443# $1 zone name
1444# $2 zone root directory prefix
1445# $3 zone ip
1446#
1447function zfs_zones_setup #zone_name zone_root zone_ip
1448{
1449 typeset zone_name=${1:-$(hostname)-z}
1450 typeset zone_root=${2:-"/zone_root"}
1451 typeset zone_ip=${3:-"10.1.1.10"}
1452 typeset prefix_ctr=$ZONE_CTR
1453 typeset pool_name=$ZONE_POOL
1454 typeset -i cntctr=5
1455 typeset -i i=0
1456
1457 # Create pool and 5 container within it
1458 #
1459 [[ -d /$pool_name ]] && $RM -rf /$pool_name
1460 log_must $ZPOOL create -f $pool_name $DISKS
1461 while ((i < cntctr)); do
1462 log_must $ZFS create $pool_name/$prefix_ctr$i
1463 ((i += 1))
1464 done
1465
1466 # create a zvol
1467 log_must $ZFS create -V 1g $pool_name/zone_zvol
1468 block_device_wait
1469
1470 #
1471 # If current system support slog, add slog device for pool
1472 #
1473 if verify_slog_support ; then
1474 typeset sdevs="/var/tmp/sdev1 /var/tmp/sdev2"
1475 log_must $MKFILE 100M $sdevs
1476 log_must $ZPOOL add $pool_name log mirror $sdevs
1477 fi
1478
1479 # this isn't supported just yet.
1480 # Create a filesystem. In order to add this to
1481 # the zone, it must have it's mountpoint set to 'legacy'
1482 # log_must $ZFS create $pool_name/zfs_filesystem
1483 # log_must $ZFS set mountpoint=legacy $pool_name/zfs_filesystem
1484
1485 [[ -d $zone_root ]] && \
1486 log_must $RM -rf $zone_root/$zone_name
1487 [[ ! -d $zone_root ]] && \
1488 log_must $MKDIR -p -m 0700 $zone_root/$zone_name
1489
1490 # Create zone configure file and configure the zone
1491 #
1492 typeset zone_conf=/tmp/zone_conf.$$
1493 $ECHO "create" > $zone_conf
1494 $ECHO "set zonepath=$zone_root/$zone_name" >> $zone_conf
1495 $ECHO "set autoboot=true" >> $zone_conf
1496 i=0
1497 while ((i < cntctr)); do
1498 $ECHO "add dataset" >> $zone_conf
1499 $ECHO "set name=$pool_name/$prefix_ctr$i" >> \
1500 $zone_conf
1501 $ECHO "end" >> $zone_conf
1502 ((i += 1))
1503 done
1504
1505 # add our zvol to the zone
1506 $ECHO "add device" >> $zone_conf
1507 $ECHO "set match=$ZVOL_DEVDIR/$pool_name/zone_zvol" >> $zone_conf
1508 $ECHO "end" >> $zone_conf
1509
1510 # add a corresponding zvol rdsk to the zone
1511 $ECHO "add device" >> $zone_conf
1512 $ECHO "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1513 $ECHO "end" >> $zone_conf
1514
1515 # once it's supported, we'll add our filesystem to the zone
1516 # $ECHO "add fs" >> $zone_conf
1517 # $ECHO "set type=zfs" >> $zone_conf
1518 # $ECHO "set special=$pool_name/zfs_filesystem" >> $zone_conf
1519 # $ECHO "set dir=/export/zfs_filesystem" >> $zone_conf
1520 # $ECHO "end" >> $zone_conf
1521
1522 $ECHO "verify" >> $zone_conf
1523 $ECHO "commit" >> $zone_conf
1524 log_must $ZONECFG -z $zone_name -f $zone_conf
1525 log_must $RM -f $zone_conf
1526
1527 # Install the zone
1528 $ZONEADM -z $zone_name install
1529 if (($? == 0)); then
1530 log_note "SUCCESS: $ZONEADM -z $zone_name install"
1531 else
1532 log_fail "FAIL: $ZONEADM -z $zone_name install"
1533 fi
1534
1535 # Install sysidcfg file
1536 #
1537 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1538 $ECHO "system_locale=C" > $sysidcfg
1539 $ECHO "terminal=dtterm" >> $sysidcfg
1540 $ECHO "network_interface=primary {" >> $sysidcfg
1541 $ECHO "hostname=$zone_name" >> $sysidcfg
1542 $ECHO "}" >> $sysidcfg
1543 $ECHO "name_service=NONE" >> $sysidcfg
1544 $ECHO "root_password=mo791xfZ/SFiw" >> $sysidcfg
1545 $ECHO "security_policy=NONE" >> $sysidcfg
1546 $ECHO "timezone=US/Eastern" >> $sysidcfg
1547
1548 # Boot this zone
1549 log_must $ZONEADM -z $zone_name boot
1550}
1551
1552#
1553# Reexport TESTPOOL & TESTPOOL(1-4)
1554#
1555function reexport_pool
1556{
1557 typeset -i cntctr=5
1558 typeset -i i=0
1559
1560 while ((i < cntctr)); do
1561 if ((i == 0)); then
1562 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1563 if ! ismounted $TESTPOOL; then
1564 log_must $ZFS mount $TESTPOOL
1565 fi
1566 else
1567 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1568 if eval ! ismounted \$TESTPOOL$i; then
1569 log_must eval $ZFS mount \$TESTPOOL$i
1570 fi
1571 fi
1572 ((i += 1))
1573 done
1574}
1575
1576#
1577# Verify a given disk is online or offline
1578#
1579# Return 0 is pool/disk matches expected state, 1 otherwise
1580#
1581function check_state # pool disk state{online,offline}
1582{
1583 typeset pool=$1
1584 typeset disk=${2#$DEV_DSKDIR/}
1585 typeset state=$3
1586
1587 $ZPOOL status -v $pool | grep "$disk" \
1588 | grep -i "$state" > /dev/null 2>&1
1589
1590 return $?
1591}
1592
1593#
1594# Get the mountpoint of snapshot
1595# For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1596# as its mountpoint
1597#
1598function snapshot_mountpoint
1599{
1600 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1601
1602 if [[ $dataset != *@* ]]; then
1603 log_fail "Error name of snapshot '$dataset'."
1604 fi
1605
1606 typeset fs=${dataset%@*}
1607 typeset snap=${dataset#*@}
1608
1609 if [[ -z $fs || -z $snap ]]; then
1610 log_fail "Error name of snapshot '$dataset'."
1611 fi
1612
1613 $ECHO $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1614}
1615
1616#
1617# Given a pool and file system, this function will verify the file system
1618# using the zdb internal tool. Note that the pool is exported and imported
1619# to ensure it has consistent state.
1620#
1621function verify_filesys # pool filesystem dir
1622{
1623 typeset pool="$1"
1624 typeset filesys="$2"
1625 typeset zdbout="/tmp/zdbout.$$"
1626
1627 shift
1628 shift
1629 typeset dirs=$@
1630 typeset search_path=""
1631
1632 log_note "Calling $ZDB to verify filesystem '$filesys'"
1633 $ZFS unmount -a > /dev/null 2>&1
1634 log_must $ZPOOL export $pool
1635
1636 if [[ -n $dirs ]] ; then
1637 for dir in $dirs ; do
1638 search_path="$search_path -d $dir"
1639 done
1640 fi
1641
1642 log_must $ZPOOL import $search_path $pool
1643
1644 $ZDB -cudi $filesys > $zdbout 2>&1
1645 if [[ $? != 0 ]]; then
1646 log_note "Output: $ZDB -cudi $filesys"
1647 $CAT $zdbout
1648 log_fail "$ZDB detected errors with: '$filesys'"
1649 fi
1650
1651 log_must $ZFS mount -a
1652 log_must $RM -rf $zdbout
1653}
1654
1655#
1656# Given a pool, and this function list all disks in the pool
1657#
1658function get_disklist # pool
1659{
1660 typeset disklist=""
1661
1662 disklist=$($ZPOOL iostat -v $1 | $NAWK '(NR >4) {print $1}' | \
1663 $GREP -v "\-\-\-\-\-" | \
1664 $EGREP -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1665
1666 $ECHO $disklist
1667}
1668
3c67d83a
TH
1669#
1670# Given a pool, and this function list all disks in the pool with their full
1671# path (like "/dev/sda" instead of "sda").
1672#
1673function get_disklist_fullpath # pool
1674{
1675 args="-P $1"
1676 get_disklist $args
1677}
1678
1679
1680
6bb24f4d
BB
1681# /**
1682# This function kills a given list of processes after a time period. We use
1683# this in the stress tests instead of STF_TIMEOUT so that we can have processes
1684# run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1685# would be listed as FAIL, which we don't want : we're happy with stress tests
1686# running for a certain amount of time, then finishing.
1687#
1688# @param $1 the time in seconds after which we should terminate these processes
1689# @param $2..$n the processes we wish to terminate.
1690# */
1691function stress_timeout
1692{
1693 typeset -i TIMEOUT=$1
1694 shift
1695 typeset cpids="$@"
1696
1697 log_note "Waiting for child processes($cpids). " \
1698 "It could last dozens of minutes, please be patient ..."
1699 log_must $SLEEP $TIMEOUT
1700
1701 log_note "Killing child processes after ${TIMEOUT} stress timeout."
1702 typeset pid
1703 for pid in $cpids; do
1704 $PS -p $pid > /dev/null 2>&1
1705 if (($? == 0)); then
1706 log_must $KILL -USR1 $pid
1707 fi
1708 done
1709}
1710
1711#
1712# Verify a given hotspare disk is inuse or avail
1713#
1714# Return 0 is pool/disk matches expected state, 1 otherwise
1715#
1716function check_hotspare_state # pool disk state{inuse,avail}
1717{
1718 typeset pool=$1
1719 typeset disk=${2#$DEV_DSKDIR/}
1720 typeset state=$3
1721
1722 cur_state=$(get_device_state $pool $disk "spares")
1723
1724 if [[ $state != ${cur_state} ]]; then
1725 return 1
1726 fi
1727 return 0
1728}
1729
1730#
1731# Verify a given slog disk is inuse or avail
1732#
1733# Return 0 is pool/disk matches expected state, 1 otherwise
1734#
1735function check_slog_state # pool disk state{online,offline,unavail}
1736{
1737 typeset pool=$1
1738 typeset disk=${2#$DEV_DSKDIR/}
1739 typeset state=$3
1740
1741 cur_state=$(get_device_state $pool $disk "logs")
1742
1743 if [[ $state != ${cur_state} ]]; then
1744 return 1
1745 fi
1746 return 0
1747}
1748
1749#
1750# Verify a given vdev disk is inuse or avail
1751#
1752# Return 0 is pool/disk matches expected state, 1 otherwise
1753#
1754function check_vdev_state # pool disk state{online,offline,unavail}
1755{
1756 typeset pool=$1
1757 typeset disk=${2#$/DEV_DSKDIR/}
1758 typeset state=$3
1759
1760 cur_state=$(get_device_state $pool $disk)
1761
1762 if [[ $state != ${cur_state} ]]; then
1763 return 1
1764 fi
1765 return 0
1766}
1767
1768#
1769# Check the output of 'zpool status -v <pool>',
1770# and to see if the content of <token> contain the <keyword> specified.
1771#
1772# Return 0 is contain, 1 otherwise
1773#
1774function check_pool_status # pool token keyword
1775{
1776 typeset pool=$1
1777 typeset token=$2
1778 typeset keyword=$3
1779
1780 $ZPOOL status -v "$pool" 2>/dev/null | $NAWK -v token="$token:" '
1781 ($1==token) {print $0}' \
1782 | $GREP -i "$keyword" > /dev/null 2>&1
1783
1784 return $?
1785}
1786
1787#
1788# These 5 following functions are instance of check_pool_status()
1789# is_pool_resilvering - to check if the pool is resilver in progress
1790# is_pool_resilvered - to check if the pool is resilver completed
1791# is_pool_scrubbing - to check if the pool is scrub in progress
1792# is_pool_scrubbed - to check if the pool is scrub completed
1793# is_pool_scrub_stopped - to check if the pool is scrub stopped
1794#
1795function is_pool_resilvering #pool
1796{
1797 check_pool_status "$1" "scan" "resilver in progress since "
1798 return $?
1799}
1800
1801function is_pool_resilvered #pool
1802{
1803 check_pool_status "$1" "scan" "resilvered "
1804 return $?
1805}
1806
1807function is_pool_scrubbing #pool
1808{
1809 check_pool_status "$1" "scan" "scrub in progress since "
1810 return $?
1811}
1812
1813function is_pool_scrubbed #pool
1814{
1815 check_pool_status "$1" "scan" "scrub repaired"
1816 return $?
1817}
1818
1819function is_pool_scrub_stopped #pool
1820{
1821 check_pool_status "$1" "scan" "scrub canceled"
1822 return $?
1823}
1824
1825#
1826# Use create_pool()/destroy_pool() to clean up the infomation in
1827# in the given disk to avoid slice overlapping.
1828#
1829function cleanup_devices #vdevs
1830{
1831 typeset pool="foopool$$"
1832
1833 if poolexists $pool ; then
1834 destroy_pool $pool
1835 fi
1836
1837 create_pool $pool $@
1838 destroy_pool $pool
1839
1840 return 0
1841}
1842
1843#
1844# Verify the rsh connectivity to each remote host in RHOSTS.
1845#
1846# Return 0 if remote host is accessible; otherwise 1.
1847# $1 remote host name
1848# $2 username
1849#
1850function verify_rsh_connect #rhost, username
1851{
1852 typeset rhost=$1
1853 typeset username=$2
1854 typeset rsh_cmd="$RSH -n"
1855 typeset cur_user=
1856
1857 $GETENT hosts $rhost >/dev/null 2>&1
1858 if (($? != 0)); then
1859 log_note "$rhost cannot be found from" \
1860 "administrative database."
1861 return 1
1862 fi
1863
1864 $PING $rhost 3 >/dev/null 2>&1
1865 if (($? != 0)); then
1866 log_note "$rhost is not reachable."
1867 return 1
1868 fi
1869
1870 if ((${#username} != 0)); then
1871 rsh_cmd="$rsh_cmd -l $username"
1872 cur_user="given user \"$username\""
1873 else
1874 cur_user="current user \"`$LOGNAME`\""
1875 fi
1876
1877 if ! $rsh_cmd $rhost $TRUE; then
1878 log_note "$RSH to $rhost is not accessible" \
1879 "with $cur_user."
1880 return 1
1881 fi
1882
1883 return 0
1884}
1885
1886#
1887# Verify the remote host connection via rsh after rebooting
1888# $1 remote host
1889#
1890function verify_remote
1891{
1892 rhost=$1
1893
1894 #
1895 # The following loop waits for the remote system rebooting.
1896 # Each iteration will wait for 150 seconds. there are
1897 # total 5 iterations, so the total timeout value will
1898 # be 12.5 minutes for the system rebooting. This number
1899 # is an approxiate number.
1900 #
1901 typeset -i count=0
1902 while ! verify_rsh_connect $rhost; do
1903 sleep 150
1904 ((count = count + 1))
1905 if ((count > 5)); then
1906 return 1
1907 fi
1908 done
1909 return 0
1910}
1911
1912#
1913# Replacement function for /usr/bin/rsh. This function will include
1914# the /usr/bin/rsh and meanwhile return the execution status of the
1915# last command.
1916#
1917# $1 usrname passing down to -l option of /usr/bin/rsh
1918# $2 remote machine hostname
1919# $3... command string
1920#
1921
1922function rsh_status
1923{
1924 typeset ruser=$1
1925 typeset rhost=$2
1926 typeset -i ret=0
1927 typeset cmd_str=""
1928 typeset rsh_str=""
1929
1930 shift; shift
1931 cmd_str="$@"
1932
1933 err_file=/tmp/${rhost}.$$.err
1934 if ((${#ruser} == 0)); then
1935 rsh_str="$RSH -n"
1936 else
1937 rsh_str="$RSH -n -l $ruser"
1938 fi
1939
1940 $rsh_str $rhost /bin/ksh -c "'$cmd_str; \
1941 print -u 2 \"status=\$?\"'" \
1942 >/dev/null 2>$err_file
1943 ret=$?
1944 if (($ret != 0)); then
1945 $CAT $err_file
1946 $RM -f $std_file $err_file
1947 log_fail "$RSH itself failed with exit code $ret..."
1948 fi
1949
1950 ret=$($GREP -v 'print -u 2' $err_file | $GREP 'status=' | \
1951 $CUT -d= -f2)
1952 (($ret != 0)) && $CAT $err_file >&2
1953
1954 $RM -f $err_file >/dev/null 2>&1
1955 return $ret
1956}
1957
1958#
1959# Get the SUNWstc-fs-zfs package installation path in a remote host
1960# $1 remote host name
1961#
1962function get_remote_pkgpath
1963{
1964 typeset rhost=$1
1965 typeset pkgpath=""
1966
1967 pkgpath=$($RSH -n $rhost "$PKGINFO -l SUNWstc-fs-zfs | $GREP BASEDIR: |\
1968 $CUT -d: -f2")
1969
1970 $ECHO $pkgpath
1971}
1972
1973#/**
1974# A function to find and locate free disks on a system or from given
1975# disks as the parameter. It works by locating disks that are in use
1976# as swap devices and dump devices, and also disks listed in /etc/vfstab
1977#
1978# $@ given disks to find which are free, default is all disks in
1979# the test system
1980#
1981# @return a string containing the list of available disks
1982#*/
1983function find_disks
1984{
1985 # Trust provided list, no attempt is made to locate unused devices.
1986 if is_linux; then
1987 $ECHO "$@"
1988 return
1989 fi
1990
1991
1992 sfi=/tmp/swaplist.$$
1993 dmpi=/tmp/dumpdev.$$
1994 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
1995
1996 $SWAP -l > $sfi
1997 $DUMPADM > $dmpi 2>/dev/null
1998
1999# write an awk script that can process the output of format
2000# to produce a list of disks we know about. Note that we have
2001# to escape "$2" so that the shell doesn't interpret it while
2002# we're creating the awk script.
2003# -------------------
2004 $CAT > /tmp/find_disks.awk <<EOF
2005#!/bin/nawk -f
2006 BEGIN { FS="."; }
2007
2008 /^Specify disk/{
2009 searchdisks=0;
2010 }
2011
2012 {
2013 if (searchdisks && \$2 !~ "^$"){
2014 split(\$2,arr," ");
2015 print arr[1];
2016 }
2017 }
2018
2019 /^AVAILABLE DISK SELECTIONS:/{
2020 searchdisks=1;
2021 }
2022EOF
2023#---------------------
2024
2025 $CHMOD 755 /tmp/find_disks.awk
2026 disks=${@:-$($ECHO "" | $FORMAT -e 2>/dev/null | /tmp/find_disks.awk)}
2027 $RM /tmp/find_disks.awk
2028
2029 unused=""
2030 for disk in $disks; do
2031 # Check for mounted
2032 $GREP "${disk}[sp]" /etc/mnttab >/dev/null
2033 (($? == 0)) && continue
2034 # Check for swap
2035 $GREP "${disk}[sp]" $sfi >/dev/null
2036 (($? == 0)) && continue
2037 # check for dump device
2038 $GREP "${disk}[sp]" $dmpi >/dev/null
2039 (($? == 0)) && continue
2040 # check to see if this disk hasn't been explicitly excluded
2041 # by a user-set environment variable
2042 $ECHO "${ZFS_HOST_DEVICES_IGNORE}" | $GREP "${disk}" > /dev/null
2043 (($? == 0)) && continue
2044 unused_candidates="$unused_candidates $disk"
2045 done
2046 $RM $sfi
2047 $RM $dmpi
2048
2049# now just check to see if those disks do actually exist
2050# by looking for a device pointing to the first slice in
2051# each case. limit the number to max_finddisksnum
2052 count=0
2053 for disk in $unused_candidates; do
2054 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2055 if [ $count -lt $max_finddisksnum ]; then
2056 unused="$unused $disk"
2057 # do not impose limit if $@ is provided
2058 [[ -z $@ ]] && ((count = count + 1))
2059 fi
2060 fi
2061 done
2062
2063# finally, return our disk list
2064 $ECHO $unused
2065}
2066
2067#
2068# Add specified user to specified group
2069#
2070# $1 group name
2071# $2 user name
2072# $3 base of the homedir (optional)
2073#
2074function add_user #<group_name> <user_name> <basedir>
2075{
2076 typeset gname=$1
2077 typeset uname=$2
2078 typeset basedir=${3:-"/var/tmp"}
2079
2080 if ((${#gname} == 0 || ${#uname} == 0)); then
2081 log_fail "group name or user name are not defined."
2082 fi
2083
2084 log_must $USERADD -g $gname -d $basedir/$uname -m $uname
2085
f74b821a
BB
2086 # Add new users to the same group and the command line utils.
2087 # This allows them to be run out of the original users home
2088 # directory as long as it permissioned to be group readable.
2089 if is_linux; then
2090 cmd_group=$(stat --format="%G" $ZFS)
2091 log_must $USERMOD -a -G $cmd_group $uname
2092 fi
2093
6bb24f4d
BB
2094 return 0
2095}
2096
2097#
2098# Delete the specified user.
2099#
2100# $1 login name
2101# $2 base of the homedir (optional)
2102#
2103function del_user #<logname> <basedir>
2104{
2105 typeset user=$1
2106 typeset basedir=${2:-"/var/tmp"}
2107
2108 if ((${#user} == 0)); then
2109 log_fail "login name is necessary."
2110 fi
2111
2112 if $ID $user > /dev/null 2>&1; then
2113 log_must $USERDEL $user
2114 fi
2115
2116 [[ -d $basedir/$user ]] && $RM -fr $basedir/$user
2117
2118 return 0
2119}
2120
2121#
2122# Select valid gid and create specified group.
2123#
2124# $1 group name
2125#
2126function add_group #<group_name>
2127{
2128 typeset group=$1
2129
2130 if ((${#group} == 0)); then
2131 log_fail "group name is necessary."
2132 fi
2133
2134 # Assign 100 as the base gid, a larger value is selected for
2135 # Linux because for many distributions 1000 and under are reserved.
2136 if is_linux; then
6bb24f4d 2137 while true; do
f74b821a 2138 $GROUPADD $group > /dev/null 2>&1
6bb24f4d
BB
2139 typeset -i ret=$?
2140 case $ret in
2141 0) return 0 ;;
6bb24f4d
BB
2142 *) return 1 ;;
2143 esac
2144 done
2145 else
2146 typeset -i gid=100
2147
2148 while true; do
2149 $GROUPADD -g $gid $group > /dev/null 2>&1
2150 typeset -i ret=$?
2151 case $ret in
2152 0) return 0 ;;
2153 # The gid is not unique
2154 4) ((gid += 1)) ;;
2155 *) return 1 ;;
2156 esac
2157 done
2158 fi
2159}
2160
2161#
2162# Delete the specified group.
2163#
2164# $1 group name
2165#
2166function del_group #<group_name>
2167{
2168 typeset grp=$1
2169 if ((${#grp} == 0)); then
2170 log_fail "group name is necessary."
2171 fi
2172
2173 if is_linux; then
2174 $GETENT group $grp > /dev/null 2>&1
2175 typeset -i ret=$?
2176 case $ret in
2177 # Group does not exist.
2178 2) return 0 ;;
2179 # Name already exists as a group name
2180 0) log_must $GROUPDEL $grp ;;
2181 *) return 1 ;;
2182 esac
2183 else
2184 $GROUPMOD -n $grp $grp > /dev/null 2>&1
2185 typeset -i ret=$?
2186 case $ret in
2187 # Group does not exist.
2188 6) return 0 ;;
2189 # Name already exists as a group name
2190 9) log_must $GROUPDEL $grp ;;
2191 *) return 1 ;;
2192 esac
2193 fi
2194
2195 return 0
2196}
2197
2198#
2199# This function will return true if it's safe to destroy the pool passed
2200# as argument 1. It checks for pools based on zvols and files, and also
2201# files contained in a pool that may have a different mountpoint.
2202#
2203function safe_to_destroy_pool { # $1 the pool name
2204
2205 typeset pool=""
2206 typeset DONT_DESTROY=""
2207
2208 # We check that by deleting the $1 pool, we're not
2209 # going to pull the rug out from other pools. Do this
2210 # by looking at all other pools, ensuring that they
2211 # aren't built from files or zvols contained in this pool.
2212
2213 for pool in $($ZPOOL list -H -o name)
2214 do
2215 ALTMOUNTPOOL=""
2216
2217 # this is a list of the top-level directories in each of the
2218 # files that make up the path to the files the pool is based on
2219 FILEPOOL=$($ZPOOL status -v $pool | $GREP /$1/ | \
2220 $AWK '{print $1}')
2221
2222 # this is a list of the zvols that make up the pool
2223 ZVOLPOOL=$($ZPOOL status -v $pool | $GREP "$ZVOL_DEVDIR/$1$" \
2224 | $AWK '{print $1}')
2225
2226 # also want to determine if it's a file-based pool using an
2227 # alternate mountpoint...
2228 POOL_FILE_DIRS=$($ZPOOL status -v $pool | \
2229 $GREP / | $AWK '{print $1}' | \
2230 $AWK -F/ '{print $2}' | $GREP -v "dev")
2231
2232 for pooldir in $POOL_FILE_DIRS
2233 do
2234 OUTPUT=$($ZFS list -H -r -o mountpoint $1 | \
2235 $GREP "${pooldir}$" | $AWK '{print $1}')
2236
2237 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2238 done
2239
2240
2241 if [ ! -z "$ZVOLPOOL" ]
2242 then
2243 DONT_DESTROY="true"
2244 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2245 fi
2246
2247 if [ ! -z "$FILEPOOL" ]
2248 then
2249 DONT_DESTROY="true"
2250 log_note "Pool $pool is built from $FILEPOOL on $1"
2251 fi
2252
2253 if [ ! -z "$ALTMOUNTPOOL" ]
2254 then
2255 DONT_DESTROY="true"
2256 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2257 fi
2258 done
2259
2260 if [ -z "${DONT_DESTROY}" ]
2261 then
2262 return 0
2263 else
2264 log_note "Warning: it is not safe to destroy $1!"
2265 return 1
2266 fi
2267}
2268
2269#
2270# Get the available ZFS compression options
2271# $1 option type zfs_set|zfs_compress
2272#
2273function get_compress_opts
2274{
2275 typeset COMPRESS_OPTS
2276 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2277 gzip-6 gzip-7 gzip-8 gzip-9"
2278
2279 if [[ $1 == "zfs_compress" ]] ; then
2280 COMPRESS_OPTS="on lzjb"
2281 elif [[ $1 == "zfs_set" ]] ; then
2282 COMPRESS_OPTS="on off lzjb"
2283 fi
2284 typeset valid_opts="$COMPRESS_OPTS"
2285 $ZFS get 2>&1 | $GREP gzip >/dev/null 2>&1
2286 if [[ $? -eq 0 ]]; then
2287 valid_opts="$valid_opts $GZIP_OPTS"
2288 fi
2289 $ECHO "$valid_opts"
2290}
2291
2292#
2293# Verify zfs operation with -p option work as expected
2294# $1 operation, value could be create, clone or rename
2295# $2 dataset type, value could be fs or vol
2296# $3 dataset name
2297# $4 new dataset name
2298#
2299function verify_opt_p_ops
2300{
2301 typeset ops=$1
2302 typeset datatype=$2
2303 typeset dataset=$3
2304 typeset newdataset=$4
2305
2306 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2307 log_fail "$datatype is not supported."
2308 fi
2309
2310 # check parameters accordingly
2311 case $ops in
2312 create)
2313 newdataset=$dataset
2314 dataset=""
2315 if [[ $datatype == "vol" ]]; then
2316 ops="create -V $VOLSIZE"
2317 fi
2318 ;;
2319 clone)
2320 if [[ -z $newdataset ]]; then
2321 log_fail "newdataset should not be empty" \
2322 "when ops is $ops."
2323 fi
2324 log_must datasetexists $dataset
2325 log_must snapexists $dataset
2326 ;;
2327 rename)
2328 if [[ -z $newdataset ]]; then
2329 log_fail "newdataset should not be empty" \
2330 "when ops is $ops."
2331 fi
2332 log_must datasetexists $dataset
2333 log_mustnot snapexists $dataset
2334 ;;
2335 *)
2336 log_fail "$ops is not supported."
2337 ;;
2338 esac
2339
2340 # make sure the upper level filesystem does not exist
2341 if datasetexists ${newdataset%/*} ; then
2342 log_must $ZFS destroy -rRf ${newdataset%/*}
2343 fi
2344
2345 # without -p option, operation will fail
2346 log_mustnot $ZFS $ops $dataset $newdataset
2347 log_mustnot datasetexists $newdataset ${newdataset%/*}
2348
2349 # with -p option, operation should succeed
2350 log_must $ZFS $ops -p $dataset $newdataset
2351 block_device_wait
2352
2353 if ! datasetexists $newdataset ; then
2354 log_fail "-p option does not work for $ops"
2355 fi
2356
2357 # when $ops is create or clone, redo the operation still return zero
2358 if [[ $ops != "rename" ]]; then
2359 log_must $ZFS $ops -p $dataset $newdataset
2360 fi
2361
2362 return 0
2363}
2364
2365#
2366# Get configuration of pool
2367# $1 pool name
2368# $2 config name
2369#
2370function get_config
2371{
2372 typeset pool=$1
2373 typeset config=$2
2374 typeset alt_root
2375
2376 if ! poolexists "$pool" ; then
2377 return 1
2378 fi
2379 alt_root=$($ZPOOL list -H $pool | $AWK '{print $NF}')
2380 if [[ $alt_root == "-" ]]; then
2381 value=$($ZDB -C $pool | $GREP "$config:" | $AWK -F: \
2382 '{print $2}')
2383 else
2384 value=$($ZDB -e $pool | $GREP "$config:" | $AWK -F: \
2385 '{print $2}')
2386 fi
2387 if [[ -n $value ]] ; then
2388 value=${value#'}
2389 value=${value%'}
2390 fi
2391 echo $value
2392
2393 return 0
2394}
2395
2396#
2397# Privated function. Random select one of items from arguments.
2398#
2399# $1 count
2400# $2-n string
2401#
2402function _random_get
2403{
2404 typeset cnt=$1
2405 shift
2406
2407 typeset str="$@"
2408 typeset -i ind
2409 ((ind = RANDOM % cnt + 1))
2410
2411 typeset ret=$($ECHO "$str" | $CUT -f $ind -d ' ')
2412 $ECHO $ret
2413}
2414
2415#
2416# Random select one of item from arguments which include NONE string
2417#
2418function random_get_with_non
2419{
2420 typeset -i cnt=$#
2421 ((cnt =+ 1))
2422
2423 _random_get "$cnt" "$@"
2424}
2425
2426#
2427# Random select one of item from arguments which doesn't include NONE string
2428#
2429function random_get
2430{
2431 _random_get "$#" "$@"
2432}
2433
2434#
2435# Detect if the current system support slog
2436#
2437function verify_slog_support
2438{
2439 typeset dir=/tmp/disk.$$
2440 typeset pool=foo.$$
2441 typeset vdev=$dir/a
2442 typeset sdev=$dir/b
2443
2444 $MKDIR -p $dir
2445 $MKFILE 64M $vdev $sdev
2446
2447 typeset -i ret=0
2448 if ! $ZPOOL create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2449 ret=1
2450 fi
2451 $RM -r $dir
2452
2453 return $ret
2454}
2455
2456#
2457# The function will generate a dataset name with specific length
2458# $1, the length of the name
2459# $2, the base string to construct the name
2460#
2461function gen_dataset_name
2462{
2463 typeset -i len=$1
2464 typeset basestr="$2"
2465 typeset -i baselen=${#basestr}
2466 typeset -i iter=0
2467 typeset l_name=""
2468
2469 if ((len % baselen == 0)); then
2470 ((iter = len / baselen))
2471 else
2472 ((iter = len / baselen + 1))
2473 fi
2474 while ((iter > 0)); do
2475 l_name="${l_name}$basestr"
2476
2477 ((iter -= 1))
2478 done
2479
2480 $ECHO $l_name
2481}
2482
2483#
2484# Get cksum tuple of dataset
2485# $1 dataset name
2486#
2487# sample zdb output:
2488# Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2489# DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2490# lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2491# fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2492function datasetcksum
2493{
2494 typeset cksum
2495 $SYNC
2496 cksum=$($ZDB -vvv $1 | $GREP "^Dataset $1 \[" | $GREP "cksum" \
2497 | $AWK -F= '{print $7}')
2498 $ECHO $cksum
2499}
2500
2501#
2502# Get cksum of file
2503# #1 file path
2504#
2505function checksum
2506{
2507 typeset cksum
2508 cksum=$($CKSUM $1 | $AWK '{print $1}')
2509 $ECHO $cksum
2510}
2511
2512#
2513# Get the given disk/slice state from the specific field of the pool
2514#
2515function get_device_state #pool disk field("", "spares","logs")
2516{
2517 typeset pool=$1
2518 typeset disk=${2#$DEV_DSKDIR/}
2519 typeset field=${3:-$pool}
2520
2521 state=$($ZPOOL status -v "$pool" 2>/dev/null | \
2522 $NAWK -v device=$disk -v pool=$pool -v field=$field \
2523 'BEGIN {startconfig=0; startfield=0; }
2524 /config:/ {startconfig=1}
2525 (startconfig==1) && ($1==field) {startfield=1; next;}
2526 (startfield==1) && ($1==device) {print $2; exit;}
2527 (startfield==1) &&
2528 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2529 echo $state
2530}
2531
2532
2533#
2534# print the given directory filesystem type
2535#
2536# $1 directory name
2537#
2538function get_fstype
2539{
2540 typeset dir=$1
2541
2542 if [[ -z $dir ]]; then
2543 log_fail "Usage: get_fstype <directory>"
2544 fi
2545
2546 #
2547 # $ df -n /
2548 # / : ufs
2549 #
2550 $DF -n $dir | $AWK '{print $3}'
2551}
2552
2553#
2554# Given a disk, label it to VTOC regardless what label was on the disk
2555# $1 disk
2556#
2557function labelvtoc
2558{
2559 typeset disk=$1
2560 if [[ -z $disk ]]; then
2561 log_fail "The disk name is unspecified."
2562 fi
2563 typeset label_file=/var/tmp/labelvtoc.$$
2564 typeset arch=$($UNAME -p)
2565
2566 if is_linux; then
2567 log_note "Currently unsupported by the test framework"
2568 return 1
2569 fi
2570
2571 if [[ $arch == "i386" ]]; then
2572 $ECHO "label" > $label_file
2573 $ECHO "0" >> $label_file
2574 $ECHO "" >> $label_file
2575 $ECHO "q" >> $label_file
2576 $ECHO "q" >> $label_file
2577
2578 $FDISK -B $disk >/dev/null 2>&1
2579 # wait a while for fdisk finishes
2580 $SLEEP 60
2581 elif [[ $arch == "sparc" ]]; then
2582 $ECHO "label" > $label_file
2583 $ECHO "0" >> $label_file
2584 $ECHO "" >> $label_file
2585 $ECHO "" >> $label_file
2586 $ECHO "" >> $label_file
2587 $ECHO "q" >> $label_file
2588 else
2589 log_fail "unknown arch type"
2590 fi
2591
2592 $FORMAT -e -s -d $disk -f $label_file
2593 typeset -i ret_val=$?
2594 $RM -f $label_file
2595 #
2596 # wait the format to finish
2597 #
2598 $SLEEP 60
2599 if ((ret_val != 0)); then
2600 log_fail "unable to label $disk as VTOC."
2601 fi
2602
2603 return 0
2604}
2605
2606#
2607# check if the system was installed as zfsroot or not
2608# return: 0 ture, otherwise false
2609#
2610function is_zfsroot
2611{
2612 $DF -n / | $GREP zfs > /dev/null 2>&1
2613 return $?
2614}
2615
2616#
2617# get the root filesystem name if it's zfsroot system.
2618#
2619# return: root filesystem name
2620function get_rootfs
2621{
2622 typeset rootfs=""
2623 rootfs=$($AWK '{if ($2 == "/" && $3 == "zfs") print $1}' \
2624 /etc/mnttab)
2625 if [[ -z "$rootfs" ]]; then
2626 log_fail "Can not get rootfs"
2627 fi
2628 $ZFS list $rootfs > /dev/null 2>&1
2629 if (($? == 0)); then
2630 $ECHO $rootfs
2631 else
2632 log_fail "This is not a zfsroot system."
2633 fi
2634}
2635
2636#
2637# get the rootfs's pool name
2638# return:
2639# rootpool name
2640#
2641function get_rootpool
2642{
2643 typeset rootfs=""
2644 typeset rootpool=""
2645 rootfs=$($AWK '{if ($2 == "/" && $3 =="zfs") print $1}' \
2646 /etc/mnttab)
2647 if [[ -z "$rootfs" ]]; then
2648 log_fail "Can not get rootpool"
2649 fi
2650 $ZFS list $rootfs > /dev/null 2>&1
2651 if (($? == 0)); then
2652 rootpool=`$ECHO $rootfs | awk -F\/ '{print $1}'`
2653 $ECHO $rootpool
2654 else
2655 log_fail "This is not a zfsroot system."
2656 fi
2657}
2658
2659#
2660# Get the sub string from specified source string
2661#
2662# $1 source string
2663# $2 start position. Count from 1
2664# $3 offset
2665#
2666function get_substr #src_str pos offset
2667{
2668 typeset pos offset
2669
2670 $ECHO $1 | \
2671 $NAWK -v pos=$2 -v offset=$3 '{print substr($0, pos, offset)}'
2672}
2673
2674#
2675# Check if the given device is physical device
2676#
2677function is_physical_device #device
2678{
2679 typeset device=${1#$DEV_DSKDIR}
2680 device=${device#$DEV_RDSKDIR}
2681
2682 if is_linux; then
2683 [[ -b "$DEV_DSKDIR/$device" ]] && \
2684 [[ -f /sys/module/loop/parameters/max_part ]]
2685 return $?
2686 else
2687 $ECHO $device | $EGREP "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
2688 return $?
2689 fi
2690}
2691
7050a65d
SV
2692#
2693# Check if the given device is a real device (ie SCSI device)
2694#
2695function is_real_device #disk
2696{
2697 typeset disk=$1
2698 [[ -z $disk ]] && log_fail "No argument for disk given."
2699
2700 if is_linux; then
2701 $LSBLK $DEV_RDSKDIR/$disk -o TYPE | $EGREP disk > /dev/null 2>&1
2702 return $?
2703 fi
2704}
2705
2706#
2707# Check if the given device is a loop device
2708#
2709function is_loop_device #disk
2710{
2711 typeset disk=$1
2712 [[ -z $disk ]] && log_fail "No argument for disk given."
2713
2714 if is_linux; then
2715 $LSBLK $DEV_RDSKDIR/$disk -o TYPE | $EGREP loop > /dev/null 2>&1
2716 return $?
2717 fi
2718}
2719
2720#
2721# Check if the given device is a multipath device and if there is a sybolic
2722# link to a device mapper and to a disk
2723# Currently no support for dm devices alone without multipath
2724#
2725function is_mpath_device #disk
2726{
2727 typeset disk=$1
2728 [[ -z $disk ]] && log_fail "No argument for disk given."
2729
2730 if is_linux; then
2731 $LSBLK $DEV_MPATHDIR/$disk -o TYPE | $EGREP mpath > /dev/null 2>&1
2732 if (($? == 0)); then
2733 $READLINK $DEV_MPATHDIR/$disk > /dev/null 2>&1
2734 return $?
2735 else
2736 return $?
2737 fi
2738 fi
2739}
2740
2741# Set the slice prefix for disk partitioning depending
2742# on whether the device is a real, multipath, or loop device.
2743# Currently all disks have to be of the same type, so only
2744# checks first disk to determine slice prefix.
2745#
2746function set_slice_prefix
2747{
2748 typeset disk
2749 typeset -i i=0
2750
2751 if is_linux; then
2752 while (( i < $DISK_ARRAY_NUM )); do
2753 disk="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2754 if ( is_mpath_device $disk ) && [[ -z $($ECHO $disk | awk 'substr($1,18,1)\
2755 ~ /^[[:digit:]]+$/') ]] || ( is_real_device $disk ); then
2756 export SLICE_PREFIX=""
2757 return 0
2758 elif ( is_mpath_device $disk || is_loop_device $disk ); then
2759 export SLICE_PREFIX="p"
2760 return 0
2761 else
2762 log_fail "$disk not supported for partitioning."
2763 fi
2764 (( i = i + 1))
2765 done
2766 fi
2767}
2768
2769#
2770# Set the directory path of the listed devices in $DISK_ARRAY_NUM
2771# Currently all disks have to be of the same type, so only
2772# checks first disk to determine device directory
2773# default = /dev (linux)
2774# real disk = /dev (linux)
2775# multipath device = /dev/mapper (linux)
2776#
2777function set_device_dir
2778{
2779 typeset disk
2780 typeset -i i=0
2781
2782 if is_linux; then
2783 while (( i < $DISK_ARRAY_NUM )); do
2784 disk="$($ECHO $DISKS | $NAWK '{print $(i + 1)}')"
2785 if is_mpath_device $disk; then
2786 export DEV_DSKDIR=$DEV_MPATHDIR
2787 return 0
2788 else
2789 export DEV_DSKDIR=$DEV_RDSKDIR
2790 return 0
2791 fi
2792 (( i = i + 1))
2793 done
2794 else
2795 export DEV_DSKDIR=$DEV_RDSKDIR
2796 fi
2797}
2798
6bb24f4d
BB
2799#
2800# Get the directory path of given device
2801#
2802function get_device_dir #device
2803{
2804 typeset device=$1
2805
2806 if ! $(is_physical_device $device) ; then
2807 if [[ $device != "/" ]]; then
2808 device=${device%/*}
2809 fi
2810 if [[ -b "$DEV_DSKDIR/$device" ]]; then
2811 device="$DEV_DSKDIR"
2812 fi
2813 $ECHO $device
2814 else
2815 $ECHO "$DEV_DSKDIR"
2816 fi
2817}
2818
2819#
2820# Get the package name
2821#
2822function get_package_name
2823{
2824 typeset dirpath=${1:-$STC_NAME}
2825
2826 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
2827}
2828
2829#
2830# Get the word numbers from a string separated by white space
2831#
2832function get_word_count
2833{
2834 $ECHO $1 | $WC -w
2835}
2836
2837#
2838# To verify if the require numbers of disks is given
2839#
2840function verify_disk_count
2841{
2842 typeset -i min=${2:-1}
2843
2844 typeset -i count=$(get_word_count "$1")
2845
2846 if ((count < min)); then
2847 log_untested "A minimum of $min disks is required to run." \
2848 " You specified $count disk(s)"
2849 fi
2850}
2851
2852function ds_is_volume
2853{
2854 typeset type=$(get_prop type $1)
2855 [[ $type = "volume" ]] && return 0
2856 return 1
2857}
2858
2859function ds_is_filesystem
2860{
2861 typeset type=$(get_prop type $1)
2862 [[ $type = "filesystem" ]] && return 0
2863 return 1
2864}
2865
2866function ds_is_snapshot
2867{
2868 typeset type=$(get_prop type $1)
2869 [[ $type = "snapshot" ]] && return 0
2870 return 1
2871}
2872
2873#
2874# Check if Trusted Extensions are installed and enabled
2875#
2876function is_te_enabled
2877{
2878 $SVCS -H -o state labeld 2>/dev/null | $GREP "enabled"
2879 if (($? != 0)); then
2880 return 1
2881 else
2882 return 0
2883 fi
2884}
2885
2886# Utility function to determine if a system has multiple cpus.
2887function is_mp
2888{
2889 if is_linux; then
2890 (($($NPROC) > 1))
2891 else
2892 (($($PSRINFO | $WC -l) > 1))
2893 fi
2894
2895 return $?
2896}
2897
2898function get_cpu_freq
2899{
2900 if is_linux; then
2901 lscpu | $AWK '/CPU MHz/ { print $3 }'
2902 else
2903 $PSRINFO -v 0 | $AWK '/processor operates at/ {print $6}'
2904 fi
2905}
2906
2907# Run the given command as the user provided.
2908function user_run
2909{
2910 typeset user=$1
2911 shift
2912
f74b821a 2913 log_note "user:$user $@"
6bb24f4d
BB
2914 eval \$SU \$user -c \"$@\" > /tmp/out 2>/tmp/err
2915 return $?
2916}
2917
2918#
2919# Check if the pool contains the specified vdevs
2920#
2921# $1 pool
2922# $2..n <vdev> ...
2923#
2924# Return 0 if the vdevs are contained in the pool, 1 if any of the specified
2925# vdevs is not in the pool, and 2 if pool name is missing.
2926#
2927function vdevs_in_pool
2928{
2929 typeset pool=$1
2930 typeset vdev
2931
2932 if [[ -z $pool ]]; then
2933 log_note "Missing pool name."
2934 return 2
2935 fi
2936
2937 shift
2938
2939 typeset tmpfile=$($MKTEMP)
2940 $ZPOOL list -Hv "$pool" >$tmpfile
2941 for vdev in $@; do
2942 $GREP -w ${vdev##*/} $tmpfile >/dev/null 2>&1
2943 [[ $? -ne 0 ]] && return 1
2944 done
2945
2946 $RM -f $tmpfile
2947
2948 return 0;
2949}
2950
679d73e9
JWK
2951function get_max
2952{
2953 typeset -l i max=$1
2954 shift
2955
2956 for i in "$@"; do
2957 max=$(echo $((max > i ? max : i)))
2958 done
2959
2960 echo $max
2961}
2962
2963function get_min
2964{
2965 typeset -l i min=$1
2966 shift
2967
2968 for i in "$@"; do
2969 min=$(echo $((min < i ? min : i)))
2970 done
2971
2972 echo $min
2973}
2974
6bb24f4d
BB
2975#
2976# Wait for newly created block devices to have their minors created.
2977#
2978function block_device_wait
2979{
2980 if is_linux; then
2981 $UDEVADM trigger
2982 $UDEVADM settle
2983 fi
2984}
1de321e6
JX
2985
2986#
2987# Synchronize all the data in pool
2988#
2989# $1 pool name
2990#
2991function sync_pool #pool
2992{
2993 typeset pool=${1:-$TESTPOOL}
2994
2995 log_must $SYNC
2996 log_must $SLEEP 2
2997 # Flush all the pool data.
2998 typeset -i ret
2999 $ZPOOL scrub $pool >/dev/null 2>&1
3000 ret=$?
3001 (( $ret != 0 )) && \
3002 log_fail "$ZPOOL scrub $pool failed."
3003
3004 while ! is_pool_scrubbed $pool; do
3005 if is_pool_resilvered $pool ; then
3006 log_fail "$pool should not be resilver completed."
3007 fi
3008 log_must $SLEEP 2
3009 done
3010}