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

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
45
46
47
48
49
50
if grep snf_image_activate_helper /proc/cmdline > /dev/null; then
    # terminate helper vm when the script exits
    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

102
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
103

104
105
106
107
108
109
110
111
    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

112
113
    export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"

114
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
115
        log_error "Supported values for OSFAMILY property are: linux|windows"
116
117
    fi

118
119
120
    # If something goes wrong with the tasks, try to umount the disk file
    # systems that are still mounted.
    trap '{ umount_all "$target"; }' ERR
121

122
123
124
125
    # 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|
126
        while read -r line; do
127
            echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
128
        done
129

130
    # Reset the handler to its original value
131
    trap report_error ERR
132
fi
133

134

135
return_success
136

Nikos Skalkotos's avatar
Nikos Skalkotos committed
137
138
139
140
141
142
143
cleanup
trap - EXIT

# never called...
exit 0

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