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
43 MSG_TYPE_TASK_START="TASK_START"
44 MSG_TYPE_TASK_END="TASK_END"
50 for arg; do cmd+=$(printf "%q " "$arg"); done
56 echo "ERROR: $@" | tee $RESULT >&2
61 echo "Warning: $@" >&2
62 echo "WARNING:$@" > "$MONITOR"
66 echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
70 echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
74 if [ ${#ERRORS[*]} -eq 0 ]; then
75 # No error message. Print stderr
76 local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
77 echo -n "STDERR:${lines}:" > "$MONITOR"
78 tail --lines=$lines "$STDERR_FILE" > "$MONITOR"
80 echo -n "ERROR:" > "$MONITOR"
81 for line in "${ERRORS[@]}"; do
82 echo "$line" > "$MONITOR"
90 if [ -e "$root_dir/etc/debian_version" ]; then
92 elif [ -e "$root_dir/etc/redhat-release" ]; then
94 elif [ -e "$root_dir/etc/slackware-version" ]; then
96 elif [ -e "$root_dir/etc/SuSE-release" ]; then
98 elif [ -e "$root_dir/etc/gentoo-release" ]; then
100 elif [ -e "$root_dir/etc/arch-release" ]; then
103 warn "Unknown base distro."
110 if [ -e "$root_dir/etc/debian_version" ]; then
112 if [ -e ${root_dir}/etc/lsb-release ]; then
113 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
114 if [ "x$ID" = "xUbuntu" ]; then
119 elif [ -e "$root_dir/etc/fedora-release" ]; then
121 elif [ -e "$root_dir/etc/centos-release" ]; then
123 elif [ -e "$root_dir/etc/redhat-release" ]; then
125 elif [ -e "$root_dir/etc/slackware-version" ]; then
127 elif [ -e "$root_dir/etc/SuSE-release" ]; then
129 elif [ -e "$root_dir/etc/gentoo-release" ]; then
131 elif [ -e "$root_dir/etc/arch-release" ]; then
134 warn "Unknown distro."
139 get_partition_table() {
141 # If the partition table is gpt then parted will raise an error if the
142 # secondary gpt is not it the end of the disk, and a warning that has to
143 # do with the "Last Usable LBA" entry in gpt.
144 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
145 log_error "Unable to read partition table for device \`${dev}'"
151 get_partition_table_type() {
154 local dev="$(sed -n 2p <<< "$ptable")"
156 IFS=':' read -ra field <<< "$dev"
161 get_partition_count() {
164 expr $(echo "$ptable" | wc -l) - 2
167 get_partition_by_num() {
171 grep "^$id:" <<< "$ptable"
174 get_last_partition() {
177 echo "$ptable" | tail -1
180 is_extended_partition() {
184 id=$($SFDISK --print-id "$dev" "$part_num")
185 if [ "$id" = "5" ]; then
192 get_extended_partition() {
194 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
196 tail -n +3 <<< "$ptable" | while read line; do
197 part_num=$(cut -d':' -f1 <<< "$line")
198 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
206 get_logical_partitions() {
209 tail -n +3 <<< "$ptable" | while read line; do
210 part_num=$(cut -d':' -f1 <<< "$line")
211 if [ $part_num -ge 5 ]; then
219 get_last_primary_partition() {
221 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
224 if output=$(grep "^$i:" <<< "$ptable"); then
232 get_partition_to_resize() {
235 table=$(get_partition_table "$dev")
237 if [ $(get_partition_count "$table") -eq 0 ]; then
241 table_type=$(get_partition_table_type "$table")
242 last_part=$(get_last_partition "$table")
243 last_part_num=$(cut -d: -f1 <<< "$last_part")
245 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
246 extended=$(get_extended_partition "$table")
247 last_primary=$(get_last_primary_partition "$table")
248 ext_num=$(cut -d: -f1 <<< "$extended")
249 prim_num=$(cut -d: -f1 <<< "$last_primary")
251 if [ "$ext_num" != "$last_prim_num" ]; then
252 echo "$last_prim_num"
254 echo "$last_part_num"
257 echo "$last_part_num"
267 IFS=":;" read -ra fields <<< "$part"
268 local id="${fields[0]}"
269 local start="${fields[1]}"
270 local end="${fields[2]}"
271 local size="${fields[3]}"
272 local fs="${fields[4]}"
273 local name="${fields[5]}"
274 local flags="${fields[6]//,/ }"
276 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
277 for flag in $flags; do
278 $PARTED -s -m $device set "$id" "$flag" on
282 enlarge_partition() {
288 if [ -z "$new_end" ]; then
289 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
293 IFS=":;" read -ra fields <<< "$part"
297 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
298 new_part="$new_part":"${fields[$i]}"
300 new_part=${new_part:1}
302 # If this is an extended partition, removing it will also remove the
303 # logical partitions it contains. We need to save them for later.
304 if [ "$ptype" = "extended" ]; then
305 local table="$(get_partition_table "$device")"
306 local logical="$(get_logical_partitions "$table")"
310 $PARTED -s -m "$device" rm "$id"
311 create_partition "$device" "$new_part" "$ptype"
313 if [ "$ptype" = "extended" ]; then
314 # Recreate logical partitions
315 echo "$logical" | while read logical_part; do
316 create_partition "$device" "$logical_part" "logical"
321 get_last_free_sector() {
325 if [ -n "$unit" ]; then
329 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
330 local ptype="$(cut -d: -f 5 <<< "$last_line")"
332 if [ "$ptype" = "free;" ]; then
338 # if something fails here, it shouldn't call cleanup again...
341 if [ ${#CLEANUP[*]} -gt 0 ]; then
342 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
343 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
344 for i in $REVERSE_INDEXES; do
345 # If something fails here, it's better to retry it for a few times
346 # before we give up with an error. This is needed for kpartx when
347 # dealing with ntfs partitions mounted through fuse. umount is not
348 # synchronous and may return while the partition is still busy. A
349 # premature attempt to delete partition mappings through kpartx on
350 # a device that hosts previously mounted ntfs partition may fail
351 # with a `device-mapper: remove ioctl failed: Device or resource
352 # busy' error. A sensible workaround for this is to wait for a
353 # while and then try again.
354 local cmd=${CLEANUP[$i]}
355 $cmd || for interval in 0.25 0.5 1 2 4; do
356 echo "Command $cmd failed!"
357 echo "I'll wait for $interval secs and will retry..."
361 if [ "$?" != "0" ]; then
372 if [ $rc -eq 0 ]; then
381 check_if_excluded() {
382 local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
383 local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
384 if [ -n "${!exclude}" ]; then
385 warn "Task ${PROGNAME:2} was excluded and will not run."
395 STDERR_FILE=$(mktemp)
396 add_cleanup rm -f "$STDERR_FILE"
397 exec 2> >(tee -a "$STDERR_FILE" >&2)
399 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :