Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.2 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_/SNF_IMAGE_/' | \
193
    while read line; do
194
        echo "export $line" >> "$target/rules"
195
    done
196
    umount "$target"
197
}
198

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

    
244
}
245

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

    
277
trap cleanup EXIT
278

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

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

    
299

    
300
SCRIPT_NAME=$(basename $0)
301

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

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

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