Remove redundant normalize_unit function
[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 SFDISK=sfdisk
31 REGLOOKUP=reglookup
32 CHNTPW=chntpw
33
34 CLEANUP=( )
35
36 add_cleanup() {
37     local cmd=""
38     for arg; do cmd+=$(printf "%q " "$arg"); done
39     CLEANUP+=("$cmd")
40 }
41
42 log_error() {
43     echo "ERROR: $@" | tee $RESULT >&2
44     exit 1
45 }
46
47 warn() {
48     echo "Warning: $@" >&2
49 }
50
51 get_base_distro() {
52     local root_dir=$1
53
54     if [ -e "$root_dir/etc/debian_version" ]; then
55         echo "debian"
56     elif [ -e "$root_dir/etc/redhat-release" ]; then
57         echo "redhat"
58     elif [ -e "$root_dir/etc/slackware-version" ]; then
59         echo "slackware"
60     elif [ -e "$root_dir/etc/SuSE-release" ]; then
61         echo "suse"
62     elif [ -e "$root_dir/etc/gentoo-release" ]; then
63         echo "gentoo"
64     else
65         warn "Unknown base distro."
66     fi
67 }
68
69 get_distro() {
70     local root_dir=$1
71
72     if [ -e "$root_dir/etc/debian_version" ]; then
73         distro="debian"
74         if [ -e ${root_dir}/etc/lsb-release ]; then
75             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
76             if [ "x$ID" = "xUbuntu" ]; then
77                 distro="ubuntu"
78             fi
79         fi
80         echo "$distro"
81     elif [ -e "$root_dir/etc/fedora-release" ]; then
82         echo "fedora"
83     elif [ -e "$root_dir/etc/centos-release" ]; then
84         echo "centos"
85     elif [ -e "$root_dir/etc/redhat-release" ]; then
86         echo "redhat"
87     elif [ -e "$root_dir/etc/slackware-version" ]; then
88         echo "slackware"
89     elif [ -e "$root_dir/etc/SuSE-release" ]; then
90         echo "suse"
91     elif [ -e "$root_dir/etc/gentoo-release" ]; then
92         echo "gentoo"
93     else
94         warn "Unknown distro."
95     fi
96 }
97
98 get_last_partition_id() {
99     local dev="$1"
100     if ! output="$("$PARTED" -s -m "$dev" print)"; then
101         log_error "Unable to read partition table for device \`${dev}'"
102     fi
103
104     last_line=$(tail -1 <<< "$output")
105
106     echo $(cut -d: -f1 <<< "$last_line")
107 }
108
109 get_partition_table() {
110     local dev="$1"
111     if ! output="$("$PARTED" -s -m "$dev" unit s print)"; then
112         log_error "Unable to read partition table for device \`${dev}'"
113     fi
114
115     echo "$output"
116 }
117
118 get_partition_table_type() {
119     local ptable="$1"
120
121     local dev="$(sed -n 2p <<< "$ptable")"
122     declare -a field
123     IFS=':' read -ra field <<< "$dev"
124
125     echo "${field[5]}"
126 }
127
128 get_partition_count() {
129     local ptable="$1"
130
131     expr $(echo "$ptable" | wc -l) - 2
132 }
133
134 get_partition_by_id() {
135     local ptable="$1"
136     local id="$2"
137
138     grep "^$id:" <<< "$ptable"
139 }
140
141 get_last_partition() {
142     local ptable="$1"
143
144     echo "$ptable" | tail -1
145 }
146
147 is_extended_partition() {
148     local dev="$1"
149     local part_num="$2"
150
151     id=$($SFDISK --print-id "$dev" "$part_num")
152     if [ "$id" = "5" ]; then
153         echo "yes"
154     else
155         echo "no"
156     fi
157 }
158
159 get_extended_partition() {
160     local ptable="$1"
161     local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
162
163     tail -n +3 <<< "$ptable" | while read line; do
164         part_num=$(cut -d':' -f1 <<< "$line")
165         if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
166             echo "$line"
167             return 0
168         fi
169     done
170     echo ""
171 }
172
173 get_logical_partitions() {
174     local ptable="$1"
175
176     tail -n +3 <<< "$ptable" | while read line; do
177         part_num=$(cut -d':' -f1 <<< "$line")
178         if [ $part_num -ge 5 ]; then
179             echo "$line"
180         fi
181     done
182
183     return 0
184 }
185
186 get_last_primary_partition() {
187     local ptable="$1"
188     local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
189
190     for i in 4 3 2 1; do
191         if output=$(grep "^$i:" <<< "$ptable"); then
192             echo "$output"
193             return 0
194         fi
195     done
196     echo ""
197 }
198
199 create_partition() {
200     local device="$1"
201     local part="$2"
202     local ptype="$3"
203
204     declare -a fields
205     IFS=":;" read -ra fields <<< "$part"
206     local id="${fields[0]}"
207     local start="${fields[1]}"
208     local end="${fields[2]}"
209     local size="${fields[3]}"
210     local fs="${fields[4]}"
211     local name="${fields[5]}"
212     local flags="${fields[6]//,/ }"
213
214     $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
215     for flag in $flags; do
216         $PARTED -s -m $device set "$id" "$flag" on
217     done
218 }
219
220 enlarge_partition() {
221     local device="$1"
222     local part="$2"
223     local ptype="$3"
224     local new_end="$4"
225
226     if [ -z "$new_end" ]; then
227         new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
228     fi
229
230     declare -a fields
231     IFS=":;" read -ra fields <<< "$part"
232     fields[2]="$new_end"
233
234     local new_part=""
235     for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
236         new_part="$new_part":"${fields[$i]}"
237     done
238     new_part=${new_part:1}
239
240     # If this is an extended partition, removing it will also remove the
241     # logical partitions it contains. We need to save them for later.
242     if [ "$ptype" = "extended" ]; then
243         local table="$(get_partition_table "$device")"
244         local logical="$(get_logical_partitions "$table")"
245     fi
246
247     id=${fields[0]}
248     $PARTED -s -m "$device" rm "$id"
249     create_partition "$device" "$new_part" "$ptype"
250
251     if [ "$ptype" = "extended" ]; then
252         # Recreate logical partitions
253         echo "$logical" | while read logical_part; do
254             create_partition "$device" "$logical_part" "logical"
255         done
256     fi
257 }
258
259 get_last_free_sector() {
260     local dev="$1"
261     local unit="$2"
262
263     if [ -n "$unit" ]; then
264         unit="unit $unit"
265     fi
266
267     local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
268     local ptype="$(cut -d: -f 5 <<< "$last_line")"
269
270     if [ "$ptype" = "free;" ]; then
271         echo "$last_line"
272     fi
273 }
274
275 cleanup() {
276     # if something fails here, it shouldn't call cleanup again...
277     trap - EXIT
278
279     if [ ${#CLEANUP[*]} -gt 0 ]; then
280         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
281         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
282         for i in $REVERSE_INDEXES; do
283             # If something fails here, it's better to retry it for a few times
284             # before we give up with an error. This is needed for kpartx when
285             # dealing with ntfs partitions mounted through fuse. umount is not
286             # synchronous and may return while the partition is still busy. A
287             # premature attempt to delete partition mappings through kpartx on
288             # a device that hosts previously mounted ntfs partition may fail
289             # with a `device-mapper: remove ioctl failed: Device or resource
290             # busy' error. A sensible workaround for this is to wait for a
291             # while and then try again.
292             local cmd=${CLEANUP[$i]}
293             $cmd || for interval in 0.25 0.5 1 2 4; do
294             echo "Command $cmd failed!"
295             echo "I'll wait for $interval secs and will retry..."
296             sleep $interval
297             $cmd && break
298         done
299         if [ "$?" != "0" ]; then
300             echo "Giving Up..."
301             exit 1;
302         fi
303     done
304   fi
305 }
306
307 check_if_excluded() {
308
309     local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
310     if [ -n "${!exclude}" ]; then
311         warn "Task $PROGNAME was excluded and will not run."
312         exit 0
313     fi
314
315     return 0
316 }
317
318 trap cleanup EXIT
319 set -o pipefail
320
321 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :