Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / common.sh @ 2a0c492d

History | View | Annotate | Download (4.6 kB)

1
# Copyright (C) 2011 GRNET S.A. 
2
# Copyright (C) 2007, 2008, 2009 Google Inc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
# General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
# 02110-1301, USA.
18

    
19
RESULT=/dev/ttyS1
20
FLOPPY_DEV=/dev/fd0
21
PROGNAME=$(basename $0)
22

    
23
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
24

    
25
# Programs
26
XMLSTARLET=xmlstarlet
27
E2FSCK=e2fsck
28
RESIZE2FS=resize2fs
29
PARTED=parted
30
REGLOOKUP=reglookup
31
CHNTPW=chntpw
32

    
33
CLEANUP=( )
34

    
35
add_cleanup() {
36
    local cmd=""
37
    for arg; do cmd+=$(printf "%q " "$arg"); done
38
    CLEANUP+=("$cmd")
39
}
40

    
41
log_error() {
42
    echo "ERROR: $@" | tee $RESULT >&2
43
    exit 1
44
}
45

    
46
warn() {
47
    echo "Warning: $@" >&2
48
}
49

    
50
get_base_distro() {
51
    local root_dir=$1
52

    
53
    if [ -e "$root_dir/etc/debian_version" ]; then
54
        echo "debian"
55
    elif [ -e "$root_dir/etc/redhat-release" ]; then
56
        echo "redhat"
57
    elif [ -e "$root_dir/etc/slackware-version" ]; then
58
        echo "slackware"
59
    elif [ -e "$root_dir/SuSE-release" ]; then
60
        echo "suse"
61
    elif [ -e "$root_dir/gentoo-release" ]; then
62
        echo "gentoo"
63
    fi
64
}
65

    
66
get_distro() {
67
    local root_dir=$1
68

    
69
    if [ -e "$root_dir/etc/debian_version" ]; then
70
        distro="debian"
71
        if [ -e ${root_dir}/etc/lsb-release ]; then
72
            ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
73
            if [ "x$ID" = "xUbuntu" ]; then
74
                distro="ubuntu"
75
            fi
76
        fi
77
        echo "$distro"
78
    elif [ -e "$root_dir/etc/fedora-release" ]; then
79
        echo "fedora"
80
    elif [ -e "$root_dir/etc/centos-release" ]; then
81
        echo "centos"
82
    elif [ -e "$root_dir/etc/redhat-release" ]; then
83
        echo "redhat"
84
    elif [ -e "$root_dir/etc/slackware-version" ]; then
85
        echo "slackware"
86
    elif [ -e "$root_dir/SuSE-release" ]; then
87
        echo "suse"
88
    elif [ -e "$root_dir/gentoo-release" ]; then
89
        echo "gentoo"
90
    fi
91
}
92

    
93
get_last_partition() {
94
    local dev="$1"
95

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

    
99
get_partition() {
100
    local dev="$1"
101
    local id="$2"
102

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

    
106
get_partition_count() {
107
    local dev="$1"
108

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

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

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

    
122
cleanup() {
123
    # if something fails here, it shouldn't call cleanup again...
124
    trap - EXIT
125

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

    
154
check_if_excluded() {
155

    
156
    local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
157
    if [ -n "${!exclude}" ]; then
158
        warn "Task $PROGNAME was excluded and will not run."
159
        exit 0
160
    fi
161

    
162
    return 0
163
}
164

    
165
trap cleanup EXIT
166

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