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 GROWFS_OPENBSD=growfs.openbsd
38 DUMPFS_OPENBSD=dumpfs.openbsd
39 DATE="date -u" # Time in UTC
47 MSG_TYPE_TASK_START="TASK_START"
48 MSG_TYPE_TASK_END="TASK_END"
54 for arg; do cmd+=$(printf "%q " "$arg"); done
65 echo "$@" > /dev/ttyS1
68 send_monitor_message_kvm() {
69 echo "$@" > /dev/ttyS2
73 xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
76 send_monitor_message_xen() {
77 #Broadcast the message
78 echo "$@" | socat "STDIO" "UDP-DATAGRAM:${BROADCAST}:${MONITOR_PORT},broadcast"
82 local cmdline item key val hypervisor domid
84 read -a cmdline < /proc/cmdline
85 for item in "${cmdline[@]}"; do
86 key=$(cut -d= -f1 <<< "$item")
87 val=$(cut -d= -f2 <<< "$item")
88 if [ "$key" = "hypervisor" ]; then
91 if [ "$key" = "rules_dev" ]; then
92 export RULES_DEV="$val"
94 if [ "$key" = "helper_ip" ]; then
96 export NETWORK="$IP/24"
97 export BROADCAST="${IP%.*}.255"
99 if [ "$key" = "monitor_port" ]; then
100 export MONITOR_PORT="$val"
104 case "$hypervisor" in
109 if [ -z "$IP" ]; then
110 echo "ERROR: \`helper_ip' not defined or empty" >&2
113 if [ -z "$MONITOR_PORT" ]; then
114 echo "ERROR: \`monitor_port' not defined or empty" >&2
117 $MOUNT -t xenfs xenfs /proc/xen
118 ip addr add "$NETWORK" dev eth0
120 ip route add default dev eth0
121 export DOMID=$(xenstore-read domid)
125 echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
135 if [ ${#ERRORS[*]} -eq 0 ]; then
136 # No error message. Print stderr
138 stderr="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
139 lines=$(wc -l <<< "$stderr")
140 msg="STDERR:${lines}:$stderr"
142 for line in "${ERRORS[@]}"; do
143 msg+="ERROR:$line"$'\n'
147 send_monitor_message_${HYPERVISOR} "$msg"
153 send_result_${HYPERVISOR} "ERROR: $@"
155 # Use return instead of exit. The set -x options will terminate the script
156 # but will also trigger ERR traps if defined.
161 echo "Warning: $@" >&2
162 send_monitor_message_${HYPERVISOR} "WARNING: $@"
165 report_task_start() {
166 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
170 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
175 # Credits to psomas@grnet.gr for this ...
176 echo o > /proc/sysrq-trigger
184 if [ -e "$root_dir/etc/debian_version" ]; then
186 elif [ -e "$root_dir/etc/redhat-release" ]; then
188 elif [ -e "$root_dir/etc/slackware-version" ]; then
190 elif [ -e "$root_dir/etc/SuSE-release" ]; then
192 elif [ -e "$root_dir/etc/gentoo-release" ]; then
194 elif [ -e "$root_dir/etc/arch-release" ]; then
196 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
198 elif [ -e "$root_dir/etc/release" ]; then
199 if grep -i netbsd "$root_dir/etc/release" &> /dev/null; then
202 warn "Unknown Unix flavor."
204 elif [ -e "$root_dir/etc/motd" ]; then
205 if grep -i ^openbsd <(head -1 "$root_dir/etc/motd") &> /dev/null; then
208 warn "Unknown Unix flavor"
211 warn "Unknown base distro."
216 local root_dir distro
219 if [ -e "$root_dir/etc/debian_version" ]; then
221 if [ -e ${root_dir}/etc/lsb-release ]; then
222 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
223 if [ "x$ID" = "xUbuntu" ]; then
228 elif [ -e "$root_dir/etc/fedora-release" ]; then
230 elif [ -e "$root_dir/etc/centos-release" ]; then
232 elif [ -e "$root_dir/etc/redhat-release" ]; then
234 elif [ -e "$root_dir/etc/slackware-version" ]; then
236 elif [ -e "$root_dir/etc/SuSE-release" ]; then
238 elif [ -e "$root_dir/etc/gentoo-release" ]; then
240 elif [ -e "$root_dir/etc/arch-release" ]; then
242 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
244 elif [ -e "$root_dir/etc/release" ]; then
245 if grep -in netbsd "$root_dir/etc/release" &> /dev/null; then
248 warn "Unknown Unix flavor"
250 elif [ -e "$root_dir/etc/motd" ]; then
251 if grep -i ^openbsd <(head -1 "$root_dir/etc/motd") &> /dev/null; then
254 warn "Unknown Unix flavor"
257 warn "Unknown distro."
261 get_partition_table() {
264 # If the partition table is gpt then parted will raise an error if the
265 # secondary gpt is not it the end of the disk, and a warning that has to
266 # do with the "Last Usable LBA" entry in gpt.
267 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
268 log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
274 get_partition_table_type() {
275 local ptable dev field
278 dev="$(sed -n 2p <<< "$ptable")"
279 IFS=':' read -ra field <<< "$dev"
284 get_partition_count() {
287 expr $(echo "$ptable" | wc -l) - 2
290 get_partition_by_num() {
294 grep "^$id:" <<< "$ptable"
297 get_last_partition() {
300 echo "$ptable" | tail -1
303 is_extended_partition() {
307 id=$($SFDISK --force --print-id "$dev" "$part_num")
308 if [ "$id" = "5" -o "$id" = "f" ]; then
315 get_extended_partition() {
318 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
320 tail -n +3 <<< "$ptable" | while read line; do
321 part_num=$(cut -d':' -f1 <<< "$line")
322 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
330 get_logical_partitions() {
331 local ptable part_num
334 tail -n +3 <<< "$ptable" | while read line; do
335 part_num=$(cut -d':' -f1 <<< "$line")
336 if [ $part_num -ge 5 ]; then
344 get_last_primary_partition() {
345 local ptable dev output
347 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
350 if output=$(grep "^$i:" <<< "$ptable"); then
358 get_partition_to_resize() {
359 local dev table table_type last_part last_part_num extended last_primary \
363 table=$(get_partition_table "$dev")
364 if [ -z "$table" ]; then
368 if [ $(get_partition_count "$table") -eq 0 ]; then
372 table_type=$(get_partition_table_type "$table")
373 last_part=$(get_last_partition "$table")
374 last_part_num=$(cut -d: -f1 <<< "$last_part")
376 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
377 extended=$(get_extended_partition "$table")
378 last_primary=$(get_last_primary_partition "$table")
379 ext_num=$(cut -d: -f1 <<< "$extended")
380 last_prim_num=$(cut -d: -f1 <<< "$last_primary")
382 if [ "$ext_num" != "$last_prim_num" ]; then
383 echo "$last_prim_num"
385 echo "$last_part_num"
388 echo "$last_part_num"
398 IFS=":;" read -ra fields <<< "$part"
399 local id="${fields[0]}"
400 local start="${fields[1]}"
401 local end="${fields[2]}"
402 local size="${fields[3]}"
403 local fs="${fields[4]}"
404 local name="${fields[5]}"
405 local flags="${fields[6]//,/ }"
407 if [ "$ptype" = "primary" -o "$ptype" = "logical" -o "$ptype" = "extended" ]; then
408 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
409 for flag in $flags; do
410 $PARTED -s -m $device set "$id" "$flag" on
414 start=${start:0:${#start}-1} # remove the s at the end
415 end=${end:0:${#end}-1} # remove the s at the end
416 $SGDISK -n "$id":"$start":"$end" -t "$id":"$ptype" "$device"
420 enlarge_partition() {
421 local device part ptype new_end fields new_part table logical id
427 if [ -z "$new_end" ]; then
428 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
432 IFS=":;" read -ra fields <<< "$part"
436 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
437 new_part="$new_part":"${fields[$i]}"
439 new_part=${new_part:1}
441 # If this is an extended partition, removing it will also remove the
442 # logical partitions it contains. We need to save them for later.
443 if [ "$ptype" = "extended" ]; then
444 table="$(get_partition_table "$device")"
445 logical="$(get_logical_partitions "$table")"
449 $PARTED -s -m "$device" rm "$id"
450 create_partition "$device" "$new_part" "$ptype"
452 if [ "$ptype" = "extended" ]; then
453 # Recreate logical partitions
454 echo "$logical" | while read logical_part; do
455 create_partition "$device" "$logical_part" "logical"
460 get_last_free_sector() {
461 local dev unit last_line ptype
465 if [ -n "$unit" ]; then
469 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
470 ptype="$(cut -d: -f 5 <<< "$last_line")"
472 if [ "$ptype" = "free;" ]; then
481 # Workaround to search for $target/Unattend.xml in an case insensitive way.
482 exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
483 if [ $(wc -l <<< "$exists") -gt 1 ]; then
484 log_error "Found multiple Unattend.xml files in the image:" $exists
494 # Unmount file systems mounted under directory `target'
495 mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
497 for mpoint in $mpoints; do
506 ufs="$($DUMPFS_UFS "$device" | head -1 | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }')"
516 log_error "Unsupported UFS type: \`$ufs' in device $device"
523 # if something fails here, it shouldn't call cleanup again...
526 if [ ${#CLEANUP[*]} -gt 0 ]; then
527 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
528 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
529 for i in $REVERSE_INDEXES; do
530 # If something fails here, it's better to retry it for a few times
531 # before we give up with an error. This is needed for kpartx when
532 # dealing with ntfs partitions mounted through fuse. umount is not
533 # synchronous and may return while the partition is still busy. A
534 # premature attempt to delete partition mappings through kpartx on
535 # a device that hosts previously mounted ntfs partition may fail
536 # with a `device-mapper: remove ioctl failed: Device or resource
537 # busy' error. A sensible workaround for this is to wait for a
538 # while and then try again.
539 local cmd=${CLEANUP[$i]}
540 $cmd || for interval in 0.25 0.5 1 2 4; do
541 echo "Command $cmd failed!"
542 echo "I'll wait for $interval secs and will retry..."
546 if [ "$?" != "0" ]; then
557 if [ $rc -eq 0 ]; then
566 check_if_excluded() {
568 name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
569 exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
570 if [ -n "${!exclude}" ]; then
571 warn "Task ${PROGNAME:2} was excluded and will not run."
579 send_result_${HYPERVISOR} "SUCCESS"
585 STDERR_FILE=$(mktemp)
586 add_cleanup rm -f "$STDERR_FILE"
587 exec 2> >(tee -a "$STDERR_FILE" >&2)
589 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :