Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.6 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

    
28
network_backend_support="@network_backend_support@"
29

    
30
MSG_TYPE_ERROR="error"
31

    
32
CLEANUP=( )
33
ERROR_MSGS=( )
34

    
35
add_cleanup() {
36
    local cmd=""
37
    for arg; do cmd+=$(printf "%q " "$arg"); done
38
    CLEANUP+=("$cmd")
39
}
40

    
41
log_error() {
42
    ERROR_MSGS+=("$@")
43
    echo "$@" >&2
44
}
45

    
46
close_fd() {
47
    local fd="$1"
48
    exec {fd}>&-
49
}
50

    
51
report_error() {
52
    local error_file=$1
53
    local monitor_fd=$2
54

    
55
    local type="$MSG_TYPE_ERROR"
56
    local location="host"
57

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

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

    
70
    report="\"type\":\"$type\","
71
    report+="\"timestamp\":$(date +%s.%N),"
72
    report+="\"location\":\"$location\","
73
    report+="\"messages\":$msg,"
74
    report+="\"stderr\":\"$stderr\"}"
75

    
76
    eval "echo $(printf "%q" "$report") >&${monitor_fd}"
77
}
78

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

    
87
            -o) old_name=$2; shift 2;;
88

    
89
            -b) blockdev=$2; shift 2;;
90

    
91
            -s) swapdev=$2; shift 2;;
92

    
93
            --) shift; break;;
94

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

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

    
142
get_api20_arguments() {
143
    get_api10_arguments
144
    if [ -z "$OSP_IMG_ID" ]; then
145
        log_error "Missing OS API Parameter: OSP_IMG_ID"
146
        exit 1
147
    fi
148
    if [ -z "$OSP_IMG_FORMAT" ]; then
149
        log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
150
        exit 1
151
    fi
152
    if [ -z "$OSP_IMG_PASSWD" ]; then
153
        log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
154
        exit 1
155
    fi
156

    
157
    IMG_ID=$OSP_IMG_ID
158
    IMG_FORMAT=$OSP_IMG_FORMAT
159
    IMG_PASSWD=$OSP_IMG_PASSWD
160
    if [ -n "$OSP_IMG_PROPERTIES" ]; then
161
        IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
162
    fi
163
    if [ -n "$OSP_IMG_PERSONALITY" ]; then
164
        IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
165
    fi
166
}
167

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

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

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

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

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

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

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

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

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

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

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

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

    
290
}
291

    
292
do_debootstrap() {
293
    local target="$1"
294

    
295
    echo "Debootstraping to create a new root filesystem:"
296

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

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

    
309
    # Save the package list
310
    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
311

    
312
    rm "$target/usr/sbin/policy-rc.d"
313

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

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

    
325
report_and_cleanup(){
326

    
327
    local err_file="$1"
328
    local fd="$2"
329

    
330
    report_error "$err_file" "$fd"
331
    cleanup
332
}
333

    
334
cleanup() {
335
    # if something fails here, it souldn't call cleanup again...
336
    trap - EXIT
337

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

    
367
trap cleanup EXIT
368

    
369
DEFAULT_FILE="@sysconfdir@/default/snf-image"
370
if [ -f "$DEFAULT_FILE" ]; then
371
    . "$DEFAULT_FILE"
372
fi
373

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

    
393
SCRIPT_NAME=$(basename $0)
394

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