Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.5 kB)

1 54080484 Nikos Skalkotos
#!/bin/bash
2 54080484 Nikos Skalkotos
3 54080484 Nikos Skalkotos
# Copyright 2011 GRNET S.A. All rights reserved.
4 54080484 Nikos Skalkotos
#
5 54080484 Nikos Skalkotos
# Redistribution and use in source and binary forms, with or without
6 54080484 Nikos Skalkotos
# modification, are permitted provided that the following conditions
7 54080484 Nikos Skalkotos
# are met:
8 54080484 Nikos Skalkotos
#
9 54080484 Nikos Skalkotos
#   1. Redistributions of source code must retain the above copyright
10 54080484 Nikos Skalkotos
#      notice, this list of conditions and the following disclaimer.
11 54080484 Nikos Skalkotos
#
12 54080484 Nikos Skalkotos
#  2. Redistributions in binary form must reproduce the above copyright
13 54080484 Nikos Skalkotos
#     notice, this list of conditions and the following disclaimer in the
14 54080484 Nikos Skalkotos
#     documentation and/or other materials provided with the distribution.
15 54080484 Nikos Skalkotos
#
16 54080484 Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 54080484 Nikos Skalkotos
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 54080484 Nikos Skalkotos
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 54080484 Nikos Skalkotos
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 54080484 Nikos Skalkotos
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 54080484 Nikos Skalkotos
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 54080484 Nikos Skalkotos
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 54080484 Nikos Skalkotos
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 54080484 Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 54080484 Nikos Skalkotos
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 54080484 Nikos Skalkotos
# SUCH DAMAGE.
27 54080484 Nikos Skalkotos
#
28 54080484 Nikos Skalkotos
# The views and conclusions contained in the software and documentation are
29 54080484 Nikos Skalkotos
# those of the authors and should not be interpreted as representing official
30 54080484 Nikos Skalkotos
# policies, either expressed or implied, of GRNET S.A.
31 54080484 Nikos Skalkotos
32 bf7c33b2 Nikos Skalkotos
. @commondir@/common.sh
33 54080484 Nikos Skalkotos
34 c349d1b3 Nikos Skalkotos
set -e
35 bad5ca1f Nikos Skalkotos
set -o pipefail
36 c349d1b3 Nikos Skalkotos
37 ff7783b8 Vangelis Koukis
if [ "x$1" != "x--force" ]; then
38 af4a3462 Vangelis Koukis
    echo "WARNING: Exiting, this command would cause the system to halt." >&2
39 af4a3462 Vangelis Koukis
    echo "Use --force if you know what you're doing." >&2
40 ff7783b8 Vangelis Koukis
    exit 1
41 ff7783b8 Vangelis Koukis
fi
42 ff7783b8 Vangelis Koukis
43 54080484 Nikos Skalkotos
# terminate helper vm when the script exits
44 bad5ca1f Nikos Skalkotos
add_cleanup telinit 0
45 54080484 Nikos Skalkotos
46 bad5ca1f Nikos Skalkotos
if [ ! -b "$FLOPPY_DEV" ]; then
47 c349d1b3 Nikos Skalkotos
    log_error "Floppy device is not present!"
48 c349d1b3 Nikos Skalkotos
fi
49 c349d1b3 Nikos Skalkotos
50 bad5ca1f Nikos Skalkotos
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
51 bad5ca1f Nikos Skalkotos
add_cleanup rmdir "$floppy"
52 54080484 Nikos Skalkotos
53 c349d1b3 Nikos Skalkotos
mount $FLOPPY_DEV $floppy
54 bad5ca1f Nikos Skalkotos
add_cleanup umount "$floppy"
55 c349d1b3 Nikos Skalkotos
56 bad5ca1f Nikos Skalkotos
if [ -f "$floppy/rules" ]; then
57 bad5ca1f Nikos Skalkotos
    source "$floppy/rules"
