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

30
RESULT=/dev/ttyS1
Nikos Skalkotos's avatar
Nikos Skalkotos committed
31
FLOPPY_DEV=/dev/fd0
32
PROGNAME=$(basename $0)
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33

Nikos Skalkotos's avatar
Nikos Skalkotos committed
34
35
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

Nikos Skalkotos's avatar
Nikos Skalkotos committed
36
37
# Programs
XMLSTARLET=xmlstarlet
Nikos Skalkotos's avatar
Nikos Skalkotos committed
38
RESIZE2FS=resize2fs
39
PARTED=parted
Nikos Skalkotos's avatar
Nikos Skalkotos committed
40
41
42

CLEANUP=( )

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

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

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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
get_base_distro() {
    local root_dir=$1

    if [ -e "$root_dir/etc/debian_version" ]; then
        echo "debian"
    elif [ -e "$root_dir/etc/redhat-release" ]; then
        echo "redhat"
    elif [ -e "$root_dir/etc/slackware-version" ]; then
        echo "slackware"
    elif [ -e "$root_dir/SuSE-release" ]; then
        echo "suse"
    elif [ -e "$root_dir/gentoo-release" ]; then
        echo "gentoo"
    fi
}

get_distro() {
    local root_dir=$1

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

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
get_last_partition() {
    local dev="$1"

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

get_partition() {
    local dev="$1"
    local id="$2"

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

get_partition_count() {
    local dev="$1"

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

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

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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
130
cleanup() {
131
    # if something fails here, it shouldn't call cleanup again...
Nikos Skalkotos's avatar
Nikos Skalkotos committed
132
133
134
135
136
137
138
139
140
141
142
143
    trap - EXIT

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

162
trap cleanup EXIT
163

164
check_if_excluded() {
165

166
167
168
169
170
171
172
173
174
175
176
177
178
    test "$PROGNAME" = "snf-image-helper" && return 0

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

    return 0
}

# Check if the execution of a task should be ommited
check_if_excluded
Nikos Skalkotos's avatar
Nikos Skalkotos committed
179
180

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