Date:        Wed, 4 Apr 2018 01:32:48 +0200
    From:        Thomas Klausner <[email protected]>
    Message-ID:  <20180403233248.swb2p7tiziks2aug@danbala>

  | Wouldn't it be better to check device availability before asking for
  | the passphrase?

Seems reasonable to me.  I am testing a modification now...
(While not all that hard, it isn't as trivial as it seems).

In the meantine, I find the appended script (next_avail) useful
rather than embedding cgdN type things in scripts, etc...

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
        local nl='
'

        sysctl -n kern.drivers | tr ',' "$nl" | tr -d '][' |    
            grep " ${dev}\$" >/dev/null 2>&1 || {
                echo >&2 "No ${dev} driver in booted kernel"
                return 1
        }

        units=$(
                sysctl -n hw.disknames                          |
                        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