58 54080484 Nikos Skalkotos
else
59 c349d1b3 Nikos Skalkotos
    log_error "Floppy does not contain \`rules\' file"
60 c308b9f9 Nikos Skalkotos
fi
61 54080484 Nikos Skalkotos
62 473f4fa5 Nikos Skalkotos
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
63 473f4fa5 Nikos Skalkotos
    properties=$(mktemp --tmpdir properties.XXXXXX)
64 473f4fa5 Nikos Skalkotos
    add_cleanup rm "$properties"
65 38552c68 Nikos Skalkotos
    echo "$SNF_IMAGE_PROPERTIES" |
66 38552c68 Nikos Skalkotos
        "@scriptsdir@/decode-properties.py" "$properties"
67 473f4fa5 Nikos Skalkotos
    source "$properties"
68 473f4fa5 Nikos Skalkotos
else
69 473f4fa5 Nikos Skalkotos
    log_error "SNF_IMAGE_PROPERTIES variable is missing"
70 473f4fa5 Nikos Skalkotos
fi
71 473f4fa5 Nikos Skalkotos
72 c349d1b3 Nikos Skalkotos
# Image mount point...
73 bad5ca1f Nikos Skalkotos
target=$(mktemp -d --tmpdir target.XXXXXX)
74 bad5ca1f Nikos Skalkotos
add_cleanup rmdir "$target"
75 54080484 Nikos Skalkotos
76 bad5ca1f Nikos Skalkotos
export SNF_IMAGE_TARGET="$target"
77 54080484 Nikos Skalkotos
78 27b1de98 Nikos Skalkotos
if [ ! -d "@tasksdir@" ]; then
79 1bb3e009 Nikos Skalkotos
    log_error "snf-image/tasks directory is missing"
80 1bb3e009 Nikos Skalkotos
fi
81 c349d1b3 Nikos Skalkotos
82 1bb3e009 Nikos Skalkotos
RUN_PARTS=$(which run-parts)
83 1bb3e009 Nikos Skalkotos
if [ -z "$RUN_PARTS" ]; then
84 cc9de6c0 Vangelis Koukis
    log_error "run-parts program is missing from the system"
85 54080484 Nikos Skalkotos
fi
86 54080484 Nikos Skalkotos
87 0468a748 Nikos Skalkotos
# If something goes wrong with the tasks, try to umount the target filesystem
88 0468a748 Nikos Skalkotos
# in case it is left mounted...
89 bad5ca1f Nikos Skalkotos
trap '{ umount "$target"; }' ERR
90 0468a748 Nikos Skalkotos
91 e50c4112 Nikos Skalkotos
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
92 e7cbe965 Nikos Skalkotos
    # Redirect standard error to standard output,
93 e7cbe965 Nikos Skalkotos
    # prepend a timestamp before each line of output.
94 e7cbe965 Nikos Skalkotos
    echo "Execute all snf-image tasks...."
95 e7cbe965 Nikos Skalkotos
    $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
96 e7cbe965 Nikos Skalkotos
        while IFS= read -r line; do
97 e7cbe965 Nikos Skalkotos
            echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
98 e7cbe965 Nikos Skalkotos
        done
99 e7cbe965 Nikos Skalkotos
fi
100 1bb3e009 Nikos Skalkotos
101 cb6b4f68 Nikos Skalkotos
# Disable the trap. If code reaches here, the filesystem is unmounted.
102 cb6b4f68 Nikos Skalkotos
trap - ERR
103 cb6b4f68 Nikos Skalkotos
104 bad5ca1f Nikos Skalkotos
echo "SUCCESS" > "$RESULT"
105 7f6bd8e0 Nikos Skalkotos
106 54080484 Nikos Skalkotos
cleanup
107 54080484 Nikos Skalkotos
trap - EXIT
108 54080484 Nikos Skalkotos
109 54080484 Nikos Skalkotos
# never called...
110 54080484 Nikos Skalkotos
exit 0
111 54080484 Nikos Skalkotos
112 54080484 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :