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
34 DATE="date -u" # Time in UTC
42 MSG_TYPE_TASK_START="TASK_START"
43 MSG_TYPE_TASK_END="TASK_END"
49 for arg; do cmd+=$(printf "%q " "$arg"); done
60 echo "$@" > /dev/ttyS1
63 send_monitor_message_kvm() {
64 echo "$@" > /dev/ttyS2
68 xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
71 send_monitor_message_xen() {
72 #Broadcast the message
73 echo "$@" | socat "STDIO" "UDP-DATAGRAM:${BROADCAST}:${MONITOR_PORT},broadcast"
77 local cmdline item key val hypervisor domid
79 read -a cmdline < /proc/cmdline
80 for item in "${cmdline[@]}"; do
81 key=$(cut -d= -f1 <<< "$item")
82 val=$(cut -d= -f2 <<< "$item")
83 if [ "$key" = "hypervisor" ]; then
86 if [ "$key" = "rules_dev" ]; then
87 export RULES_DEV="$val"
89 if [ "$key" = "helper_ip" ]; then
91 export NETWORK="$IP/24"
92 export BROADCAST="${IP%.*}.255"
94 if [ "$key" = "monitor_port" ]; then
95 export MONITOR_PORT="$val"
104 if [ -z "$IP" ]; then
105 echo "ERROR: \`helper_ip' not defined or empty" >&2
108 if [ -z "$MONITOR_PORT" ]; then
109 echo "ERROR: \`monitor_port' not defined or empty" >&2
112 $MOUNT -t xenfs xenfs /proc/xen
113 ip addr add "$NETWORK" dev eth0
115 ip route add default dev eth0
116 export DOMID=$(xenstore-read domid)
120 echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
130 if [ ${#ERRORS[*]} -eq 0 ]; then
131 # No error message. Print stderr
133 lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
134 msg="STDERR:${lines}"
135 msg+=$(tail --lines=$lines "$STDERR_FILE")
137 for line in "${ERRORS[@]}"; do
138 msg+="ERROR:$line"$'\n'
142 send_monitor_message_${HYPERVISOR} "$msg"
148 send_monitor_message_${HYPERVISOR} "ERROR: $@"
149 send_result_${HYPERVISOR} "ERROR: $@"
151 # Use return instead of exit. The set -x options will terminate the script
152 # but will also trigger ERR traps if defined.
157 echo "Warning: $@" >&2
158 send_monitor_message_${HYPERVISOR} "WARNING: $@"
161 report_task_start() {
162 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
166 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
171 # Credits to psomas@grnet.gr for this ...
172 echo o > /proc/sysrq-trigger
180 if [ -e "$root_dir/etc/debian_version" ]; then
182 elif [ -e "$root_dir/etc/redhat-release" ]; then
184 elif [ -e "$root_dir/etc/slackware-version" ]; then
186 elif [ -e "$root_dir/etc/SuSE-release" ]; then
188 elif [ -e "$root_dir/etc/gentoo-release" ]; then
190 elif [ -e "$root_dir/etc/arch-release" ]; then
193 warn "Unknown base distro."
198 local root_dir distro
201 if [ -e "$root_dir/etc/debian_version" ]; then
203 if [ -e ${root_dir}/etc/lsb-release ]; then
204 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
205 if [ "x$ID" = "xUbuntu" ]; then
210 elif [ -e "$root_dir/etc/fedora-release" ]; then
212 elif [ -e "$root_dir/etc/centos-release" ]; then
214 elif [ -e "$root_dir/etc/redhat-release" ]; then
216 elif [ -e "$root_dir/etc/slackware-version" ]; then
218 elif [ -e "$root_dir/etc/SuSE-release" ]; then
220 elif [ -e "$root_dir/etc/gentoo-release" ]; then
222 elif [ -e "$root_dir/etc/arch-release" ]; then
225 warn "Unknown distro."
230 get_partition_table() {
233 # If the partition table is gpt then parted will raise an error if the
234 # secondary gpt is not it the end of the disk, and a warning that has to
235 # do with the "Last Usable LBA" entry in gpt.
236 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
237 log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
243 get_partition_table_type() {
244 local ptable dev field
247 dev="$(sed -n 2p <<< "$ptable")"
248 IFS=':' read -ra field <<< "$dev"
253 get_partition_count() {
256 expr $(echo "$ptable" | wc -l) - 2
259 get_partition_by_num() {
263 grep "^$id:" <<< "$ptable"
266 get_last_partition() {
269 echo "$ptable" | tail -1
272 is_extended_partition() {
276 id=$($SFDISK --print-id "$dev" "$part_num")
277 if [ "$id" = "5" -o "$id" = "f" ]; then
284 get_extended_partition() {
287 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
289 tail -n +3 <<< "$ptable" | while read line; do
290 part_num=$(cut -d':' -f1 <<< "$line")
291 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
299 get_logical_partitions() {
300 local ptable part_num
303 tail -n +3 <<< "$ptable" | while read line; do
304 part_num=$(cut -d':' -f1 <<< "$line")
305 if [ $part_num -ge 5 ]; then
313 get_last_primary_partition() {
314 local ptable dev output
316 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
319 if output=$(grep "^$i:" <<< "$ptable"); then
327 get_partition_to_resize() {
328 local dev table table_type last_part last_part_num extended last_primary \
332 table=$(get_partition_table "$dev")
334 if [ $(get_partition_count "$table") -eq 0 ]; then
338 table_type=$(get_partition_table_type "$table")
339 last_part=$(get_last_partition "$table")
340 last_part_num=$(cut -d: -f1 <<< "$last_part")
342 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
343 extended=$(get_extended_partition "$table")
344 last_primary=$(get_last_primary_partition "$table")
345 ext_num=$(cut -d: -f1 <<< "$extended")
346 last_prim_num=$(cut -d: -f1 <<< "$last_primary")
348 if [ "$ext_num" != "$last_prim_num" ]; then
349 echo "$last_prim_num"
351 echo "$last_part_num"
354 echo "$last_part_num"
364 IFS=":;" read -ra fields <<< "$part"
365 local id="${fields[0]}"
366 local start="${fields[1]}"
367 local end="${fields[2]}"
368 local size="${fields[3]}"
369 local fs="${fields[4]}"
370 local name="${fields[5]}"
371 local flags="${fields[6]//,/ }"
373 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
374 for flag in $flags; do
375 $PARTED -s -m $device set "$id" "$flag" on
379 enlarge_partition() {
380 local device part ptype new_end fields new_part table logical id
386 if [ -z "$new_end" ]; then
387 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
391 IFS=":;" read -ra fields <<< "$part"
395 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
396 new_part="$new_part":"${fields[$i]}"
398 new_part=${new_part:1}
400 # If this is an extended partition, removing it will also remove the
401 # logical partitions it contains. We need to save them for later.
402 if [ "$ptype" = "extended" ]; then
403 table="$(get_partition_table "$device")"
404 logical="$(get_logical_partitions "$table")"
408 $PARTED -s -m "$device" rm "$id"
409 create_partition "$device" "$new_part" "$ptype"
411 if [ "$ptype" = "extended" ]; then
412 # Recreate logical partitions
413 echo "$logical" | while read logical_part; do
414 create_partition "$device" "$logical_part" "logical"
419 get_last_free_sector() {
420 local dev unit last_line ptype
424 if [ -n "$unit" ]; then
428 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
429 ptype="$(cut -d: -f 5 <<< "$last_line")"
431 if [ "$ptype" = "free;" ]; then
440 # Workaround to search for $target/Unattend.xml in an case insensitive way.
441 exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
442 if [ $(wc -l <<< "$exists") -gt 1 ]; then
443 log_error "Found multiple Unattend.xml files in the image:" $exists
453 # Unmount file systems mounted under directory `target'
454 mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
456 for mpoint in $mpoints; do
462 # if something fails here, it shouldn't call cleanup again...
465 if [ ${#CLEANUP[*]} -gt 0 ]; then
466 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
467 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
468 for i in $REVERSE_INDEXES; do
469 # If something fails here, it's better to retry it for a few times
470 # before we give up with an error. This is needed for kpartx when
471 # dealing with ntfs partitions mounted through fuse. umount is not
472 # synchronous and may return while the partition is still busy. A
473 # premature attempt to delete partition mappings through kpartx on
474 # a device that hosts previously mounted ntfs partition may fail
475 # with a `device-mapper: remove ioctl failed: Device or resource
476 # busy' error. A sensible workaround for this is to wait for a
477 # while and then try again.
478 local cmd=${CLEANUP[$i]}
479 $cmd || for interval in 0.25 0.5 1 2 4; do
480 echo "Command $cmd failed!"
481 echo "I'll wait for $interval secs and will retry..."
485 if [ "$?" != "0" ]; then
496 if [ $rc -eq 0 ]; then
505 check_if_excluded() {
507 name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
508 exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
509 if [ -n "${!exclude}" ]; then
510 warn "Task ${PROGNAME:2} was excluded and will not run."
519 send_result_${HYPERVISOR} "SUCCESS"
525 STDERR_FILE=$(mktemp)
526 add_cleanup rm -f "$STDERR_FILE"
527 exec 2> >(tee -a "$STDERR_FILE" >&2)
529 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :