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