Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / create @ f3e43ab1

History | View | Annotate | Download (6.5 kB)

1
#!/bin/bash
2

    
3
# Copyright (C) 2011 GRNET S.A. 
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19

    
20
set -e
21
set -o pipefail
22

    
23
. common.sh
24

    
25
ganeti_os_main
26

    
27
# Save stderr to a file
28
stderr=$(mktemp)
29
add_cleanup rm "$stderr"
30
exec 2> >(tee -a "$stderr" >&2)
31

    
32
monitor_pipe=$(mktemp -u)
33
mkfifo -m 600 "$monitor_pipe"
34
add_cleanup rm -f "$monitor_pipe"
35

    
36
if [ -n "$PROGRESS_MONITOR" ]; then
37
    { sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } &
38
    monitor_pid="$!"
39
else
40
    sed -u 's|^|MONITOR MSG: |g' < "$monitor_pipe" &
41
    monitor_pid="$!"
42
fi
43

    
44
# Create file descriptor to monitor_pipe
45
exec {MONITOR_FD}>${monitor_pipe}
46
add_cleanup  close_fd ${MONITOR_FD}
47

    
48
# Ignore sigpipe signals. If progress monitor is dead and snf-image-host tries
49
# to output something to the opened pipe, then a sigpipe will be raised. If we
50
# do not catch this, the program will terminate.
51
trap "" SIGPIPE
52

    
53
trap "report_and_cleanup $(printf "%q" "$stderr")" EXIT
54

    
55
case $BACKEND_TYPE in
56
    local)
57
        image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE"
58
        if [ ! -e "$image_file" ]; then
59
            log_error "Image file \`$image_file' does not exit."
60
            exit 1
61
        fi
62
        image_size="$(stat -L -c %s "$image_file")"
63
        ;;
64
    null)
65
        image_file=/dev/null
66
        image_size=0
67
        # Treat it as local file from now on...
68
        BACKEND_TYPE="local"
69
        ;;
70
    network)
71
        image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
72
        image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
73
        ;;
74
    pithos)
75
        # For security reasons pass the database url to pithcat as an
76
        # environmental variable.
77
        export PITHCAT_INPUT_DB="$PITHOS_DB"
78
        export PITHCAT_INPUT_DATA="$PITHOS_DATA"
79
        cmd_args="$(printf "%q" "${IMAGE_NAME}")"
80
        image_cmd="./pithcat $cmd_args"
81
        image_size=$(./pithcat -s  $cmd_args)
82
        ;;
83
esac
84

    
85
# If the target device is not a real block device we'll first losetup it.
86
# This is needed for file disks.
87
if [ ! -b "$blockdev" ]; then
88
    original_blockdev="$blockdev"
89
    blockdev=$($LOSETUP -sf "$blockdev")
90
    add_cleanup $LOSETUP -d "$blockdev"
91
fi
92

    
93
case "$IMAGE_TYPE" in
94
    ntfsdump|extdump)
95
        # Create partitions
96
        format_disk0 "$blockdev" "$IMAGE_TYPE"
97

    
98
        # Install a new MBR
99
        $INSTALL_MBR -p 1 -i n "$blockdev"
100

    
101
        target="$(map_disk0 "$blockdev")-1" #the root device
102
        add_cleanup unmap_disk0 "$blockdev"
103
        snf_export_PROPERTY_ROOT_PARTITION=1
104
        if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
105
            snf_export_PROPERTY_OSFAMILY="windows"
106
        else
107
            snf_export_PROPERTY_OSFAMILY="linux"
108
        fi
109
        ;;
110
    diskdump)
111
        target="$blockdev"
112
        ;;
113
    *)
114
        log_error "Unknown Image format: \`$IMAGE_TYPE'"
115
        exit 1
116
        ;;
117
esac
118

    
119
log_info "Starting image copy..."
120
monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size"
121
if [ "$BACKEND_TYPE" = "local" ]; then
122
    # dd the dump to its new home :-)
123
    # Deploying an image file on a target block device is a streaming copy
124
    # operation. Enable the direct I/O flag on the output fd to avoid polluting
125
    # the host cache with useless data.
126
    $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
127
else
128
    $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
129
fi
130
log_info "Image copy finished."
131

    
132
# Create a floppy image
133
floppy=$(mktemp --tmpdir floppy.XXXXXX)
134
add_cleanup rm "$floppy"
135

    
136
snf_export_DEV=/dev/vda
137
snf_export_TYPE="$IMG_FORMAT"
138
snf_export_PASSWORD="$IMG_PASSWD"
139
snf_export_HOSTNAME="$instance"
140
if [ -n "$IMG_PROPERTIES" ]; then
141
    snf_export_PROPERTIES="$IMG_PROPERTIES"
142
fi
143
if [ -n "$IMG_PERSONALITY" ]; then
144
    snf_export_PERSONALITY="$IMG_PERSONALITY"
145
fi
146

    
147
create_floppy "$floppy"
148

    
149
# Invoke the helper vm to do the dirty job...
150
jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
151
add_cleanup rmdir "$jail"
152

    
153
mount tmpfs -t tmpfs "$jail" -o size=50M
154
add_cleanup umount "$jail"
155

    
156
result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
157
add_cleanup rm "$result_file"
158

    
159
snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
160
add_cleanup rm "$snapshot"
161

    
162
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
163

    
164
echo -n "$($DATE +%Y:%m:%d-%H:%M:%S.%N) " >&2
165
log_info "Starting customization VM..."
166
set +e
167
$TIMEOUT -k "$HELPER_HARD_TIMEOUT" "$HELPER_SOFT_TIMEOUT" \
168
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
169
    -drive file="$blockdev",format=raw,if=virtio,cache=none \
170
    -boot c -serial stdio -serial "file:$(printf "%q" "$result_file")" \
171
    -serial file:>(./helper-monitor.py ${MONITOR_FD}) \
172
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
173
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
174
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
175
    2>&1 | sed -u 's|^|HELPER: |g'
176
rc=$?
177
set -e
178
if [ $rc -ne 0 ]; then
179
    if [ $rc -eq 124 ];  then
180
        log_error "Image customization was terminated. Did not finish on time."
181
    elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
182
        log_error "Image customization was killed. Did not finish on time."
183
    elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
184
        log_error "Image customization was terminated by a SIGPIPE."
185
        log_error "Maybe progress monitor has died unexpectedly."
186
    elif [ $rc -eq 125 ]; then
187
        log_error "Internal Error. Image customization could not start."
188
        log_error "timeout did not manage to run."
189
    else
190
        log_error "Image customization died unexpectedly (return code $rc)."
191
    fi
192
    exit 1
193
else
194
    echo -n "$($DATE +%Y:%m:%d-%H:%M:%S.%N)" >&2
195
    log_info "Customization VM finished."
196
fi
197

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

    
201
if [ "x$result" != "xSUCCESS" ]; then
202
    log_error "Image customization failed."
203
    exit 1
204
fi
205

    
206
log_info "Image customization finished successfully."
207

    
208
# Execute cleanups
209
cleanup
210
trap - EXIT
211

    
212
exit 0
213

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