Browse code

Add map_nbd function

Dean Troyer authored on 2011/11/02 05:46:14
Showing 3 changed files
... ...
@@ -239,26 +239,35 @@ rm -f $VM_DIR/disk
239 239
 # Create our instance fs
240 240
 qemu-img create -f qcow2 -b $VM_IMAGE disk
241 241
 
242
+# Finds the next available NBD device
243
+# Exits script if error connecting or none free
244
+# map_nbd image
245
+# returns full nbd device path
246
+function map_nbd {
247
+    for i in `seq 0 15`; do
248
+        if [ ! -e /sys/block/nbd$i/pid ]; then
249
+            NBD=/dev/nbd$i
250
+            # Connect to nbd and wait till it is ready
251
+            qemu-nbd -c $NBD $1
252
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
253
+                echo "Couldn't connect $NBD"
254
+                exit 1
255
+            fi
256
+            break
257
+        fi
258
+    done
259
+    if [ -z "$NBD" ]; then
260
+        echo "No free NBD slots"
261
+        exit 1
262
+    fi
263
+    echo $NBD
264
+}
265
+
242 266
 # Make sure we have nbd-ness
243 267
 modprobe nbd max_part=63
244 268
 
245 269
 # Set up nbd
246
-for i in `seq 0 15`; do
247
-    if [ ! -e /sys/block/nbd$i/pid ]; then
248
-        NBD=/dev/nbd$i
249
-        # Connect to nbd and wait till it is ready
250
-        qemu-nbd -c $NBD disk
251
-        if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
252
-            echo "Couldn't connect $NBD"
253
-            exit 1
254
-        fi
255
-        break
256
-    fi
257
-done
258
-if [ -z "$NBD" ]; then
259
-    echo "No free NBD slots"
260
-    exit 1
261
-fi
270
+NBD=`map_nbd disk`
262 271
 NBD_DEV=`basename $NBD`
263 272
 
264 273
 # Mount the instance
... ...
@@ -10,6 +10,9 @@ if [ ! "$#" -eq "1" ]; then
10 10
     exit 1
11 11
 fi
12 12
 
13
+# Set up nbd
14
+modprobe nbd max_part=63
15
+
13 16
 # Echo commands
14 17
 set -o xtrace
15 18
 
... ...
@@ -43,30 +46,42 @@ STACKSH_PARAMS=${STACKSH_PARAMS:-}
43 43
 # Option to use the version of devstack on which we are currently working
44 44
 USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
45 45
 
46
-# Set up nbd
47
-modprobe nbd max_part=63
48
-NBD=${NBD:-/dev/nbd9}
49
-NBD_DEV=`basename $NBD`
50
-
51 46
 # clean install
52 47
 if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then
53 48
     $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img
54
-#    # copy kernel modules...
55
-#    # NOTE(ja): is there a better way to do this?
56
-#    cp -pr /lib/modules/`uname -r` $CACHEDIR/$DIST_NAME-base/lib/modules
57
-#    # a simple password - pass
58
-#    echo root:pass | chroot $CACHEDIR/$DIST_NAME-base chpasswd
59 49
 fi
60 50
 
61
-# prime image with as many apt/pips as we can
62
-if [ ! -r $CACHEDIR/$DIST_NAME-dev.img ]; then
63
-    cp -p $CACHEDIR/$DIST_NAME-base.img $CACHEDIR/$DIST_NAME-dev.img
64
-
65
-    qemu-nbd -c $NBD $CACHEDIR/$DIST_NAME-dev.img
66
-    if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
67
-        echo "Couldn't connect $NBD"
51
+# Finds the next available NBD device
52
+# Exits script if error connecting or none free
53
+# map_nbd image
54
+# returns full nbd device path
55
+function map_nbd {
56
+    for i in `seq 0 15`; do
57
+        if [ ! -e /sys/block/nbd$i/pid ]; then
58
+            NBD=/dev/nbd$i
59
+            # Connect to nbd and wait till it is ready
60
+            qemu-nbd -c $NBD $1
61
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
62
+                echo "Couldn't connect $NBD"
63
+                exit 1
64
+            fi
65
+            break
66
+        fi
67
+    done
68
+    if [ -z "$NBD" ]; then
69
+        echo "No free NBD slots"
68 70
         exit 1
69 71
     fi
72
+    echo $NBD
73
+}
74
+
75
+# prime image with as many apt/pips as we can
76
+DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
77
+DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
78
+if [ ! -r $DEV_FILE ]; then
79
+    cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
80
+
81
+    NBD=`map_nbd $DEV_FILE_TMP`
70 82
     MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
