Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / snf-image-helper.in @ 1bda0902

History | View | Annotate | Download (3.9 kB)

1
#!/bin/bash
2

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

    
20
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
21

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

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

    
33
export PATH
34

    
35
. @commondir@/common.sh
36

    
37
set -e
38

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

    
42
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
    add_cleanup system_poweroff
47
else
48
    log_error "Kernel command line activation flag: " \
49
              "\`snf_image_activate_helper' is missing"
50
fi
51

    
52
prepare_helper
53

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

    
58
rules=$(mktemp -d --tmpdir rules.XXXXXX)
59
add_cleanup rmdir "$rules"
60

    
61
$MOUNT $RULES_DEV $rules
62
add_cleanup umount "$rules"
63

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

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

    
74
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
75
    properties=$(mktemp --tmpdir properties.XXXXXX)
76
    add_cleanup rm "$properties"
77
    if ! echo "$SNF_IMAGE_PROPERTIES" | \
78
        "@scriptsdir@/decode-properties.py" "$properties"; then
79

    
80
        log_error "Unable to decode image properties. " \
81
             "Please check if the variable is in valid json format."
82
    fi
83
    source "$properties"
84
fi
85

    
86
# Image mount point...
87
target=$(mktemp -d --tmpdir target.XXXXXX)
88
add_cleanup rmdir "$target"
89

    
90
export SNF_IMAGE_TARGET="$target"
91

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

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

    
101

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

    
104
    if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" ]; then
105
        log_error "Required image property \`OSFAMILY' is missing or empty."
106
    fi
107

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

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

    
116
    SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"
117
    if [ -z "$SNF_IMAGE_RESIZE_PART" ]; then
118
        exit 0
119
    fi
120

    
121
    export SNF_IMAGE_RESIZE_PART
122

    
123
    # If something goes wrong with the tasks, try to umount the disk file
124
    # systems that are still mounted.
125
    trap '{ umount_all "$target"; }' ERR
126

    
127
    # Redirect standard error to standard output,
128
    # prepend a timestamp before each line of output.
129
    echo "Execute all snf-image tasks...."
130
    $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
131
        while read -r line; do
132
            echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
133
        done
134

    
135
    # Reset the handler to its original value
136
    trap report_error ERR
137
fi
138

    
139

    
140
return_success
141

    
142
cleanup
143
trap - EXIT
144

    
145
# never called...
146
exit 0
147

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