Statistics
| Branch: | Tag: | Revision:

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

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
PROGRESS_MONITOR="snf-progress-monitor"
38

    
39
progress_monitor_support="@progress_monitor_support@"
40

    
41
CLEANUP=( )
42

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

    
49
log_error() {
50
    echo "$@" >&2
51
}
52

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

    
61
            -o) old_name=$2; shift 2;;
62

    
63
            -b) blockdev=$2; shift 2;;
64

    
65
            -s) swapdev=$2; shift 2;;
66

    
67
            --) shift; break;;
68

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

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

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

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

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

    
163
unmap_disk0() {
164
    $KPARTX -d -p- "$1"
165
}
166

    
167
format_disk0() {
168
    local device="$1"
169
    local image_type="$2"
170

    
171
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
172

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

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

    
182
create_floppy() {
183
    local img=$1
184

    
185
    local target=$(mktemp -d)
186
    add_cleanup rmdir "$target"
187

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

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

    
243
}
244

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

    
276
trap cleanup EXIT
277

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

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

    
298

    
299
SCRIPT_NAME=$(basename $0)
300

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

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

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