snf-image-helper.in 3.18 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
23
24
25
if [ $$ -eq 1 ]; then
    mount / -o remount
    /etc/init.d/udev start
    hwclock -u -s
Nikos Skalkotos's avatar
Nikos Skalkotos committed
26

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

32
33
34
35
. @commondir@/common.sh

set -e

36
37
38
39
40
41
42
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
43

44
if [ ! -b "$FLOPPY_DEV" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
45
46
47
    log_error "Floppy device is not present!"
fi

48
49
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
add_cleanup rmdir "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
50

Nikos Skalkotos's avatar
Nikos Skalkotos committed
51
mount $FLOPPY_DEV $floppy
52
add_cleanup umount "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
53

54
55
if [ -f "$floppy/rules" ]; then
    source "$floppy/rules"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
56
else
Nikos Skalkotos's avatar
Nikos Skalkotos committed
57
    log_error "Floppy does not contain \`rules\' file"
58
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
59

60
61
62
63
if [ -f "$floppy/unattend.xml" ]; then
    export SNF_IMAGE_UNATTEND="$floppy/unattend.xml"
fi

64
65
66
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
    properties=$(mktemp --tmpdir properties.XXXXXX)
    add_cleanup rm "$properties"
67
68
    echo "$SNF_IMAGE_PROPERTIES" |
        "@scriptsdir@/decode-properties.py" "$properties"
69
70
71
    source "$properties"
fi

Nikos Skalkotos's avatar
Nikos Skalkotos committed
72
# Image mount point...
73
74
target=$(mktemp -d --tmpdir target.XXXXXX)
add_cleanup rmdir "$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
75

76
export SNF_IMAGE_TARGET="$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
77

78
if [ ! -d "@tasksdir@" ]; then
79
80
    log_error "snf-image/tasks directory is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
81

82
83
RUN_PARTS=$(which run-parts)
if [ -z "$RUN_PARTS" ]; then
84
    log_error "run-parts program is missing from the system"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
85
86
fi

87
88
# If something goes wrong with the tasks, try to umount the target filesystem
# in case it is left mounted...
89
trap '{ umount "$target"; }' ERR
90

91
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
92

93
94
    export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"

95
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
96
        log_error "Supported values for OSFAMILY property are: linux|windows"
97
98
    fi

99
100
101
102
103
    # 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|
        while IFS= read -r line; do
104
            echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
105
106
        done
fi
107

108
109
110
# Disable the trap. If code reaches here, the filesystem is unmounted.
trap - ERR

111
echo "SUCCESS" > "$RESULT"
112

Nikos Skalkotos's avatar
Nikos Skalkotos committed
113
114
115
116
117
118
119
cleanup
trap - EXIT

# never called...
exit 0

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