Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / snf-image-helper.in @ 2091e53b

History | View | Annotate | Download (3.5 kB)

1
#!/bin/bash
2

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

    
32
. @commondir@/common.sh
33

    
34
set -e
35
set -o pipefail
36

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

    
43
# terminate helper vm when the script exits
44
add_cleanup telinit 0
45

    
46
if [ ! -b "$FLOPPY_DEV" ]; then
47
    log_error "Floppy device is not present!"
48
fi
49

    
50
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
51
add_cleanup rmdir "$floppy"
52

    
53
mount $FLOPPY_DEV $floppy
54
add_cleanup umount "$floppy"
55

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

    
62
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
63
    properties=$(mktemp --tmpdir properties.XXXXXX)
64
    add_cleanup rm "$properties"
65
    echo "$SNF_IMAGE_PROPERTIES" |
66
        "@scriptsdir@/decode-properties.py" "$properties"
67
    source "$properties"
68
else
69
    log_error "SNF_IMAGE_PROPERTIES variable is missing"
70
fi
71

    
72
# Image mount point...
73
target=$(mktemp -d --tmpdir target.XXXXXX)
74
add_cleanup rmdir "$target"
75

    
76
export SNF_IMAGE_TARGET="$target"
77

    
78
if [ ! -d "@tasksdir@" ]; then
79
    log_error "snf-image/tasks directory is missing"
80
fi
81

    
82
RUN_PARTS=$(which run-parts)
83
if [ -z "$RUN_PARTS" ]; then
84
    log_error "run-parts program is missing from the system"
85
fi
86

    
87
# If something goes wrong with the tasks, try to umount the target filesystem
88
# in case it is left mounted...
89
trap '{ umount "$target"; }' ERR
90

    
91
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
92
    # Redirect standard error to standard output,
93
    # prepend a timestamp before each line of output.
94
    echo "Execute all snf-image tasks...."
95
    $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
96
        while IFS= read -r line; do
97
            echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
98
        done
99
fi
100

    
101
# Disable the trap. If code reaches here, the filesystem is unmounted.
102
trap - ERR
103

    
104
echo "SUCCESS" > "$RESULT"
105

    
106
cleanup
107
trap - EXIT
108

    
109
# never called...
110
exit 0
111

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