Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ 66840d7a

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

    
40
progress_monitor_support="@progress_monitor_support@"
41

    
42
CLEANUP=( )
43

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

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

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

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

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

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

    
68
            --) shift; break;;
69

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
242
}
243

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

    
275
trap cleanup EXIT
276

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

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

    
297

    
298
SCRIPT_NAME=$(basename $0)
299

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

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

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