Statistics
| Branch: | Tag: | Revision:

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

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
REGLOOKUP=reglookup
41
CHNTPW=chntpw
42

    
43
CLEANUP=( )
44

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

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

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

    
60
get_base_distro() {
61
    local root_dir=$1
62

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

    
76
get_distro() {
77
    local root_dir=$1
78

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

    
103
get_last_partition() {
104
    local dev="$1"
105

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

    
109
get_partition() {
110
    local dev="$1"
111
    local id="$2"
112

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

    
116
get_partition_count() {
117
    local dev="$1"
118

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

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

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

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

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

    
164

    
165
check_if_excluded() {
166

    
167
    test "$PROGNAME" = "snf-image-helper" && return 0
168

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

    
175
    return 0
176
}
177

    
178
trap cleanup EXIT
179

    
180
# Check if the execution of a task should be ommited
181
check_if_excluded
182

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