Bump version to 0.12-1~precise
[snf-image] / snf-image-helper / common.sh
1 # Copyright (C) 2011, 2012, 2013 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 PROGNAME=$(basename $0)
20
21 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
22
23 # Programs
24 XMLSTARLET=xmlstarlet
25 TUNE2FS=tune2fs
26 RESIZE2FS=resize2fs
27 PARTED=parted
28 SFDISK=sfdisk
29 MKSWAP=mkswap
30 BLKID=blkid
31 BLOCKDEV=blockdev
32 REGLOOKUP=reglookup
33 CHNTPW=chntpw
34 SGDISK=sgdisk
35 GROWFS_UFS=growfs.ufs
36 DUMPFS_UFS=dumpfs.ufs
37 DATE="date -u" # Time in UTC
38 EATMYDATA=eatmydata
39 MOUNT="mount -n"
40
41 CLEANUP=( )
42 ERRORS=( )
43 WARNINGS=( )
44
45 MSG_TYPE_TASK_START="TASK_START"
46 MSG_TYPE_TASK_END="TASK_END"
47
48 STDERR_LINE_SIZE=10
49
50 add_cleanup() {
51     local cmd=""
52     for arg; do cmd+=$(printf "%q " "$arg"); done
53     CLEANUP+=("$cmd")
54 }
55
56 close_fd() {
57     local fd=$1
58
59     exec {fd}>&-
60 }
61
62 send_result_kvm() {
63     echo "$@" > /dev/ttyS1
64 }
65
66 send_monitor_message_kvm() {
67     echo "$@" > /dev/ttyS2
68 }
69
70 send_result_xen() {
71     xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
72 }
73
74 send_monitor_message_xen() {
75     #Broadcast the message
76     echo "$@" | socat "STDIO" "UDP-DATAGRAM:${BROADCAST}:${MONITOR_PORT},broadcast"
77 }
78
79 prepare_helper() {
80         local cmdline item key val hypervisor domid
81
82         read -a cmdline  < /proc/cmdline
83         for item in "${cmdline[@]}"; do
84             key=$(cut -d= -f1 <<< "$item")
85             val=$(cut -d= -f2 <<< "$item")
86             if [ "$key" = "hypervisor" ]; then
87                 hypervisor="$val"
88             fi
89             if [ "$key" = "rules_dev" ]; then
90                 export RULES_DEV="$val"
91             fi
92             if [ "$key" = "helper_ip" ]; then
93                 export IP="$val"
94                 export NETWORK="$IP/24"
95                 export BROADCAST="${IP%.*}.255"
96             fi
97             if [ "$key" = "monitor_port" ]; then
98                 export MONITOR_PORT="$val"
99             fi
100         done
101
102     case "$hypervisor" in
103     kvm)
104         HYPERVISOR=kvm
105         ;;
106     xen-hvm|xen-pvm)
107         if [ -z "$IP" ]; then
108             echo "ERROR: \`helper_ip' not defined or empty" >&2
109             exit 1
110         fi
111         if [ -z "$MONITOR_PORT" ]; then
112             echo "ERROR: \`monitor_port' not defined or empty" >&2
113             exit 1
114         fi
115         $MOUNT -t xenfs xenfs /proc/xen
116         ip addr add "$NETWORK" dev eth0
117         ip link set eth0 up
118         ip route add default dev eth0
119         export DOMID=$(xenstore-read domid)
120         HYPERVISOR=xen
121         ;;
122     *)
123         echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
124         exit 1
125         ;;
126     esac
127
128     export HYPERVISOR
129 }
130
131 report_error() {
132     msg=""
133     if [ ${#ERRORS[*]} -eq 0 ]; then
134         # No error message. Print stderr
135         local lines stderr
136         stderr="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
137         lines=$(wc -l <<< "$stderr")
138         msg="STDERR:${lines}:$stderr"
139     else
140         for line in "${ERRORS[@]}"; do
141             msg+="ERROR:$line"$'\n'
142         done
143     fi
144
145     send_monitor_message_${HYPERVISOR} "$msg"
146 }
147
148 log_error() {
149     ERRORS+=("$*")
150
151     send_result_${HYPERVISOR} "ERROR: $@"
152
153     # Use return instead of exit. The set -x options will terminate the script
154     # but will also trigger ERR traps if defined.
155     return 1
156 }
157
158 warn() {
159     echo "Warning: $@" >&2
160     send_monitor_message_${HYPERVISOR} "WARNING: $@"
161 }
162
163 report_task_start() {
164     send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
165 }
166
167 report_task_end() {
168     send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
169 }
170
171 system_poweroff() {
172     while [ 1 ]; do
173         # Credits to psomas@grnet.gr for this ...
174         echo o > /proc/sysrq-trigger
175         sleep 1
176     done
177 }
178
179 get_base_distro() {
180     local root_dir=$1
181
182     if [ -e "$root_dir/etc/debian_version" ]; then
183         echo "debian"
184     elif [ -e "$root_dir/etc/redhat-release" ]; then
185         echo "redhat"
186     elif [ -e "$root_dir/etc/slackware-version" ]; then
187         echo "slackware"
188     elif [ -e "$root_dir/etc/SuSE-release" ]; then
189         echo "suse"
190     elif [ -e "$root_dir/etc/gentoo-release" ]; then
191         echo "gentoo"
192     elif [ -e "$root_dir/etc/arch-release" ]; then
193         echo "arch"
194     elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
195         echo "freebsd"
196     elif [ -e "$root_dir/etc/release" ]; then
197         if grep -in netbsd "$root_dir/etc/release" &> /dev/null; then
198             echo "netbsd"
199         else
200             warn "Unknown Unix flavor."
201         fi
202     elif [ -e "$root_dir/etc/magic" ]; then
203         if grep -in openbsd "$root_dir/etc/magic" &> /dev/null; then
204             echo "openbsd"
205         else
206             warn "Unknown Unix flavor"
207         fi
208     else
209         warn "Unknown base distro."
210     fi
211 }
212
213 get_distro() {
214     local root_dir distro
215     root_dir=$1
216
217     if [ -e "$root_dir/etc/debian_version" ]; then
218         distro="debian"
219         if [ -e ${root_dir}/etc/lsb-release ]; then
220             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
221             if [ "x$ID" = "xUbuntu" ]; then
222                 distro="ubuntu"
223             fi
224         fi
225         echo "$distro"
226     elif [ -e "$root_dir/etc/fedora-release" ]; then
227         echo "fedora"
228     elif [ -e "$root_dir/etc/centos-release" ]; then
229         echo "centos"
230     elif [ -e "$root_dir/etc/redhat-release" ]; then
231         echo "redhat"
232     elif [ -e "$root_dir/etc/slackware-version" ]; then
233         echo "slackware"
234     elif [ -e "$root_dir/etc/SuSE-release" ]; then
235         echo "suse"
236     elif [ -e "$root_dir/etc/gentoo-release" ]; then
237         echo "gentoo"
238     elif [ -e "$root_dir/etc/arch-release" ]; then
239         echo "arch"
240     elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
241         echo "freebsd"
242     elif [ -e "$root_dir/etc/release" ]; then
243         if grep -in netbsd "$root_dir/etc/release" &> /dev/null; then
244             echo "netbsd"
245         else
246             warn "Unknown Unix flavor"
247         fi
248     elif [ -e "$root_dir/etc/magic" ]; then
249         if grep -in openbsd "$root_dir/etc/magic" &> /dev/null; then
250             echo "openbsd"
251         else
252             warn "Unknown Unix flavor"
253         fi
254     else
255         warn "Unknown distro."
256     fi
257 }
258
259 get_partition_table() {
260     local dev output
261     dev="$1"
262     # If the partition table is gpt then parted will raise an error if the
263     # secondary gpt is not it the end of the disk, and a warning that has to
264     # do with the "Last Usable LBA" entry in gpt.
265     if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
266         log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
267     fi
268
269     echo "$output"
270 }
271
272 get_partition_table_type() {
273     local ptable dev field
274     ptable="$1"
275
276     dev="$(sed -n 2p <<< "$ptable")"
277     IFS=':' read -ra field <<< "$dev"
278
279     echo "${field[5]}"
280 }
281
282 get_partition_count() {
283     local ptable="$1"
284
285     expr $(echo "$ptable" | wc -l) - 2
286 }
287
288 get_partition_by_num() {
289     local ptable="$1"
290     local id="$2"
291
292     grep "^$id:" <<< "$ptable"
293 }
294
295 get_last_partition() {
296     local ptable="$1"
297
298     echo "$ptable" | tail -1
299 }
300
301 is_extended_partition() {
302     local dev="$1"
303     local part_num="$2"
304
305     id=$($SFDISK --force --print-id "$dev" "$part_num")
306     if [ "$id" = "5" -o "$id" = "f" ]; then
307         echo "yes"
308     else
309         echo "no"
310     fi
311 }
312
313 get_extended_partition() {
314     local ptable dev
315     ptable="$1"
316     dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
317
318     tail -n +3 <<< "$ptable" | while read line; do
319         part_num=$(cut -d':' -f1 <<< "$line")
320         if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
321             echo "$line"
322             return 0
323         fi
324     done
325     echo ""
326 }
327
328 get_logical_partitions() {
329     local ptable part_num
330     ptable="$1"
331
332     tail -n +3 <<< "$ptable" | while read line; do
333         part_num=$(cut -d':' -f1 <<< "$line")
334         if [ $part_num -ge 5 ]; then
335             echo "$line"
336         fi
337     done
338
339     return 0
340 }
341
342 get_last_primary_partition() {
343     local ptable dev output
344     ptable="$1"
345     dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
346
347     for i in 4 3 2 1; do
348         if output=$(grep "^$i:" <<< "$ptable"); then
349             echo "$output"
350             return 0
351         fi
352     done
353     echo ""
354 }
355
356 get_partition_to_resize() {
357     local dev table table_type last_part last_part_num extended last_primary \
358         ext_num prim_num
359     dev="$1"
360
361     table=$(get_partition_table "$dev")
362     if [ -z "$table" ]; then
363         return 0
364     fi
365
366     if [ $(get_partition_count "$table") -eq 0 ]; then
367         return 0
368     fi
369
370     table_type=$(get_partition_table_type "$table")
371     last_part=$(get_last_partition "$table")
372     last_part_num=$(cut -d: -f1 <<< "$last_part")
373
374     if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
375         extended=$(get_extended_partition "$table")
376         last_primary=$(get_last_primary_partition "$table")
377         ext_num=$(cut -d: -f1 <<< "$extended")
378         last_prim_num=$(cut -d: -f1 <<< "$last_primary")
379
380         if [ "$ext_num" != "$last_prim_num" ]; then
381             echo "$last_prim_num"
382         else
383             echo "$last_part_num"
384         fi
385     else
386         echo "$last_part_num"
387     fi
388 }
389
390 create_partition() {
391     local device="$1"
392     local part="$2"
393     local ptype="$3"
394
395     local fields=()
396     IFS=":;" read -ra fields <<< "$part"
397     local id="${fields[0]}"
398     local start="${fields[1]}"
399     local end="${fields[2]}"
400     local size="${fields[3]}"
401     local fs="${fields[4]}"
402     local name="${fields[5]}"
403     local flags="${fields[6]//,/ }"
404
405     if [ "$ptype" = "primary" -o "$ptype" = "logical" -o "$ptype" = "extended" ]; then
406         $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
407         for flag in $flags; do
408             $PARTED -s -m $device set "$id" "$flag" on
409         done
410     else
411         # For gpt
412         start=${start:0:${#start}-1} # remove the s at the end
413         end=${end:0:${#end}-1} # remove the s at the end
414         $SGDISK -n "$id":"$start":"$end" -t "$id":"$ptype" "$device"
415     fi
416 }
417
418 enlarge_partition() {
419     local device part ptype new_end fields new_part table logical id
420     device="$1"
421     part="$2"
422     ptype="$3"
423     new_end="$4"
424
425     if [ -z "$new_end" ]; then
426         new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
427     fi
428
429     fields=()
430     IFS=":;" read -ra fields <<< "$part"
431     fields[2]="$new_end"
432
433     new_part=""
434     for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
435         new_part="$new_part":"${fields[$i]}"
436     done
437     new_part=${new_part:1}
438
439     # If this is an extended partition, removing it will also remove the
440     # logical partitions it contains. We need to save them for later.
441     if [ "$ptype" = "extended" ]; then
442         table="$(get_partition_table "$device")"
443         logical="$(get_logical_partitions "$table")"
444     fi
445
446     id=${fields[0]}
447     $PARTED -s -m "$device" rm "$id"
448     create_partition "$device" "$new_part" "$ptype"
449
450     if [ "$ptype" = "extended" ]; then
451         # Recreate logical partitions
452         echo "$logical" | while read logical_part; do
453             create_partition "$device" "$logical_part" "logical"
454         done
455     fi
456 }
457
458 get_last_free_sector() {
459     local dev unit last_line ptype
460     dev="$1"
461     unit="$2"
462
463     if [ -n "$unit" ]; then
464         unit="unit $unit"
465     fi
466
467     last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
468     ptype="$(cut -d: -f 5 <<< "$last_line")"
469
470     if [ "$ptype" = "free;" ]; then
471         echo "$last_line"
472     fi
473 }
474
475 get_unattend() {
476     local target exists
477     target="$1"
478
479     # Workaround to search for $target/Unattend.xml in an case insensitive way.
480     exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
481     if [ $(wc -l <<< "$exists") -gt 1 ]; then
482         log_error "Found multiple Unattend.xml files in the image:" $exists
483     fi
484
485     echo "$exists"
486 }
487
488 umount_all() {
489     local target mpoints
490     target="$1"
491
492     # Unmount file systems mounted under directory `target'
493     mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
494
495     for mpoint in $mpoints; do
496         umount $mpoint
497     done
498 }
499
500 get_ufstype() {
501     local device ufs
502
503     device="$1"
504     ufs="$($DUMPFS_UFS "$device" | head -1 | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }')"
505
506     case "$ufs" in
507         UFS1)
508             echo 44bsd
509             ;;
510         UFS2)
511             echo ufs2
512             ;;
513         *)
514             log_error "Unsupported UFS type: \`$ufs' in device $device"
515             echo ""
516             ;;
517     esac
518 }
519
520 cleanup() {
521     # if something fails here, it shouldn't call cleanup again...
522     trap - EXIT
523
524     if [ ${#CLEANUP[*]} -gt 0 ]; then
525         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
526         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
527         for i in $REVERSE_INDEXES; do
528             # If something fails here, it's better to retry it for a few times
529             # before we give up with an error. This is needed for kpartx when
530             # dealing with ntfs partitions mounted through fuse. umount is not
531             # synchronous and may return while the partition is still busy. A
532             # premature attempt to delete partition mappings through kpartx on
533             # a device that hosts previously mounted ntfs partition may fail
534             # with a `device-mapper: remove ioctl failed: Device or resource
535             # busy' error. A sensible workaround for this is to wait for a
536             # while and then try again.
537             local cmd=${CLEANUP[$i]}
538             $cmd || for interval in 0.25 0.5 1 2 4; do
539             echo "Command $cmd failed!"
540             echo "I'll wait for $interval secs and will retry..."
541             sleep $interval
542             $cmd && break
543         done
544         if [ "$?" != "0" ]; then
545             echo "Giving Up..."
546             exit 1;
547         fi
548     done
549   fi
550 }
551
552 task_cleanup() {
553     local rc=$?
554
555     if [ $rc -eq 0 ]; then
556        report_task_end
557     else
558        report_error
559     fi
560
561     cleanup
562 }
563
564 check_if_excluded() {
565     local name exclude
566     name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
567     exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
568     if [ -n "${!exclude}" ]; then
569         warn "Task ${PROGNAME:2} was excluded and will not run."
570         exit 0
571     fi
572
573     return 0
574 }
575
576 return_success() {
577     send_result_${HYPERVISOR} "SUCCESS"
578 }
579
580 trap cleanup EXIT
581 set -o pipefail
582
583 STDERR_FILE=$(mktemp)
584 add_cleanup rm -f "$STDERR_FILE"
585 exec 2> >(tee -a "$STDERR_FILE" >&2)
586
587 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :