Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / create @ 7b8cdc65

History | View | Annotate | Download (4.3 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
if [ "$IMAGE_TYPE" = custom ]; then
28
    image_file=/dev/null
29
    image_size=\
30
        $($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
31
else
32
    image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.$IMAGE_TYPE"
33
    image_size="$(stat -L -c %s "$image_file")"
34
fi
35

    
36
if [ ! -e "$image_file" ]; then
37
    log_error "Image file \`$image_file' does not exit."
38
    exit 1
39
fi
40

    
41
monitor="" #Empty if progress monitor support is disabled
42
if [ "$progress_monitor_support" = "yes" ]; then
43
    monitor="$PROGRESS_MONITOR \
44
        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
45
fi
46

    
47
# If the target device is not a real block device we'll first losetup it.
48
# This is needed for file disks.
49
if [ ! -b "$blockdev" ]; then
50
    original_blockdev="$blockdev"
51
    blockdev=$($LOSETUP -sf "$blockdev")
52
    add_cleanup $LOSETUP -d "$blockdev"
53
fi
54

    
55
case "$IMAGE_TYPE" in
56
    ntfsdump|extdump)
57
        # Create partitions
58
        format_disk0 "$blockdev" "$IMAGE_TYPE"
59

    
60
        # Install a new MBR
61
        $INSTALL_MBR -p 1 -i n "$blockdev"
62

    
63
        target="$(map_disk0 "$blockdev")-1" #the root device
64
        add_cleanup unmap_disk0 "$blockdev"
65
        snf_export_PROPERTY_ROOT_PARTITION=1
66
        if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
67
            snf_export_PROPERTY_OSFAMILY="windows"
68
        else
69
            snf_export_PROPERTY_OSFAMILY="linux"
70
        fi
71
        ;;
72
    diskdump|custom)
73
        target="$blockdev"
74
        ;;
75
esac
76

    
77
if [ "$IMAGE_TYPE" = "custom" ]; then
78
    "$CURL" "$IMAGE_NAME" 2>/dev/null |\
79
        $monitor dd bs=4M of="$target" oflag=direct
80
else
81
    # dd the dump to its new home :-)
82
    # Deploying an image file on a target block device is a streaming copy
83
    # operation. Enable the direct I/O flag on the output fd to avoid polluting
84
    # the host cache with useless data.
85
    $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
86
fi
87

    
88
# Create a floppy image
89
floppy=$(mktemp --tmpdir floppy.XXXXXX)
90
add_cleanup rm "$floppy"
91

    
92
snf_export_DEV=/dev/vda
93
snf_export_TYPE="$IMG_FORMAT"
94
snf_export_PASSWORD="$IMG_PASSWD"
95
snf_export_HOSTNAME="$instance"
96
if [ -n "$IMG_PROPERTIES" ]; then
97
    snf_export_PROPERTIES="$IMG_PROPERTIES"
98
fi
99
if [ -n "$IMG_PERSONALITY" ]; then
100
    snf_export_PERSONALITY="$IMG_PERSONALITY"
101
fi
102

    
103
create_floppy "$floppy"
104

    
105
# Invoke the helper vm to do the dirty job...
106
result_file=$(mktemp --tmpdir result.XXXXXX)
107
add_cleanup rm "$result_file"
108

    
109
snapshot=$(mktemp --tmpdir helperXXXXXX.img)
110
add_cleanup rm "$snapshot"
111

    
112
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
113

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

    
125
if [ $? -ne 0 ]; then
126
    if [ $? -gt 128 ];  then
127
        log_error "Helper was terminated. Did not finish on time."
128
    fi
129
    exit 1
130
fi
131

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

    
135
if [ "x$result" != "xSUCCESS" ]; then
136
    log_error "Helper VM returned error"
137
    exit 1
138
fi
139

    
140
# Execute cleanups
141
cleanup
142
trap - EXIT
143

    
144
exit 0
145

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