snf-image-helper.in 2.93 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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
20
. @commondir@/common.sh
Nikos Skalkotos's avatar
Nikos Skalkotos committed
21

Nikos Skalkotos's avatar
Nikos Skalkotos committed
22
23
set -e

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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
30
# terminate helper vm when the script exits
31
add_cleanup telinit 0
Nikos Skalkotos's avatar
Nikos Skalkotos committed
32

33
if [ ! -b "$FLOPPY_DEV" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
34
35
36
    log_error "Floppy device is not present!"
fi

37
38
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
add_cleanup rmdir "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
39

Nikos Skalkotos's avatar
Nikos Skalkotos committed
40
mount $FLOPPY_DEV $floppy
41
add_cleanup umount "$floppy"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
42

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

49
50
51
52
if [ -f "$floppy/unattend.xml" ]; then
    export SNF_IMAGE_UNATTEND="$floppy/unattend.xml"
fi

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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
61
# Image mount point...
62
63
target=$(mktemp -d --tmpdir target.XXXXXX)
add_cleanup rmdir "$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
64

65
export SNF_IMAGE_TARGET="$target"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
66

67
if [ ! -d "@tasksdir@" ]; then
68
69
    log_error "snf-image/tasks directory is missing"
fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
70

71
72
RUN_PARTS=$(which run-parts)
if [ -z "$RUN_PARTS" ]; then
73
    log_error "run-parts program is missing from the system"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
74
75
fi

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

80
if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
81

82
83
    export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"

84
    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
85
        log_error "Supported values for OSFAMILY property are: linux|windows"
86
87
    fi

88
89
90
91
92
93
94
95
    # 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
            echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
        done
fi
96

97
98
99
# Disable the trap. If code reaches here, the filesystem is unmounted.
trap - ERR

100
return_success
101

Nikos Skalkotos's avatar
Nikos Skalkotos committed
102
103
104
105
106
107
108
cleanup
trap - EXIT

# never called...
exit 0

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