Add swap partition if SWAP property is defined
[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 normalize_unit() {
99     unit=$(tr [a-z] [A-Z] <<< $1)
100
101     case $unit in
102         "S") echo "s";;
103         "B"|"") echo "B";;
104         "KB") echo "kB";;
105         "MB") echo "MB";;
106         "GB") echo "GB";;
107         *)  log_error "Unknown unit type: \`$1'";;
108     esac
109 }
110
111 get_last_partition_id() {
112     local dev="$1"
113     if ! output="$("$PARTED" -s -m "$dev" print)"; then
114         log_error "Unable to read partition table for device \`${dev}'"
115     fi
116
117     last_line=$(tail -1 <<< "$output")
118
119     echo $(cut -d: -f1 <<< "$last_line")
120 }
121
122 get_partition_table() {
123     local dev="$1"
124     if ! output="$("$PARTED" -s -m "$dev" unit s print)"; then
125         log_error "Unable to read partition table for device \`${dev}'"
126     fi
127
128     echo "$output"
129 }
130
131 get_partition_table_type() {
132     local ptable="$1"
133
134     local dev="$(sed -n 2p <<< "$ptable")"
135     declare -a field
136     IFS=':' read -ra field <<< "$dev"
137
138     echo "${field[5]}"
139 }
140
141 get_partition_count() {
142     local ptable="$1"
143
144     expr $(echo "$ptable" | wc -l) - 2
145 }
146
147 get_partition_by_id() {
148     local ptable="$1"
149     local id="$2"
150
151     grep "^$id:" <<< "$ptable"
152 }
153
154 get_last_partition() {
155     local ptable="$1"
156
157     echo "$ptable" | tail -1
158 }
159
160 is_extended_partition() {
161     local dev="$1"
162     local part_num="$2"
163
164     id=$($SFDISK --print-id "$dev" "$part_num")
165     if [ "$id" = "5" ]; then
166         echo "yes"
167     else
168         echo "no"
169     fi
170 }
171
172 get_extended_partition() {
173     local ptable="$1"
174     local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
175
176     tail -n +3 <<< "$ptable" | while read line; do
177         part_num=$(cut -d':' -f1 <<< "$line")
178         if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
179             echo "$line"
180             return 0
181         fi
182     done
183     echo ""
184 }
185
186 get_logical_partitions() {
187     local ptable="$1"
188
189     tail -n +3 <<< "$ptable" | while read line; do
190         part_num=$(cut -d':' -f1 <<< "$line")
191         if [ $part_num -ge 5 ]; then
192             echo "$line"
193         fi
194     done
195
196     return 0
197 }
198
199 get_last_primary_partition() {
200     local ptable="$1"
201     local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
202
203     for i in 4 3 2 1; do
204         if output=$(grep "^$i:" <<< "$ptable"); then
205             echo "$output"
206             return 0
207         fi
208     done
209     echo ""
210 }
211
212 create_partition() {
213     local device="$1"
214     local part="$2"
215     local ptype="$3"
216
217     declare -a fields
218     IFS=":;" read -ra fields <<< "$part"
219     local id="${fields[0]}"
220     local start="${fields[1]}"
221     local end="${fields[2]}"
222     local size="${fields[3]}"
223     local fs="${fields[4]}"
224     local name="${fields[5]}"
225     local flags="${fields[6]//,/ }"
226
227     $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
228     for flag in $flags; do
229         $PARTED -s -m $device set "$id" "$flag" on
230     done
231 }
232
233 enlarge_partition() {
234     local device="$1"
235     local part="$2"
236     local ptype="$3"
237     local new_end="$4"
238
239     if [ -z "$new_end" ]; then
240         new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
241     fi
242
243     declare -a fields
244     IFS=":;" read -ra fields <<< "$part"
245     fields[2]="$new_end"
246
247     local new_part=""
248     for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
249         new_part="$new_part":"${fields[$i]}"
250     done
251     new_part=${new_part:1}
252
253     # If this is an extended partition, removing it will also remove the
254     # logical partitions it contains. We need to save them for later.
255     if [ "$ptype" = "extended" ]; then
256         local table="$(get_partition_table "$device")"
257         local logical="$(get_logical_partitions "$table")"
258     fi
259
260     id=${fields[0]}
261     $PARTED -s -m "$device" rm "$id"
262     create_partition "$device" "$new_part" "$ptype"
263
264     if [ "$ptype" = "extended" ]; then
265         # Recreate logical partitions
266         echo "$logical" | while read logical_part; do
267             create_partition "$device" "$logical_part" "logical"
268         done
269     fi
270 }
271
272 get_last_free_sector() {
273     local dev="$1"
274     local unit="$2"
275
276     if [ -n "$unit" ]; then
277         unit="unit $unit"
278     fi
279
280     local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
281     local ptype="$(cut -d: -f 5 <<< "$last_line")"
282
283     if [ "$ptype" = "free;" ]; then
284         echo "$last_line"
285     fi
286 }
287
288 cleanup() {
289     # if something fails here, it shouldn't call cleanup again...
290     trap - EXIT
291
292     if [ ${#CLEANUP[*]} -gt 0 ]; then
293         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
294         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
295         for i in $REVERSE_INDEXES; do
296             # If something fails here, it's better to retry it for a few times
297             # before we give up with an error. This is needed for kpartx when
298             # dealing with ntfs partitions mounted through fuse. umount is not
299             # synchronous and may return while the partition is still busy. A
300             # premature attempt to delete partition mappings through kpartx on
301             # a device that hosts previously mounted ntfs partition may fail
302             # with a `device-mapper: remove ioctl failed: Device or resource
303             # busy' error. A sensible workaround for this is to wait for a
304             # while and then try again.
305             local cmd=${CLEANUP[$i]}
306             $cmd || for interval in 0.25 0.5 1 2 4; do
307             echo "Command $cmd failed!"
308             echo "I'll wait for $interval secs and will retry..."
309             sleep $interval
310             $cmd && break
311         done
312         if [ "$?" != "0" ]; then
313             echo "Giving Up..."
314             exit 1;
315         fi
316     done
317   fi
318 }
319
320 check_if_excluded() {
321
322     local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
323     if [ -n "${!exclude}" ]; then
324         warn "Task $PROGNAME was excluded and will not run."
325         exit 0
326     fi
327
328     return 0
329 }
330
331 trap cleanup EXIT
332 set -o pipefail
333
334 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :