Statistics
| Branch: | Tag: | Revision:

root / host / common.sh.in @ 1e89f48c

History | View | Annotate | Download (8 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
HELPER_VM=@HELPER@
36

    
37
progress_monitor_support="@progress_monitor_support@"
38

    
39
CLEANUP=( )
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
}
127

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

    
147
unmap_disk0() {
148
    $KPARTX -d -p- $1
149
}
150

    
151
format_disk0() {
152
    local device="$1"
153
    local image_type="$2"
154
    
155
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
156

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

    
161
    $sfdisk_cmd > /dev/null <<EOF
162
2048,,${part_id["$image_type"]},*
163
EOF
164
}
165

    
166
create_floppy() {
167
    local img=$1
168

    
169
    local target=$(mktemp -d) || exit 1
170
    CLEANUP+=("rmdir $target")
171

    
172
    dd bs=512 count=2880 if=/dev/zero of=$img
173
    mkfs.ext2 -F $img > /dev/null
174
    mount $img $target -o loop
175
    set|egrep ^snf_export_\\w+=|sed -e 's/^snf_export_/SNF_IMAGE_/' > $target/rules
176
    umount $target
177
}
178

    
179
cleanup() {
180
    if [ ${#CLEANUP[*]} -gt 0 ]; then
181
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
182
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
183
        for i in $REVERSE_INDEXES; do
184
            ${CLEANUP[$i]}
185
        done
186
    fi
187
}
188

    
189
trap cleanup EXIT
190

    
191
DEFAULT_FILE="@sysconfdir@/default/snf-image"
192
if [ -f "$DEFAULT_FILE" ]; then
193
    . "$DEFAULT_FILE"
194
fi
195

    
196
: ${ARCH:="x86_64"}
197
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/snf-image/hooks"}
198
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
199
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
200

    
201
SCRIPT_NAME=$(basename $0)
202

    
203
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
204
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
205
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
206
else
207
    for dir in /lib/udev /sbin; do
208
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
209
            VOL_ID="$dir/vol_id -u"
210
            VOL_TYPE="$dir/vol_id -t"
211
        fi
212
    done
213
fi
214

    
215
if [ -z "$VOL_ID" ]; then
216
    log_error "vol_id or blkid not found, please install udev or util-linux"
217
    exit 1
218
fi
219

    
220

    
221
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
222
    OS_API_VERSION=5
223
    GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
224
    if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
225
    get_api5_arguments $GETOPT_RESULT
226
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
227
    get_api10_arguments
228
elif [ "$OS_API_VERSION" = "20" ]; then
229
    get_api20_arguments
230
    IMAGE_NAME=$IMG_ID
231
    IMAGE_TYPE=$IMG_FORMAT
232
else
233
    log_error "Unknown OS API VERSION $OS_API_VERSION"
234
    exit 1
235
fi
236

    
237
if [ -n "$OS_VARIANT" ]; then
238
    if [ ! -d "$VARIANTS_DIR" ]; then
239
        log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
240
        exit 1
241
    fi
242
    VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
243
    if [ -f "$VARIANT_CONFIG" ]; then
244
        . "$VARIANT_CONFIG"
245
    else
246
        if grep -qxF "$OS_VARIANT" variants.list; then
247
            log_error "ERROR: instance-image configuration error"
248
            log_error "  Published variant $OS_VARIANT is missing its config" \
249
                      "file"
250
            log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
251
            log_error "  (by removing $OS_VARIANT from variants.list)"
252
        else
253
            log_error "Unofficial variant $OS_VARIANT is unsupported"
254
            log_error "Most probably this is a user error, forcing a wrong name"
255
            log_error "To support this variant please create file" \
256
                        "$VARIANT_CONFIG"
257
        fi
258
        exit 1
259
    fi
260
fi
261

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