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

3
# Copyright (C) 2011-2015 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

20
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
Nikos Skalkotos's avatar
Nikos Skalkotos committed
21

22
if [ $$ -eq 1 ]; then
23
24
    #mount / -o remount
    mount -t tmpfs -o size=20m tmpfs /tmp
25
    /etc/init.d/udev start
26
    #hwclock -u -s
Nikos Skalkotos's avatar
Nikos Skalkotos committed
27

28
29
30
    (exec $0) &
    wait
    exit 0 # Hopefully this is never called...
31
32
fi

33
34
export PATH

Nikos Skalkotos's avatar
Nikos Skalkotos committed
35
. @commondir@/common.sh
Nikos Skalkotos's avatar
Nikos Skalkotos committed
36

Nikos Skalkotos's avatar
Nikos Skalkotos committed
37
38
set -e

39
40
41
42
# Enable errtrace to make functions inherit the ERR trap
set -o errtrace

trap report_error ERR
43

44
if grep snf_image_activate_helper /proc/cmdline > /dev/null; then
45
    # terminate helper VM when the script exits
46
47
48
49
50
    add_cleanup system_poweroff
else
    log_error "Kernel command line activation flag: " \
              "\`snf_image_activate_helper' is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
51

Nikos Skalkotos's avatar
Nikos Skalkotos committed
52
53
prepare_helper

54
55
if [ ! -b "$RULES_DEV" ]; then
    log_error "Device file hosting the rules file: \`$RULES_DEV' does not exist"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
56
57
fi

58
59
rules=$(mktemp -d --tmpdir rules.XXXXXX)
add_cleanup rmdir "$rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
60

61
62
$MOUNT $RULES_DEV $rules
add_cleanup umount "$rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
63

64
65
if [ -f "$rules/rules" ]; then
    source "$rules/rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
66
else
67
    log_error "$RULES_DEV does not contain \`rules\' file"
68
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
69

Nikos Skalkotos's avatar
Nikos Skalkotos committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Export SNF_IMAGE_DEV. This is an array and exporting arrays is not supported
# in bash. We need to encode it's values to multiple variables
encode_array SNF_IMAGE_DEV

for ((i = 0; i < SNF_IMAGE_DEV_COUNT; i++)); do
    export "SNF_IMAGE_DEV_$i"
done
export SNF_IMAGE_DEV_COUNT

# For backwards compatibility set the value of the SNF_IMAGE_DEV variable to
# the first value of the array
unset SNF_IMAGE_DEV
export SNF_IMAGE_DEV=${SNF_IMAGE_DEV_0}


85
86
if [ -f "$rules/unattend.xml" ]; then
    export SNF_IMAGE_UNATTEND="$rules/unattend.xml"
87
88
fi

89
90
91
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
    properties=$(mktemp --tmpdir properties.XXXXXX)
    add_cleanup rm "$properties"
92
93
94
95
96
97
    if ! echo "$SNF_IMAGE_PROPERTIES" | \
        "@scriptsdir@/decode-properties.py" "$properties"; then

        log_error "Unable to decode image properties. " \
             "Please check if the variable is in valid json format."
    fi
98
99
100
    source "$properties"
fi

Nikos Skalkotos's avatar
Nikos Skalkotos committed
101
# Image mount point...
102
103
target=$(mktemp -d --tmpdir target.XXXXXX)
add_cleanup rmdir "$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
104

105
export SNF_IMAGE_TARGET="$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
106

107
if [ ! -d "@tasksdir@" ]; then
108
109
    log_error "snf-image/tasks directory is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
110

111
112
RUN_PARTS=$(which run-parts)
if [ -z "$RUN_PARTS" ]; then
113
    log_error "run-parts program is missing from the system"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
114
115
fi

116
if ! check_yes_no SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS; then
117

118
119
120
121
122
    if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" -o "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
        warn "Needed image properties are missing. Autodetecting them..."
        detect_image_properties "$SNF_IMAGE_DEV"

        if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" ]; then
123
            warn "Detected OSFAMILY: $DETECTED_OSFAMILY"
124
125
126
127
            export SNF_IMAGE_PROPERTY_OSFAMILY="$DETECTED_OSFAMILY"
        fi

        if [ "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
128
            warn "Detected ROOT_PARTITION: $DETECTED_ROOT_PARTITION"
129
130
131
132
            export SNF_IMAGE_PROPERTY_ROOT_PARTITION="$DETECTED_ROOT_PARTITION"
        fi
    fi

133
134
135
136
137
138
139
140
    if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" ]; then
        log_error "Required image property \`OSFAMILY' is missing or empty."
    fi

    if [ "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
        log_error "Required image property \`ROOT_PARTITION' is missing or empty."
    fi

141
142
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows|windows-legacy|freebsd|openbsd|netbsd)$ ]]; then
        log_error "Supported values for OSFAMILY property are: linux|windows|windows-legacy|freebsd|openbsd|netbsd"
143
144
    fi

145
146
147
148
149
150
151
    SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"
    if [ -z "$SNF_IMAGE_RESIZE_PART" ]; then
        exit 0
    fi

    export SNF_IMAGE_RESIZE_PART

152
153
154
    # If something goes wrong with the tasks, try to umount the disk file
    # systems that are still mounted.
    trap '{ umount_all "$target"; }' ERR
155

156
157
158
159
    # 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|
160
        while read -r line; do
161
            echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
162
        done
163

164
    # Reset the handler to its original value
165
    trap report_error ERR
166
else
167
    warn "EXCLUDE_ALL_TASKS is set. All configuration tasks were prevented from running."
168
fi
169

170

171
return_success
172

Nikos Skalkotos's avatar
Nikos Skalkotos committed
173
174
175
176
177
178
179
cleanup
trap - EXIT

# never called...
exit 0

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