1 # Copyright (C) 2011 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
23 PROGNAME=$(basename $0)
25 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
38 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
57 if [ ${#ERRORS[*]} -eq 0 ]; then
58 # No error message. Print stderr
60 lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
61 echo -n "STDERR:${lines}:" > "$MONITOR"
62 tail --lines=$lines "$STDERR_FILE" > "$MONITOR"
64 for line in "${ERRORS[@]}"; do
65 echo "ERROR:$line" > "$MONITOR"
72 echo "ERROR: $@" | tee $RESULT >&2
74 # Use return instead of exit. The set -x options will terminate the script
75 # but will also trigger ERR traps if defined.
80 echo "Warning: $@" >&2
81 echo "WARNING:$@" > "$MONITOR"
85 echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
89 echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
94 # Credits to psomas@grnet.gr for this ...
95 echo o > /proc/sysrq-trigger
103 if [ -e "$root_dir/etc/debian_version" ]; then
105 elif [ -e "$root_dir/etc/redhat-release" ]; then
107 elif [ -e "$root_dir/etc/slackware-version" ]; then
109 elif [ -e "$root_dir/etc/SuSE-release" ]; then
111 elif [ -e "$root_dir/etc/gentoo-release" ]; then
113 elif [ -e "$root_dir/etc/arch-release" ]; then
116 warn "Unknown base distro."
121 local root_dir distro
124 if [ -e "$root_dir/etc/debian_version" ]; then
126 if [ -e ${root_dir}/etc/lsb-release ]; then
127 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
128 if [ "x$ID" = "xUbuntu" ]; then
133 elif [ -e "$root_dir/etc/fedora-release" ]; then
135 elif [ -e "$root_dir/etc/centos-release" ]; then
137 elif [ -e "$root_dir/etc/redhat-release" ]; then
139 elif [ -e "$root_dir/etc/slackware-version" ]; then
141 elif [ -e "$root_dir/etc/SuSE-release" ]; then
143 elif [ -e "$root_dir/etc/gentoo-release" ]; then
145 elif [ -e "$root_dir/etc/arch-release" ]; then
148 warn "Unknown distro."
153 get_partition_table() {
156 # If the partition table is gpt then parted will raise an error if the
157 # secondary gpt is not it the end of the disk, and a warning that has to
158 # do with the "Last Usable LBA" entry in gpt.
159 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
160 log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
166 get_partition_table_type() {
167 local ptable dev field
170 dev="$(sed -n 2p <<< "$ptable")"
171 IFS=':' read -ra field <<< "$dev"
176 get_partition_count() {
179 expr $(echo "$ptable" | wc -l) - 2
182 get_partition_by_num() {
186 grep "^$id:" <<< "$ptable"
189 get_last_partition() {
192 echo "$ptable" | tail -1
195 is_extended_partition() {
199 id=$($SFDISK --print-id "$dev" "$part_num")
200 if [ "$id" = "5" -o "$id" = "f" ]; then
207 get_extended_partition() {
210 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
212 tail -n +3 <<< "$ptable" | while read line; do
213 part_num=$(cut -d':' -f1 <<< "$line")
214 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
222 get_logical_partitions() {
223 local ptable part_num
226 tail -n +3 <<< "$ptable" | while read line; do
227 part_num=$(cut -d':' -f1 <<< "$line")
228 if [ $part_num -ge 5 ]; then
236 get_last_primary_partition() {
237 local ptable dev output
239 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
242 if output=$(grep "^$i:" <<< "$ptable"); then
250 get_partition_to_resize() {
251 local dev table table_type last_part last_part_num extended last_primary \
255 table=$(get_partition_table "$dev")
257 if [ $(get_partition_count "$table") -eq 0 ]; then
261 table_type=$(get_partition_table_type "$table")
262 last_part=$(get_last_partition "$table")
263 last_part_num=$(cut -d: -f1 <<< "$last_part")
265 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
266 extended=$(get_extended_partition "$table")
267 last_primary=$(get_last_primary_partition "$table")
268 ext_num=$(cut -d: -f1 <<< "$extended")
269 prim_num=$(cut -d: -f1 <<< "$last_primary")
271 if [ "$ext_num" != "$last_prim_num" ]; then
272 echo "$last_prim_num"
274 echo "$last_part_num"
277 echo "$last_part_num"
287 IFS=":;" read -ra fields <<< "$part"
288 local id="${fields[0]}"
289 local start="${fields[1]}"
290 local end="${fields[2]}"
291 local size="${fields[3]}"
292 local fs="${fields[4]}"
293 local name="${fields[5]}"
294 local flags="${fields[6]//,/ }"
296 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
297 for flag in $flags; do
298 $PARTED -s -m $device set "$id" "$flag" on
302 enlarge_partition() {
303 local device part ptype new_end fields new_part table logical id
309 if [ -z "$new_end" ]; then
310 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
314 IFS=":;" read -ra fields <<< "$part"
318 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
319 new_part="$new_part":"${fields[$i]}"
321 new_part=${new_part:1}
323 # If this is an extended partition, removing it will also remove the
324 # logical partitions it contains. We need to save them for later.
325 if [ "$ptype" = "extended" ]; then
326 table="$(get_partition_table "$device")"
327 logical="$(get_logical_partitions "$table")"
331 $PARTED -s -m "$device" rm "$id"
332 create_partition "$device" "$new_part" "$ptype"
334 if [ "$ptype" = "extended" ]; then
335 # Recreate logical partitions
336 echo "$logical" | while read logical_part; do
337 create_partition "$device" "$logical_part" "logical"
342 get_last_free_sector() {
343 local dev unit last_line ptype
347 if [ -n "$unit" ]; then
351 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
352 ptype="$(cut -d: -f 5 <<< "$last_line")"
354 if [ "$ptype" = "free;" ]; then
363 # Workaround to search for $target/Unattend.xml in an case insensitive way.
364 exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
365 if [ $(wc -l <<< "$exists") -gt 1 ]; then
366 log_error "Found multiple Unattend.xml files in the image:" $exists
373 # if something fails here, it shouldn't call cleanup again...
376 if [ ${#CLEANUP[*]} -gt 0 ]; then
377 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
378 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
379 for i in $REVERSE_INDEXES; do
380 # If something fails here, it's better to retry it for a few times
381 # before we give up with an error. This is needed for kpartx when
382 # dealing with ntfs partitions mounted through fuse. umount is not
383 # synchronous and may return while the partition is still busy. A
384 # premature attempt to delete partition mappings through kpartx on
385 # a device that hosts previously mounted ntfs partition may fail
386 # with a `device-mapper: remove ioctl failed: Device or resource
387 # busy' error. A sensible workaround for this is to wait for a
388 # while and then try again.
389 local cmd=${CLEANUP[$i]}
390 $cmd || for interval in 0.25 0.5 1 2 4; do
391 echo "Command $cmd failed!"
392 echo "I'll wait for $interval secs and will retry..."
396 if [ "$?" != "0" ]; then
407 if [ $rc -eq 0 ]; then
416 check_if_excluded() {
418 name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
419 exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
420 if [ -n "${!exclude}" ]; then
421 warn "Task ${PROGNAME:2} was excluded and will not run."
431 STDERR_FILE=$(mktemp)
432 add_cleanup rm -f "$STDERR_FILE"
433 exec 2> >(tee -a "$STDERR_FILE" >&2)
435 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :