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