Revision d51a95a8 snf-image-host/common.sh.in

b/snf-image-host/common.sh.in
183 183
    umount $target
184 184
}
185 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

  
186 231
cleanup() {
232
# if something fails here, it souldn't call cleanup again...
233
    trap - EXIT
187 234
    if [ ${#CLEANUP[*]} -gt 0 ]; then
188 235
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
189 236
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
190 237
        for i in $REVERSE_INDEXES; do
191
            ${CLEANUP[$i]}
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
192 253
        done
193
    fi
254
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
255
    done
256
  fi
194 257
}
195 258

  
196 259
trap cleanup EXIT
......
203 266
: ${ARCH:="x86_64"}
204 267
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
205 268
: ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
206
: ${HELPER:="@HELPER@"}
269
: ${HELPER_DIR:="@HELPER_DIR@"}
270
: ${HELPER_IMG:="@HELPER_IMG@"}
207 271
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
208 272
: ${HELPER_INITRD:="@HELPER_INITRD@"}
209 273
: ${HELPER_SOFT_TIMEOUT=120}
210 274
: ${HELPER_HARD_TIMEOUT=15}
211
: ${HELPER_USR="nobody"}
275
: ${HELPER_USER="nobody"}
212 276

  
213 277
SCRIPT_NAME=$(basename $0)
214 278

  
......
229 293
    exit 1
230 294
fi
231 295

  
232

  
233
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
234
    OS_API_VERSION=5
235
    GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
236
    if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
237
    get_api5_arguments $GETOPT_RESULT
238
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
239
    get_api10_arguments
240
elif [ "$OS_API_VERSION" = "20" ]; then
241
    get_api20_arguments
242
    IMAGE_NAME=$IMG_ID
243
    IMAGE_TYPE=$IMG_FORMAT
244
else
245
    log_error "Unknown OS API VERSION $OS_API_VERSION"
246
    exit 1
247
fi
248

  
249
if [ -n "$OS_VARIANT" ]; then
250
    if [ ! -d "$VARIANTS_DIR" ]; then
251
        log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
252
        exit 1
253
    fi
254
    VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
255
    if [ -f "$VARIANT_CONFIG" ]; then
256
        . "$VARIANT_CONFIG"
257
    else
258
        if grep -qxF "$OS_VARIANT" variants.list; then
259
            log_error "ERROR: instance-image configuration error"
260
            log_error "  Published variant $OS_VARIANT is missing its config" \
261
                      "file"
262
            log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
263
            log_error "  (by removing $OS_VARIANT from variants.list)"
264
        else
265
            log_error "Unofficial variant $OS_VARIANT is unsupported"
266
            log_error "Most probably this is a user error, forcing a wrong name"
267
            log_error "To support this variant please create file" \
268
                        "$VARIANT_CONFIG"
269
        fi
270
        exit 1
271
    fi
272
fi
273

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

Also available in: Unified diff