Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / create @ d4296b92

History | View | Annotate | Download (4.9 kB)

1 f4bd39d8 Nikos Skalkotos
#!/bin/bash
2 f4bd39d8 Nikos Skalkotos
3 7e5d635b Nikos Skalkotos
# Copyright (C) 2011 GRNET S.A. 
4 f4bd39d8 Nikos Skalkotos
#
5 7e5d635b Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
6 7e5d635b Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
7 7e5d635b Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
8 7e5d635b Nikos Skalkotos
# (at your option) any later version.
9 f4bd39d8 Nikos Skalkotos
#
10 7e5d635b Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
11 7e5d635b Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 7e5d635b Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 7e5d635b Nikos Skalkotos
# General Public License for more details.
14 f4bd39d8 Nikos Skalkotos
#
15 7e5d635b Nikos Skalkotos
# You should have received a copy of the GNU General Public License
16 7e5d635b Nikos Skalkotos
# along with this program; if not, write to the Free Software
17 7e5d635b Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 7e5d635b Nikos Skalkotos
# 02110-1301, USA.
19 f4bd39d8 Nikos Skalkotos
20 f4bd39d8 Nikos Skalkotos
set -e
21 bad5ca1f Nikos Skalkotos
set -o pipefail
22 f4bd39d8 Nikos Skalkotos
23 f4bd39d8 Nikos Skalkotos
. common.sh
24 f4bd39d8 Nikos Skalkotos
25 d51a95a8 Nikos Skalkotos
ganeti_os_main
26 d51a95a8 Nikos Skalkotos
27 d71f59e9 Nikos Skalkotos
case $BACKEND_TYPE in
28 d71f59e9 Nikos Skalkotos
    local)
29 d71f59e9 Nikos Skalkotos
        image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE"
30 d71f59e9 Nikos Skalkotos
        if [ ! -e "$image_file" ]; then
31 d71f59e9 Nikos Skalkotos
            log_error "Image file \`$image_file' does not exit."
32 d71f59e9 Nikos Skalkotos
            exit 1
33 d71f59e9 Nikos Skalkotos
        fi
34 d71f59e9 Nikos Skalkotos
        image_size="$(stat -L -c %s "$image_file")"
35 d71f59e9 Nikos Skalkotos
        ;;
36 d71f59e9 Nikos Skalkotos
    network)
37 d71f59e9 Nikos Skalkotos
        image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null"
38 d71f59e9 Nikos Skalkotos
        image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2)
39 d71f59e9 Nikos Skalkotos
        ;;
40 d71f59e9 Nikos Skalkotos
    pithos)
41 71238d01 Nikos Skalkotos
        cmd_args="--db $(printf "%q" "${PITHOS_DB}") \
42 71238d01 Nikos Skalkotos
                  --data $(printf "%q" "${PITHOS_DATA}") \
43 71238d01 Nikos Skalkotos
                  $(printf "%q" "${IMAGE_NAME}")"
44 d71f59e9 Nikos Skalkotos
        image_cmd="./pithcat $cmd_args"
45 d71f59e9 Nikos Skalkotos
        image_size=$(./pithcat -s  $cmd_args)
46 d71f59e9 Nikos Skalkotos
        ;;
47 d71f59e9 Nikos Skalkotos
esac
48 f4bd39d8 Nikos Skalkotos
49 b1fef76e Nikos Skalkotos
monitor="" # Empty if no progress monitor program is not defined.
50 b1fef76e Nikos Skalkotos
if [ -n "$PROGRESS_MONITOR" ]; then
51 02485c69 Nikos Skalkotos
    monitor="$PROGRESS_MONITOR \
52 bad5ca1f Nikos Skalkotos
        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
53 f4bd39d8 Nikos Skalkotos
fi
54 f4bd39d8 Nikos Skalkotos
55 f4bd39d8 Nikos Skalkotos
# If the target device is not a real block device we'll first losetup it.
56 f4bd39d8 Nikos Skalkotos
# This is needed for file disks.
57 bad5ca1f Nikos Skalkotos
if [ ! -b "$blockdev" ]; then
58 bad5ca1f Nikos Skalkotos
    original_blockdev="$blockdev"
59 02485c69 Nikos Skalkotos
    blockdev=$($LOSETUP -sf "$blockdev")
60 02485c69 Nikos Skalkotos
    add_cleanup $LOSETUP -d "$blockdev"
61 f4bd39d8 Nikos Skalkotos
fi
62 f4bd39d8 Nikos Skalkotos
63 b206849d Nikos Skalkotos
case "$IMAGE_TYPE" in
64 7a474322 Nikos Skalkotos
    ntfsdump|extdump)
65 3f4f15f3 Nikos Skalkotos
        # Create partitions
66 b206849d Nikos Skalkotos
        format_disk0 "$blockdev" "$IMAGE_TYPE"
67 b206849d Nikos Skalkotos
68 3f4f15f3 Nikos Skalkotos
        # Install a new MBR
69 02485c69 Nikos Skalkotos
        $INSTALL_MBR -p 1 -i n "$blockdev"
70 3f4f15f3 Nikos Skalkotos
71 b206849d Nikos Skalkotos
        target="$(map_disk0 "$blockdev")-1" #the root device
72 b206849d Nikos Skalkotos
        add_cleanup unmap_disk0 "$blockdev"
73 27acc2e4 Nikos Skalkotos
        snf_export_PROPERTY_ROOT_PARTITION=1
74 b635b72c Nikos Skalkotos
        if [ "$IMAGE_TYPE" = "ntfsdump" ]; then
75 b635b72c Nikos Skalkotos
            snf_export_PROPERTY_OSFAMILY="windows"
76 b635b72c Nikos Skalkotos
        else
77 b635b72c Nikos Skalkotos
            snf_export_PROPERTY_OSFAMILY="linux"
78 b635b72c Nikos Skalkotos
        fi
79 b206849d Nikos Skalkotos
        ;;
80 d71f59e9 Nikos Skalkotos
    diskdump)
81 b206849d Nikos Skalkotos
        target="$blockdev"
82 b206849d Nikos Skalkotos
        ;;
83 71238d01 Nikos Skalkotos
    *)
84 71238d01 Nikos Skalkotos
        log_error "Unknown Image format: \`$IMAGE_TYPE'"
85 71238d01 Nikos Skalkotos
        exit 1
86 71238d01 Nikos Skalkotos
        ;;
87 b206849d Nikos Skalkotos
esac
88 f4bd39d8 Nikos Skalkotos
89 d71f59e9 Nikos Skalkotos
if [ "$BACKEND_TYPE" = "local" ]; then
90 db0078b9 Nikos Skalkotos
    # dd the dump to its new home :-)
91 db0078b9 Nikos Skalkotos
    # Deploying an image file on a target block device is a streaming copy
92 db0078b9 Nikos Skalkotos
    # operation. Enable the direct I/O flag on the output fd to avoid polluting
93 db0078b9 Nikos Skalkotos
    # the host cache with useless data.
94 db0078b9 Nikos Skalkotos
    $monitor dd bs=4M if="$image_file" of="$target" oflag=direct
95 d71f59e9 Nikos Skalkotos
else
96 d71f59e9 Nikos Skalkotos
    $image_cmd | $monitor dd bs=4M of="$target" oflag=direct
97 db0078b9 Nikos Skalkotos
fi
98 f4bd39d8 Nikos Skalkotos
99 e036304a Nikos Skalkotos
# Create a floppy image
100 bad5ca1f Nikos Skalkotos
floppy=$(mktemp --tmpdir floppy.XXXXXX)
101 bad5ca1f Nikos Skalkotos
add_cleanup rm "$floppy"
102 e036304a Nikos Skalkotos
103 cc4a63ae Nikos Skalkotos
snf_export_DEV=/dev/vda
104 bad5ca1f Nikos Skalkotos
snf_export_TYPE="$IMG_FORMAT"
105 bad5ca1f Nikos Skalkotos
snf_export_PASSWORD="$IMG_PASSWD"
106 bad5ca1f Nikos Skalkotos
snf_export_HOSTNAME="$instance"
107 7b8cdc65 Nikos Skalkotos
if [ -n "$IMG_PROPERTIES" ]; then
108 78e9f209 Nikos Skalkotos
    snf_export_PROPERTIES="$IMG_PROPERTIES"
