Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Trouble booting from thumb drive...



In the previous message in this thread, I said ...

        Here's a little function/script that

So, here's an improved version, that actually correctly finds the
first available free device (rather than returning the one after the
biggest one in use), and which also checks to determine that the
device selected actually exists in /dev to be available to use.
(It does assume that there are no missing /dev entries, that is,
if vnd3 is in use, then /dev/vnd[0-2]* all will exist).

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          |
                        tr ' ' '\012'           |
                        grep '^'"${dev}"'[0-9]' |
                        sort -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




Home | Main Index | Thread Index | Old Index