Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ df1c62e2

History | View | Annotate | Download (11.9 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
TIMELIMIT="timelimit"
26
CURL="curl"
27

    
28
network_backend_support="@network_backend_support@"
29

    
30
# Use file descriptors in the range 3-9. File descriptors below 3 are used for
31
# standard input, output, and error, the ones above 9 may be used by the shell
32
# internally.
33
MONITOR_FD=9
34

    
35
ERROR_TYPE="ganeti-error"
36

    
37
CLEANUP=( )
38
ERROR_MSGS=( )
39

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

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

    
51
close_fd() {
52
    local fd="$1"
53
    eval "exec $fd>&-"
54
}
55

    
56
report_error() {
57
    local error_file=$1
58

    
59
    local id=$(sed 's/"/\\"/g' <<< "$INSTANCE_NAME")
60
    local type="$ERROR_TYPE"
61
    local location="host"
62

    
63
    local msg="["
64
    for err in "${ERROR_MSGS[@]}"; do
65
        msg+="\"$(sed 's/"/\\"/g' <<< "$err")\","
66
    done
67
    if [ ${#msg} -gt 1 ]; then
68
        # remove last comma (,)
69
        msg="${msg%?}"
70
    fi
71
    msg+="]"
72

    
73
    local stderr="$(cat "$error_file" | sed 's/"/\\"/g')"
74

    
75
    report="{\"id\":\"$id\","
76
    report+="\"type\":\"$type\"," \
77
    report+="\"timestamp\":$(date +%s.%N)," \
78
    report+="\"location\":\"$location\"," \
79
    report+="\"messages\":$msg," \
80
    report+="\"stderr\":\"$stderr\"}"
81

    
82
    eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
83
}
84

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

    
93
            -o) old_name=$2; shift 2;;
94

    
95
            -b) blockdev=$2; shift 2;;
96

    
97
            -s) swapdev=$2; shift 2;;
98

    
99
            --) shift; break;;
100

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

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

    
148
get_api20_arguments() {
149
    get_api10_arguments
150
    if [ -z "$OSP_IMG_ID" ]; then
151
        log_error "Missing OS API Parameter: OSP_IMG_ID"
152
        exit 1
153
    fi
154
    if [ -z "$OSP_IMG_FORMAT" ]; then
155
        log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
156
        exit 1
157
    fi
158
    if [ -z "$OSP_IMG_PASSWD" ]; then
159
        log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
160
        exit 1
161
    fi
162

    
163
    IMG_ID=$OSP_IMG_ID
164
    IMG_FORMAT=$OSP_IMG_FORMAT
165
    IMG_PASSWD=$OSP_IMG_PASSWD
166
    if [ -n "$OSP_IMG_PROPERTIES" ]; then
167
        IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
168
    fi
169
    if [ -n "$OSP_IMG_PERSONALITY" ]; then
170
        IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
171
    fi
172
}
173

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

    
193
unmap_disk0() {
194
    $KPARTX -d -p- "$1"
195
}
196

    
197
format_disk0() {
198
    local device="$1"
199
    local image_type="$2"
200

    
201
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
202

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

    
207
    $sfdisk_cmd > /dev/null <<EOF
208
2048,,${part_id["$image_type"]},*
209
EOF
210
}
211

    
212
create_floppy() {
213
    local img=$1
214

    
215
    local target=$(mktemp -d)
216
    add_cleanup rmdir "$target"
217

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

    
233
get_backend_type() {
234
    local id=$1
235

    
236
    if [[ "$id" =~ ^pithos: ]]; then
237
        echo "pithos"
238
    elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
239
        if [ "$network_backend_support" = "yes" ]; then
240
            echo "network";
241
        else
242
            log_error "Retrieving images from the network is not supported."
243
            exit 1
244
        fi
245
    else
246
        echo "local";
247
    fi
248
}
249

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

    
296
}
297

    
298
do_debootstrap() {
299
    local target="$1"
300

    
301
    echo "Debootstraping to create a new root filesystem:"
302

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

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

    
315
    # Save the package list
316
    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
317

    
318
    rm "$target/usr/sbin/policy-rc.d"
319

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

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

    
331
report_and_cleanup(){
332

    
333
    local err_file="$1"
334

    
335
    report_error "$err_file"
336
    cleanup
337
}
338

    
339
cleanup() {
340
    # if something fails here, it souldn't call cleanup again...
341
    trap - EXIT
342

    
343
    if [ ${#CLEANUP[*]} -gt 0 ]; then
344
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
345
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
346
        for i in $REVERSE_INDEXES; do
347
            # If something fails here, it's better to retry it for a few times
348
            # before we give up with an error. This is needed for kpartx when
349
            # dealing with ntfs partitions mounted through fuse. umount is not
350
            # synchronous and may return while the partition is still busy. A
351
            # premature attempt to delete partition mappings through kpartx on
352
            # a device that hosts previously mounted ntfs partition may fail
353
            # with errors like this one:
354
            # `device-mapper: remove ioctl failed: Device or resource busy'
355
            # A sensible workaround for this is to wait for a while and then
356
            # retry it.
357
            local cmd=${CLEANUP[$i]}
358
            $cmd || for interval in 0.25 0.5 1 2 4; do
359
            echo "Command $cmd failed!"
360
            echo "I'll wait for $interval secs and will retry..."
361
            sleep $interval
362
            $cmd && break
363
        done
364
        if [ "$?" != "0" ]; then
365
            echo "Giving Up..."
366
            exit 1;
367
        fi
368
    done
369
  fi
370
}
371

    
372
trap cleanup EXIT
373

    
374
DEFAULT_FILE="@sysconfdir@/default/snf-image"
375
if [ -f "$DEFAULT_FILE" ]; then
376
    . "$DEFAULT_FILE"
377
fi
378

    
379
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
380
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
381
: ${HELPER_DIR:="@HELPER_DIR@"}
382
: ${HELPER_IMG:="@HELPER_IMG@"}
383
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
384
: ${HELPER_INITRD:="@HELPER_INITRD@"}
385
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
386
: ${HELPER_SOFT_TIMEOUT:=15}
387
: ${HELPER_HARD_TIMEOUT:=5}
388
: ${HELPER_USER:="nobody"}
389
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
390
: ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
391
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
392
: ${HELPER_MIRROR:=""}
393
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
394
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
395
: ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
396
: ${UNATTEND:="@UNATTEND@"}
397

    
398
SCRIPT_NAME=$(basename $0)
399

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