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
21 PROGNAME=$(basename $0)
23 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
37 for arg; do cmd+=$(printf "%q " "$arg"); done
42 echo "ERROR: $@" | tee $RESULT >&2
47 echo "Warning: $@" >&2
53 if [ -e "$root_dir/etc/debian_version" ]; then
55 elif [ -e "$root_dir/etc/redhat-release" ]; then
57 elif [ -e "$root_dir/etc/slackware-version" ]; then
59 elif [ -e "$root_dir/etc/SuSE-release" ]; then
61 elif [ -e "$root_dir/etc/gentoo-release" ]; then
64 warn "Unknown base distro."
71 if [ -e "$root_dir/etc/debian_version" ]; then
73 if [ -e ${root_dir}/etc/lsb-release ]; then
74 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
75 if [ "x$ID" = "xUbuntu" ]; then
80 elif [ -e "$root_dir/etc/fedora-release" ]; then
82 elif [ -e "$root_dir/etc/centos-release" ]; then
84 elif [ -e "$root_dir/etc/redhat-release" ]; then
86 elif [ -e "$root_dir/etc/slackware-version" ]; then
88 elif [ -e "$root_dir/etc/SuSE-release" ]; then
90 elif [ -e "$root_dir/etc/gentoo-release" ]; then
93 warn "Unknown distro."
97 check_partition_table() {
99 if ! "$PARTED" -s "$dev" print; then
100 log_error "Unable to read partition table for device \`${dev}'"
104 get_last_partition() {
107 "$PARTED" -s -m "$dev" unit s print | tail -1
114 "$PARTED" -s -m "$dev" unit s print | grep "^$id"
117 get_partition_count() {
120 expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
123 get_last_free_sector() {
125 local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
126 local type=$(echo "$last_line" | cut -d: -f 5)
128 if [ "$type" = "free;" ]; then
129 echo "$last_line" | cut -d: -f 3
134 # if something fails here, it shouldn't call cleanup again...
137 if [ ${#CLEANUP[*]} -gt 0 ]; then
138 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
139 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
140 for i in $REVERSE_INDEXES; do
141 # If something fails here, it's better to retry it for a few times
142 # before we give up with an error. This is needed for kpartx when
143 # dealing with ntfs partitions mounted through fuse. umount is not
144 # synchronous and may return while the partition is still busy. A
145 # premature attempt to delete partition mappings through kpartx on a
146 # device that hosts previously mounted ntfs partition may fail with
147 # a `device-mapper: remove ioctl failed: Device or resource busy'
148 # error. A sensible workaround for this is to wait for a while and
150 local cmd=${CLEANUP[$i]}
151 $cmd || for interval in 0.25 0.5 1 2 4; do
152 echo "Command $cmd failed!"
153 echo "I'll wait for $interval secs and will retry..."
157 if [ "$?" != "0" ]; then
165 check_if_excluded() {
167 local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
168 if [ -n "${!exclude}" ]; then
169 warn "Task $PROGNAME was excluded and will not run."
178 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :