Update ChangeLog and configure for version 0.3.6
[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 TUNE2FS=tune2fs
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/etc/SuSE-release" ]; then
60         echo "suse"
61     elif [ -e "$root_dir/etc/gentoo-release" ]; then
62         echo "gentoo"
63     else
64         warn "Unknown base distro."
65     fi
66 }
67
68 get_distro() {
69     local root_dir=$1
70
71     if [ -e "$root_dir/etc/debian_version" ]; then
72         distro="debian"
73         if [ -e ${root_dir}/etc/lsb-release ]; then
74             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
75             if [ "x$ID" = "xUbuntu" ]; then
76                 distro="ubuntu"
77             fi
78         fi
79         echo "$distro"
80     elif [ -e "$root_dir/etc/fedora-release" ]; then
81         echo "fedora"
82     elif [ -e "$root_dir/etc/centos-release" ]; then
83         echo "centos"
84     elif [ -e "$root_dir/etc/redhat-release" ]; then
85         echo "redhat"
86     elif [ -e "$root_dir/etc/slackware-version" ]; then
87         echo "slackware"
88     elif [ -e "$root_dir/etc/SuSE-release" ]; then
89         echo "suse"
90     elif [ -e "$root_dir/etc/gentoo-release" ]; then
91         echo "gentoo"
92     else
93         warn "Unknown distro."
94     fi
95 }
96
97 check_partition_table() {
98     local dev="$1"
99     if ! "$PARTED" -s "$dev" print; then
100         log_error "Unable to read partition table for device \`${dev}'"
101     fi
102 }
103
104 get_last_partition() {
105     local dev="$1"
106
107     "$PARTED" -s -m "$dev" unit s print | tail -1
108 }
109
110 get_partition() {
111     local dev="$1"
112     local id="$2"
113
114     "$PARTED" -s -m "$dev" unit s print | grep "^$id" 
115 }
116
117 get_partition_count() {
118     local dev="$1"
119
120      expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
121 }
122
123 get_last_free_sector() {
124     local dev="$1"
125     local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
126     local type=$(echo "$last_line" | cut -d: -f 5)
127
128     if [ "$type" = "free;" ]; then
129         echo "$last_line" | cut -d: -f 3
130     fi
131 }
132
133 cleanup() {
134     # if something fails here, it shouldn't call cleanup again...
135     trap - EXIT
136
137     if [ ${#CLEANUP[*]} -gt 0 ]; then
138         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
139         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
140         for i in $REVERSE_INDEXES; do
141             # If something fails here, it's better to retry it for a few times
142             # before we give up with an error. This is needed for kpartx when
143             # dealing with ntfs partitions mounted through fuse. umount is not
144             # synchronous and may return while the partition is still busy. A
145             # premature attempt to delete partition mappings through kpartx on a
146             # device that hosts previously mounted ntfs partition may fail with
147             # a `device-mapper: remove ioctl failed: Device or resource busy'
148             # error. A sensible workaround for this is to wait for a while and
149             # then try again.
150             local cmd=${CLEANUP[$i]}
151             $cmd || for interval in 0.25 0.5 1 2 4; do
152             echo "Command $cmd failed!"
153             echo "I'll wait for $interval secs and will retry..."
154             sleep $interval
155             $cmd && break
156         done
157         if [ "$?" != "0" ]; then
158             echo "Giving Up..."
159             exit 1;
160         fi
161     done
162   fi
163 }
164
165 check_if_excluded() {
166
167     local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
168     if [ -n "${!exclude}" ]; then
169         warn "Task $PROGNAME was excluded and will not run."
170         exit 0
171     fi
172
173     return 0
174 }
175
176 trap cleanup EXIT
177 set -o pipefail
178 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :