common.sh 4.28 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
Nikos Skalkotos's avatar
Nikos Skalkotos committed
32

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

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

CLEANUP=( )

log_error() {
42
    echo "ERROR: $@" | tee $RESULT >&2
Nikos Skalkotos's avatar
Nikos Skalkotos committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    exit 1
}

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
            ID=$(grep $DISTRIB_ID=${root_dir}/etc/lsb-release | cut -d= -f2)
            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
76
    elif [ -e "$root_dir/etc/centos-release" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        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
}

cleanup() {
# if something fails here, it souldn't call cleanup again...
    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
            # an  `device-mapper: remove ioctl failed: Device or resource busy'
            # 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
113
114
115
116
	if [ "$?" != "0" ]; then
            echo "Giving Up..."
            exit 1;
        fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
117
118
119
120
121
122
123
    done
  fi
}

trap cleanup EXIT

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