Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / snf-image-helper.in @ c334f836

History | View | Annotate | Download (2.8 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
. @commondir@/common.sh
21

    
22
set -e
23
set -o pipefail
24

    
25
if [ "x$1" != "x--force" ]; then
26
    echo "WARNING: Exiting, this command would cause the system to halt." >&2
27
    echo "Use --force if you know what you're doing." >&2
28
    exit 1
29
fi
30

    
31
# terminate helper vm when the script exits
32
add_cleanup telinit 0
33

    
34
if [ ! -b "$FLOPPY_DEV" ]; then
35
    log_error "Floppy device is not present!"
36
fi
37

    
38
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
39
add_cleanup rmdir "$floppy"
40

    
41
mount $FLOPPY_DEV $floppy
42
add_cleanup umount "$floppy"
43

    
44
if [ -f "$floppy/rules" ]; then
45
    source "$floppy/rules"
46
else
47
    log_error "Floppy does not contain \`rules\' file"
48
fi
49

    
50
if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
51
    properties=$(mktemp --tmpdir properties.XXXXXX)
52
    add_cleanup rm "$properties"
53
    echo "$SNF_IMAGE_PROPERTIES" |
54
        "@scriptsdir@/decode-properties.py" "$properties"
55
    source "$properties"
56
fi
57

    
58
# Image mount point...
59
target=$(mktemp -d --tmpdir target.XXXXXX)
60
add_cleanup rmdir "$target"
61

    
62
export SNF_IMAGE_TARGET="$target"
63

    
64
if [ ! -d "@tasksdir@" ]; then
65
    log_error "snf-image/tasks directory is missing"
66
fi
67

    
68
RUN_PARTS=$(which run-parts)
69
if [ -z "$RUN_PARTS" ]; then
70
    log_error "run-parts program is missing from the system"
71
fi
72

    
73
# If something goes wrong with the tasks, try to umount the target filesystem
74
# in case it is left mounted...
75
trap '{ umount "$target"; }' ERR
76

    
77
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
78

    
79
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
80
        log_error "Supported values for OSFAMILY propertry are: linux|windows"
81
    fi
82

    
83
    # Redirect standard error to standard output,
84
    # prepend a timestamp before each line of output.
85
    echo "Execute all snf-image tasks...."
86
    $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
87
        while IFS= read -r line; do
88
            echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
89
        done
90
fi
91

    
92
# Disable the trap. If code reaches here, the filesystem is unmounted.
93
trap - ERR
94

    
95
echo "SUCCESS" > "$RESULT"
96

    
97
cleanup
98
trap - EXIT
99

    
100
# never called...
101
exit 0
102

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