Statistics
| Branch: | Tag: | Revision:

root / host / common.sh.in @ f4bd39d8

History | View | Annotate | Download (7.6 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

    
36
progress_monitor_support="@progress_monitor_support@"
37

    
38
CLEANUP=( )
39

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

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

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

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

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

    
58
            --) shift; break;;
59

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

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

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

    
122
    IMG_ID=$OSP_IMG_ID
123
    IMG_FORMAT=$OSP_IMG_FORMAT
124
    IMG_PASSWD=$OSP_IMG_PASSWD
125
}
126

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

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

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

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

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

    
165
cleanup() {
166
    if [ ${#CLEANUP[*]} -gt 0 ]; then
167
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
168
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
169
        for i in $REVERSE_INDEXES; do
170
            ${CLEANUP[$i]}
171
        done
172
    fi
173
}
174

    
175
trap cleanup EXIT
176

    
177
DEFAULT_FILE="@sysconfdir@/default/snf-image"
178
if [ -f "$DEFAULT_FILE" ]; then
179
    . "$DEFAULT_FILE"
180
fi
181

    
182
: ${ARCH:="x86_64}
183
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/snf-image/hooks"}
184
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
185
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
186

    
187
SCRIPT_NAME=$(basename $0)
188

    
189
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
190
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
191
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
192
else
193
    for dir in /lib/udev /sbin; do
194
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
195
            VOL_ID="$dir/vol_id -u"
196
            VOL_TYPE="$dir/vol_id -t"
197
        fi
198
    done
199
fi
200

    
201
if [ -z "$VOL_ID" ]; then
202
    log_error "vol_id or blkid not found, please install udev or util-linux"
203
    exit 1
204
fi
205

    
206

    
207
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
208
    OS_API_VERSION=5
209
    GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
210
    if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
211
    get_api5_arguments $GETOPT_RESULT
212
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
213
    get_api10_arguments
214
elif [ "$OS_API_VERSION" = "20" ]; then
215
    get_api20_arguments
216
    IMAGE_NAME=$IMG_ID
217
    IMAGE_TYPE=$IMG_FORMAT
218
else
219
    log_error "Unknown OS API VERSION $OS_API_VERSION"
220
    exit 1
221
fi
222

    
223
if [ -n "$OS_VARIANT" ]; then
224
    if [ ! -d "$VARIANTS_DIR" ]; then
225
        log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
226
        exit 1
227
    fi
228
    VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
229
    if [ -f "$VARIANT_CONFIG" ]; then
230
        . "$VARIANT_CONFIG"
231
    else
232
        if grep -qxF "$OS_VARIANT" variants.list; then
233
            log_error "ERROR: instance-image configuration error"
234
            log_error "  Published variant $OS_VARIANT is missing its config" \
235
                      "file"
236
            log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
237
            log_error "  (by removing $OS_VARIANT from variants.list)"
238
        else
239
            log_error "Unofficial variant $OS_VARIANT is unsupported"
240
            log_error "Most probably this is a user error, forcing a wrong name"
241
            log_error "To support this variant please create file" \
242
                        "$VARIANT_CONFIG"
243
        fi
244
        exit 1
245
    fi
246
fi
247

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