Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.5 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
PROGRESS_MONITOR="snf-progress-monitor"
35
INSTALL_MBR="install-mbr"
36
TIMELIMIT="timelimit"
37

    
38
progress_monitor_support="@progress_monitor_support@"
39

    
40
CLEANUP=( )
41

    
42
log_error() {
43
    echo "$@" >&2
44
}
45

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

    
54
            -o) old_name=$2; shift 2;;
55

    
56
            -b) blockdev=$2; shift 2;;
57

    
58
            -s) swapdev=$2; shift 2;;
59

    
60
            --) shift; break;;
61

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

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

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

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

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

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

    
155
format_disk0() {
156
    local device="$1"
157
    local image_type="$2"
158
    
159
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
160

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

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

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

    
173
    local target=$(mktemp -d) || exit 1
174
    CLEANUP+=("rmdir $target")
175

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

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

    
229
}
230

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

    
259
trap cleanup EXIT
260

    
261
DEFAULT_FILE="@sysconfdir@/default/snf-image"
262
if [ -f "$DEFAULT_FILE" ]; then
263
    . "$DEFAULT_FILE"
264
fi
265

    
266
: ${ARCH:="x86_64"}
267
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
268
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
269
: ${HELPER_DIR:="@HELPER_DIR@"}
270
: ${HELPER_IMG:="@HELPER_IMG@"}
271
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
272
: ${HELPER_INITRD:="@HELPER_INITRD@"}
273
: ${HELPER_SOFT_TIMEOUT=120}
274
: ${HELPER_HARD_TIMEOUT=15}
275
: ${HELPER_USER="nobody"}
276

    
277
SCRIPT_NAME=$(basename $0)
278

    
279
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
280
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
281
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
282
else
283
    for dir in /lib/udev /sbin; do
284
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
285
            VOL_ID="$dir/vol_id -u"
286
            VOL_TYPE="$dir/vol_id -t"
287
        fi
288
    done
289
fi
290

    
291
if [ -z "$VOL_ID" ]; then
292
    log_error "vol_id or blkid not found, please install udev or util-linux"
293
    exit 1
294
fi
295

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