snf-image-helper.in 4.66 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

70
71
if [ -f "$rules/unattend.xml" ]; then
    export SNF_IMAGE_UNATTEND="$rules/unattend.xml"
72
73
fi

74
75
76
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
    properties=$(mktemp --tmpdir properties.XXXXXX)
    add_cleanup rm "$properties"
77
78
79
80
81
82
    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
83
84
85
    source "$properties"
fi

Nikos Skalkotos's avatar
Nikos Skalkotos committed
86
# Image mount point...
87
88
target=$(mktemp -d --tmpdir target.XXXXXX)
add_cleanup rmdir "$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
89

90
export SNF_IMAGE_TARGET="$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
91

92
if [ ! -d "@tasksdir@" ]; then
93
94
    log_error "snf-image/tasks directory is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
95

96
97
RUN_PARTS=$(which run-parts)
if [ -z "$RUN_PARTS" ]; then
98
    log_error "run-parts program is missing from the system"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
99
100
fi

101
if ! check_yes_no SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS; then
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    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
            echo "Detected OSFAMILY: $DETECTED_OSFAMILY"
            export SNF_IMAGE_PROPERTY_OSFAMILY="$DETECTED_OSFAMILY"
        fi

        if [ "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
            echo "Detected ROOT_PARTITION: $DETECTED_ROOT_PARTITION"
            export SNF_IMAGE_PROPERTY_ROOT_PARTITION="$DETECTED_ROOT_PARTITION"
        fi
    fi

118
119
120
121
122
123
124
125
    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

126
127
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows|freebsd|openbsd|netbsd)$ ]]; then
        log_error "Supported values for OSFAMILY property are: linux|windows|freebsd|openbsd|netbsd"
128
129
    fi

130
131
132
133
134
135
136
    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

137
138
139
    # If something goes wrong with the tasks, try to umount the disk file
    # systems that are still mounted.
    trap '{ umount_all "$target"; }' ERR
140

141
142
143
144
    # 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|
145
        while read -r line; do
146
            echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
147
        done
148

149
    # Reset the handler to its original value
150
    trap report_error ERR
151
else
152
    warn "EXCLUDE_ALL_TASKS is set. All configuration tasks were prevented from running."
153
fi
154

155

156
return_success
157

Nikos Skalkotos's avatar
Nikos Skalkotos committed
158
159
160
161
162
163
164
cleanup
trap - EXIT

# never called...
exit 0

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