Date:        Sun, 17 Jan 2016 23:26:35 +0100
    From:        Michael van Elst <mlel...@serpens.de>
    Message-ID:  <20160117222634.ga5...@serpens.de>

  | I'd rather have something that lists existing devices, allocates
  | a fresh one and tells me the name and works for all such pseudo disks.

I use the following script.   You will probably want to remove the
"echo cgd0" bit - I do that to permanently reserve cgd0 from this kind
of use (all my systems use that one for one particular purpose.)

I call it next_avail - usage is something like

        VND=$( next_avail vnd )
        CGD=$( next_avail cgd )
        
(and whatever else is similar, I know there is at least one more, but I
have forgotten which it is .. oh yes, raid of course...)

kre

#! /bin/sh

# Usage: next_avail cloner-type-disk
#       eg; next_avail vnd
#                       (or cgd or raid ...)

next_avail ()
{
        local dev="$1"
        local N=$(( ${#dev} + 1 ))
        local unit units

        units=$(
                ( sysctl -n hw.disknames ; echo cgd0 )  |
                        tr ' ' '\012'                   |
                        grep '^'"${dev}"'[0-9]'         |
                        sort -u -n -k 1.$N                      )

        test -z "${units}" && {
                test -e "/dev/${dev}0a" || {
                        echo >&2 "No ${dev}s available!"
                        return 1
                }
                echo "${dev}0"
                return
        }

        N=0
        for unit in ${units}
        do
                if [ "${unit}" = "${dev}${N}" ]
                then
                        N=$(( N + 1 ))
                else
                        echo "${dev}${N}"
                        return
                fi
        done

        test -e /dev/"${dev}${N}a" || {
                echo >&2 "All ${dev}s in use"
                return 1
        }

        echo "${dev}${N}"
}


for A
do
        next_avail "${A}"
done


Reply via email to