Fix copyright and license notices throughout
[snf-image] / snf-image-helper / common.sh
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 RESIZE2FS=resize2fs
28 PARTED=parted
29 REGLOOKUP=reglookup
30 CHNTPW=chntpw
31
32 CLEANUP=( )
33
34 add_cleanup() {
35     local cmd=""
36     for arg; do cmd+=$(printf "%q " "$arg"); done
37     CLEANUP+=("$cmd")
38 }
39
40 log_error() {
41     echo "ERROR: $@" | tee $RESULT >&2
42     exit 1
43 }
44
45 warn() {
46     echo "Warning: $@" >&2
47 }
48
49 get_base_distro() {
50     local root_dir=$1
51
52     if [ -e "$root_dir/etc/debian_version" ]; then
53         echo "debian"
54     elif [ -e "$root_dir/etc/redhat-release" ]; then
55         echo "redhat"
56     elif [ -e "$root_dir/etc/slackware-version" ]; then
57         echo "slackware"
58     elif [ -e "$root_dir/SuSE-release" ]; then
59         echo "suse"
60     elif [ -e "$root_dir/gentoo-release" ]; then
61         echo "gentoo"
62     fi
63 }
64
65 get_distro() {
66     local root_dir=$1
67
68     if [ -e "$root_dir/etc/debian_version" ]; then
69         distro="debian"
70         if [ -e ${root_dir}/etc/lsb-release ]; then
71             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
72             if [ "x$ID" = "xUbuntu" ]; then
73                 distro="ubuntu"
74             fi
75         fi
76         echo "$distro"
77     elif [ -e "$root_dir/etc/fedora-release" ]; then
78         echo "fedora"
79     elif [ -e "$root_dir/etc/centos-release" ]; then
80         echo "centos"
81     elif [ -e "$root_dir/etc/redhat-release" ]; then
82         echo "redhat"
83     elif [ -e "$root_dir/etc/slackware-version" ]; then
84         echo "slackware"
85     elif [ -e "$root_dir/SuSE-release" ]; then
86         echo "suse"
87     elif [ -e "$root_dir/gentoo-release" ]; then
88         echo "gentoo"
89     fi
90 }
91
92 get_last_partition() {
93     local dev="$1"
94
95     "$PARTED" -s -m "$dev" unit s print | tail -1
96 }
97
98 get_partition() {
99     local dev="$1"
100     local id="$2"
101
102     "$PARTED" -s -m "$dev" unit s print | grep "^$id" 
103 }
104
105 get_partition_count() {
106     local dev="$1"
107
108      expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
109 }
110
111 get_last_free_sector() {
112     local dev="$1"
113     local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
114     local type=$(echo "$last_line" | cut -d: -f 5)
115
116     if [ "$type" = "free;" ]; then
117         echo "$last_line" | cut -d: -f 3
118     fi
119 }
120
121 cleanup() {
122     # if something fails here, it shouldn't call cleanup again...
123     trap - EXIT
124
125     if [ ${#CLEANUP[*]} -gt 0 ]; then
126         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
127         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
128         for i in $REVERSE_INDEXES; do
129             # If something fails here, it's better to retry it for a few times
130             # before we give up with an error. This is needed for kpartx when
131             # dealing with ntfs partitions mounted through fuse. umount is not
132             # synchronous and may return while the partition is still busy. A
133             # premature attempt to delete partition mappings through kpartx on a
134             # device that hosts previously mounted ntfs partition may fail with
135             # a `device-mapper: remove ioctl failed: Device or resource busy'
136             # error. A sensible workaround for this is to wait for a while and
137             # then try again.
138             local cmd=${CLEANUP[$i]}
139             $cmd || for interval in 0.25 0.5 1 2 4; do
140             echo "Command $cmd failed!"
141             echo "I'll wait for $interval secs and will retry..."
142             sleep $interval
143             $cmd && break
144         done
145         if [ "$?" != "0" ]; then
146             echo "Giving Up..."
147             exit 1;
148         fi
149     done
150   fi
151 }
152
153
154 check_if_excluded() {
155
156     test "$PROGNAME" = "snf-image-helper" && return 0
157
158     eval local do_exclude=\$SNF_IMAGE_PROPERTY_EXCLUDE_${PROGNAME:2}_TASK
159     if [ -n "$do_exclude" ]; then
160         warn "Task $PROGNAME was excluded and will not run."
161         exit 0
162     fi
163
164     return 0
165 }
166
167 trap cleanup EXIT
168
169 # Check if the execution of a task should be ommited
170 check_if_excluded
171
172 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :