Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / create @ b635b72c

History | View | Annotate | Download (5 kB)

1
#!/bin/bash
2

    
3
# Copyright 2011 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
7
# are met:
8
#
9
#   1. Redistributions of source code must retain the above copyright
10
#      notice, this list of conditions and the following disclaimer.
11
#
12
#  2. Redistributions in binary form must reproduce the above copyright
13
#     notice, this list of conditions and the following disclaimer in the
14
#     documentation and/or other materials provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
# SUCH DAMAGE.
27
#
28
# The views and conclusions contained in the software and documentation are
29
# those of the authors and should not be interpreted as representing official
30
# policies, either expressed or implied, of GRNET S.A.
31

    
32
set -e
33
set -o pipefail
34

    
35
. common.sh
36

    
37
ganeti_os_main
38

    
39
if [ "$IMAGE_TYPE" = custom ]; then
40
    image_file=/dev/null
41
    image_size=\
42
        $("$CURL" -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
43
else
44
    image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.$IMAGE_TYPE"
45
    image_size="$(stat -L -c %s "$image_file")"
46
fi
47

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

    
53
monitor="" #Empty if progress monitor support is disabled
54
if [ "$progress_monitor_support" = "yes" ]; then
55
    monitor="$(printf "%q" "$PROGRESS_MONITOR") \
56
        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
57
fi
58

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

    
67
case "$IMAGE_TYPE" in
68
    ntfsdump|extdump)
69
        # Create partitions
70
        format_disk0 "$blockdev" "$IMAGE_TYPE"
71

    
72
        # Install a new MBR
73
        "$INSTALL_MBR" -p 1 -i n "$blockdev"
74

    
75
        target="$(map_disk0 "$blockdev")-1" #the root device
76
        add_cleanup unmap_disk0 "$blockdev"
77
        snf_export_PROPERTY_ROOT_PARTITION=1
78
        if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
79
            snf_export_PROPERTY_OSFAMILY="windows"
80
        else
81
            snf_export_PROPERTY_OSFAMILY="linux"
82
        fi
83
        ;;
84
    diskdump|custom)
85
        target="$blockdev"
86
        ;;
87
esac
88

    
89
if [ "$IMAGE_TYPE" = "custom" ]; then
90
    "$CURL" "$IMAGE_NAME" 2>/dev/null |\
91
        $monitor dd bs=4M of="$target" oflag=direct
92
else
93
    # dd the dump to its new home :-)
94
    # Deploying an image file on a target block device is a streaming copy
95
    # operation. Enable the direct I/O flag on the output fd to avoid polluting
96
    # the host cache with useless data.
97
    $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
98
fi
99

    
100
# Create a floppy image
101
floppy=$(mktemp --tmpdir floppy.XXXXXX)
102
add_cleanup rm "$floppy"
103

    
104
snf_export_DEV=/dev/vda
105
snf_export_TYPE="$IMG_FORMAT"
106
snf_export_PASSWORD="$IMG_PASSWD"
107
snf_export_HOSTNAME="$instance"
108
snf_export_PROPERTIES="$IMG_PROPERTIES"
109
if [ -n "$IMG_PERSONALITY" ]; then
110
    snf_export_PERSONALITY="$IMG_PERSONALITY"
111
fi
112

    
113
create_floppy "$floppy"
114

    
115
# Invoke the helper vm to do the dirty job...
116
result_file=$(mktemp --tmpdir result.XXXXXX)
117
add_cleanup rm "$result_file"
118

    
119
snapshot=$(mktemp --tmpdir helperXXXXXX.img)
120
add_cleanup rm "$snapshot"
121

    
122
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
123

    
124
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
125
"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
126
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
127
    -drive file="$blockdev",format=raw,if=virtio,cache=none \
128
    -boot c -serial stdio -serial file:"$result_file" \
129
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
130
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
131
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
132
    2>&1 | sed 's|^|HELPER: |g'
133
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
134

    
135
if [ $? -ne 0 ]; then
136
    if [ $? -gt 128 ];  then
137
        log_error "Helper was terminated. Did not finish on time."
138
    fi
139
    exit 1
140
fi
141

    
142
# Read the first line. This will remove \r and \n chars
143
result=$(sed 's|\r||g' "$result_file" | head -1)
144

    
145
if [ "x$result" != "xSUCCESS" ]; then
146
    log_error "Helper VM returned error"
147
    exit 1
148
fi
149

    
150
# Execute cleanups
151
cleanup
152
trap - EXIT
153

    
154
exit 0
155

    
156
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :