Created
December 29, 2014 06:18
-
-
Save hansode/0db516d8616d908a5f0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# requires: | |
# bash | |
# | |
set -e | |
set -o pipefail | |
function nbd_used?() { | |
local devpath=${1} | |
local find=0 line= | |
while read line; do | |
[[ ${line} == "" ]] && continue | |
set ${line} | |
[[ "${1}" == "43" ]] || continue | |
if [[ ${devpath} == /dev/${4} ]]; then | |
find=1 | |
fi | |
done < /proc/partitions | |
[[ "${find}" == "1" ]] | |
} | |
function find_free_nbd_device() { | |
local nbd= | |
for nbd in /dev/nbd*; do | |
case "${nbd}" in | |
*p[0-9]) continue ;; | |
esac | |
nbd_used? ${nbd} && continue | |
echo ${nbd} | |
break | |
done | |
# free nbd device not found | |
return 1 | |
} | |
find_free_nbd_device |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment