Replace losetup in update-helper with the $LOSETUP
[snf-image] / snf-image-host / create
index 14893c2..7cc1c0b 100755 (executable)
 # policies, either expressed or implied, of GRNET S.A.
 
 set -e
+set -o pipefail
 
 . common.sh
 
+ganeti_os_main
+
 case "$IMAGE_TYPE" in
     extdump)
-       IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.extdump";;
+       image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.extdump";;
     ntfsdump)
-        IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.ntfsdump";;
+        image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.ntfsdump";;
     *)
         log_error "Unknown image type: \`$IMAGE_TYPE'.";
         exit 1
 esac
 
-if [ ! -e "$IMAGE_FILE" ]; then
-    log_error "Image file \`$IMAGE_FILE' does not exit."
+if [ ! -e "$image_file" ]; then
+    log_error "Image file \`$image_file' does not exit."
     exit 1
 fi
 
-MONITOR="" #Empty if progress monitor support is disabled
+monitor="" #Empty if progress monitor support is disabled
 if [ "$progress_monitor_support" = "yes" ]; then
-    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
-    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
+    image_size="$(stat -L -c %s "$image_file")"
+    monitor="$(printf "%q" "$PROGRESS_MONITOR") \
+        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
 fi
 
 # If the target device is not a real block device we'll first losetup it.
 # This is needed for file disks.
-if [ ! -b $blockdev ]; then
-    ORIGINAL_BLOCKDEV=$blockdev
-    blockdev=$($LOSETUP -sf $blockdev)
-    CLEANUP+=("$LOSETUP -d $blockdev")
+if [ ! -b "$blockdev" ]; then
+    original_blockdev="$blockdev"
+    blockdev=$("$LOSETUP" -sf "$blockdev")
+    add_cleanup "$LOSETUP" -d "$blockdev"
 fi
 
-format_disk0 $blockdev ${IMAGE_TYPE}
+format_disk0 "$blockdev" "$IMAGE_TYPE"
 
-filesystem_dev=$(map_disk0 $blockdev)
-CLEANUP+=("unmap_disk0 $blockdev")
+filesystem_dev=$(map_disk0 "$blockdev")
+add_cleanup unmap_disk0 "$blockdev"
 
 root_dev="${filesystem_dev}-1"
 
@@ -73,32 +77,37 @@ root_dev="${filesystem_dev}-1"
 # Deploying an image file on a target block device is a streaming
 # copy operation. Enable the direct I/O flag on the output fd to 
 # avoid polluting the host cache with useless data.
-$MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
+$monitor dd bs=4M if="$image_file" of="$root_dev" oflag=direct
 
 # Create a floppy image
-floppy=$(mktemp --tmpdir floppy.XXXXXXXX)
-CLEANUP+=("rm -f $floppy")
+floppy=$(mktemp --tmpdir floppy.XXXXXX)
+add_cleanup rm "$floppy"
 
 snf_export_DEV=/dev/vda
-snf_export_TYPE=${IMG_FORMAT}
-snf_export_PASSWORD=${IMG_PASSWD}
-snf_export_HOSTNAME=${instance}
+snf_export_TYPE="$IMG_FORMAT"
+snf_export_PASSWORD="$IMG_PASSWD"
+snf_export_HOSTNAME="$instance"
 if [ -n "$IMG_PERSONALITY" ]; then
-    snf_export_PERSONALITY=${IMG_PERSONALITY}
+    snf_export_PERSONALITY="$IMG_PERSONALITY"
 fi
 
-create_floppy $floppy
+create_floppy "$floppy"
 
 # Invoke the helper vm to do the dirty job...
-result_file=$(mktemp --tmpdir result.XXXXXXXX)
-CLEANUP+=("rm -f $result_file")
-
-$TIMELIMIT -t $HELPER_SOFT_TIMEOUT -T $HELPER_HARD_TIMEOUT \
-    kvm -runas $HELPER_USR -drive file=${HELPER},snapshot=on \
-    -drive file=$root_dev,format=raw,if=virtio,cache=none \
-    -boot c -serial stdio -serial file:$result_file -fda $floppy \
-    -vga none -nographic -parallel none -monitor null -nographic \
-    -kernel ${HELPER_KERNEL} -initrd ${HELPER_INITRD} \
+result_file=$(mktemp --tmpdir result.XXXXXX)
+add_cleanup rm "$result_file"
+
+snapshot=$(mktemp --tmpdir helperXXXXXX.img)
+add_cleanup rm "$snapshot"
+
+"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
+
+"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
+    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
+    -drive file="$root_dev",format=raw,if=virtio,cache=none \
+    -boot c -serial stdio -serial file:"$result_file" \
+    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
+    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
     -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8" \
     2>&1 | sed 's|^|HELPER: |g'
 
@@ -110,7 +119,7 @@ if [ $? -ne 0 ]; then
 fi
 
 # Read the first line. This will remove \r and \n chars
-result=$(sed 's|\r||g' $result_file | xargs echo)
+result=$(sed 's|\r||g' "$result_file" | xargs echo)
 
 if [ "x$result" != "xSUCCESS" ]; then
     log_error "Helper VM returned error"
@@ -118,7 +127,7 @@ if [ "x$result" != "xSUCCESS" ]; then
 fi
 
 # Install a new MBR
-$INSTALL_MBR -p 1 -i n ${blockdev}
+"$INSTALL_MBR" -p 1 -i n "$blockdev"
 
 # Execute cleanups
 cleanup