Add support for archlinux
[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 MONITOR=/dev/ttyS2
21
22 FLOPPY_DEV=/dev/fd0
23 PROGNAME=$(basename $0)
24
25 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
26
27 # Programs
28 XMLSTARLET=xmlstarlet
29 TUNE2FS=tune2fs
30 RESIZE2FS=resize2fs
31 PARTED=parted
32 SFDISK=sfdisk
33 MKSWAP=mkswap
34 BLKID=blkid
35 BLOCKDEV=blockdev
36 REGLOOKUP=reglookup
37 CHNTPW=chntpw
38
39 CLEANUP=( )
40 ERRORS=( )
41 WARNINGS=( )
42
43 MSG_TYPE_TASK_START="TASK_START"
44 MSG_TYPE_TASK_END="TASK_END"
45
46 STDERR_LINE_SIZE=10
47
48 add_cleanup() {
49     local cmd=""
50     for arg; do cmd+=$(printf "%q " "$arg"); done
51     CLEANUP+=("$cmd")
52 }
53
54 log_error() {
55     ERRORS+=("$@")
56     echo "ERROR: $@" | tee $RESULT >&2
57     exit 1
58 }
59
60 warn() {
61     echo "Warning: $@" >&2
62     echo "WARNING:$@" > "$MONITOR"
63 }
64
65 report_task_start() {
66     echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
67 }
68
69 report_task_end() {
70     echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
71 }
72
73 report_error() {
74     if [ ${#ERRORS[*]} -eq 0 ]; then
75         # No error message. Print stderr
76         local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
77         echo -n "STDERR:${lines}:" > "$MONITOR"
78         tail --lines=$lines  "$STDERR_FILE" > "$MONITOR"
79     else
80         echo -n "ERROR:" > "$MONITOR"
81         for line in "${ERRORS[@]}"; do
82             echo "$line" > "$MONITOR"
83         done
84     fi
85 }
86
87 get_base_distro() {
88     local root_dir=$1
89
90     if [ -e "$root_dir/etc/debian_version" ]; then
91         echo "debian"
92     elif [ -e "$root_dir/etc/redhat-release" ]; then
93         echo "redhat"
94     elif [ -e "$root_dir/etc/slackware-version" ]; then
95         echo "slackware"
96     elif [ -e "$root_dir/etc/SuSE-release" ]; then
97         echo "suse"
98     elif [ -e "$root_dir/etc/gentoo-release" ]; then
99         echo "gentoo"
100     elif [ -e "$root_dir/etc/arch-release" ]; then
101         echo "arch"
102     else
103         warn "Unknown base distro."
104     fi
105 }
106
107 get_distro() {
108     local root_dir=$1
109
110     if [ -e "$root_dir/etc/debian_version" ]; then
111         distro="debian"
112         if [ -e ${root_dir}/etc/lsb-release ]; then
113             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
114             if [ "x$ID" = "xUbuntu" ]; then
115                 distro="ubuntu"
116             fi
117         fi
118         echo "$distro"
119     elif [ -e "$root_dir/etc/fedora-release" ]; then
120         echo "fedora"
121     elif [ -e "$root_dir/etc/centos-release" ]; then
122         echo "centos"
123     elif [ -e "$root_dir/etc/redhat-release" ]; then
124         echo "redhat"
125     elif [ -e "$root_dir/etc/slackware-version" ]; then
126         echo "slackware"
127     elif [ -e "$root_dir/etc/SuSE-release" ]; then
128         echo "suse"
129     elif [ -e "$root_dir/etc/gentoo-release" ]; then
130         echo "gentoo"
131     elif [ -e "$root_dir/etc/arch-release" ]; then
132         echo "arch"
133     else
134         warn "Unknown distro."
135     fi
136 }
137
138
139 get_partition_table() {
140     local dev="$1"
141     # If the partition table is gpt then parted will raise an error if the
142     # secondary gpt is not it the end of the disk, and a warning that has to
143     # do with the "Last Usable LBA" entry in gpt.
144     if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
145         log_error "Unable to read partition table for device \`${dev}'"
146     fi
147
148     echo "$output"
149 }
150
151 get_partition_table_type() {
152     local ptable="$1"
153
154     local dev="$(sed -n 2p <<< "$ptable")"
155     declare -a field
156     IFS=':' read -ra field <<< "$dev"
157
158     echo "${field[5]}"
159 }
160
161 get_partition_count() {
162     local ptable="$1"
163
164     expr $(echo "$ptable" | wc -l) - 2
165 }
166
167 get_partition_by_num() {
168     local ptable="$1"
169     local id="$2"
170
171     grep "^$id:" <<< "$ptable"
172 }
173
174 get_last_partition() {
175     local ptable="$1"
176
177     echo "$ptable" | tail -1
178 }
179
180 is_extended_partition() {
181     local dev="$1"
182     local part_num="$2"
183
184     id=$($SFDISK --print-id "$dev" "$part_num")
185     if [ "$id" = "5" ]; then
186         echo "yes"
187     else
188         echo "no"
189     fi
190 }
191
192 get_extended_partition() {
193     local ptable="$1"
194     local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
195
196     tail -n +3 <<< "$ptable" | while read line; do
197         part_num=$(cut -d':' -f1 <<< "$line")
198         if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
199             echo "$line"
200             return 0
201         fi
202     done
203     echo ""
204 }
205
206 get_logical_partitions() {
207     local ptable="$1"
208
209     tail -n +3 <<< "$ptable" | while read line; do
210         part_num=$(cut -d':' -f1 <<< "$line")
211         if [ $part_num -ge 5 ]; then
212             echo "$line"
213         fi
214     done
215
216     return 0
217 }
218
219 get_last_primary_partition() {
220     local ptable="$1"
221     local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
222
223     for i in 4 3 2 1; do
224         if output=$(grep "^$i:" <<< "$ptable"); then
225             echo "$output"
226             return 0
227         fi
228     done
229     echo ""
230 }
231
232 get_partition_to_resize() {
233     local dev="$1"
234
235     table=$(get_partition_table "$dev")
236
237     if [ $(get_partition_count "$table") -eq 0 ]; then
238         return 0
239     fi
240
241     table_type=$(get_partition_table_type "$table")
242     last_part=$(get_last_partition "$table")
243     last_part_num=$(cut -d: -f1 <<< "$last_part")
244
245     if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
246         extended=$(get_extended_partition "$table")
247         last_primary=$(get_last_primary_partition "$table")
248         ext_num=$(cut -d: -f1 <<< "$extended")
249         prim_num=$(cut -d: -f1 <<< "$last_primary")
250
251         if [ "$ext_num" != "$last_prim_num" ]; then
252             echo "$last_prim_num"
253         else
254             echo "$last_part_num"
255         fi
256     else
257         echo "$last_part_num"
258     fi
259 }
260
261 create_partition() {
262     local device="$1"
263     local part="$2"
264     local ptype="$3"
265
266     declare -a fields
267     IFS=":;" read -ra fields <<< "$part"
268     local id="${fields[0]}"
269     local start="${fields[1]}"
270     local end="${fields[2]}"
271     local size="${fields[3]}"
272     local fs="${fields[4]}"
273     local name="${fields[5]}"
274     local flags="${fields[6]//,/ }"
275
276     $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
277     for flag in $flags; do
278         $PARTED -s -m $device set "$id" "$flag" on
279     done
280 }
281
282 enlarge_partition() {
283     local device="$1"
284     local part="$2"
285     local ptype="$3"
286     local new_end="$4"
287
288     if [ -z "$new_end" ]; then
289         new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
290     fi
291
292     declare -a fields
293     IFS=":;" read -ra fields <<< "$part"
294     fields[2]="$new_end"
295
296     local new_part=""
297     for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
298         new_part="$new_part":"${fields[$i]}"
299     done
300     new_part=${new_part:1}
301
302     # If this is an extended partition, removing it will also remove the
303     # logical partitions it contains. We need to save them for later.
304     if [ "$ptype" = "extended" ]; then
305         local table="$(get_partition_table "$device")"
306         local logical="$(get_logical_partitions "$table")"
307     fi
308
309     id=${fields[0]}
310     $PARTED -s -m "$device" rm "$id"
311     create_partition "$device" "$new_part" "$ptype"
312
313     if [ "$ptype" = "extended" ]; then
314         # Recreate logical partitions
315         echo "$logical" | while read logical_part; do
316             create_partition "$device" "$logical_part" "logical"
317         done
318     fi
319 }
320
321 get_last_free_sector() {
322     local dev="$1"
323     local unit="$2"
324
325     if [ -n "$unit" ]; then
326         unit="unit $unit"
327     fi
328
329     local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
330     local ptype="$(cut -d: -f 5 <<< "$last_line")"
331
332     if [ "$ptype" = "free;" ]; then
333         echo "$last_line"
334     fi
335 }
336
337 cleanup() {
338     # if something fails here, it shouldn't call cleanup again...
339     trap - EXIT
340
341     if [ ${#CLEANUP[*]} -gt 0 ]; then
342         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
343         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
344         for i in $REVERSE_INDEXES; do
345             # If something fails here, it's better to retry it for a few times
346             # before we give up with an error. This is needed for kpartx when
347             # dealing with ntfs partitions mounted through fuse. umount is not
348             # synchronous and may return while the partition is still busy. A
349             # premature attempt to delete partition mappings through kpartx on
350             # a device that hosts previously mounted ntfs partition may fail
351             # with a `device-mapper: remove ioctl failed: Device or resource
352             # busy' error. A sensible workaround for this is to wait for a
353             # while and then try again.
354             local cmd=${CLEANUP[$i]}
355             $cmd || for interval in 0.25 0.5 1 2 4; do
356             echo "Command $cmd failed!"
357             echo "I'll wait for $interval secs and will retry..."
358             sleep $interval
359             $cmd && break
360         done
361         if [ "$?" != "0" ]; then
362             echo "Giving Up..."
363             exit 1;
364         fi
365     done
366   fi
367 }
368
369 task_cleanup() {
370     rc=$?
371
372     if [ $rc -eq 0 ]; then
373        report_task_end
374     else
375        report_error
376     fi
377
378     cleanup
379 }
380
381 check_if_excluded() {
382     local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
383     local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
384     if [ -n "${!exclude}" ]; then
385         warn "Task ${PROGNAME:2} was excluded and will not run."
386         exit 0
387     fi
388
389     return 0
390 }
391
392 trap cleanup EXIT
393 set -o pipefail
394
395 STDERR_FILE=$(mktemp)
396 add_cleanup rm -f "$STDERR_FILE"
397 exec 2> >(tee -a "$STDERR_FILE" >&2)
398
399 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :