snf-image-helper.in 3.14 KB
Newer Older
Nikos Skalkotos's avatar
Nikos Skalkotos committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash

# Copyright 2011 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of GRNET S.A.

Nikos Skalkotos's avatar
Nikos Skalkotos committed
32
. @commondir@/common.sh
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33

Nikos Skalkotos's avatar
Nikos Skalkotos committed
34
set -e
35
set -o pipefail
Nikos Skalkotos's avatar
Nikos Skalkotos committed
36

37
if [ "x$1" != "x--force" ]; then
38
39
    echo "WARNING: Exiting, this command would cause the system to halt." >&2
    echo "Use --force if you know what you're doing." >&2
40
41
42
    exit 1
fi

Nikos Skalkotos's avatar
Nikos Skalkotos committed
43
# terminate helper vm when the script exits
44
add_cleanup telinit 0
Nikos Skalkotos's avatar
Nikos Skalkotos committed
45

46
if [ ! -b "$FLOPPY_DEV" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
47
48
49
    log_error "Floppy device is not present!"
fi

50
51
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
add_cleanup rmdir "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
52

Nikos Skalkotos's avatar
Nikos Skalkotos committed
53
mount $FLOPPY_DEV $floppy
54
add_cleanup umount "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
55

56
57
if [ -f "$floppy/rules" ]; then
    source "$floppy/rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
58
else
Nikos Skalkotos's avatar
Nikos Skalkotos committed
59
    log_error "Floppy does not contain \`rules\' file"
60
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
61

Nikos Skalkotos's avatar
Nikos Skalkotos committed
62
# Image mount point...
63
64
target=$(mktemp -d --tmpdir target.XXXXXX)
add_cleanup rmdir "$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
65

66
export SNF_IMAGE_TARGET="$target"
67
export SNF_IMAGE_ROOTDEV="${SNF_IMAGE_DEV}${SNF_IMAGE_ROOT}"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
68

69
if [ ! -d "@tasksdir@" ]; then
70
71
    log_error "snf-image/tasks directory is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
72

73
74
RUN_PARTS=$(which run-parts)
if [ -z "$RUN_PARTS" ]; then
75
    log_error "run-parts program is missing from the system"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
76
77
fi

78
79
# If something goes wrong with the tasks, try to umount the target filesystem
# in case it is left mounted...
80
trap '{ umount "$target"; }' ERR
81

82
83
# Redirect standard error to standard output,
# prepend a timestamp before each line of output.
84
echo "Execute all snf-image tasks...."
85
86
87
88
$RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
    while IFS= read -r line; do
        echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
    done
89

90
91
92
# Disable the trap. If code reaches here, the filesystem is unmounted.
trap - ERR

93
echo "SUCCESS" > "$RESULT"
94

Nikos Skalkotos's avatar
Nikos Skalkotos committed
95
96
97
98
99
100
101
cleanup
trap - EXIT

# never called...
exit 0

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