Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ 53a5929f

History | View | Annotate | Download (12.4 kB)

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

    
19
AWK="awk"
20
KPARTX="kpartx"
21
LOSETUP="losetup"
22
SFDISK="sfdisk"
23
QEMU_IMG="qemu-img"
24
INSTALL_MBR="install-mbr"
25
TIMEOUT="timeout"
26
CURL="curl"
27
DATE="date -u" # Time in UTC
28

    
29
# Temporary use stderr as monitoring file descriptor.
30
# `create' will overwrite this
31
MONITOR_FD="2"
32

    
33
MSG_TYPE_ERROR="image-error"
34
MSG_TYPE_INFO="image-info"
35

    
36
CLEANUP=( )
37
ERROR_MSGS=( )
38

    
39
add_cleanup() {
40
    local cmd=""
41
    for arg; do cmd+=$(printf "%q " "$arg"); done
42
    CLEANUP+=("$cmd")
43
}
44

    
45
log_error() {
46
    ERROR_MSGS+=("$@")
47
    echo "$*" >&2
48
}
49

    
50

    
51
log_info() {
52
    echo "$*" >&2
53

    
54
    local report="$(./host-monitor.py info <<< "$*")"
55

    
56
    eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
57
}
58

    
59

    
60
close_fd() {
61
    local fd="$1"
62
    exec {fd}>&-
63
}
64

    
65
report_error() {
66
    local error_file=$1
67
    local report=""
68
    if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
69
        local msg=""
70
        for err in "${ERROR_MSGS[@]}"; do
71
            msg+="$(echo "$err")"
72
        done
73
        report="$(./host-monitor.py error <<< "$msg")"
74
    else
75
        report=$(tail -10 "$error_file" | ./host-monitor.py stderr)
76
    fi
77

    
78
    eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
79
}
80

    
81
get_api5_arguments() {
82
    GETOPT_RESULT=$*
83
    # Note the quotes around `$TEMP': they are essential!
84
    eval set -- "$GETOPT_RESULT"
85
    while true; do
86
        case "$1" in
87
            -i|-n) instance=$2; shift 2;;
88

    
89
            -o) old_name=$2; shift 2;;
90

    
91
            -b) blockdev=$2; shift 2;;
92

    
93
            -s) swapdev=$2; shift 2;;
94

    
95
            --) shift; break;;
96

    
97
            *)  log_error "Internal error!" >&2; exit 1;;
98
        esac
99
    done
100
    if [ -z "$instance" -o -z "$blockdev" ]; then
101
        log_error "Missing OS API Argument (-i, -n, or -b)"
102
        exit 1
103
    fi
104
    if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
105
        log_error "Missing OS API Argument -s (swapdev)"
106
        exit 1
107
    fi
108
    if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
109
        log_error "Missing OS API Argument -o (old_name)"
110
        exit 1
111
    fi
112
}
113

    
114
get_api10_arguments() {
115
    if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
116
        log_error "Missing OS API Variable:"
117
        log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
118
        exit 1
119
    fi
120
    instance=$INSTANCE_NAME
121
    if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
122
        log_error "At least one disk is needed"
123
        exit 1
124
    fi
125
    if [ "$SCRIPT_NAME" = "export" ]; then
126
        if [ -z "$EXPORT_DEVICE" ]; then
127
            log_error "Missing OS API Variable EXPORT_DEVICE"
128
        fi
129
        blockdev=$EXPORT_DEVICE
130
    elif [ "$SCRIPT_NAME" = "import" ]; then
131
        if [ -z "$IMPORT_DEVICE" ]; then
132
        log_error "Missing OS API Variable IMPORT_DEVICE"
133
        fi
134
        blockdev=$IMPORT_DEVICE
135
    else
136
        blockdev=$DISK_0_PATH
137
    fi
138
    if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
139
        log_error "Missing OS API Variable OLD_INSTANCE_NAME"
140
    fi
141
    old_name=$OLD_INSTANCE_NAME
142
}
143

    
144
get_api20_arguments() {
145
    get_api10_arguments
146

    
147
    local required_osparams="IMG_ID IMG_FORMAT IMG_PASSWD"
148
    local osparams="$required_osparams IMG_PROPERTIES IMG_PERSONALITY CONFIG_URL"
149

    
150
    # Store OSP_VAR in VAR
151
    for param in $osparams; do
152
        eval $param=\"\$OSP_$param\"
153
    done
154

    
155
    if [ -n "$CONFIG_URL" ]; then
156
	source <($CURL -f "$CONFIG_URL" | ./decode-config.py $osparams)
157
    fi
158

    
159
    for var in $required_osparams; do
160
        if [ -z "${!var}" ]; then
161
             log_error "Missing OS API Parameter: ${var}"
162
             exit 1
163
        fi
164
    done
165
}
166

    
167
map_disk0() {
168
    blockdev="$1"
169
    filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
170
                            grep -m 1 -- "-1.*$blockdev" | \
171
                            $AWK '{print $1}')
