Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / common.sh @ e50c4112

History | View | Annotate | Download (5.4 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

    
30
RESULT=/dev/ttyS1
31
FLOPPY_DEV=/dev/fd0
32
PROGNAME=$(basename $0)
33

    
34
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
35

    
36
# Programs
37
XMLSTARLET=xmlstarlet
38
RESIZE2FS=resize2fs
39
PARTED=parted
40

    
41
CLEANUP=( )
42

    
43
add_cleanup() {
44
    local cmd=""
45
    for arg; do cmd+=$(printf "%q " "$arg"); done
46
    CLEANUP+=("$cmd")
47
}
48

    
49
log_error() {
50
    echo "ERROR: $@" | tee $RESULT >&2
51
    exit 1
52
}
53

    
54
warn() {
55
    echo "Warning: $@" >&2
56
}
57

    
58
get_base_distro() {
59
    local root_dir=$1
60

    
61
    if [ -e "$root_dir/etc/debian_version" ]; then
62
        echo "debian"
63
    elif [ -e "$root_dir/etc/redhat-release" ]; then
64
        echo "redhat"
65
    elif [ -e "$root_dir/etc/slackware-version" ]; then
66
        echo "slackware"
67
    elif [ -e "$root_dir/SuSE-release" ]; then
68
        echo "suse"
69
    elif [ -e "$root_dir/gentoo-release" ]; then
70
        echo "gentoo"
71
    fi
72
}
73

    
74
get_distro() {
75
    local root_dir=$1
76

    
77
    if [ -e "$root_dir/etc/debian_version" ]; then
78
        distro="debian"
79
        if [ -e ${root_dir}/etc/lsb-release ]; then
80
            ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
81
            if [ "x$ID" = "xUbuntu" ]; then
82
                distro="ubuntu"
83
            fi
84
        fi
85
        echo "$distro"
86
    elif [ -e "$root_dir/etc/fedora-release" ]; then
87
        echo "fedora"
88
    elif [ -e "$root_dir/etc/centos-release" ]; then
89
        echo "centos"
90
    elif [ -e "$root_dir/etc/redhat-release" ]; then
91
        echo "redhat"
92
    elif [ -e "$root_dir/etc/slackware-version" ]; then
93
        echo "slackware"
94
    elif [ -e "$root_dir/SuSE-release" ]; then
95
        echo "suse"
96
    elif [ -e "$root_dir/gentoo-release" ]; then
97
        echo "gentoo"
98
    fi
99
}
100

    
101
get_last_partition() {
102
    local dev="$1"
103

    
104
    "$PARTED" -s -m "$dev" unit s print | tail -1
105
}
106

    
107
get_partition() {
108
    local dev="$1"
109
    local id="$2"
110

    
111
    "$PARTED" -s -m "$dev" unit s print | grep "^$id" 
112
}
113

    
114
get_partition_count() {
115
    local dev="$1"
116

    
117
     expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
118
}
119

    
120
get_last_free_sector() {
121
    local dev="$1"
122
    local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
123
    local type=$(echo "$last_line" | cut -d: -f 5)
124

    
125
    if [ "$type" = "free;" ]; then
126
        echo "$last_line" | cut -d: -f 3
127
    fi
128
}
129

    
130
cleanup() {
131
    # if something fails here, it shouldn't call cleanup again...
132
    trap - EXIT
133

    
134
    if [ ${#CLEANUP[*]} -gt 0 ]; then
135
        LAST_ELEMENT=$((${#CLEANUP[*]}-1))
136
        REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
137
        for i in $REVERSE_INDEXES; do
138
            # If something fails here, it's better to retry it for a few times
139
            # before we give up with an error. This is needed for kpartx when
140
            # dealing with ntfs partitions mounted through fuse. umount is not
141
            # synchronous and may return while the partition is still busy. A
142
            # premature attempt to delete partition mappings through kpartx on a
143
            # device that hosts previously mounted ntfs partition may fail with
144
            # a `device-mapper: remove ioctl failed: Device or resource busy'
145
            # error. A sensible workaround for this is to wait for a while and
146
            # then try again.
147
            local cmd=${CLEANUP[$i]}
148
            $cmd || for interval in 0.25 0.5 1 2 4; do
149
            echo "Command $cmd failed!"
150
            echo "I'll wait for $interval secs and will retry..."
151
            sleep $interval
152
            $cmd && break
153
        done
154
	if [ "$?" != "0" ]; then
155
            echo "Giving Up..."
156
            exit 1;
157
        fi
158
    done
159
  fi
160
}
161

    
162

    
163
check_if_excluded() {
164

    
165
    test "$PROGNAME" = "snf-image-helper" && return 0
166

    
167
    eval local do_exclude=\$SNF_IMAGE_PROPERTY_EXCLUDE_${PROGNAME:2}_TASK
168
    if [ -n "$do_exclude" ]; then
169
        warn "Task $PROGNAME was excluded and will not run."
170
        exit 0
171
    fi
172

    
173
    return 0
174
}
175

    
176
trap cleanup EXIT
177

    
178
# Check if the execution of a task should be ommited
179
check_if_excluded
180

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