Add map_nbd function
diff --git a/tools/get_uec_image.sh b/tools/get_uec_image.sh
index 935feba..de37842 100755
--- a/tools/get_uec_image.sh
+++ b/tools/get_uec_image.sh
@@ -111,25 +111,33 @@
     qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
 fi
 
+# Finds the next available NBD device
+# Exits script if error connecting or none free
+# map_nbd image
+# returns full nbd device path
+function map_nbd {
+    for i in `seq 0 15`; do
+        if [ ! -e /sys/block/nbd$i/pid ]; then
+            NBD=/dev/nbd$i
+            # Connect to nbd and wait till it is ready
+            qemu-nbd -c $NBD $1
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
+                echo "Couldn't connect $NBD"
+                exit 1
+            fi
+            break
+        fi
+    done
+    if [ -z "$NBD" ]; then
+        echo "No free NBD slots"
+        exit 1
+    fi
+    echo $NBD
+}
+
 # Set up nbd
 modprobe nbd max_part=63
-for i in `seq 1 15`; do
-    if [ ! -e /sys/block/nbd$i/pid ]; then
-        NBD=/dev/nbd$i
-        # Connect to nbd and wait till it is ready
-        qemu-nbd -c $NBD $IMG_FILE_TMP
-        if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
-            echo "Couldn't connect $NBD"
-            exit 1
-        fi
-        break
-    fi
-done
-if [ -z "$NBD" ]; then
-    echo "No free NBD slots"
-    exit 1
-fi
-NBD_DEV=`basename $NBD`
+NBD=`map_nbd $IMG_FILE_TMP`
 
 # Resize partition 1 to full size of the disk image
 echo "d