#!/bin/ksh93 LIC='[-?$Id$] [-copyright?Copyright (c) 2013 Jens Elkner. All rights reserved.] [-license?CDDL 1.0]' SDIR=$( cd ${ dirname $0; }; printf "$PWD" ) typeset -r FPROG=${.sh.file} typeset -r PROG=${FPROG##*/} #MAC_PREFIX=12:34:56 typeset -r LIBNAME="${.sh.file}" . ${LIBNAME%/*}/log.kshlib . ${LIBNAME%/*}/man.kshlib . ${LIBNAME%/*}/utils.kshlib function getZoneIf { [[ -z $1 ]] && return if [[ -n ${1} ]]; then integer OFF=0; typeset LAST='' OVER='' X while (( OFF < 255 )); do X=${ dladm show-vnic -p -o over,macaddress ${1}${OFF} 2>/dev/null; } if [[ -z $X ]]; then VNIC=${1}${OFF} if [[ -n ${LAST} ]]; then MAC=${LAST#*:} OVER=${.sh.match%:} integer -i16 N=16#${MAC##*:} (( N++ )) MAC="${MAC%:*}:${N#*#}" fi break fi LAST="$X" (( OFF++ )) done (( OFF )) && X='-r' || X='' dladm show-phys -p -o link,device | sort -k2,2 -t: $X | \ while IFS=':' read L D ; do [[ $L == ${OVER} || ${L:0:7} == 'vboxnet' ]] && continue DEFAULTIF=$L break done fi } function checkInUse { MAC=${ normalizeMac ${MAC} ; } typeset LINE NMAC IF dladm show-vnic -p -o link,macaddress | while read LINE ; do NMAC=${LINE#*:} IF=${.sh.match%:} NMAC=${ normalizeMac ${NMAC} ; } if [[ ${MAC} == ${NMAC} ]]; then Log.warn "+ dladm delete-vnic ${IF}" break fi done } function showUsage { typeset WHAT="$2" getopts -a "$PROG" "${ print ${Man.FUNC[$WHAT]}; }" OPT --man } Man.addFunc MAIN '' '[+NAME?'"$PROG"' - get the default interface and mac address to use for a VNIC] [+DESCRIPTION?Determines the default interface and MAC address to use for a new virtual network interface card (vnic) for a new zone. If \azname\a is given, the zone with the given name gets taken into account and a complete \bdladm\b(1M) command gets printed, otherwise the interface and mac address, only.] [h:help?Print this help and exit.] [F:functions?Print out a list of all defined functions. Just invokes the \btypeset +f\b builtin.] [H:usage]:[function?Show the usage information for the given function if available and exit. See also option \b-F\b.] [T:trace]:[fname_list?A comma or whitspace separated list of function names, which should be traced during execution.] \n\n[\azname\a] ' X="${ print ${Man.FUNC[MAIN]} ; }" while getopts "${X}" option ; do case "$option" in h) showUsage ${PROG} MAIN ; exit 0 ;; F) typeset +f ; exit 0 ;; H) if [[ ${OPTARG%_t} != $OPTARG ]]; then $OPTARG --man # self-defined types else showUsage "$OPTARG" "$OPTARG" # function fi exit 0 ;; T) typeset -ft ${OPTARG//,/ } ;; esac done X=$((OPTIND-1)) shift $X VNIC='' getDefaultIfAndMac getZoneIf ${1} checkInUse if [[ -n ${1} ]]; then print "+ dladm create-vnic -m ${MAC} -l ${DEFAULTIF} ${VNIC}" else print "${DEFAULTIF} ${MAC}" fi