Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / common.sh.in @ 21be5a41

History | View | Annotate | Download (11.2 kB)

1 7e5d635b Nikos Skalkotos
# Copyright (C) 2011 GRNET S.A. 
2 7e5d635b Nikos Skalkotos
# Copyright (C) 2007, 2008, 2009 Google Inc.
3 f4bd39d8 Nikos Skalkotos
#
4 7e5d635b Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
5 7e5d635b Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
6 7e5d635b Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
7 7e5d635b Nikos Skalkotos
# (at your option) any later version.
8 f4bd39d8 Nikos Skalkotos
#
9 7e5d635b Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
10 7e5d635b Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
11 7e5d635b Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 7e5d635b Nikos Skalkotos
# General Public License for more details.
13 f4bd39d8 Nikos Skalkotos
#
14 7e5d635b Nikos Skalkotos
# You should have received a copy of the GNU General Public License
15 7e5d635b Nikos Skalkotos
# along with this program; if not, write to the Free Software
16 7e5d635b Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 7e5d635b Nikos Skalkotos
# 02110-1301, USA.
18 f4bd39d8 Nikos Skalkotos
19 f4bd39d8 Nikos Skalkotos
AWK="awk"
20 f4bd39d8 Nikos Skalkotos
KPARTX="kpartx"
21 f4bd39d8 Nikos Skalkotos
LOSETUP="losetup"
22 f4bd39d8 Nikos Skalkotos
SFDISK="sfdisk"
23 5cbb15b9 Nikos Skalkotos
QEMU_IMG="qemu-img"
24 0c8625b9 Nikos Skalkotos
INSTALL_MBR="install-mbr"
25 9999d40d Nikos Skalkotos
TIMELIMIT="timelimit"
26 14de44dc Nikos Skalkotos
CURL="curl"
27 f4bd39d8 Nikos Skalkotos
28 b1fef76e Nikos Skalkotos
29 d71f59e9 Nikos Skalkotos
network_backend_support="@network_backend_support@"
30 f4bd39d8 Nikos Skalkotos
31 f4bd39d8 Nikos Skalkotos
CLEANUP=( )
32 f4bd39d8 Nikos Skalkotos
33 bad5ca1f Nikos Skalkotos
add_cleanup() {
34 bad5ca1f Nikos Skalkotos
    local cmd=""
35 bad5ca1f Nikos Skalkotos
    for arg; do cmd+=$(printf "%q " "$arg"); done
36 bad5ca1f Nikos Skalkotos
    CLEANUP+=("$cmd")
37 bad5ca1f Nikos Skalkotos
}
38 bad5ca1f Nikos Skalkotos
39 f4bd39d8 Nikos Skalkotos
log_error() {
40 f4bd39d8 Nikos Skalkotos
    echo "$@" >&2
41 f4bd39d8 Nikos Skalkotos
}
42 f4bd39d8 Nikos Skalkotos
43 f4bd39d8 Nikos Skalkotos
get_api5_arguments() {
44 f4bd39d8 Nikos Skalkotos
    GETOPT_RESULT=$*
45 f4bd39d8 Nikos Skalkotos
    # Note the quotes around `$TEMP': they are essential!
46 f4bd39d8 Nikos Skalkotos
    eval set -- "$GETOPT_RESULT"
47 f4bd39d8 Nikos Skalkotos
    while true; do
48 f4bd39d8 Nikos Skalkotos
        case "$1" in
49 f4bd39d8 Nikos Skalkotos
            -i|-n) instance=$2; shift 2;;
50 f4bd39d8 Nikos Skalkotos
51 f4bd39d8 Nikos Skalkotos
            -o) old_name=$2; shift 2;;
52 f4bd39d8 Nikos Skalkotos
53 f4bd39d8 Nikos Skalkotos
            -b) blockdev=$2; shift 2;;
54 f4bd39d8 Nikos Skalkotos
55 f4bd39d8 Nikos Skalkotos
            -s) swapdev=$2; shift 2;;
56 f4bd39d8 Nikos Skalkotos
57 f4bd39d8 Nikos Skalkotos
            --) shift; break;;
58 f4bd39d8 Nikos Skalkotos
59 f4bd39d8 Nikos Skalkotos
            *)  log_error "Internal error!" >&2; exit 1;;
60 f4bd39d8 Nikos Skalkotos
        esac
61 f4bd39d8 Nikos Skalkotos
    done
62 f4bd39d8 Nikos Skalkotos
    if [ -z "$instance" -o -z "$blockdev" ]; then
63 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Argument (-i, -n, or -b)"
64 f4bd39d8 Nikos Skalkotos
        exit 1
65 f4bd39d8 Nikos Skalkotos
    fi
66 f4bd39d8 Nikos Skalkotos
    if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
67 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Argument -s (swapdev)"
68 f4bd39d8 Nikos Skalkotos
        exit 1
69 f4bd39d8 Nikos Skalkotos
    fi
70 f4bd39d8 Nikos Skalkotos
    if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
71 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Argument -o (old_name)"
72 f4bd39d8 Nikos Skalkotos
        exit 1
73 f4bd39d8 Nikos Skalkotos
    fi
74 f4bd39d8 Nikos Skalkotos
}
75 f4bd39d8 Nikos Skalkotos
76 f4bd39d8 Nikos Skalkotos
get_api10_arguments() {
77 f4bd39d8 Nikos Skalkotos
    if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
78 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Variable:"
79 f4bd39d8 Nikos Skalkotos
        log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
80 f4bd39d8 Nikos Skalkotos
        exit 1
81 f4bd39d8 Nikos Skalkotos
    fi
82 f4bd39d8 Nikos Skalkotos
    instance=$INSTANCE_NAME
83 f4bd39d8 Nikos Skalkotos
    if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
84 f4bd39d8 Nikos Skalkotos
        log_error "At least one disk is needed"
85 f4bd39d8 Nikos Skalkotos
        exit 1
86 f4bd39d8 Nikos Skalkotos
    fi
87 f4bd39d8 Nikos Skalkotos
    if [ "$SCRIPT_NAME" = "export" ]; then
88 f4bd39d8 Nikos Skalkotos
        if [ -z "$EXPORT_DEVICE" ]; then
89 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Variable EXPORT_DEVICE"
90 f4bd39d8 Nikos Skalkotos
    fi
91 f4bd39d8 Nikos Skalkotos
    blockdev=$EXPORT_DEVICE
92 f4bd39d8 Nikos Skalkotos
    elif [ "$SCRIPT_NAME" = "import" ]; then
93 f4bd39d8 Nikos Skalkotos
        if [ -z "$IMPORT_DEVICE" ]; then
94 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Variable IMPORT_DEVICE"
95 f4bd39d8 Nikos Skalkotos
        fi
96 f4bd39d8 Nikos Skalkotos
        blockdev=$IMPORT_DEVICE
97 f4bd39d8 Nikos Skalkotos
    else
98 f4bd39d8 Nikos Skalkotos
        blockdev=$DISK_0_PATH
99 f4bd39d8 Nikos Skalkotos
    fi
100 f4bd39d8 Nikos Skalkotos
    if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
101 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Variable OLD_INSTANCE_NAME"
102 f4bd39d8 Nikos Skalkotos
    fi
103 f4bd39d8 Nikos Skalkotos
    old_name=$OLD_INSTANCE_NAME
104 f4bd39d8 Nikos Skalkotos
}
105 f4bd39d8 Nikos Skalkotos
106 f4bd39d8 Nikos Skalkotos
get_api20_arguments() {
107 a1d894c5 Nikos Skalkotos
    get_api10_arguments
108 a1d894c5 Nikos Skalkotos
    if [ -z "$OSP_IMG_ID" ]; then
109 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Parameter: OSP_IMG_ID"
110 f4bd39d8 Nikos Skalkotos
        exit 1
111 f4bd39d8 Nikos Skalkotos
    fi
112 a1d894c5 Nikos Skalkotos
    if [ -z "$OSP_IMG_FORMAT" ]; then
113 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
114 f4bd39d8 Nikos Skalkotos
        exit 1
115 f4bd39d8 Nikos Skalkotos
    fi
116 a1d894c5 Nikos Skalkotos
    if [ -z "$OSP_IMG_PASSWD" ]; then
117 f4bd39d8 Nikos Skalkotos
        log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
118 f4bd39d8 Nikos Skalkotos
        exit 1
119 f4bd39d8 Nikos Skalkotos
    fi
120 f4bd39d8 Nikos Skalkotos
121 f4bd39d8 Nikos Skalkotos
    IMG_ID=$OSP_IMG_ID
122 f4bd39d8 Nikos Skalkotos
    IMG_FORMAT=$OSP_IMG_FORMAT
123 f4bd39d8 Nikos Skalkotos
    IMG_PASSWD=$OSP_IMG_PASSWD
124 baf8926c Nikos Skalkotos
    if [ -n "$OSP_IMG_PROPERTIES" ]; then
125 78e9f209 Nikos Skalkotos
        IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
126 78e9f209 Nikos Skalkotos
    fi
127 58356445 Nikos Skalkotos
    if [ -n "$OSP_IMG_PERSONALITY" ]; then
128 78e9f209 Nikos Skalkotos
        IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
129 8b9d0a95 Constantinos Venetsanopoulos
    fi
130 f4bd39d8 Nikos Skalkotos
}
131 a1d894c5 Nikos Skalkotos
132 f4bd39d8 Nikos Skalkotos
map_disk0() {
133 f4bd39d8 Nikos Skalkotos
    blockdev="$1"
134 a1d894c5 Nikos Skalkotos
    filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
135 f4bd39d8 Nikos Skalkotos
                            grep -m 1 -- "-1.*$blockdev" | \
136 a1d894c5 Nikos Skalkotos
                            $AWK '{print $1}')
137 f4bd39d8 Nikos Skalkotos
    if [ -z "$filesystem_dev_base" ]; then
138 f4bd39d8 Nikos Skalkotos
        log_error "Cannot interpret kpartx output and get partition mapping"
139 f4bd39d8 Nikos Skalkotos
        exit 1
140 f4bd39d8 Nikos Skalkotos
    fi
141 bad5ca1f Nikos Skalkotos
    $KPARTX -a -p- "$blockdev" > /dev/null
142 f4bd39d8 Nikos Skalkotos
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
143 f4bd39d8 Nikos Skalkotos
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
144 f4bd39d8 Nikos Skalkotos
        log_error "Can't find kpartx mapped partition:" \
145 f4bd39d8 Nikos Skalkotos
                                            "/dev/mapper/$filesystem_dev_base"
146 f4bd39d8 Nikos Skalkotos
        exit 1
147 f4bd39d8 Nikos Skalkotos
    fi
148 f4bd39d8 Nikos Skalkotos
    echo "$filesystem_dev"
149 f4bd39d8 Nikos Skalkotos
}
150 f4bd39d8 Nikos Skalkotos
151 f4bd39d8 Nikos Skalkotos
unmap_disk0() {
152 bad5ca1f Nikos Skalkotos
    $KPARTX -d -p- "$1"
153 f4bd39d8 Nikos Skalkotos
}
154 f4bd39d8 Nikos Skalkotos
155 f4bd39d8 Nikos Skalkotos
format_disk0() {
156 f4bd39d8 Nikos Skalkotos
    local device="$1"
157 f4bd39d8 Nikos Skalkotos
    local image_type="$2"
158 23996b5d Nikos Skalkotos
159 f4bd39d8 Nikos Skalkotos
    declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
160 f4bd39d8 Nikos Skalkotos
161 f4bd39d8 Nikos Skalkotos
    # The -f is needed, because we use an optimal alignment and sfdisk complains
162 f4bd39d8 Nikos Skalkotos
    # about partitions not ending on clylinder boundary.
163 f4bd39d8 Nikos Skalkotos
    local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
164 f4bd39d8 Nikos Skalkotos
165 f4bd39d8 Nikos Skalkotos
    $sfdisk_cmd > /dev/null <<EOF
166 f4bd39d8 Nikos Skalkotos
2048,,${part_id["$image_type"]},*
167 f4bd39d8 Nikos Skalkotos
EOF
168 f4bd39d8 Nikos Skalkotos
}
169 f4bd39d8 Nikos Skalkotos
170 e036304a Nikos Skalkotos
create_floppy() {
171 e036304a Nikos Skalkotos
    local img=$1
172 e036304a Nikos Skalkotos
173 bad5ca1f Nikos Skalkotos
    local target=$(mktemp -d)
174 bad5ca1f Nikos Skalkotos
    add_cleanup rmdir "$target"
175 e036304a Nikos Skalkotos
176 bad5ca1f Nikos Skalkotos
    dd bs=512 count=2880 if=/dev/zero of="$img"
177 bad5ca1f Nikos Skalkotos
    mkfs.ext2 -F "$img" > /dev/null
178 bad5ca1f Nikos Skalkotos
    mount "$img" "$target" -o loop
179 66840d7a Nikos Skalkotos
    set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
180 66840d7a Nikos Skalkotos
        > "$target/rules"
181 8689d50f Nikos Skalkotos
    if [ -n "$UNATTEND" ]; then
182 8689d50f Nikos Skalkotos
        if [ -f "$UNATTEND" ]; then
183 8689d50f Nikos Skalkotos
            cat "$UNATTEND" > "$target/unattend.xml"
184 8689d50f Nikos Skalkotos
        else
185 8689d50f Nikos Skalkotos
            log_error "Unattend file: \`"$UNATTEND"' does not exist"
186 8689d50f Nikos Skalkotos
        fi
187 8689d50f Nikos Skalkotos
    fi
188 bad5ca1f Nikos Skalkotos
    umount "$target"
189 e036304a Nikos Skalkotos
}
190 e036304a Nikos Skalkotos
191 d71f59e9 Nikos Skalkotos
get_backend_type() {
192 d71f59e9 Nikos Skalkotos
    local id=$1
193 d71f59e9 Nikos Skalkotos
194 d71f59e9 Nikos Skalkotos
    if [[ "$id" =~ ^pithos: ]]; then
195 643d7283 Nikos Skalkotos
        echo "pithos"
196 d71f59e9 Nikos Skalkotos
    elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
197 d71f59e9 Nikos Skalkotos
        if [ "$network_backend_support" = "yes" ]; then
198 d71f59e9 Nikos Skalkotos
            echo "network";
199 d71f59e9 Nikos Skalkotos
        else
200 d71f59e9 Nikos Skalkotos
            log_error "Retrieving images from the network is not supported."
201 d71f59e9 Nikos Skalkotos
            exit 1
202 d71f59e9 Nikos Skalkotos
        fi
203 d71f59e9 Nikos Skalkotos
    else
204 d71f59e9 Nikos Skalkotos
        echo "local";
205 d71f59e9 Nikos Skalkotos
    fi
206 d71f59e9 Nikos Skalkotos
}
207 d71f59e9 Nikos Skalkotos
208 d51a95a8 Nikos Skalkotos
# this one is only to be called by create
209 d51a95a8 Nikos Skalkotos
ganeti_os_main() {
210 d51a95a8 Nikos Skalkotos
    if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
211 d51a95a8 Nikos Skalkotos
        OS_API_VERSION=5
212 d51a95a8 Nikos Skalkotos
        GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
213 d51a95a8 Nikos Skalkotos
        if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
214 d51a95a8 Nikos Skalkotos
        get_api5_arguments $GETOPT_RESULT
215 d51a95a8 Nikos Skalkotos
    elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
216 d51a95a8 Nikos Skalkotos
        get_api10_arguments
217 d51a95a8 Nikos Skalkotos
    elif [ "$OS_API_VERSION" = "20" ]; then
218 d51a95a8 Nikos Skalkotos
        get_api20_arguments
219 d71f59e9 Nikos Skalkotos
        IMAGE_NAME="$IMG_ID"
220 d71f59e9 Nikos Skalkotos
        IMAGE_TYPE="$IMG_FORMAT"
221 d71f59e9 Nikos Skalkotos
        BACKEND_TYPE=$(get_backend_type $IMG_ID)
222 d51a95a8 Nikos Skalkotos
    else
223 d51a95a8 Nikos Skalkotos
        log_error "Unknown OS API VERSION $OS_API_VERSION"
224 d51a95a8 Nikos Skalkotos
        exit 1
225 d51a95a8 Nikos Skalkotos
    fi
226 d51a95a8 Nikos Skalkotos
    
227 d51a95a8 Nikos Skalkotos
    if [ -n "$OS_VARIANT" ]; then
228 d51a95a8 Nikos Skalkotos
        if [ ! -d "$VARIANTS_DIR" ]; then
229 d51a95a8 Nikos Skalkotos
            log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
230 d51a95a8 Nikos Skalkotos
            exit 1
231 d51a95a8 Nikos Skalkotos
        fi
232 d51a95a8 Nikos Skalkotos
        VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
233 d51a95a8 Nikos Skalkotos
        if [ -f "$VARIANT_CONFIG" ]; then
234 d51a95a8 Nikos Skalkotos
            . "$VARIANT_CONFIG"
235 d51a95a8 Nikos Skalkotos
        else
236 d51a95a8 Nikos Skalkotos
            if grep -qxF "$OS_VARIANT" variants.list; then
237 d51a95a8 Nikos Skalkotos
                log_error "ERROR: instance-image configuration error"
238 23996b5d Nikos Skalkotos
                log_error "  Published variant $OS_VARIANT is missing its" \
239 23996b5d Nikos Skalkotos
                    "config file"
240 23996b5d Nikos Skalkotos
                log_error "  Please create $VARIANT_CONFIG or unpublish the" \
241 23996b5d Nikos Skalkotos
                    "variant"
242 d51a95a8 Nikos Skalkotos
                log_error "  (by removing $OS_VARIANT from variants.list)"
243 d51a95a8 Nikos Skalkotos
            else
244 d51a95a8 Nikos Skalkotos
                log_error "Unofficial variant $OS_VARIANT is unsupported"
245 23996b5d Nikos Skalkotos
                log_error "Most probably this is a user error, forcing a" \
246 23996b5d Nikos Skalkotos
                    "wrong name"
247 d51a95a8 Nikos Skalkotos
                log_error "To support this variant please create file" \
248 23996b5d Nikos Skalkotos
                    "$VARIANT_CONFIG"
249 d51a95a8 Nikos Skalkotos
            fi
250 d51a95a8 Nikos Skalkotos
            exit 1
251 d51a95a8 Nikos Skalkotos
        fi
252 d51a95a8 Nikos Skalkotos
    fi
253 d51a95a8 Nikos Skalkotos
254 d51a95a8 Nikos Skalkotos
}
255 d51a95a8 Nikos Skalkotos
256 448a829f Nikos Skalkotos
do_debootstrap() {
257 d259b27c Nikos Skalkotos
    local target="$1"
258 448a829f Nikos Skalkotos
259 448a829f Nikos Skalkotos
    echo "Debootstraping to create a new root filesystem:"
260 448a829f Nikos Skalkotos
261 448a829f Nikos Skalkotos
    # Create a policy-rc.d file to deny init script execution
262 448a829f Nikos Skalkotos
    mkdir -p "$target/usr/sbin"
263 448a829f Nikos Skalkotos
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
264 448a829f Nikos Skalkotos
#!/bin/sh
265 448a829f Nikos Skalkotos
exit 101
266 448a829f Nikos Skalkotos
EOF
267 448a829f Nikos Skalkotos
    chmod +x "$target/usr/sbin/policy-rc.d"
268 448a829f Nikos Skalkotos
269 448a829f Nikos Skalkotos
    debootstrap --arch $(dpkg --print-architecture) \
270 448a829f Nikos Skalkotos
        --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
271 448a829f Nikos Skalkotos
        "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
272 448a829f Nikos Skalkotos
273 d259b27c Nikos Skalkotos
    # Save the package list
274 d259b27c Nikos Skalkotos
    chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
275 d259b27c Nikos Skalkotos
276 448a829f Nikos Skalkotos
    rm "$target/usr/sbin/policy-rc.d"
277 448a829f Nikos Skalkotos
278 448a829f Nikos Skalkotos
    # remove the downloaded debs, as they are no longer needed
279 448a829f Nikos Skalkotos
    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
280 448a829f Nikos Skalkotos
        xargs -r0 rm -f
281 448a829f Nikos Skalkotos
282 448a829f Nikos Skalkotos
    local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
283 448a829f Nikos Skalkotos
    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
284 448a829f Nikos Skalkotos
        { rm "$tmp_cache"; false; }
285 448a829f Nikos Skalkotos
    # Overwrite the default cache file. Not the user specified if present.
286 448a829f Nikos Skalkotos
    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
287 448a829f Nikos Skalkotos
}
288 448a829f Nikos Skalkotos
289 f4bd39d8 Nikos Skalkotos
cleanup() {
290 d51a95a8 Nikos Skalkotos
# if something fails here, it souldn't call cleanup again...
291 d51a95a8 Nikos Skalkotos
    trap - EXIT
292 f4bd39d8 Nikos Skalkotos
    if [ ${#CLEANUP[*]} -gt 0 ]; then
293 f4bd39d8 Nikos Skalkotos
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
294 f4bd39d8 Nikos Skalkotos
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
295 f4bd39d8 Nikos Skalkotos
        for i in $REVERSE_INDEXES; do
296 d51a95a8 Nikos Skalkotos
            # If something fails here, it's better to retry it for a few times
297 d51a95a8 Nikos Skalkotos
            # before we give up with an error. This is needed for kpartx when
298 d51a95a8 Nikos Skalkotos
            # dealing with ntfs partitions mounted through fuse. umount is not
299 d51a95a8 Nikos Skalkotos
            # synchronous and may return while the partition is still busy. A
300 643d7283 Nikos Skalkotos
            # premature attempt to delete partition mappings through kpartx on
301 643d7283 Nikos Skalkotos
            # a device that hosts previously mounted ntfs partition may fail
302 643d7283 Nikos Skalkotos
            # with errors like this one:
303 643d7283 Nikos Skalkotos
            # `device-mapper: remove ioctl failed: Device or resource busy'
304 643d7283 Nikos Skalkotos
            # A sensible workaround for this is to wait for a while and then
305 643d7283 Nikos Skalkotos
            # retry it.
306 d51a95a8 Nikos Skalkotos
            local cmd=${CLEANUP[$i]}
307 d51a95a8 Nikos Skalkotos
            $cmd || for interval in 0.25 0.5 1 2 4; do
308 d51a95a8 Nikos Skalkotos
            echo "Command $cmd failed!"
309 d51a95a8 Nikos Skalkotos
            echo "I'll wait for $interval secs and will retry..."
310 d51a95a8 Nikos Skalkotos
            sleep $interval
311 d51a95a8 Nikos Skalkotos
            $cmd && break
312 f4bd39d8 Nikos Skalkotos
        done
313 9aac6898 Nikos Skalkotos
        if [ "$?" != "0" ]; then
314 9aac6898 Nikos Skalkotos
            echo "Giving Up..."
315 9aac6898 Nikos Skalkotos
            exit 1;
316 9aac6898 Nikos Skalkotos
        fi
317 d51a95a8 Nikos Skalkotos
    done
318 d51a95a8 Nikos Skalkotos
  fi
319 f4bd39d8 Nikos Skalkotos
}
320 f4bd39d8 Nikos Skalkotos
321 f4bd39d8 Nikos Skalkotos
trap cleanup EXIT
322 f4bd39d8 Nikos Skalkotos
323 f4bd39d8 Nikos Skalkotos
DEFAULT_FILE="@sysconfdir@/default/snf-image"
324 f4bd39d8 Nikos Skalkotos
if [ -f "$DEFAULT_FILE" ]; then
325 f4bd39d8 Nikos Skalkotos
    . "$DEFAULT_FILE"
326 f4bd39d8 Nikos Skalkotos
fi
327 f4bd39d8 Nikos Skalkotos
328 f4bd39d8 Nikos Skalkotos
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
329 f4bd39d8 Nikos Skalkotos
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
330 d51a95a8 Nikos Skalkotos
: ${HELPER_DIR:="@HELPER_DIR@"}
331 d51a95a8 Nikos Skalkotos
: ${HELPER_IMG:="@HELPER_IMG@"}
332 9999d40d Nikos Skalkotos
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
333 9999d40d Nikos Skalkotos
: ${HELPER_INITRD:="@HELPER_INITRD@"}
334 9aac6898 Nikos Skalkotos
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
335 bad5ca1f Nikos Skalkotos
: ${HELPER_SOFT_TIMEOUT:=15}
336 bad5ca1f Nikos Skalkotos
: ${HELPER_HARD_TIMEOUT:=5}
337 bad5ca1f Nikos Skalkotos
: ${HELPER_USER:="nobody"}
338 bad5ca1f Nikos Skalkotos
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
339 d259b27c Nikos Skalkotos
: ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
340 b126203e Nikos Skalkotos
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
341 5a31f7ef Nikos Skalkotos
: ${HELPER_MIRROR:=""}
342 d71f59e9 Nikos Skalkotos
: ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
343 198f8249 Nikos Skalkotos
: ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
344 b1fef76e Nikos Skalkotos
: ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
345 8689d50f Nikos Skalkotos
: ${UNATTEND:="@UNATTEND@"}
346 f4bd39d8 Nikos Skalkotos
347 f4bd39d8 Nikos Skalkotos
SCRIPT_NAME=$(basename $0)
348 f4bd39d8 Nikos Skalkotos
349 f4bd39d8 Nikos Skalkotos
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
350 f4bd39d8 Nikos Skalkotos
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
351 f4bd39d8 Nikos Skalkotos
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
352 f4bd39d8 Nikos Skalkotos
else
353 f4bd39d8 Nikos Skalkotos
    for dir in /lib/udev /sbin; do
354 f4bd39d8 Nikos Skalkotos
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
355 f4bd39d8 Nikos Skalkotos
            VOL_ID="$dir/vol_id -u"
356 f4bd39d8 Nikos Skalkotos
            VOL_TYPE="$dir/vol_id -t"
357 f4bd39d8 Nikos Skalkotos
        fi
358 f4bd39d8 Nikos Skalkotos
    done
359 f4bd39d8 Nikos Skalkotos
fi
360 f4bd39d8 Nikos Skalkotos
361 f4bd39d8 Nikos Skalkotos
if [ -z "$VOL_ID" ]; then
362 f4bd39d8 Nikos Skalkotos
    log_error "vol_id or blkid not found, please install udev or util-linux"
363 f4bd39d8 Nikos Skalkotos
    exit 1
364 f4bd39d8 Nikos Skalkotos
fi
365 f4bd39d8 Nikos Skalkotos
366 f4bd39d8 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :