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
44 MSG_TYPE_TASK_START="TASK_START"
45 MSG_TYPE_TASK_END="TASK_END"
51 for arg; do cmd+=$(printf "%q " "$arg"); done
57 echo "ERROR: $@" | tee $RESULT >&2
62 echo "Warning: $@" >&2
63 echo "WARNING:$@" > "$MONITOR"
67 echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
71 echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
75 if [ ${#ERRORS[*]} -eq 0 ]; then
76 # No error message. Print stderr
77 local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
78 echo -n "STDERR:${lines}:" > "$MONITOR"
79 tail --lines=$lines "$STDERR_FILE" > "$MONITOR"
81 echo -n "ERROR:" > "$MONITOR"
82 for line in "${ERRORS[@]}"; do
83 echo "$line" > "$MONITOR"
89 echo o > /proc/sysrq-trigger
95 if [ -e "$root_dir/etc/debian_version" ]; then
97 elif [ -e "$root_dir/etc/redhat-release" ]; then
99 elif [ -e "$root_dir/etc/slackware-version" ]; then
101 elif [ -e "$root_dir/etc/SuSE-release" ]; then
103 elif [ -e "$root_dir/etc/gentoo-release" ]; then
105 elif [ -e "$root_dir/etc/arch-release" ]; then
108 warn "Unknown base distro."
115 if [ -e "$root_dir/etc/debian_version" ]; then
117 if [ -e ${root_dir}/etc/lsb-release ]; then
118 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
119 if [ "x$ID" = "xUbuntu" ]; then
124 elif [ -e "$root_dir/etc/fedora-release" ]; then
126 elif [ -e "$root_dir/etc/centos-release" ]; then
128 elif [ -e "$root_dir/etc/redhat-release" ]; then
130 elif [ -e "$root_dir/etc/slackware-version" ]; then
132 elif [ -e "$root_dir/etc/SuSE-release" ]; then
134 elif [ -e "$root_dir/etc/gentoo-release" ]; then
136 elif [ -e "$root_dir/etc/arch-release" ]; then
139 warn "Unknown distro."
144 get_partition_table() {
146 # If the partition table is gpt then parted will raise an error if the
147 # secondary gpt is not it the end of the disk, and a warning that has to
148 # do with the "Last Usable LBA" entry in gpt.
149 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
150 log_error "Unable to read partition table for device \`${dev}'"
156 get_partition_table_type() {
159 local dev="$(sed -n 2p <<< "$ptable")"
161 IFS=':' read -ra field <<< "$dev"
166 get_partition_count() {
169 expr $(echo "$ptable" | wc -l) - 2
172 get_partition_by_num() {
176 grep "^$id:" <<< "$ptable"
179 get_last_partition() {
182 echo "$ptable" | tail -1
185 is_extended_partition() {
189 id=$($SFDISK --print-id "$dev" "$part_num")
190 if [ "$id" = "5" ]; then
197 get_extended_partition() {
199 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
201 tail -n +3 <<< "$ptable" | while read line; do
202 part_num=$(cut -d':' -f1 <<< "$line")
203 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
211 get_logical_partitions() {
214 tail -n +3 <<< "$ptable" | while read line; do
215 part_num=$(cut -d':' -f1 <<< "$line")
216 if [ $part_num -ge 5 ]; then
224 get_last_primary_partition() {
226 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
229 if output=$(grep "^$i:" <<< "$ptable"); then
237 get_partition_to_resize() {
240 table=$(get_partition_table "$dev")
242 if [ $(get_partition_count "$table") -eq 0 ]; then
246 table_type=$(get_partition_table_type "$table")
247 last_part=$(get_last_partition "$table")
248 last_part_num=$(cut -d: -f1 <<< "$last_part")
250 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
251 extended=$(get_extended_partition "$table")
252 last_primary=$(get_last_primary_partition "$table")
253 ext_num=$(cut -d: -f1 <<< "$extended")
254 prim_num=$(cut -d: -f1 <<< "$last_primary")
256 if [ "$ext_num" != "$last_prim_num" ]; then
257 echo "$last_prim_num"
259 echo "$last_part_num"
262 echo "$last_part_num"
272 IFS=":;" read -ra fields <<< "$part"
273 local id="${fields[0]}"
274 local start="${fields[1]}"
275 local end="${fields[2]}"
276 local size="${fields[3]}"
277 local fs="${fields[4]}"
278 local name="${fields[5]}"
279 local flags="${fields[6]//,/ }"
281 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
282 for flag in $flags; do
283 $PARTED -s -m $device set "$id" "$flag" on
287 enlarge_partition() {
293 if [ -z "$new_end" ]; then
294 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
298 IFS=":;" read -ra fields <<< "$part"
302 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
303 new_part="$new_part":"${fields[$i]}"
305 new_part=${new_part:1}
307 # If this is an extended partition, removing it will also remove the
308 # logical partitions it contains. We need to save them for later.
309 if [ "$ptype" = "extended" ]; then
310 local table="$(get_partition_table "$device")"
311 local logical="$(get_logical_partitions "$table")"
315 $PARTED -s -m "$device" rm "$id"
316 create_partition "$device" "$new_part" "$ptype"
318 if [ "$ptype" = "extended" ]; then
319 # Recreate logical partitions
320 echo "$logical" | while read logical_part; do
321 create_partition "$device" "$logical_part" "logical"
326 get_last_free_sector() {
330 if [ -n "$unit" ]; then
334 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
335 local ptype="$(cut -d: -f 5 <<< "$last_line")"
337 if [ "$ptype" = "free;" ]; then
343 # if something fails here, it shouldn't call cleanup again...
346 if [ ${#CLEANUP[*]} -gt 0 ]; then
347 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
348 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
349 for i in $REVERSE_INDEXES; do
350 # If something fails here, it's better to retry it for a few times
351 # before we give up with an error. This is needed for kpartx when
352 # dealing with ntfs partitions mounted through fuse. umount is not
353 # synchronous and may return while the partition is still busy. A
354 # premature attempt to delete partition mappings through kpartx on
355 # a device that hosts previously mounted ntfs partition may fail
356 # with a `device-mapper: remove ioctl failed: Device or resource
357 # busy' error. A sensible workaround for this is to wait for a
358 # while and then try again.
359 local cmd=${CLEANUP[$i]}
360 $cmd || for interval in 0.25 0.5 1 2 4; do
361 echo "Command $cmd failed!"
362 echo "I'll wait for $interval secs and will retry..."
366 if [ "$?" != "0" ]; then
377 if [ $rc -eq 0 ]; then
386 check_if_excluded() {
387 local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
388 local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
389 if [ -n "${!exclude}" ]; then
390 warn "Task ${PROGNAME:2} was excluded and will not run."
400 STDERR_FILE=$(mktemp)
401 add_cleanup rm -f "$STDERR_FILE"
402 exec 2> >(tee -a "$STDERR_FILE" >&2)
404 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :