Update ChangeLog and configure.ac for ver. 0.7.4
[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     /etc/init.d/udev start
25     hwclock -u -s
26
27     (exec $0) &
28     wait
29     exit 0 # Hopefully this is never called...
30 fi
31
32 . @commondir@/common.sh
33
34 set -e
35
36 # Enable errtrace to make functions inherit the ERR trap
37 set -o errtrace
38
39 trap report_error ERR
40
41 if grep snf_image_activate_helper /proc/cmdline > /dev/null; then
42     # terminate helper vm when the script exits
43     add_cleanup system_poweroff
44 else
45     log_error "Kernel command line activation flag: " \
46               "\`snf_image_activate_helper' is missing"
47 fi
48
49 if [ ! -b "$FLOPPY_DEV" ]; then
50     log_error "Floppy device is not present!"
51 fi
52
53 floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
54 add_cleanup rmdir "$floppy"
55
56 mount $FLOPPY_DEV $floppy
57 add_cleanup umount "$floppy"
58
59 if [ -f "$floppy/rules" ]; then
60     source "$floppy/rules"
61 else
62     log_error "Floppy does not contain \`rules\' file"
63 fi
64
65 if [ -f "$floppy/unattend.xml" ]; then
66     export SNF_IMAGE_UNATTEND="$floppy/unattend.xml"
67 fi
68
69 if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
70     properties=$(mktemp --tmpdir properties.XXXXXX)
71     add_cleanup rm "$properties"
72     if ! echo "$SNF_IMAGE_PROPERTIES" | \
73         "@scriptsdir@/decode-properties.py" "$properties"; then
74
75         log_error "Unable to decode image properties. " \
76              "Please check if the variable is in valid json format."
77     fi
78     source "$properties"
79 fi
80
81 # Image mount point...
82 target=$(mktemp -d --tmpdir target.XXXXXX)
83 add_cleanup rmdir "$target"
84
85 export SNF_IMAGE_TARGET="$target"
86
87 if [ ! -d "@tasksdir@" ]; then
88     log_error "snf-image/tasks directory is missing"
89 fi
90
91 RUN_PARTS=$(which run-parts)
92 if [ -z "$RUN_PARTS" ]; then
93     log_error "run-parts program is missing from the system"
94 fi
95
96
97 if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
98
99     if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "" ]; then
100         log_error "Required image property \`OSFAMILY' is missing or empty."
101     fi
102
103     if [ "$SNF_IMAGE_PROPERTY_ROOT_PARTITION" = "" ]; then
104         log_error "Required image property \`ROOT_PARTITION' is missing or empty."
105     fi
106
107     export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"
108
109     if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
110         log_error "Supported values for OSFAMILY property are: linux|windows"
111     fi
112
113     # If something goes wrong with the tasks, try to umount the disk file
114     # systems that are still mounted.
115     trap '{ umount_all "$target"; }' ERR
116
117     # Redirect standard error to standard output,
118     # prepend a timestamp before each line of output.
119     echo "Execute all snf-image tasks...."
120     $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
121         while read -r line; do
122             echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
123         done
124
125     # Reset the handler to its original value
126     trap report_error ERR
127 fi
128
129
130 echo "SUCCESS" > "$RESULT"
131
132 cleanup
133 trap - EXIT
134
135 # never called...
136 exit 0
137
138 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :