libtest.shlib revision 52244c0958bdf281ca42932b449f644b4decfdc2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# CDDL HEADER START
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# The contents of this file are subject to the terms of the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Common Development and Distribution License (the "License").
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# You may not use this file except in compliance with the License.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# or http://www.opensolaris.org/os/licensing.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# See the License for the specific language governing permissions
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# and limitations under the License.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# CDDL HEADER END
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Use is subject to license terms.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# Copyright (c) 2012, 2014 by Delphix. All rights reserved.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy. ${STF_TOOLS}/contrib/include/logapi.shlib
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren KennedyZFS=${ZFS:-/usr/sbin/zfs}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren KennedyZPOOL=${ZPOOL:-/usr/sbin/zpool}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Determine whether a dataset is mounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 filesystem type; optional - defaulted to zfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if dataset is mounted; 1 if unmounted; 2 on error
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction ismounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fstype=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $fstype ]] && fstype=zfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset out dir name ret
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $fstype in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy zfs)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ "$1" == "/"* ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for out in $($ZFS mount | $AWK '{print $2}'); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $1 == $out ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for out in $($ZFS mount | $AWK '{print $1}'); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $1 == $out ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ufs|nfs)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy out=$($DF -F $fstype $1 2>/dev/null)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ret=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($ret != 0)) && return $ret
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dir=${out%%\(*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dir=${dir%% *}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy name=${out##*\(}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy name=${name%%\)*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy name=${name%% *}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if a dataset is mounted; 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 filesystem type; optional - defaulted to zfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction mounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ismounted $1 $2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if a dataset is unmounted; 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 filesystem type; optional - defaulted to zfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction unmounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ismounted $1 $2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 1)) && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# split line on ","
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 - line to split
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction splitline
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $1 | $SED "s/,/ /g"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_setup_noexit "$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_pass
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a list of disks, setup storage pools and datasets.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_setup_noexit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset container=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset volume=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if poolexists $TESTPOOL ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy destroy_pool $TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $TESTPOOL $disklist
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy reexport_pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $MKDIR -p $TESTDIR || log_unresolved Could not create $TESTDIR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $container ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -rf $TESTDIR1 || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_unresolved Could not remove $TESTDIR1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $MKDIR -p $TESTDIR1 || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_unresolved Could not create $TESTDIR1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTCTR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set canmount=off $TESTPOOL/$TESTCTR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTCTR/$TESTFS1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set mountpoint=$TESTDIR1 \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $TESTPOOL/$TESTCTR/$TESTFS1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $volume ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create -V $VOLSIZE $TESTPOOL/$TESTVOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTVOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a list of disks, setup a storage pool, file system and
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# a container.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_container_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_setup "$disklist" "true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a list of disks, setup a storage pool,file system
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# and a volume.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_volume_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_setup "$disklist" "" "true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a list of disks, setup a storage pool,file system,
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# a container and a volume.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_container_volume_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_setup "$disklist" "true" "true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# filesystem
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 Existing filesystem or volume name. Default, $TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 snapshot name. Default, $TESTSNAP
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction create_snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs_vol=${1:-$TESTFS}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset snap=${2:-$TESTSNAP}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if snapexists $fs_vol@$snap; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$fs_vol@$snap already exists."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy datasetexists $fs_vol || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$fs_vol must exist."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS snapshot $fs_vol@$snap
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Create a clone from a snapshot, default clone name is $TESTCLONE.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 Clone name, $TESTPOOL/$TESTCLONE is default.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction create_clone # snapshot clone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset clone=${2:-$TESTPOOL/$TESTCLONE}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $snap ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Snapshot name is undefined."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $clone ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Clone name is undefined."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS clone $snap $clone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_mirror_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_mirror_setup_noexit $1 $2 $3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_pass
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a pair of disks, set up a storage pool and dataset for the mirror
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @parameters: $1 the primary side of the mirror
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 the secondary side of the mirror
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @uses: ZPOOL ZFS TESTPOOL TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_mirror_setup_noexit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy readonly func="default_mirror_setup_noexit"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset primary=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset secondary=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $primary ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$func: No parameters passed"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $secondary ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$func: No secondary partition passed"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $TESTPOOL mirror $@
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# create a number of mirrors.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# We create a number($1) of 2 way mirrors using the pairs of disks named
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# on the command line. These mirrors are *not* mounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @parameters: $1 the number of mirrors to create
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $... the devices to use to create the mirrors on
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @uses: ZPOOL ZFS TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction setup_mirrors
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i nmirrors=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((nmirrors > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must test -n "$1" -a -n "$2"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$TESTPOOL$nmirrors ]] && $RM -rf /$TESTPOOL$nmirrors
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $TESTPOOL$nmirrors mirror $1 $2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift 2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((nmirrors = nmirrors - 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# create a number of raidz pools.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# We create a number($1) of 2 raidz pools using the pairs of disks named
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# on the command line. These pools are *not* mounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @parameters: $1 the number of pools to create
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $... the devices to use to create the pools on
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @uses: ZPOOL ZFS TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction setup_raidzs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i nraidzs=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((nraidzs > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must test -n "$1" -a -n "$2"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$TESTPOOL$nraidzs ]] && $RM -rf /$TESTPOOL$nraidzs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $TESTPOOL$nraidzs raidz $1 $2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift 2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((nraidzs = nraidzs - 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Destroy the configured testpool mirrors.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# the mirrors are of the form ${TESTPOOL}{number}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @uses: ZPOOL ZFS TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction destroy_mirrors
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_cleanup_noexit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_pass
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a minimum of two disks, set up a storage pool and dataset for the raid-z
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 the list of disks
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_raidz_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist="$*"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disks=(${disklist[*]})
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ ${#disks[*]} -lt 2 ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "A raid-z requires a minimum of two disks."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$TESTPOOL ]] && $RM -rf /$TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $TESTPOOL raidz $1 $2 $3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_pass
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Common function used to cleanup storage pools and datasets.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Invoked at the start of the test suite to ensure the system
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is in a known state, and also at the end of each set of
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# sub-tests to ensure errors from one set of tests doesn't
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# impact the execution of the next set.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_cleanup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_cleanup_noexit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_pass
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_cleanup_noexit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset exclude=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Destroying the pool will also destroy any
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # filesystems it contains.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS unmount -a > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy exclude=`eval $ECHO \"'(${KEEP})'\"`
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ALL_POOLS=$($ZPOOL list -H -o name \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $GREP -v "$NO_POOLS" | $EGREP -v "$exclude")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Here, we loop through the pools we're allowed to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # destroy, only destroying them if it's safe to do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # so.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while [ ! -z ${ALL_POOLS} ]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for pool in ${ALL_POOLS}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if safe_to_destroy_pool $pool ;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy destroy_pool $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ALL_POOLS=$($ZPOOL list -H -o name \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $GREP -v "$NO_POOLS" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $EGREP -v "$exclude")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS mount -a
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for fs in $($ZFS list -H -o name \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $GREP "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy datasetexists $fs && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy -Rf $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Need cleanup here to avoid garbage dir left.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for fs in $($ZFS list -H -o name); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $fs == /$ZONE_POOL ]] && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d $fs ]] && log_must $RM -rf $fs/*
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # the default value
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for fs in $($ZFS list -H -o name); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set reservation=none $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set recordsize=128K $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set mountpoint=/$fs $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset enc=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy enc=$(get_prop encryption $fs)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ "$enc" == "off" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set checksum=on $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set compression=off $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set atime=on $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set devices=off $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set exec=on $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set setuid=on $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set readonly=off $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set snapdir=hidden $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set aclmode=groupmask $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS set aclinherit=secure $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d $TESTDIR ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $TESTDIR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Common function used to cleanup storage pools, file systems
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# and containers.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction default_container_cleanup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! is_global_zone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy reexport_pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ismounted $TESTPOOL/$TESTCTR/$TESTFS1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $? -eq 0 ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS unmount $TESTPOOL/$TESTCTR/$TESTFS1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy datasetexists $TESTPOOL/$TESTCTR/$TESTFS1 && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy -R $TESTPOOL/$TESTCTR/$TESTFS1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy datasetexists $TESTPOOL/$TESTCTR && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy -Rf $TESTPOOL/$TESTCTR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -e $TESTDIR1 ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $TESTDIR1 > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy default_cleanup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Common function used to cleanup snapshot of file system or volume. Default to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# delete the file system's snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 snapshot name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction destroy_snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! snapexists $snap; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "'$snap' does not existed."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # For the sake of the value which come from 'get_prop' is not equal
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # to the really mountpoint when the snapshot is unmounted. So, firstly
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # check and make sure this snapshot's been mounted in current system.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset mtpt=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ismounted $snap; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy mtpt=$(get_prop mountpoint $snap)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? != 0)) && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "get_prop mountpoint $snap failed."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy $snap
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $mtpt != "" && -d $mtpt ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Common function used to cleanup clone.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 clone name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction destroy_clone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset clone=${1:-$TESTPOOL/$TESTCLONE}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! datasetexists $clone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "'$clone' does not existed."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # With the same reason in destroy_snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset mtpt=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ismounted $clone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy mtpt=$(get_prop mountpoint $clone)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? != 0)) && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "get_prop mountpoint $clone failed."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy $clone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $mtpt != "" && -d $mtpt ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if a snapshot exists; $? otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 - snapshot name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction snapexists
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS list -H -t snapshot "$1" > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Set a property to a certain value on a dataset.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Sets a property of the dataset to the value as passed in.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @param:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 dataset who's property is being set
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 property to set
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3 value to set property to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @return:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# 0 if the property could be set.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# non-zero otherwise.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @use: ZFS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction dataset_setprop
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fn=dataset_setprop
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($# < 3)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "$fn: Insufficient parameters (need 3, had $#)"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset output=
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy output=$($ZFS set $2=$3 $1 2>&1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rv=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((rv != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Setting property on $1 failed."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "property $2=$3"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Return Code: $rv"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Output: $output"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $rv
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Assign suite defined dataset properties.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# This function is used to apply the suite's defined default set of
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# properties to a dataset.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @parameters: $1 dataset to use
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @returns:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# 0 if the dataset has been altered.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# 1 if no pool name was passed in.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# 2 if the dataset could not be found.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# 3 if the dataset could not have it's properties set.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction dataset_set_defaultproperties
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dataset="$1"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $dataset ]] && return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset confset=
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i found=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for confset in $($ZFS list); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $dataset = $confset ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy found=1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy break
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $found -eq 0 ]] && return 2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $COMPRESSION_PROP ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dataset_setprop $dataset compression $COMPRESSION_PROP || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $CHECKSUM_PROP ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dataset_setprop $dataset checksum $CHECKSUM_PROP || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check a numeric assertion
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @parameter: $@ the assertion to check
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @output: big loud notice if assertion failed
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @use: log_fail
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction assert
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($@)) || log_fail "$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Function to format partition size of a disk
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a disk cxtxdx reduces all partitions
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# to 0 size
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction zero_partitions #<whole_disk_name>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset diskname=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset i
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for i in 0 1 3 4 5 6 7
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy set_partition $i "" 0mb $diskname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a slice, size and disk, this function
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# formats the slice to the specified size.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Size should be specified with units as per
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# the `format` command requirements eg. 100mb 3gb
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i slicenum=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset start=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset size=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=$4
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $slicenum || -z $size || -z $disk ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "The slice, size or disk name is unspecified."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset format_file=/var/tmp/format_in.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "partition" >$format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "$slicenum" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "$start" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "$size" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "label" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "q" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "q" >> $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $FORMAT -e -s -d $disk -f $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ret_val=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -f $format_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $ret_val -ne 0 ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Unable to format $disk slice $slicenum to $size"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the end cyl of the given slice
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_endslice #<disk> <slice>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset slice=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $disk || -z $slice ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "The disk name or slice number is unspecified."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disk=${disk#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disk=${disk#/dev/rdsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disk=${disk%s*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ratio=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ratio=$($PRTVTOC /dev/rdsk/${disk}s2 | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "sectors\/cylinder" | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $AWK '{print $2}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((ratio == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i endcyl=$($PRTVTOC -h /dev/rdsk/${disk}s2 |
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $NAWK -v token="$slice" '{if ($1==token) print $6}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((endcyl = (endcyl + 1) / ratio))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy echo $endcyl
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a size,disk and total slice number, this function formats the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# disk slices from 0 to the total slice number with the same specified
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# size.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction partition_disk #<slice_size> <whole_disk_name> <total_slices>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i i=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset slice_size=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk_name=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset total_slices=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cyl
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy zero_partitions $disk_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((i < $total_slices)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((i == 2)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((i = i + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy set_partition $i "$cyl" $slice_size $disk_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cyl=$(get_endslice $disk_name $i)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((i = i+1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# This function continues to write to a filenum number of files into dirnum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# number of directories until either $FILE_WRITE returns an error or the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# maximum number of files per directory have been written.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Usage:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return value: 0 on success
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# non 0 on error
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Where :
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# destdir: is the directory where everything is to be created under
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# dirnum: the maximum number of subdirectories to use, -1 no limit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# filenum: the maximum number of files per subdirectory
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# bytes: number of bytes to write
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# num_writes: numer of types to write out bytes
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# data: the data that will be writen
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# E.g.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# file_fs /testdir 20 25 1024 256 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Note: bytes * num_writes equals the size of the testfile
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction fill_fs # destdir dirnum filenum bytes num_writes data
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset destdir=${1:-$TESTDIR}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i dirnum=${2:-50}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i filenum=${3:-50}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i bytes=${4:-8192}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i num_writes=${5:-10240}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i data=${6:-0}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i odirnum=1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i idirnum=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i fn=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i retval=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $MKDIR -p $destdir/$idirnum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while (($odirnum > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((dirnum >= 0 && idirnum >= dirnum)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy odirnum=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy break
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $FILE_WRITE -o create -f $destdir/$idirnum/$TESTFILE.$fn \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy -b $bytes -c $num_writes -d $data
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy retval=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($retval != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy odirnum=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy break
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($fn >= $filenum)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fn=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((idirnum = idirnum + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $MKDIR -p $destdir/$idirnum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((fn = fn + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $retval
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Simple function to get the specified property. If unable to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# get the property then exits.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Note property is in 'parsable' format (-p)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_prop # property dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset prop_val
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset prop=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dataset=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy prop_val=$($ZFS get -pH -o value $prop $dataset 2>/dev/null)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $? -ne 0 ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Unable to get $prop property for dataset " \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "$dataset"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $prop_val
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Simple function to get the specified property of pool. If unable to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# get the property then exits.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_pool_prop # property pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset prop_val
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset prop=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if poolexists $pool ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy prop_val=$($ZPOOL get $prop $pool 2>/dev/null | $TAIL -1 | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $AWK '{print $3}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $? -ne 0 ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Unable to get $prop property for pool " \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "$pool"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Pool $pool not exists."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $prop_val
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if a pool exists; $? otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 - pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction poolexists
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $pool ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "No pool name given."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZPOOL get name "$pool" > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if all the specified datasets exist; $? otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1-n dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction datasetexists
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($# == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "No dataset name given."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while (($# > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS get name $1 > /dev/null 2>&1 || \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return 0 if none of the specified datasets exists, otherwise return 1.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1-n dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction datasetnonexists
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($# == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "No dataset name given."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while (($# > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy && return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a mountpoint, or a dataset name, determine if it is shared.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Returns 0 if shared, 1 otherwise.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_shared
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $fs != "/"* ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if datasetnonexists "$fs" ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy mtpt=$(get_prop mountpoint "$fs")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $mtpt in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy none|legacy|-) return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *) fs=$mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for mtpt in `$SHARE | $AWK '{print $2}'` ; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $mtpt == $fs ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset stat=$($SVCS -H -o STA nfs/server:default)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $stat != "ON" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Current nfs/server status: $stat"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a mountpoint, determine if it is not shared.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Returns 0 if not shared, 1 otherwise.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction not_shared
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy is_shared $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Helper function to unshare a mountpoint.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction unshare_fs #fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy is_shared $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS unshare $fs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check NFS server status and trigger it online.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction setup_nfs_server
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Cannot share directory in non-global zone.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! is_global_zone; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Cannot trigger NFS server by sharing in LZ."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset nfs_fmri="svc:/network/nfs/server:default"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $($SVCS -Ho STA $nfs_fmri) != "ON" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Only really sharing operation can enable NFS server
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # to online permanently.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dummy=/tmp/dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -d $dummy ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $MKDIR $dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SHARE $dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Waiting for fmri's status to be the final status.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Otherwise, in transition, an asterisk (*) is appended for
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # instances, unshare will reverse status to 'DIS' again.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Waiting for 1's at least.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SLEEP 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy timeout=10
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while [[ timeout -ne 0 && $($SVCS -Ho STA $nfs_fmri) == *'*' ]]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SLEEP 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((timeout -= 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $UNSHARE $dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $dummy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Current NFS status: '$($SVCS -Ho STA,FMRI $nfs_fmri)'"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# To verify whether calling process is in global zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if in global zone, 1 in non-global zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_global_zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cur_zone=$($ZONENAME 2>/dev/null)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $cur_zone != "global" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify whether test is permitted to run from
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# global zone, local zone, or both
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 zone limit, could be "global", "local", or "both"(no limit)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if permitted, otherwise exit with log_unsupported
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_runnable # zone limit
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset limit=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $limit ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $limit in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy global|both)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy local) log_unsupported "Test is unable to run from "\
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "global zone."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *) log_note "Warning: unknown limit $limit - " \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "use both."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $limit in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy local|both)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy global) log_unsupported "Test is unable to run from "\
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "local zone."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *) log_note "Warning: unknown limit $limit - " \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "use both."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy reexport_pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if create successfully or the pool exists; $? otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Note: In local zones, this function should return 0 silently.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 - pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2-n - [keyword] devs_list
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction create_pool #pool devs_list
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=${1%%/*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $pool ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Missing pool name."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if poolexists $pool ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy destroy_pool $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$pool ]] && $RM -rf /$pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $pool $@
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if destroy successfully or the pool exists; $? otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Note: In local zones, this function should return 0 silently.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 - pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Destroy pool with the given parameters.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction destroy_pool #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=${1%%/*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $pool ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "No pool name given."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if is_global_zone ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if poolexists "$pool" ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy mtpt=$(get_prop mountpoint "$pool")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL destroy -f $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d $mtpt ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $mtpt
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Pool not exist. ($pool)"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Firstly, create a pool with 5 datasets. Then, create a single zone and
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# export the 5 datasets to it. In addition, we also add a ZFS filesystem
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# and a zvol device to the zone.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 zone name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 zone root directory prefix
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3 zone ip
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction zfs_zones_setup #zone_name zone_root zone_ip
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset zone_name=${1:-$(hostname)-z}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset zone_root=${2:-"/zone_root"}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset zone_ip=${3:-"10.1.1.10"}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset prefix_ctr=$ZONE_CTR
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool_name=$ZONE_POOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i cntctr=5
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i i=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Create pool and 5 container within it
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d /$pool_name ]] && $RM -rf /$pool_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL create -f $pool_name $DISKS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((i < cntctr)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create $pool_name/$prefix_ctr$i
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((i += 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # create a zvol
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS create -V 1g $pool_name/zone_zvol
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # If current system support slog, add slog device for pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if verify_slog_support ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset sdevs="/var/tmp/sdev1 /var/tmp/sdev2"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $MKFILE 100M $sdevs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL add $pool_name log mirror $sdevs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # this isn't supported just yet.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Create a filesystem. In order to add this to
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # the zone, it must have it's mountpoint set to 'legacy'
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # log_must $ZFS create $pool_name/zfs_filesystem
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # log_must $ZFS set mountpoint=legacy $pool_name/zfs_filesystem
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d $zone_root ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $zone_root/$zone_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ ! -d $zone_root ]] && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $MKDIR -p -m 0700 $zone_root/$zone_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Create zone configure file and configure the zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset zone_conf=/tmp/zone_conf.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "create" > $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "set zonepath=$zone_root/$zone_name" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "set autoboot=true" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy i=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((i < cntctr)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "add dataset" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "set name=$pool_name/$prefix_ctr$i" >> \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "end" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((i += 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # add our zvol to the zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "add device" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "end" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # add a corresponding zvol rdsk to the zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "add device" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "set match=/dev/zvol/rdsk/$pool_name/zone_zvol" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "end" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # once it's supported, we'll add our filesystem to the zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ECHO "add fs" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ECHO "set type=zfs" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ECHO "set special=$pool_name/zfs_filesystem" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ECHO "set dir=/export/zfs_filesystem" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ECHO "end" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "verify" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "commit" >> $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZONECFG -z $zone_name -f $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -f $zone_conf
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Install the zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZONEADM -z $zone_name install
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "SUCCESS: $ZONEADM -z $zone_name install"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "FAIL: $ZONEADM -z $zone_name install"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Install sysidcfg file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "system_locale=C" > $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "terminal=dtterm" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "network_interface=primary {" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "hostname=$zone_name" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "}" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "name_service=NONE" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "root_password=mo791xfZ/SFiw" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "security_policy=NONE" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "timezone=US/Eastern" >> $sysidcfg
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Boot this zone
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZONEADM -z $zone_name boot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Reexport TESTPOOL & TESTPOOL(1-4)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction reexport_pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i cntctr=5
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i i=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((i < cntctr)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((i == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! ismounted $TESTPOOL; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS mount $TESTPOOL
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if eval ! ismounted \$TESTPOOL$i; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must eval $ZFS mount \$TESTPOOL$i
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((i += 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify a given disk is online or offline
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 is pool/disk matches expected state, 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_state # pool disk state{online,offline}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=${2#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset state=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZPOOL status -v $pool | grep "$disk" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | grep -i "$state" > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the mountpoint of snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# as its mountpoint
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction snapshot_mountpoint
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $dataset != *@* ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Error name of snapshot '$dataset'."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset fs=${dataset%@*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset snap=${dataset#*@}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $fs || -z $snap ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Error name of snapshot '$dataset'."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a pool and file system, this function will verify the file system
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# using the zdb internal tool. Note that the pool is exported and imported
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# to ensure it has consistent state.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_filesys # pool filesystem dir
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool="$1"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset filesys="$2"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset zdbout="/tmp/zdbout.$$"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dirs=$@
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset search_path=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Calling $ZDB to verify filesystem '$filesys'"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS unmount -a > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL export $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $dirs ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for dir in $dirs ; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy search_path="$search_path -d $dir"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZPOOL import $search_path $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZDB -cudi $filesys > $zdbout 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $? != 0 ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Output: $ZDB -cudi $filesys"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CAT $zdbout
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$ZDB detected errors with: '$filesys'"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS mount -a
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -rf $zdbout
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a pool, and this function list all disks in the pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_disklist # pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disklist=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disklist=$($ZPOOL iostat -v $1 | $NAWK '(NR >4) {print $1}' | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP -v "\-\-\-\-\-" | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $EGREP -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $disklist
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Destroy all existing metadevices and state database
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction destroy_metas
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset metad
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for metad in $($METASTAT -p | $AWK '{print $1}'); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $METACLEAR -rf $metad
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for metad in $($METADB | $CUT -f6 | $GREP dev | $UNIQ); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $METADB -fd $metad
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# /**
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# This function kills a given list of processes after a time period. We use
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# this in the stress tests instead of STF_TIMEOUT so that we can have processes
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# would be listed as FAIL, which we don't want : we're happy with stress tests
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# running for a certain amount of time, then finishing.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @param $1 the time in seconds after which we should terminate these processes
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @param $2..$n the processes we wish to terminate.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# */
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction stress_timeout
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i TIMEOUT=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cpids="$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Waiting for child processes($cpids). " \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "It could last dozens of minutes, please be patient ..."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SLEEP $TIMEOUT
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Killing child processes after ${TIMEOUT} stress timeout."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pid
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for pid in $cpids; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $PS -p $pid > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $KILL -USR1 $pid
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify a given hotspare disk is inuse or avail
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 is pool/disk matches expected state, 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_hotspare_state # pool disk state{inuse,avail}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=${2#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset state=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cur_state=$(get_device_state $pool $disk "spares")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $state != ${cur_state} ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify a given slog disk is inuse or avail
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 is pool/disk matches expected state, 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_slog_state # pool disk state{online,offline,unavail}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=${2#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset state=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cur_state=$(get_device_state $pool $disk "logs")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $state != ${cur_state} ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify a given vdev disk is inuse or avail
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 is pool/disk matches expected state, 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_vdev_state # pool disk state{online,offline,unavail}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=${2#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset state=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cur_state=$(get_device_state $pool $disk)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $state != ${cur_state} ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check the output of 'zpool status -v <pool>',
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# and to see if the content of <token> contain the <keyword> specified.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 is contain, 1 otherwise
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_pool_status # pool token keyword
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset token=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset keyword=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZPOOL status -v "$pool" 2>/dev/null | $NAWK -v token="$token:" '
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ($1==token) {print $0}' \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $GREP -i "$keyword" > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# These 5 following functions are instance of check_pool_status()
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is_pool_resilvering - to check if the pool is resilver in progress
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is_pool_resilvered - to check if the pool is resilver completed
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is_pool_scrubbing - to check if the pool is scrub in progress
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is_pool_scrubbed - to check if the pool is scrub completed
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is_pool_scrub_stopped - to check if the pool is scrub stopped
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_pool_resilvering #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy check_pool_status "$1" "scan" "resilver in progress since "
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_pool_resilvered #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy check_pool_status "$1" "scan" "resilvered "
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_pool_scrubbing #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy check_pool_status "$1" "scan" "scrub in progress since "
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_pool_scrubbed #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy check_pool_status "$1" "scan" "scrub repaired"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_pool_scrub_stopped #pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy check_pool_status "$1" "scan" "scrub canceled"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Use create_pool()/destroy_pool() to clean up the infomation in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# in the given disk to avoid slice overlapping.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction cleanup_devices #vdevs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool="foopool$$"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if poolexists $pool ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy destroy_pool $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy create_pool $pool $@
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy destroy_pool $pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify the rsh connectivity to each remote host in RHOSTS.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Return 0 if remote host is accessible; otherwise 1.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 remote host name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 username
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_rsh_connect #rhost, username
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rhost=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset username=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rsh_cmd="$RSH -n"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cur_user=
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GETENT hosts $rhost >/dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "$rhost cannot be found from" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "administrative database."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $PING $rhost 3 >/dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "$rhost is not reachable."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#username} != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rsh_cmd="$rsh_cmd -l $username"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cur_user="given user \"$username\""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cur_user="current user \"`$LOGNAME`\""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! $rsh_cmd $rhost $TRUE; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "$RSH to $rhost is not accessible" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "with $cur_user."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify the remote host connection via rsh after rebooting
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 remote host
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_remote
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rhost=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # The following loop waits for the remote system rebooting.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Each iteration will wait for 150 seconds. there are
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # total 5 iterations, so the total timeout value will
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # be 12.5 minutes for the system rebooting. This number
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # is an approxiate number.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i count=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ! verify_rsh_connect $rhost; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy sleep 150
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((count = count + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((count > 5)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Replacement function for /usr/bin/rsh. This function will include
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# the /usr/bin/rsh and meanwhile return the execution status of the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# last command.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 usrname passing down to -l option of /usr/bin/rsh
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 remote machine hostname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3... command string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction rsh_status
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ruser=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rhost=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ret=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cmd_str=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rsh_str=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift; shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cmd_str="$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy err_file=/tmp/${rhost}.$$.err
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#ruser} == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rsh_str="$RSH -n"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rsh_str="$RSH -n -l $ruser"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $rsh_str $rhost /usr/bin/ksh -c "'$cmd_str; \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy print -u 2 \"status=\$?\"'" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy >/dev/null 2>$err_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ret=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($ret != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CAT $err_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -f $std_file $err_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$RSH itself failed with exit code $ret..."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ret=$($GREP -v 'print -u 2' $err_file | $GREP 'status=' | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CUT -d= -f2)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($ret != 0)) && $CAT $err_file >&2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -f $err_file >/dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $ret
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the SUNWstc-fs-zfs package installation path in a remote host
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 remote host name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_remote_pkgpath
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rhost=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pkgpath=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy pkgpath=$($RSH -n $rhost "$PKGINFO -l SUNWstc-fs-zfs | $GREP BASEDIR: |\
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CUT -d: -f2")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $pkgpath
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#/**
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# A function to find and locate free disks on a system or from given
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# disks as the parameter. It works by locating disks that are in use
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# as swap devices, SVM devices, and dump devices, and also disks
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# listed in /etc/vfstab
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $@ given disks to find which are free, default is all disks in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# the test system
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# @return a string containing the list of available disks
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#*/
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction find_disks
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy sfi=/tmp/swaplist.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy msi=/tmp/metastat.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dmpi=/tmp/dumpdev.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy max_finddisksnum=${MAX_FINDDISKSNUM:-6}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SWAP -l > $sfi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $METASTAT -c > $msi 2>/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $DUMPADM > $dmpi 2>/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# write an awk script that can process the output of format
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# to produce a list of disks we know about. Note that we have
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# to escape "$2" so that the shell doesn't interpret it while
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# we're creating the awk script.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# -------------------
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CAT > /tmp/find_disks.awk <<EOF
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#!/bin/nawk -f
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy BEGIN { FS="."; }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy /^Specify disk/{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy searchdisks=0;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (searchdisks && \$2 !~ "^$"){
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy split(\$2,arr," ");
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy print arr[1];
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy /^AVAILABLE DISK SELECTIONS:/{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy searchdisks=1;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren KennedyEOF
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#---------------------
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $CHMOD 755 /tmp/find_disks.awk
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy disks=${@:-$($ECHO "" | $FORMAT -e 2>/dev/null | /tmp/find_disks.awk)}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM /tmp/find_disks.awk
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy unused=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for disk in $disks; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Check for mounted
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "${disk}[sp]" /etc/mnttab >/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Check for swap
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "${disk}[sp]" $sfi >/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Check for SVM
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "${disk}" $msi >/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # check for dump device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "${disk}[sp]" $dmpi >/dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # check to see if this disk hasn't been explicitly excluded
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # by a user-set environment variable
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "${ZFS_HOST_DEVICES_IGNORE}" | $GREP "${disk}" > /dev/null
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (($? == 0)) && continue
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy unused_candidates="$unused_candidates $disk"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM $sfi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM $msi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM $dmpi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# now just check to see if those disks do actually exist
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# by looking for a device pointing to the first slice in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# each case. limit the number to max_finddisksnum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy count=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for disk in $unused_candidates; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ -b /dev/dsk/${disk}s0 ]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ $count -lt $max_finddisksnum ]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy unused="$unused $disk"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # do not impose limit if $@ is provided
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $@ ]] && ((count = count + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# finally, return our disk list
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $unused
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Add specified user to specified group
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 group name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 user name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3 base of the homedir (optional)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction add_user #<group_name> <user_name> <basedir>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset gname=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset uname=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset basedir=${3:-"/var/tmp"}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#gname} == 0 || ${#uname} == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "group name or user name are not defined."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $USERADD -g $gname -d $basedir/$uname -m $uname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Delete the specified user.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 login name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 base of the homedir (optional)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction del_user #<logname> <basedir>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset user=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset basedir=${2:-"/var/tmp"}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#user} == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "login name is necessary."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if $ID $user > /dev/null 2>&1; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $USERDEL $user
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -d $basedir/$user ]] && $RM -fr $basedir/$user
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Select valid gid and create specified group.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 group name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction add_group #<group_name>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset group=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#group} == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "group name is necessary."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Assign 100 as the base gid
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i gid=100
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while true; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GROUPADD -g $gid $group > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ret=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $ret in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy 0) return 0 ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # The gid is not unique
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy 4) ((gid += 1)) ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *) return 1 ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Delete the specified group.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 group name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction del_group #<group_name>
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset grp=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((${#grp} == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "group name is necessary."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GROUPMOD -n $grp $grp > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ret=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $ret in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Group does not exist.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy 6) return 0 ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # Name already exists as a group name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy 9) log_must $GROUPDEL $grp ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *) return 1 ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# This function will return true if it's safe to destroy the pool passed
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# as argument 1. It checks for pools based on zvols and files, and also
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# files contained in a pool that may have a different mountpoint.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction safe_to_destroy_pool { # $1 the pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset DONT_DESTROY=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # We check that by deleting the $1 pool, we're not
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # going to pull the rug out from other pools. Do this
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # by looking at all other pools, ensuring that they
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # aren't built from files or zvols contained in this pool.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for pool in $($ZPOOL list -H -o name)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ALTMOUNTPOOL=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # this is a list of the top-level directories in each of the
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # files that make up the path to the files the pool is based on
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy FILEPOOL=$($ZPOOL status -v $pool | $GREP /$1/ | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $AWK '{print $1}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # this is a list of the zvols that make up the pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ZVOLPOOL=$($ZPOOL status -v $pool | $GREP "/dev/zvol/dsk/$1$" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $AWK '{print $1}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # also want to determine if it's a file-based pool using an
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # alternate mountpoint...
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy POOL_FILE_DIRS=$($ZPOOL status -v $pool | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP / | $AWK '{print $1}' | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $AWK -F/ '{print $2}' | $GREP -v "dev")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for pooldir in $POOL_FILE_DIRS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy OUTPUT=$($ZFS list -H -r -o mountpoint $1 | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $GREP "${pooldir}$" | $AWK '{print $1}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ ! -z "$ZVOLPOOL" ]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy DONT_DESTROY="true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Pool $pool is built from $ZVOLPOOL on $1"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ ! -z "$FILEPOOL" ]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy DONT_DESTROY="true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Pool $pool is built from $FILEPOOL on $1"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ ! -z "$ALTMOUNTPOOL" ]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy DONT_DESTROY="true"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [ -z "${DONT_DESTROY}" ]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "Warning: it is not safe to destroy $1!"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get IP address of hostname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 hostname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction getipbyhost
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ip
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ip=`$ARP $1 2>/dev/null | $AWK -F\) '{print $1}' \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $AWK -F\('{print $2}'`
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $ip
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Setup iSCSI initiator to target
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 target hostname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction iscsi_isetup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # check svc:/network/iscsi_initiator:default state, try to enable it
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # if the state is not ON
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ISCSII_FMRI="svc:/network/iscsi_initiator:default"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ "ON" != $($SVCS -H -o sta $ISCSII_FMRI) ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SVCADM enable $ISCSII_FMRI
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i retry=20
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while [[ "ON" != $($SVCS -H -o sta $ISCSII_FMRI) && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ($retry -ne 0) ]]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((retry = retry - 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SLEEP 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ "ON" != $($SVCS -H -o sta $ISCSII_FMRI) ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$ISCSII_FMRI service can not be enabled!"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ISCSIADM add discovery-address $(getipbyhost $1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ISCSIADM modify discovery --sendtargets enable
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $DEVFSADM -i iscsi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check whether iscsi parameter is set as remote
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return 0 if iscsi is set as remote, otherwise 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction check_iscsi_remote
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $iscsi == "remote" ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check if a volume is a valide iscsi target
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 volume name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return 0 if suceeds, otherwise, return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_iscsi_target
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dataset=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset target targets
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $dataset ]] && return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy targets=$($ISCSITADM list target | $GREP "Target:" | $AWK '{print $2}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $targets ]] && return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy for target in $targets; do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $dataset == $target ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the iSCSI name of a target
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 target name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction iscsi_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset target=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ -z $target ]] && log_fail "No parameter."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! is_iscsi_target $target ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Not a target."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy name=$($ISCSITADM list target $target | $GREP "iSCSI Name:" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $AWK '{print $2}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# check svc:/system/iscsitgt:default state, try to enable it if the state
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# is not ON
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction iscsitgt_setup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -f $ISCSITGTFILE
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ "ON" == $($SVCS -H -o sta $ISCSITGT_FMRI) ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_note "iscsitgt is already enabled"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SVCADM enable -t $ISCSITGT_FMRI
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i retry=20
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while [[ "ON" != $($SVCS -H -o sta $ISCSITGT_FMRI) && \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ($retry -ne 0) ]]
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SLEEP 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((retry = retry - 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ "ON" != $($SVCS -H -o sta $ISCSITGT_FMRI) ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$ISCSITGT_FMRI service can not be enabled!"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $TOUCH $ISCSITGTFILE
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# set DISABLED state of svc:/system/iscsitgt:default
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# which is the most suiteable state if $ISCSITGTFILE exists
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction iscsitgt_cleanup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -e $ISCSITGTFILE ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $SVCADM disable $ISCSITGT_FMRI
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $RM -f $ISCSITGTFILE
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Close iSCSI initiator to target
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 target hostname
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction iscsi_iclose
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ISCSIADM modify discovery --sendtargets disable
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ISCSIADM remove discovery-address $(getipbyhost $1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $DEVFSADM -Cv
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the available ZFS compression options
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 option type zfs_set|zfs_compress
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_compress_opts
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset COMPRESS_OPTS
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy gzip-6 gzip-7 gzip-8 gzip-9"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $1 == "zfs_compress" ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy COMPRESS_OPTS="on lzjb"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy elif [[ $1 == "zfs_set" ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy COMPRESS_OPTS="on off lzjb"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset valid_opts="$COMPRESS_OPTS"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS get 2>&1 | $GREP gzip >/dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $? -eq 0 ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy valid_opts="$valid_opts $GZIP_OPTS"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "$valid_opts"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Verify zfs operation with -p option work as expected
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 operation, value could be create, clone or rename
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 dataset type, value could be fs or vol
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3 dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $4 new dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_opt_p_ops
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ops=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset datatype=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dataset=$3
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset newdataset=$4
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $datatype != "fs" && $datatype != "vol" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$datatype is not supported."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # check parameters accordingly
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy case $ops in
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy create)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy newdataset=$dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy dataset=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $datatype == "vol" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ops="create -V $VOLSIZE"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy clone)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $newdataset ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "newdataset should not be empty" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "when ops is $ops."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must datasetexists $dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must snapexists $dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rename)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $newdataset ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "newdataset should not be empty" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy "when ops is $ops."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must datasetexists $dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_mustnot snapexists $dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy *)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "$ops is not supported."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ;;
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy esac
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # make sure the upper level filesystem does not exist
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if datasetexists ${newdataset%/*} ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS destroy -rRf ${newdataset%/*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # without -p option, operation will fail
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_mustnot $ZFS $ops $dataset $newdataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_mustnot datasetexists $newdataset ${newdataset%/*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # with -p option, operation should succeed
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS $ops -p $dataset $newdataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! datasetexists $newdataset ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "-p option does not work for $ops"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # when $ops is create or clone, redo the operation still return zero
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $ops != "rename" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_must $ZFS $ops -p $dataset $newdataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get configuration of pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 config name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_config
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset config=$2
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset alt_root
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! poolexists "$pool" ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy alt_root=$($ZPOOL list -H $pool | $AWK '{print $NF}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $alt_root == "-" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy value=$($ZDB -C $pool | $GREP "$config:" | $AWK -F: \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy '{print $2}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy value=$($ZDB -e $pool | $GREP "$config:" | $AWK -F: \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy '{print $2}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -n $value ]] ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy value=${value#'}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy value=${value%'}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy echo $value
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Privated function. Random select one of items from arguments.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 count
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2-n string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction _random_get
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cnt=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy shift
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset str="$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ind
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((ind = RANDOM % cnt + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset ret=$($ECHO "$str" | $CUT -f $ind -d ' ')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $ret
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Random select one of item from arguments which include NONE string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction random_get_with_non
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i cnt=$#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((cnt =+ 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy _random_get "$cnt" "$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Random select one of item from arguments which doesn't include NONE string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction random_get
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy _random_get "$#" "$@"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Detect if the current system support slog
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_slog_support
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dir=/tmp/disk.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=foo.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset vdev=$dir/a
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset sdev=$dir/b
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $MKDIR -p $dir
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $MKFILE 64M $vdev $sdev
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ret=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! $ZPOOL create -n $pool $vdev log $sdev > /dev/null 2>&1; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ret=1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -r $dir
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $ret
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# The function will generate a dataset name with specific length
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1, the length of the name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2, the base string to construct the name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction gen_dataset_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i len=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset basestr="$2"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i baselen=${#basestr}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i iter=0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset l_name=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((len % baselen == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((iter = len / baselen))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((iter = len / baselen + 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy while ((iter > 0)); do
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy l_name="${l_name}$basestr"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ((iter -= 1))
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy done
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $l_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get cksum tuple of dataset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 dataset name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# sample zdb output:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction datasetcksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SYNC
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cksum=$($ZDB -vvv $1 | $GREP "^Dataset $1 \[" | $GREP "cksum" \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy | $AWK -F= '{print $7}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $cksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get cksum of file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# #1 file path
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction checksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset cksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy cksum=$($CKSUM $1 | $AWK '{print $1}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $cksum
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the given disk/slice state from the specific field of the pool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_device_state #pool disk field("", "spares","logs")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pool=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=${2#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset field=${3:-$pool}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy state=$($ZPOOL status -v "$pool" 2>/dev/null | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $NAWK -v device=$disk -v pool=$pool -v field=$field \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy 'BEGIN {startconfig=0; startfield=0; }
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy /config:/ {startconfig=1}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (startconfig==1) && ($1==field) {startfield=1; next;}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (startfield==1) && ($1==device) {print $2; exit;}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy (startfield==1) &&
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy echo $state
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# print the given directory filesystem type
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 directory name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_fstype
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dir=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $dir ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Usage: get_fstype <directory>"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # $ df -n /
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # / : ufs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $DF -n $dir | $AWK '{print $3}'
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Given a disk, label it to VTOC regardless what label was on the disk
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 disk
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction labelvtoc
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset disk=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z $disk ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "The disk name is unspecified."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset label_file=/var/tmp/labelvtoc.$$
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset arch=$($UNAME -p)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $arch == "i386" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "label" > $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "0" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "q" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "q" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $FDISK -B $disk >/dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # wait a while for fdisk finishes
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SLEEP 60
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy elif [[ $arch == "sparc" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "label" > $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "0" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "q" >> $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "unknown arch type"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $FORMAT -e -s -d $disk -f $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i ret_val=$?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $RM -f $label_file
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy # wait the format to finish
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy #
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SLEEP 60
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((ret_val != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "unable to label $disk as VTOC."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# check if the system was installed as zfsroot or not
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return: 0 ture, otherwise false
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_zfsroot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $DF -n / | $GREP zfs > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# get the root filesystem name if it's zfsroot system.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return: root filesystem name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_rootfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rootfs=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rootfs=$($AWK '{if ($2 == "/" && $3 == "zfs") print $1}' \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy /etc/mnttab)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z "$rootfs" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Can not get rootfs"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS list $rootfs > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $rootfs
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "This is not a zfsroot system."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# get the rootfs's pool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# return:
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# rootpool name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_rootpool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rootfs=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset rootpool=""
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rootfs=$($AWK '{if ($2 == "/" && $3 =="zfs") print $1}' \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy /etc/mnttab)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ -z "$rootfs" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "Can not get rootpool"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ZFS list $rootfs > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? == 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy rootpool=`$ECHO $rootfs | awk -F\/ '{print $1}'`
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $rootpool
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_fail "This is not a zfsroot system."
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the sub string from specified source string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $1 source string
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $2 start position. Count from 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# $3 offset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_substr #src_str pos offset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset pos offset
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $1 | \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $NAWK -v pos=$2 -v offset=$3 '{print substr($0, pos, offset)}'
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check if the given device is physical device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_physical_device #device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset device=${1#/dev/dsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy device=${device#/dev/rdsk/}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $device | $EGREP "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return $?
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the directory path of given device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_device_dir #device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset device=$1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ! $(is_physical_device $device) ; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if [[ $device != "/" ]]; then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy device=${device%/*}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $device
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO "/dev/dsk"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the package name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_package_name
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset dirpath=${1:-$STC_NAME}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Get the word numbers from a string separated by white space
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction get_word_count
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $ECHO $1 | $WC -w
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# To verify if the require numbers of disks is given
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction verify_disk_count
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i min=${2:-1}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset -i count=$(get_word_count "$1")
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if ((count < min)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy log_untested "A minimum of $min disks is required to run." \
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy " You specified $count disk(s)"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction ds_is_volume
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset type=$(get_prop type $1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $type = "volume" ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction ds_is_filesystem
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset type=$(get_prop type $1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $type = "filesystem" ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction ds_is_snapshot
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy typeset type=$(get_prop type $1)
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy [[ $type = "snapshot" ]] && return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy# Check if Trusted Extensions are installed and enabled
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy#
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedyfunction is_te_enabled
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy{
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy $SVCS -H -o state labeld 2>/dev/null | $GREP "enabled"
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy if (($? != 0)); then
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 1
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy else
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy return 0
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy fi
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Utility function to determine if a system has multiple cpus.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction is_mp
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy (($($PSRINFO | $WC -l) > 1))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Run the given command as the user provided.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction user_run
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset user=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy shift
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy eval \$SU \$user -c \"$@\" > /tmp/out 2>/tmp/err
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return $?
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy#
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# Check if the pool contains the specified vdevs
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy#
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# $1 pool
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# $2..n <vdev> ...
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy#
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# Return 0 if the vdevs are contained in the pool, 1 if any of the specified
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy# vdevs is not in the pool, and 2 if pool name is missing.
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy#
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedyfunction vdevs_in_pool
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy{
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy typeset pool=$1
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy typeset vdev
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy if [[ -z $pool ]]; then
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy log_note "Missing pool name."
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy return 2
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy fi
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy shift
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy typeset tmpfile=$($MKTEMP)
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy $ZPOOL list -Hv "$pool" >$tmpfile
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy for vdev in $@; do
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy $GREP -w ${vdev##*/} $tmpfile >/dev/null 2>&1
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy [[ $? -ne 0 ]] && return 1
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy done
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy $RM -f $tmpfile
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy return 0;
52244c0958bdf281ca42932b449f644b4decfdc2John Wren Kennedy}