Merge pull request #130 from cloudbuilders/fix_races

Address races in NBD and images
This commit is contained in:
Jesse Andrews 2011-10-31 19:02:07 -07:00
commit 02a0869615
2 changed files with 45 additions and 26 deletions

View file

@ -229,15 +229,8 @@ EOF
ROOTFS=$VM_DIR/root ROOTFS=$VM_DIR/root
mkdir -p $ROOTFS mkdir -p $ROOTFS
# Make sure we have nbd-ness
modprobe nbd max_part=63
# Which NBD device to use?
NBD=${NBD:-/dev/nbd$GUEST_NETWORK}
# Clean up from previous runs # Clean up from previous runs
umount $ROOTFS || echo 'ok' umount $ROOTFS || echo 'ok'
qemu-nbd -d $NBD || echo 'ok'
# Clean up old runs # Clean up old runs
cd $VM_DIR cd $VM_DIR
@ -246,12 +239,27 @@ rm -f $VM_DIR/disk
# Create our instance fs # Create our instance fs
qemu-img create -f qcow2 -b $VM_IMAGE disk qemu-img create -f qcow2 -b $VM_IMAGE disk
# Connect our nbd and wait till it is mountable # Make sure we have nbd-ness
qemu-nbd -c $NBD disk modprobe nbd max_part=63
if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
echo "Couldn't connect $NBD" # Set up 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 disk
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 exit 1
fi fi
NBD_DEV=`basename $NBD`
# Mount the instance # Mount the instance
mount ${NBD}p1 $ROOTFS mount ${NBD}p1 $ROOTFS
@ -344,9 +352,6 @@ echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS
# Give stack ownership over $DEST so it may do the work needed # Give stack ownership over $DEST so it may do the work needed
chroot $ROOTFS chown -R stack $DEST chroot $ROOTFS chown -R stack $DEST
# GRUB 2 wants to see /dev
mount -o bind /dev $ROOTFS/dev
# Set the hostname # Set the hostname
echo $GUEST_NAME > $ROOTFS/etc/hostname echo $GUEST_NAME > $ROOTFS/etc/hostname
@ -355,6 +360,9 @@ if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
fi fi
# GRUB 2 wants to see /dev
mount -o bind /dev $ROOTFS/dev
# Change boot params so that we get a console log # Change boot params so that we get a console log
G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1` G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub

View file

@ -51,6 +51,7 @@ fi
# Default args # Default args
DIST_NAME=$1 DIST_NAME=$1
IMG_FILE=$2 IMG_FILE=$2
IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
case $FORMAT in case $FORMAT in
kvm|qcow2) FORMAT=qcow2 kvm|qcow2) FORMAT=qcow2
@ -88,11 +89,6 @@ case $DIST_NAME in
;; ;;
esac esac
# Set up nbd
modprobe nbd max_part=63
NBD=${NBD:-/dev/nbd9}
NBD_DEV=`basename $NBD`
# Prepare the base image # Prepare the base image
# Get the UEC image # Get the UEC image
@ -103,24 +99,37 @@ fi
if [ "$FORMAT" = "qcow2" ]; then if [ "$FORMAT" = "qcow2" ]; then
# Just copy image # Just copy image
cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
else else
# Convert image # Convert image
qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
fi fi
# Resize the image if necessary # Resize the image if necessary
if [ $ROOTSIZE -gt 2000 ]; then if [ $ROOTSIZE -gt 2000 ]; then
# Resize the container # Resize the container
qemu-img resize $IMG_FILE +$((ROOTSIZE - 2000))M qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
fi fi
# Connect to nbd and wait till it is ready # Set up nbd
qemu-nbd -c $NBD $IMG_FILE modprobe nbd max_part=63
if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then for i in `seq 1 15`; do
echo "Couldn't connect $NBD" 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 exit 1
fi fi
NBD_DEV=`basename $NBD`
# Resize partition 1 to full size of the disk image # Resize partition 1 to full size of the disk image
echo "d echo "d
@ -153,3 +162,5 @@ rm -f $MNTDIR/etc/resolv.conf
umount $MNTDIR umount $MNTDIR
rmdir $MNTDIR rmdir $MNTDIR
qemu-nbd -d $NBD qemu-nbd -d $NBD
mv $IMG_FILE_TMP $IMG_FILE