Revision bad5ca1f snf-image-host/create

b/snf-image-host/create
30 30
# policies, either expressed or implied, of GRNET S.A.
31 31

  
32 32
set -e
33
set -o pipefail
33 34

  
34 35
. common.sh
35 36

  
......
37 38

  
38 39
case "$IMAGE_TYPE" in
39 40
    extdump)
40
	IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.extdump";;
41
	image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.extdump";;
41 42
    ntfsdump)
42
        IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.ntfsdump";;
43
        image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.ntfsdump";;
43 44
    *)
44 45
        log_error "Unknown image type: \`$IMAGE_TYPE'.";
45 46
        exit 1
46 47
esac
47 48

  
48
if [ ! -e "$IMAGE_FILE" ]; then
49
    log_error "Image file \`$IMAGE_FILE' does not exit."
49
if [ ! -e "$image_file" ]; then
50
    log_error "Image file \`$image_file' does not exit."
50 51
    exit 1
51 52
fi
52 53

  
53
MONITOR="" #Empty if progress monitor support is disabled
54
monitor="" #Empty if progress monitor support is disabled
54 55
if [ "$progress_monitor_support" = "yes" ]; then
55
    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
56
    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
56
    image_size="$(stat -L -c %s "$IMAGE_FILE")"
57
    monitor="$(printf "%q" "$PROGRESS_MONITOR") \
58
        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
57 59
fi
58 60

  
59 61
# If the target device is not a real block device we'll first losetup it.
60 62
# This is needed for file disks.
61
if [ ! -b $blockdev ]; then
62
    ORIGINAL_BLOCKDEV=$blockdev
63
    blockdev=$($LOSETUP -sf $blockdev)
64
    CLEANUP+=("$LOSETUP -d $blockdev")
63
if [ ! -b "$blockdev" ]; then
64
    original_blockdev="$blockdev"
65
    blockdev=$("$LOSETUP" -sf "$blockdev")
66
    add_cleanup "$LOSETUP" -d "$blockdev"
65 67
fi
66 68

  
67
format_disk0 $blockdev ${IMAGE_TYPE}
69
format_disk0 "$blockdev" "$IMAGE_TYPE"
68 70

  
69
filesystem_dev=$(map_disk0 $blockdev)
70
CLEANUP+=("unmap_disk0 $blockdev")
71
filesystem_dev=$(map_disk0 "$blockdev")
72
add_cleanup unmap_disk0 "$blockdev"
71 73

  
72 74
root_dev="${filesystem_dev}-1"
73 75

  
......
75 77
# Deploying an image file on a target block device is a streaming
76 78
# copy operation. Enable the direct I/O flag on the output fd to 
77 79
# avoid polluting the host cache with useless data.
78
$MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
80
$monitor dd bs=4M if="$IMAGE_FILE" of="$root_dev" oflag=direct
79 81

  
80 82
# Create a floppy image
81
floppy=$(mktemp --tmpdir floppy.XXXXXXXX)
82
CLEANUP+=("rm -f $floppy")
83
floppy=$(mktemp --tmpdir floppy.XXXXXX)
84
add_cleanup rm "$floppy"
83 85

  
84 86
snf_export_DEV=/dev/vda
85
snf_export_TYPE=${IMG_FORMAT}
86
snf_export_PASSWORD=${IMG_PASSWD}
87
snf_export_HOSTNAME=${instance}
87
snf_export_TYPE="$IMG_FORMAT"
88
snf_export_PASSWORD="$IMG_PASSWD"
89
snf_export_HOSTNAME="$instance"
88 90
if [ -n "$IMG_PERSONALITY" ]; then
89
    snf_export_PERSONALITY=${IMG_PERSONALITY}
91
    snf_export_PERSONALITY="$IMG_PERSONALITY"
90 92
fi
91 93

  
92
create_floppy $floppy
94
create_floppy "$floppy"
93 95

  
94 96
# Invoke the helper vm to do the dirty job...
95
result_file=$(mktemp --tmpdir result.XXXXXXXX)
96
CLEANUP+=("rm -f $result_file")
97
result_file=$(mktemp --tmpdir result.XXXXXX)
98
add_cleanup rm "$result_file"
97 99

  
98
image_file=$(mktemp --tmpdir helperXXXXXXXX.img)
99
CLEANUP+=("rm -f $image_file")
100
snapshot=$(mktemp --tmpdir helperXXXXXX.img)
101
add_cleanup rm "$snapshot"
100 102

  
101
$QEMU_IMG create -f qcow2 -b ${HELPER_IMG} $image_file
103
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
102 104

  
103
$TIMELIMIT -t $HELPER_SOFT_TIMEOUT -T $HELPER_HARD_TIMEOUT \
104
    kvm -runas $HELPER_USER -drive file=${image_file} \
105
    -drive file=$root_dev,format=raw,if=virtio,cache=none \
106
    -boot c -serial stdio -serial file:$result_file -fda $floppy \
107
    -vga none -nographic -parallel none -monitor null -nographic \
108
    -kernel ${HELPER_KERNEL} -initrd ${HELPER_INITRD} \
105
"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
106
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
107
    -drive file="$root_dev",format=raw,if=virtio,cache=none \
108
    -boot c -serial stdio -serial file:"$result_file" \
109
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
110
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
109 111
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8" \
110 112
    2>&1 | sed 's|^|HELPER: |g'
111 113

  
......
117 119
fi
118 120

  
119 121
# Read the first line. This will remove \r and \n chars
120
result=$(sed 's|\r||g' $result_file | xargs echo)
122
result=$(sed 's|\r||g' "$result_file" | xargs echo)
121 123

  
122 124
if [ "x$result" != "xSUCCESS" ]; then
123 125
    log_error "Helper VM returned error"
......
125 127
fi
126 128

  
127 129
# Install a new MBR
128
$INSTALL_MBR -p 1 -i n ${blockdev}
130
"$INSTALL_MBR" -p 1 -i n "$blockdev"
129 131

  
130 132
# Execute cleanups
131 133
cleanup

Also available in: Unified diff