71 83
     mount -t ext4 ${NBD}p1 $MNTDIR
72 84
     cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
... ...
@@ -82,7 +97,8 @@ if [ ! -r $CACHEDIR/$DIST_NAME-dev.img ]; then
82 82
     chroot $MNTDIR chown stack $DEST
83 83
 
84 84
     # a simple password - pass
85
-    echo stack:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
85
+    echo stack:pass | chroot $MNTDIR chpasswd
86
+    echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
86 87
 
87 88
     # and has sudo ability (in the future this should be limited to only what
88 89
     # stack requires)
... ...
@@ -91,27 +107,29 @@ if [ ! -r $CACHEDIR/$DIST_NAME-dev.img ]; then
91 91
     umount $MNTDIR
92 92
     rmdir $MNTDIR
93 93
     qemu-nbd -d $NBD
94
+    mv $DEV_FILE_TMP $DEV_FILE
94 95
 fi
96
+rm -f $DEV_FILE_TMP
95 97
 
96 98
 # clone git repositories onto the system
97 99
 # ======================================
98 100
 
101
+IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
102
+
99 103
 if [ ! -r $IMG_FILE ]; then
100
-    qemu-nbd -c $NBD $CACHEDIR/$DIST_NAME-dev.img
101
-    if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
102
-        echo "Couldn't connect $NBD"
103
-        exit 1
104
-    fi
104
+    NBD=`map_nbd $DEV_FILE`
105 105
 
106 106
     # Pre-create the image file
107 107
     # FIXME(dt): This should really get the partition size to
108 108
     #            pre-create the image file
109
-    dd if=/dev/zero of=$IMG_FILE bs=1 count=1 seek=$((2*1024*1024*1024))
109
+    dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
110 110
     # Create filesystem image for RAM disk
111
-    dd if=${NBD}p1 of=$IMG_FILE bs=1M
111
+    dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
112 112
 
113 113
     qemu-nbd -d $NBD
114
+    mv $IMG_FILE_TMP $IMG_FILE
114 115
 fi
116
+rm -f $IMG_FILE_TMP
115 117
 
116 118
 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
117 119
 mount -t ext4 -o loop $IMG_FILE $MNTDIR
... ...
@@ -111,25 +111,33 @@ if [ $ROOTSIZE -gt 2000 ]; then
111 111
     qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
112 112
 fi
113 113
 
114
-# Set up nbd
115
-modprobe nbd max_part=63
116
-for i in `seq 1 15`; do
117
-    if [ ! -e /sys/block/nbd$i/pid ]; then
118
-        NBD=/dev/nbd$i
119
-        # Connect to nbd and wait till it is ready
120
-        qemu-nbd -c $NBD $IMG_FILE_TMP
121
-        if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
122
-            echo "Couldn't connect $NBD"
123
-            exit 1
114
+# Finds the next available NBD device
115
+# Exits script if error connecting or none free
116
+# map_nbd image
117
+# returns full nbd device path
118
+function map_nbd {
119
+    for i in `seq 0 15`; do
120
+        if [ ! -e /sys/block/nbd$i/pid ]; then
121
+            NBD=/dev/nbd$i
122
+            # Connect to nbd and wait till it is ready
123
+            qemu-nbd -c $NBD $1
124
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
125
+                echo "Couldn't connect $NBD"
126
+                exit 1
127
+            fi
128
+            break
124 129
         fi
125
-        break
130
+    done
131
+    if [ -z "$NBD" ]; then
132
+        echo "No free NBD slots"
133
+        exit 1
126 134
     fi
127
-done
128
-if [ -z "$NBD" ]; then
129
-    echo "No free NBD slots"
130
-    exit 1
131
-fi
132
-NBD_DEV=`basename $NBD`
135
+    echo $NBD
136
+}
137
+
138
+# Set up nbd
139
+modprobe nbd max_part=63
140
+NBD=`map_nbd $IMG_FILE_TMP`
133 141
 
134 142
 # Resize partition 1 to full size of the disk image
135 143
 echo "d