snf-image-helper.in 2.69 KB
Newer Older
Nikos Skalkotos's avatar
Nikos Skalkotos committed
1
2
#!/bin/bash

Nikos Skalkotos's avatar
Nikos Skalkotos committed
3
# Copyright (C) 2011 GRNET S.A. 
Nikos Skalkotos's avatar
Nikos Skalkotos committed
4
#
Nikos Skalkotos's avatar
Nikos Skalkotos committed
5
6
7
8
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
Nikos Skalkotos's avatar
Nikos Skalkotos committed
9
#
Nikos Skalkotos's avatar
Nikos Skalkotos committed
10
11
12
13
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
Nikos Skalkotos's avatar
Nikos Skalkotos committed
14
#
Nikos Skalkotos's avatar
Nikos Skalkotos committed
15
16
17
18
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
Nikos Skalkotos's avatar
Nikos Skalkotos committed
19

Nikos Skalkotos's avatar
Nikos Skalkotos committed
20
. @commondir@/common.sh
Nikos Skalkotos's avatar
Nikos Skalkotos committed
21

Nikos Skalkotos's avatar
Nikos Skalkotos committed
22
set -e
23
set -o pipefail
Nikos Skalkotos's avatar
Nikos Skalkotos committed
24

25
if [ "x$1" != "x--force" ]; then
26
27
    echo "WARNING: Exiting, this command would cause the system to halt." >&2
    echo "Use --force if you know what you're doing." >&2
28
29
30
    exit 1
fi

Nikos Skalkotos's avatar
Nikos Skalkotos committed
31
# terminate helper vm when the script exits
32
add_cleanup telinit 0
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33

34
if [ ! -b "$FLOPPY_DEV" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
35
36
37
    log_error "Floppy device is not present!"
fi

38
39
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
add_cleanup rmdir "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
40

Nikos Skalkotos's avatar
Nikos Skalkotos committed
41
mount $FLOPPY_DEV $floppy
42
add_cleanup umount "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
43

44
45
if [ -f "$floppy/rules" ]; then
    source "$floppy/rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
46
else
Nikos Skalkotos's avatar
Nikos Skalkotos committed
47
    log_error "Floppy does not contain \`rules\' file"
48
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
49

50
51
52
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
    properties=$(mktemp --tmpdir properties.XXXXXX)
    add_cleanup rm "$properties"
53
54
    echo "$SNF_IMAGE_PROPERTIES" |
        "@scriptsdir@/decode-properties.py" "$properties"
55
56
57
58
59
    source "$properties"
else
    log_error "SNF_IMAGE_PROPERTIES variable is missing"
fi

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

64
export SNF_IMAGE_TARGET="$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
65

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

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

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

79
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
80
81
82
83
84
85
86
87
    # Redirect standard error to standard output,
    # prepend a timestamp before each line of output.
    echo "Execute all snf-image tasks...."
    $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
fi
88

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

92
echo "SUCCESS" > "$RESULT"
93

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

# never called...
exit 0

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