Bump version to 0.10.1
[snf-image] / snf-image-helper / snf-image-helper.in
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     export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"
113
114     if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows|freebsd)$ ]]; then
115         log_error "Supported values for OSFAMILY property are: linux|windows|freebsd"
116     fi
117
118     # If something goes wrong with the tasks, try to umount the disk file
119     # systems that are still mounted.
120     trap '{ umount_all "$target"; }' ERR
121
122     # Redirect standard error to standard output,
123     # prepend a timestamp before each line of output.
124     echo "Execute all snf-image tasks...."
125     $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
137 cleanup
138 trap - EXIT
139
140 # never called...
141 exit 0
142
143 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :