Skip to content

Instantly share code, notes, and snippets.

@hansode
Created December 29, 2014 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansode/0db516d8616d908a5f0f to your computer and use it in GitHub Desktop.
Save hansode/0db516d8616d908a5f0f to your computer and use it in GitHub Desktop.
#!/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