172
    if [ -z "$filesystem_dev_base" ]; then
173
        log_error "Cannot interpret kpartx output and get partition mapping"
174
        exit 1
175
    fi
176
    $KPARTX -a -p- "$blockdev" > /dev/null
177
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
178
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
179
        log_error "Can't find kpartx mapped partition:" \
180
                                            "/dev/mapper/$filesystem_dev_base"
181
        exit 1
182
    fi
183
    echo "$filesystem_dev"
184
}
185

    
186
unmap_disk0() {
187
    $KPARTX -d -p- "$1"
188
}
189

    
190
format_disk0() {
191
    local device="$1"
192
    local image_type="$2"
193

    
194
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
195

    
196
    # The -f is needed, because we use an optimal alignment and sfdisk complains
197
    # about partitions not ending on clylinder boundary.
198
    local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
199

    
200
    $sfdisk_cmd > /dev/null <<EOF
201
2048,,${part_id["$image_type"]},*
202
EOF
203
}
204

    
205
create_floppy() {
206
    local img=$1
207

    
208
    local target=$(mktemp -d)
209
    add_cleanup rmdir "$target"
210

    
211
    dd bs=512 count=2880 if=/dev/zero of="$img"
212
    mkfs.ext2 -F "$img" > /dev/null
213
    mount "$img" "$target" -o loop
214
    set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
215
        > "$target/rules"
216
    if [ -n "$UNATTEND" ]; then
217
        if [ -f "$UNATTEND" ]; then
218
            cat "$UNATTEND" > "$target/unattend.xml"
219
        else
220
            log_error "Unattend file: \`"$UNATTEND"' does not exist"
221
        fi
222
    fi
223
    umount "$target"
224
}
225

    
226
get_backend_type() {
227
    local id=$1
228

    
229
    if [[ "$id" =~ ^pithos: ]]; then
230
        echo "pithos"
231
    elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
232
        if [ "$network_backend_support" = "yes" ]; then
233
            echo "network";
234
        else
235
            log_error "Retrieving images from the network is not supported."
236
            exit 1
237
        fi
238
    elif [ "$id" = "null" ]; then
239
        echo "null"
240
    else
241
        echo "local"
242
    fi
243
}
244

    
245
# this one is only to be called by create
246
ganeti_os_main() {
247
    if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
248
        OS_API_VERSION=5
249
        GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
250
        if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
251
        get_api5_arguments $GETOPT_RESULT
252
    elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
253
        get_api10_arguments
254
    elif [ "$OS_API_VERSION" = "20" ]; then
255
        get_api20_arguments
256
        IMAGE_NAME="$IMG_ID"
257
        IMAGE_TYPE="$IMG_FORMAT"
258
        BACKEND_TYPE=$(get_backend_type $IMG_ID)
259
    else
260
        log_error "Unknown OS API VERSION $OS_API_VERSION"
261
        exit 1
262
    fi
263
    
264
    if [ -n "$OS_VARIANT" ]; then
265
        if [ ! -d "$VARIANTS_DIR" ]; then
266
            log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
267
            exit 1
268
        fi
269
        VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
270
        if [ -f "$VARIANT_CONFIG" ]; then
271
            . "$VARIANT_CONFIG"
272
        else
273
            if grep -qxF "$OS_VARIANT" variants.list; then
274
                log_error "ERROR: instance-image configuration error"
275
                log_error "  Published variant $OS_VARIANT is missing its" \
276
                    "config file"
277
                log_error "  Please create $VARIANT_CONFIG or unpublish the" \
278
                    "variant"
279
                log_error "  (by removing $OS_VARIANT from variants.list)"
280
            else
281
                log_error "Unofficial variant $OS_VARIANT is unsupported"
282
                log_error "Most probably this is a user error, forcing a" \
283
                    "wrong name"
284
                log_error "To support this variant please create file" \
285
                    "$VARIANT_CONFIG"
286
            fi
287
            exit 1
288
        fi
289
    fi
290

    
291
}
292

    
293
do_debootstrap() {
294
    local target="$1"
295
    local cache="$2"
296
    local pkgs="$3"
297

    
298
    echo "Debootstraping to create a new root filesystem:"
299

    
300
    # Create a policy-rc.d file to deny init script execution
301
    mkdir -p "$target/usr/sbin"
302
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
303
#!/bin/sh
304
exit 101
305
EOF
306
    chmod +x "$target/usr/sbin/policy-rc.d"
307

    
308
    debootstrap --arch $(dpkg --print-architecture) \
309
        --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
310
        "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
311

    
312
    # Save the package list
313
    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$pkgs"
314

    
315
    rm "$target/usr/sbin/policy-rc.d"
316

    
317
    # remove the downloaded debs, as they are no longer needed
318
    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
319
        xargs -r0 rm -f
320

    
321
    local tmp_cache=$(mktemp "$cache.XXXXXX")
322
    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
323
        { rm "$tmp_cache"; false; }
324
    # Overwrite the default cache file. Not the user specified if present.
325
    mv -f "$tmp_cache" "$cache"
326
}
327

    
328
do_multistrap() {
329
   local target="$1"
330
   local cache="$2"
331
   local pkgs="$3"
332

    
333
    # Create a policy-rc.d file to deny init script execution
334
    mkdir -p "$target/usr/sbin"
335
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
336
#!/bin/sh
337
exit 101
338
EOF
339
    chmod +x "$target/usr/sbin/policy-rc.d"
340

    
341
   multistrap -d "$target" -a $(dpkg --print-architecture) \
342
        -f "$MULTISTRAP_CONFIG" 2>&1 | sed -u -e 's/^/MULTISTRAP: /g'
343

    
344
   # Save the package list
345
   chroot "$target" dpkg-query -W -f "\${Package}\n" > "$pkgs"
346

    
347
   rm "$target/usr/sbin/policy-rc.d"
348

    
349
   local tmp_cache=$(mktemp "$cache.XXXXXX")
350
   tar cf "$tmp_cache" --one-file-system -C "$target" . || \
351
        { rm "$tmp_cache"; false; }
352
   # Overwrite the default cache file. Not the user specified if present.
353
   mv -f "$tmp_cache" "$cache"
354
}
355

    
356
report_and_cleanup(){
357

    
358
    local err_file="$1"
359

    
360
    report_error "$err_file"
361
    cleanup
362
}
363

    
364
cleanup() {
365
    # if something fails here, it souldn't call cleanup again...
366
    trap - EXIT
367

    
368
    if [ ${#CLEANUP[*]} -gt 0 ]; then
369
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
370
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
371
        for i in $REVERSE_INDEXES; do
372
            # If something fails here, it's better to retry it for a few times
373
            # before we give up with an error. This is needed for kpartx when
374
            # dealing with ntfs partitions mounted through fuse. umount is not
375
            # synchronous and may return while the partition is still busy. A
376
            # premature attempt to delete partition mappings through kpartx on
377
            # a device that hosts previously mounted ntfs partition may fail
378
            # with errors like this one:
379
            # `device-mapper: remove ioctl failed: Device or resource busy'
380
            # A sensible workaround for this is to wait for a while and then
381
            # retry it.
382
            local cmd=${CLEANUP[$i]}
383
            $cmd || for interval in 0.25 0.5 1 2 4; do
384
            echo "Command $cmd failed!"
385
            echo "I'll wait for $interval secs and will retry..."
386
            sleep $interval
387
            $cmd && break
388
        done
389
        if [ "$?" != "0" ]; then
390
            echo "Giving Up..."
391
            exit 1;
392
        fi
393
    done
394
  fi
395
}
396

    
397
trap cleanup EXIT
398

    
399
DEFAULT_FILE="@sysconfdir@/default/snf-image"
400
if [ -f "$DEFAULT_FILE" ]; then
401
    . "$DEFAULT_FILE"
402
fi
403

    
404
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
405
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
406
: ${IMAGE_DEBUG:="no"}
407
: ${HELPER_DIR:="@HELPER_DIR@"}
408
: ${HELPER_CACHE_DIR:="@HELPER_CACHE_DIR@"}
409
: ${HELPER_IMG:="@HELPER_IMG@"}
410
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
411
: ${HELPER_INITRD:="@HELPER_INITRD@"}
412
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
413
: ${HELPER_SOFT_TIMEOUT:=20}
414
: ${HELPER_HARD_TIMEOUT:=5}
415
: ${HELPER_USER:="nobody"}
416
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
417
: ${HELPER_MIRROR:=""}
418
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
419
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
420
: ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
421
: ${UNATTEND:="@UNATTEND@"}
422
: ${MULTISTRAP_CONFIG:="@MULTISTRAP_CONFIG@"}
423

    
424
SCRIPT_NAME=$(basename $0)
425

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