Statistics
| Branch: | Tag: | Revision:

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

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

    
29
pithos_backend_support="@pithos_backend_support@"
30
network_backend_support="@network_backend_support@"
31

    
32
CLEANUP=( )
33

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

    
40
log_error() {
41
    echo "$@" >&2
42
}
43

    
44
get_api5_arguments() {
45
    GETOPT_RESULT=$*
46
    # Note the quotes around `$TEMP': they are essential!
47
    eval set -- "$GETOPT_RESULT"
48
    while true; do
49
        case "$1" in
50
            -i|-n) instance=$2; shift 2;;
51

    
52
            -o) old_name=$2; shift 2;;
53

    
54
            -b) blockdev=$2; shift 2;;
55

    
56
            -s) swapdev=$2; shift 2;;
57

    
58
            --) shift; break;;
59

    
60
            *)  log_error "Internal error!" >&2; exit 1;;
61
        esac
62
    done
63
    if [ -z "$instance" -o -z "$blockdev" ]; then
64
        log_error "Missing OS API Argument (-i, -n, or -b)"
65
        exit 1
66
    fi
67
    if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
68
        log_error "Missing OS API Argument -s (swapdev)"
69
        exit 1
70
    fi
71
    if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
72
        log_error "Missing OS API Argument -o (old_name)"
73
        exit 1
74
    fi
75
}
76

    
77
get_api10_arguments() {
78
    if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
79
        log_error "Missing OS API Variable:"
80
        log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
81
        exit 1
82
    fi
83
    instance=$INSTANCE_NAME
84
    if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
85
        log_error "At least one disk is needed"
86
        exit 1
87
    fi
88
    if [ "$SCRIPT_NAME" = "export" ]; then
89
        if [ -z "$EXPORT_DEVICE" ]; then
90
        log_error "Missing OS API Variable EXPORT_DEVICE"
91
    fi
92
    blockdev=$EXPORT_DEVICE
93
    elif [ "$SCRIPT_NAME" = "import" ]; then
94
        if [ -z "$IMPORT_DEVICE" ]; then
95
        log_error "Missing OS API Variable IMPORT_DEVICE"
96
        fi
97
        blockdev=$IMPORT_DEVICE
98
    else
99
        blockdev=$DISK_0_PATH
100
    fi
101
    if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
102
        log_error "Missing OS API Variable OLD_INSTANCE_NAME"
103
    fi
104
    old_name=$OLD_INSTANCE_NAME
105
}
106

    
107
get_api20_arguments() {
108
    get_api10_arguments
109
    if [ -z "$OSP_IMG_ID" ]; then
110
        log_error "Missing OS API Parameter: OSP_IMG_ID"
111
        exit 1
112
    fi
113
    if [ -z "$OSP_IMG_FORMAT" ]; then
114
        log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
115
        exit 1
116
    fi
117
    if [ -z "$OSP_IMG_PASSWD" ]; then
118
        log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
119
        exit 1
120
    fi
121

    
122
    IMG_ID=$OSP_IMG_ID
123
    IMG_FORMAT=$OSP_IMG_FORMAT
124
    IMG_PASSWD=$OSP_IMG_PASSWD
125
    if [ -n "$OSP_IMG_PROPERTIES" ]; then
126
        IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
127
    fi
128
    if [ -n "$OSP_IMG_PERSONALITY" ]; then
129
        IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
130
    fi
131
}
132

    
133
map_disk0() {
134
    blockdev="$1"
135
    filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
136
                            grep -m 1 -- "-1.*$blockdev" | \
137
                            $AWK '{print $1}')
138
    if [ -z "$filesystem_dev_base" ]; then
139
        log_error "Cannot interpret kpartx output and get partition mapping"
140
        exit 1
141
    fi
142
    $KPARTX -a -p- "$blockdev" > /dev/null
143
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
144
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
145
        log_error "Can't find kpartx mapped partition:" \
146
                                            "/dev/mapper/$filesystem_dev_base"
147
        exit 1
148
    fi
149
    echo "$filesystem_dev"
150
}
151

    
152
unmap_disk0() {
153
    $KPARTX -d -p- "$1"
154
}
155

    
156
format_disk0() {
157
    local device="$1"
158
    local image_type="$2"
159

    
160
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
161

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

    
166
    $sfdisk_cmd > /dev/null <<EOF
167
2048,,${part_id["$image_type"]},*
168
EOF
169
}
170

    
171
create_floppy() {
172
    local img=$1
173

    
174
    local target=$(mktemp -d)
175
    add_cleanup rmdir "$target"
176

    
177
    dd bs=512 count=2880 if=/dev/zero of="$img"
178
    mkfs.ext2 -F "$img" > /dev/null
179
    mount "$img" "$target" -o loop
180
    set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
181
        > "$target/rules"
182
    umount "$target"
183
}
184

    
185
get_backend_type() {
186
    local id=$1
187

    
188
    if [[ "$id" =~ ^pithos: ]]; then
189
        if [ "$pithos_backend_support" = "yes" ]; then
190
            echo "pithos"
191
        else
192
            log_error "Retrieving images from pithos is not supported"
193
            exit 1
194
        fi
195
    elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
196
        if [ "$network_backend_support" = "yes" ]; then
197
            echo "network";
198
        else
199
            log_error "Retrieving images from the network is not supported."
200
            exit 1
201
        fi
202
    else
203
        echo "local";
204
    fi
205
}
206

    
207
# this one is only to be called by create
208
ganeti_os_main() {
209
    if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
210
        OS_API_VERSION=5
211
        GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
212
        if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
213
        get_api5_arguments $GETOPT_RESULT
214
    elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
215
        get_api10_arguments
216
    elif [ "$OS_API_VERSION" = "20" ]; then
217
        get_api20_arguments
218
        IMAGE_NAME="$IMG_ID"
219
        IMAGE_TYPE="$IMG_FORMAT"
220
        BACKEND_TYPE=$(get_backend_type $IMG_ID)
221
    else
222
        log_error "Unknown OS API VERSION $OS_API_VERSION"
223
        exit 1
224
    fi
225
    
226
    if [ -n "$OS_VARIANT" ]; then
227
        if [ ! -d "$VARIANTS_DIR" ]; then
228
            log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
229
            exit 1
230
        fi
231
        VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
232
        if [ -f "$VARIANT_CONFIG" ]; then
233
            . "$VARIANT_CONFIG"
234
        else
235
            if grep -qxF "$OS_VARIANT" variants.list; then
236
                log_error "ERROR: instance-image configuration error"
237
                log_error "  Published variant $OS_VARIANT is missing its" \
238
                    "config file"
239
                log_error "  Please create $VARIANT_CONFIG or unpublish the" \
240
                    "variant"
241
                log_error "  (by removing $OS_VARIANT from variants.list)"
242
            else
243
                log_error "Unofficial variant $OS_VARIANT is unsupported"
244
                log_error "Most probably this is a user error, forcing a" \
245
                    "wrong name"
246
                log_error "To support this variant please create file" \
247
                    "$VARIANT_CONFIG"
248
            fi
249
            exit 1
250
        fi
251
    fi
252

    
253
}
254

    
255
do_debootstrap() {
256
    local target=$1
257

    
258
    echo "Debootstraping to create a new root filesystem:"
259

    
260
    # Create a policy-rc.d file to deny init script execution
261
    mkdir -p "$target/usr/sbin"
262
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
263
#!/bin/sh
264
exit 101
265
EOF
266
    chmod +x "$target/usr/sbin/policy-rc.d"
267

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

    
272
    rm "$target/usr/sbin/policy-rc.d"
273

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

    
278
    local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
279
    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
280
        { rm "$tmp_cache"; false; }
281
    # Overwrite the default cache file. Not the user specified if present.
282
    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
283
}
284

    
285
cleanup() {
286
# if something fails here, it souldn't call cleanup again...
287
    trap - EXIT
288
    if [ ${#CLEANUP[*]} -gt 0 ]; then
289
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
290
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
291
        for i in $REVERSE_INDEXES; do
292
            # If something fails here, it's better to retry it for a few times
293
            # before we give up with an error. This is needed for kpartx when
294
            # dealing with ntfs partitions mounted through fuse. umount is not
295
            # synchronous and may return while the partition is still busy. A
296
            # premature attempt to delete partition mappings through kpartx on a
297
            # device that hosts previously mounted ntfs partition may fail with
298
            # an  `device-mapper: remove ioctl failed: Device or resource busy'
299
            # error. A sensible workaround for this is to wait for a while and
300
            # then try again.
301
            local cmd=${CLEANUP[$i]}
302
            $cmd || for interval in 0.25 0.5 1 2 4; do
303
            echo "Command $cmd failed!"
304
            echo "I'll wait for $interval secs and will retry..."
305
            sleep $interval
306
            $cmd && break
307
        done
308
        if [ "$?" != "0" ]; then
309
            echo "Giving Up..."
310
            exit 1;
311
        fi
312
    done
313
  fi
314
}
315

    
316
trap cleanup EXIT
317

    
318
DEFAULT_FILE="@sysconfdir@/default/snf-image"
319
if [ -f "$DEFAULT_FILE" ]; then
320
    . "$DEFAULT_FILE"
321
fi
322

    
323
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
324
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
325
: ${HELPER_DIR:="@HELPER_DIR@"}
326
: ${HELPER_IMG:="@HELPER_IMG@"}
327
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
328
: ${HELPER_INITRD:="@HELPER_INITRD@"}
329
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
330
: ${HELPER_SOFT_TIMEOUT:=15}
331
: ${HELPER_HARD_TIMEOUT:=5}
332
: ${HELPER_USER:="nobody"}
333
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
334
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw"}
335
: ${HELPER_MIRROR:=""}
336
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
337
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
338
: ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
339

    
340
SCRIPT_NAME=$(basename $0)
341

    
342
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
343
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
344
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
345
else
346
    for dir in /lib/udev /sbin; do
347
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
348
            VOL_ID="$dir/vol_id -u"
349
            VOL_TYPE="$dir/vol_id -t"
350
        fi
351
    done
352
fi
353

    
354
if [ -z "$VOL_ID" ]; then
355
    log_error "vol_id or blkid not found, please install udev or util-linux"
356
    exit 1
357
fi
358

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