1 # Copyright (C) 2011, 2012, 2013 GRNET S.A.
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
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.
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.
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
19 PROGNAME=$(basename $0)
21 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
37 DATE="date -u" # Time in UTC
45 MSG_TYPE_TASK_START="TASK_START"
46 MSG_TYPE_TASK_END="TASK_END"
52 for arg; do cmd+=$(printf "%q " "$arg"); done
63 echo "$@" > /dev/ttyS1
66 send_monitor_message_kvm() {
67 echo "$@" > /dev/ttyS2
71 xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
74 send_monitor_message_xen() {
75 #Broadcast the message
76 echo "$@" | socat "STDIO" "UDP-DATAGRAM:${BROADCAST}:${MONITOR_PORT},broadcast"
80 local cmdline item key val hypervisor domid
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
89 if [ "$key" = "rules_dev" ]; then
90 export RULES_DEV="$val"
92 if [ "$key" = "helper_ip" ]; then
94 export NETWORK="$IP/24"
95 export BROADCAST="${IP%.*}.255"
97 if [ "$key" = "monitor_port" ]; then
98 export MONITOR_PORT="$val"
102 case "$hypervisor" in
107 if [ -z "$IP" ]; then
108 echo "ERROR: \`helper_ip' not defined or empty" >&2
111 if [ -z "$MONITOR_PORT" ]; then
112 echo "ERROR: \`monitor_port' not defined or empty" >&2
115 $MOUNT -t xenfs xenfs /proc/xen
116 ip addr add "$NETWORK" dev eth0
118 ip route add default dev eth0
119 export DOMID=$(xenstore-read domid)
123 echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
133 if [ ${#ERRORS[*]} -eq 0 ]; then
134 # No error message. Print stderr
136 stderr="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
137 lines=$(wc -l <<< "$stderr")
138 msg="STDERR:${lines}:$stderr"
140 for line in "${ERRORS[@]}"; do
141 msg+="ERROR:$line"$'\n'
145 send_monitor_message_${HYPERVISOR} "$msg"
151 send_result_${HYPERVISOR} "ERROR: $@"
153 # Use return instead of exit. The set -x options will terminate the script
154 # but will also trigger ERR traps if defined.
159 echo "Warning: $@" >&2
160 send_monitor_message_${HYPERVISOR} "WARNING: $@"
163 report_task_start() {
164 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
168 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
173 # Credits to psomas@grnet.gr for this ...
174 echo o > /proc/sysrq-trigger
182 if [ -e "$root_dir/etc/debian_version" ]; then
184 elif [ -e "$root_dir/etc/redhat-release" ]; then
186 elif [ -e "$root_dir/etc/slackware-version" ]; then
188 elif [ -e "$root_dir/etc/SuSE-release" ]; then
190 elif [ -e "$root_dir/etc/gentoo-release" ]; then
192 elif [ -e "$root_dir/etc/arch-release" ]; then
194 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
196 elif [ -e "$root_dir/etc/release" ]; then
197 if grep -in netbsd "$root_dir/etc/release" &> /dev/null; then
200 warn "Unknown Unix flavor."
202 elif [ -e "$root_dir/etc/magic" ]; then
203 if grep -in openbsd "$root_dir/etc/magic" &> /dev/null; then
206 warn "Unknown Unix flavor"
209 warn "Unknown base distro."
214 local root_dir distro
217 if [ -e "$root_dir/etc/debian_version" ]; then
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
226 elif [ -e "$root_dir/etc/fedora-release" ]; then
228 elif [ -e "$root_dir/etc/centos-release" ]; then
230 elif [ -e "$root_dir/etc/redhat-release" ]; then
232 elif [ -e "$root_dir/etc/slackware-version" ]; then
234 elif [ -e "$root_dir/etc/SuSE-release" ]; then
236 elif [ -e "$root_dir/etc/gentoo-release" ]; then
238 elif [ -e "$root_dir/etc/arch-release" ]; then
240 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
242 elif [ -e "$root_dir/etc/release" ]; then
243 if grep -in netbsd "$root_dir/etc/release" &> /dev/null; then
246 warn "Unknown Unix flavor"
248 elif [ -e "$root_dir/etc/magic" ]; then
249 if grep -in openbsd "$root_dir/etc/magic" &> /dev/null; then
252 warn "Unknown Unix flavor"
255 warn "Unknown distro."
259 get_partition_table() {
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."
272 get_partition_table_type() {
273 local ptable dev field
276 dev="$(sed -n 2p <<< "$ptable")"
277 IFS=':' read -ra field <<< "$dev"
282 get_partition_count() {
285 expr $(echo "$ptable" | wc -l) - 2
288 get_partition_by_num() {
292 grep "^$id:" <<< "$ptable"
295 get_last_partition() {
298 echo "$ptable" | tail -1
301 is_extended_partition() {
305 id=$($SFDISK --force --print-id "$dev" "$part_num")
306 if [ "$id" = "5" -o "$id" = "f" ]; then
313 get_extended_partition() {
316 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
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
328 get_logical_partitions() {
329 local ptable part_num
332 tail -n +3 <<< "$ptable" | while read line; do
333 part_num=$(cut -d':' -f1 <<< "$line")
334 if [ $part_num -ge 5 ]; then
342 get_last_primary_partition() {
343 local ptable dev output
345 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
348 if output=$(grep "^$i:" <<< "$ptable"); then
356 get_partition_to_resize() {
357 local dev table table_type last_part last_part_num extended last_primary \
361 table=$(get_partition_table "$dev")
362 if [ -z "$table" ]; then
366 if [ $(get_partition_count "$table") -eq 0 ]; then
370 table_type=$(get_partition_table_type "$table")
371 last_part=$(get_last_partition "$table")
372 last_part_num=$(cut -d: -f1 <<< "$last_part")
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")
380 if [ "$ext_num" != "$last_prim_num" ]; then
381 echo "$last_prim_num"
383 echo "$last_part_num"
386 echo "$last_part_num"
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]//,/ }"
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
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"
418 enlarge_partition() {
419 local device part ptype new_end fields new_part table logical id
425 if [ -z "$new_end" ]; then
426 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
430 IFS=":;" read -ra fields <<< "$part"
434 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
435 new_part="$new_part":"${fields[$i]}"
437 new_part=${new_part:1}
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")"
447 $PARTED -s -m "$device" rm "$id"
448 create_partition "$device" "$new_part" "$ptype"
450 if [ "$ptype" = "extended" ]; then
451 # Recreate logical partitions
452 echo "$logical" | while read logical_part; do
453 create_partition "$device" "$logical_part" "logical"
458 get_last_free_sector() {
459 local dev unit last_line ptype
463 if [ -n "$unit" ]; then
467 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
468 ptype="$(cut -d: -f 5 <<< "$last_line")"
470 if [ "$ptype" = "free;" ]; then
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
492 # Unmount file systems mounted under directory `target'
493 mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
495 for mpoint in $mpoints; do
504 ufs="$($DUMPFS_UFS "$device" | head -1 | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }')"
514 log_error "Unsupported UFS type: \`$ufs' in device $device"
521 # if something fails here, it shouldn't call cleanup again...
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..."
544 if [ "$?" != "0" ]; then
555 if [ $rc -eq 0 ]; then
564 check_if_excluded() {
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."
577 send_result_${HYPERVISOR} "SUCCESS"
583 STDERR_FILE=$(mktemp)
584 add_cleanup rm -f "$STDERR_FILE"
585 exec 2> >(tee -a "$STDERR_FILE" >&2)
587 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :