Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ 14de44dc

History | View | Annotate | Download (10.1 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

    
30
AWK="awk"
31
KPARTX="kpartx"
32
LOSETUP="losetup"
33
SFDISK="sfdisk"
34
QEMU_IMG="qemu-img"
35
INSTALL_MBR="install-mbr"
36
TIMELIMIT="timelimit"
37
CURL="curl"
38
WGET="wget"
39
PROGRESS_MONITOR="snf-progress-monitor"
40

    
41
progress_monitor_support="@progress_monitor_support@"
42

    
43
CLEANUP=( )
44

    
45
add_cleanup() {
46
    local cmd=""
47
    for arg; do cmd+=$(printf "%q " "$arg"); done
48
    CLEANUP+=("$cmd")
49
}
50

    
51
log_error() {
52
    echo "$@" >&2
53
}
54

    
55
get_api5_arguments() {
56
    GETOPT_RESULT=$*
57
    # Note the quotes around `$TEMP': they are essential!
58
    eval set -- "$GETOPT_RESULT"
59
    while true; do
60
        case "$1" in
61
            -i|-n) instance=$2; shift 2;;
62

    
63
            -o) old_name=$2; shift 2;;
64

    
65
            -b) blockdev=$2; shift 2;;
66

    
67
            -s) swapdev=$2; shift 2;;
68

    
69
            --) shift; break;;
70

    
71
            *)  log_error "Internal error!" >&2; exit 1;;
72
        esac
73
    done
74
    if [ -z "$instance" -o -z "$blockdev" ]; then
75
        log_error "Missing OS API Argument (-i, -n, or -b)"
76
        exit 1
77
    fi
78
    if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
79
        log_error "Missing OS API Argument -s (swapdev)"
80
        exit 1
81
    fi
82
    if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
83
        log_error "Missing OS API Argument -o (old_name)"
84
        exit 1
85
    fi
86
}
87

    
88
get_api10_arguments() {
89
    if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
90
        log_error "Missing OS API Variable:"
91
        log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
92
        exit 1
93
    fi
94
    instance=$INSTANCE_NAME
95
    if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
96
        log_error "At least one disk is needed"
97
        exit 1
98
    fi
99
    if [ "$SCRIPT_NAME" = "export" ]; then
100
        if [ -z "$EXPORT_DEVICE" ]; then
101
        log_error "Missing OS API Variable EXPORT_DEVICE"
102
    fi
103
    blockdev=$EXPORT_DEVICE
104
    elif [ "$SCRIPT_NAME" = "import" ]; then
105
        if [ -z "$IMPORT_DEVICE" ]; then
106
        log_error "Missing OS API Variable IMPORT_DEVICE"
107
        fi
108
        blockdev=$IMPORT_DEVICE
109
    else
110
        blockdev=$DISK_0_PATH
111
    fi
112
    if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
113
        log_error "Missing OS API Variable OLD_INSTANCE_NAME"
114
    fi
115
    old_name=$OLD_INSTANCE_NAME
116
}
117

    
118
get_api20_arguments() {
119
    get_api10_arguments
120
    if [ -z "$OSP_IMG_ID" ]; then
121
        log_error "Missing OS API Parameter: OSP_IMG_ID"
122
        exit 1
123
    fi
124
    if [ -z "$OSP_IMG_FORMAT" ]; then
125
        log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
126
        exit 1
127
    fi
128
    if [ -z "$OSP_IMG_PASSWD" ]; then
129
        log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
130
        exit 1
131
    fi
132
    if [ -z "$OSP_IMG_PROPERTIES" ]; then
133
        log_error "Missing OS API Parameter: OSP_IMG_PROPERTIES"
134
        exit 1
135
    fi
136

    
137
    IMG_ID=$OSP_IMG_ID
138
    IMG_FORMAT=$OSP_IMG_FORMAT
139
    IMG_PASSWD=$OSP_IMG_PASSWD
140
    IMG_PROPERTIES=$OSP_IMG_PROPERTIES
141
    if [ -n "$OSP_IMG_PERSONALITY" ]; then
142
        IMG_PERSONALITY=$OSP_IMG_PERSONALITY
143
    fi
144
}
145

    
146
map_disk0() {
147
    blockdev="$1"
148
    filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
149
                            grep -m 1 -- "-1.*$blockdev" | \
150
                            $AWK '{print $1}')
151
    if [ -z "$filesystem_dev_base" ]; then
152
        log_error "Cannot interpret kpartx output and get partition mapping"
153
        exit 1
154
    fi
155
    $KPARTX -a -p- "$blockdev" > /dev/null
156
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
157
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
158
        log_error "Can't find kpartx mapped partition:" \
159
                                            "/dev/mapper/$filesystem_dev_base"
160
        exit 1
161
    fi
162
    echo "$filesystem_dev"
163
}
164

    
165
unmap_disk0() {
166
    $KPARTX -d -p- "$1"
167
}
168

    
169
format_disk0() {
170
    local device="$1"
171
    local image_type="$2"
172

    
173
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
174

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

    
179
    $sfdisk_cmd > /dev/null <<EOF
180
2048,,${part_id["$image_type"]},*
181
EOF
182
}
183

    
184
create_floppy() {
185
    local img=$1
186

    
187
    local target=$(mktemp -d)
188
    add_cleanup rmdir "$target"
189

    
190
    dd bs=512 count=2880 if=/dev/zero of="$img"
191
    mkfs.ext2 -F "$img" > /dev/null
192
    mount "$img" "$target" -o loop
193
    set | egrep ^snf_export_\\w+=|sed -e 's/^snf_export_/SNF_IMAGE_/' | \
194
    while read line; do
195
        echo "export $line" >> "$target/rules"
196
    done
197
    umount "$target"
198
}
199

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

    
245
}
246

    
247
cleanup() {
248
# if something fails here, it souldn't call cleanup again...
249
    trap - EXIT
250
    if [ ${#CLEANUP[*]} -gt 0 ]; then
251
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
252
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
253
        for i in $REVERSE_INDEXES; do
254
            # If something fails here, it's better to retry it for a few times
255
            # before we give up with an error. This is needed for kpartx when
256
            # dealing with ntfs partitions mounted through fuse. umount is not
257
            # synchronous and may return while the partition is still busy. A
258
            # premature attempt to delete partition mappings through kpartx on a
259
            # device that hosts previously mounted ntfs partition may fail with
260
            # an  `device-mapper: remove ioctl failed: Device or resource busy'
261
            # error. A sensible workaround for this is to wait for a while and
262
            # then try again.
263
            local cmd=${CLEANUP[$i]}
264
            $cmd || for interval in 0.25 0.5 1 2 4; do
265
            echo "Command $cmd failed!"
266
            echo "I'll wait for $interval secs and will retry..."
267
            sleep $interval
268
            $cmd && break
269
        done
270
        if [ "$?" != "0" ]; then
271
            echo "Giving Up..."
272
            exit 1;
273
        fi
274
    done
275
  fi
276
}
277

    
278
trap cleanup EXIT
279

    
280
DEFAULT_FILE="@sysconfdir@/default/snf-image"
281
if [ -f "$DEFAULT_FILE" ]; then
282
    . "$DEFAULT_FILE"
283
fi
284

    
285
: ${ARCH:="x86_64"}
286
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
287
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
288
: ${HELPER_DIR:="@HELPER_DIR@"}
289
: ${HELPER_IMG:="@HELPER_IMG@"}
290
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
291
: ${HELPER_INITRD:="@HELPER_INITRD@"}
292
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
293
: ${HELPER_SOFT_TIMEOUT:=15}
294
: ${HELPER_HARD_TIMEOUT:=5}
295
: ${HELPER_USER:="nobody"}
296
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
297
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted"}
298
: ${HELPER_MIRROR:=""}
299

    
300

    
301
SCRIPT_NAME=$(basename $0)
302

    
303
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
304
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
305
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
306
else
307
    for dir in /lib/udev /sbin; do
308
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
309
            VOL_ID="$dir/vol_id -u"
310
            VOL_TYPE="$dir/vol_id -t"
311
        fi
312
    done
313
fi
314

    
315
if [ -z "$VOL_ID" ]; then
316
    log_error "vol_id or blkid not found, please install udev or util-linux"
317
    exit 1
318
fi
319

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