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