Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ 198f8249

History | View | Annotate | Download (10.1 kB)

1
# Copyright (C) 2011 GRNET S.A. 
2
# Copyright (C) 2007, 2008, 2009 Google Inc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
# General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
# 02110-1301, USA.
18

    
19
AWK="awk"
20
KPARTX="kpartx"
21
LOSETUP="losetup"
22
SFDISK="sfdisk"
23
QEMU_IMG="qemu-img"
24
INSTALL_MBR="install-mbr"
25
TIMELIMIT="timelimit"
26
CURL="curl"
27
PROGRESS_MONITOR="snf-progress-monitor"
28

    
29
progress_monitor_support="@progress_monitor_support@"
30
pithos_backend_support="@pithos_backend_support@"
31
network_backend_support="@network_backend_support@"
32

    
33
CLEANUP=( )
34

    
35
add_cleanup() {
36
    local cmd=""
37
    for arg; do cmd+=$(printf "%q " "$arg"); done
38
    CLEANUP+=("$cmd")
39
}
40

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

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

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

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

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

    
59
            --) shift; break;;
60

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

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

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

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

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

    
153
unmap_disk0() {
154
    $KPARTX -d -p- "$1"
155
}
156

    
157
format_disk0() {
158
    local device="$1"
159
    local image_type="$2"
160

    
161
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
162

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

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

    
172
create_floppy() {
173
    local img=$1
174

    
175
    local target=$(mktemp -d)
176
    add_cleanup rmdir "$target"
177

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

    
186
get_backend_type() {
187
    local id=$1
188

    
189
    if [[ "$id" =~ ^pithos: ]]; then
190
        if [ "$pithos_backend_support" = "yes" ]; then
191
            echo "pithos"
192
        else
193
            log_error "Retrieving images from pithos is not supported"
194
            exit 1
195
        fi
196
    elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
197
        if [ "$network_backend_support" = "yes" ]; then
198
            echo "network";
199
        else
200
            log_error "Retrieving images from the network is not supported."
201
            exit 1
202
        fi
203
    else
204
        echo "local";
205
    fi
206
}
207

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

    
254
}
255

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

    
287
trap cleanup EXIT
288

    
289
DEFAULT_FILE="@sysconfdir@/default/snf-image"
290
if [ -f "$DEFAULT_FILE" ]; then
291
    . "$DEFAULT_FILE"
292
fi
293

    
294
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
295
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
296
: ${HELPER_DIR:="@HELPER_DIR@"}
297
: ${HELPER_IMG:="@HELPER_IMG@"}
298
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
299
: ${HELPER_INITRD:="@HELPER_INITRD@"}
300
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
301
: ${HELPER_SOFT_TIMEOUT:=15}
302
: ${HELPER_HARD_TIMEOUT:=5}
303
: ${HELPER_USER:="nobody"}
304
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
305
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw"}
306
: ${HELPER_MIRROR:=""}
307
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
308
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
309

    
310
SCRIPT_NAME=$(basename $0)
311

    
312
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
313
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
314
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
315
else
316
    for dir in /lib/udev /sbin; do
317
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
318
            VOL_ID="$dir/vol_id -u"
319
            VOL_TYPE="$dir/vol_id -t"
320
        fi
321
    done
322
fi
323

    
324
if [ -z "$VOL_ID" ]; then
325
    log_error "vol_id or blkid not found, please install udev or util-linux"
326
    exit 1
327
fi
328

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