109 78e9f209 Nikos Skalkotos
fi
110 58356445 Nikos Skalkotos
if [ -n "$IMG_PERSONALITY" ]; then
111 bad5ca1f Nikos Skalkotos
    snf_export_PERSONALITY="$IMG_PERSONALITY"
112 8b9d0a95 Constantinos Venetsanopoulos
fi
113 a1d894c5 Nikos Skalkotos
114 bad5ca1f Nikos Skalkotos
create_floppy "$floppy"
115 e036304a Nikos Skalkotos
116 fea9eb04 Nikos Skalkotos
# Invoke the helper vm to do the dirty job...
117 0762ba51 Nikos Skalkotos
jail=$(mktemp -d --tmpdir tmpfsXXXXXXX)
118 0762ba51 Nikos Skalkotos
add_cleanup rmdir "$jail"
119 0762ba51 Nikos Skalkotos
120 0762ba51 Nikos Skalkotos
mount tmpfs -t tmpfs "$jail" -o size=50M
121 0762ba51 Nikos Skalkotos
add_cleanup umount "$jail"
122 0762ba51 Nikos Skalkotos
123 0762ba51 Nikos Skalkotos
result_file=$(mktemp --tmpdir="$jail" result.XXXXXX)
124 bad5ca1f Nikos Skalkotos
add_cleanup rm "$result_file"
125 fea9eb04 Nikos Skalkotos
126 0762ba51 Nikos Skalkotos
snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img)
127 bad5ca1f Nikos Skalkotos
add_cleanup rm "$snapshot"
128 5cbb15b9 Nikos Skalkotos
129 bad5ca1f Nikos Skalkotos
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
130 5cbb15b9 Nikos Skalkotos
131 4e11824c Nikos Skalkotos
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..."
132 02485c69 Nikos Skalkotos
$TIMELIMIT -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
133 bad5ca1f Nikos Skalkotos
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
134 b206849d Nikos Skalkotos
    -drive file="$blockdev",format=raw,if=virtio,cache=none \
135 bad5ca1f Nikos Skalkotos
    -boot c -serial stdio -serial file:"$result_file" \
136 bad5ca1f Nikos Skalkotos
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
137 bad5ca1f Nikos Skalkotos
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
138 af4a3462 Vangelis Koukis
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \
139 3b179b45 Nikos Skalkotos
    2>&1 | sed 's|^|HELPER: |g'
140 4e11824c Nikos Skalkotos
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished."
141 f25fb7f4 Nikos Skalkotos
142 9999d40d Nikos Skalkotos
if [ $? -ne 0 ]; then
143 9999d40d Nikos Skalkotos
    if [ $? -gt 128 ];  then
144 9999d40d Nikos Skalkotos
        log_error "Helper was terminated. Did not finish on time."
145 9999d40d Nikos Skalkotos
    fi
146 9999d40d Nikos Skalkotos
    exit 1
147 9999d40d Nikos Skalkotos
fi
148 9999d40d Nikos Skalkotos
149 9e713e52 Nikos Skalkotos
# Read the first line. This will remove \r and \n chars
150 456b77f1 Nikos Skalkotos
result=$(sed 's|\r||g' "$result_file" | head -1)
151 f25fb7f4 Nikos Skalkotos
152 f25fb7f4 Nikos Skalkotos
if [ "x$result" != "xSUCCESS" ]; then
153 3c8f7e2f Nikos Skalkotos
    log_error "Helper VM returned error:"
154 3c8f7e2f Nikos Skalkotos
    log_error "$result"
155 f25fb7f4 Nikos Skalkotos
    exit 1
156 f25fb7f4 Nikos Skalkotos
fi
157 f25fb7f4 Nikos Skalkotos
158 f4bd39d8 Nikos Skalkotos
# Execute cleanups
159 f4bd39d8 Nikos Skalkotos
cleanup
160 f4bd39d8 Nikos Skalkotos
trap - EXIT
161 f4bd39d8 Nikos Skalkotos
162 f4bd39d8 Nikos Skalkotos
exit 0
163 f4bd39d8 Nikos Skalkotos
164 f4bd39d8 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :