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
38 for arg; do cmd+=$(printf "%q " "$arg"); done
43 echo "ERROR: $@" | tee $RESULT >&2
48 echo "Warning: $@" >&2
54 if [ -e "$root_dir/etc/debian_version" ]; then
56 elif [ -e "$root_dir/etc/redhat-release" ]; then
58 elif [ -e "$root_dir/etc/slackware-version" ]; then
60 elif [ -e "$root_dir/etc/SuSE-release" ]; then
62 elif [ -e "$root_dir/etc/gentoo-release" ]; then
65 warn "Unknown base distro."
72 if [ -e "$root_dir/etc/debian_version" ]; then
74 if [ -e ${root_dir}/etc/lsb-release ]; then
75 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
76 if [ "x$ID" = "xUbuntu" ]; then
81 elif [ -e "$root_dir/etc/fedora-release" ]; then
83 elif [ -e "$root_dir/etc/centos-release" ]; then
85 elif [ -e "$root_dir/etc/redhat-release" ]; then
87 elif [ -e "$root_dir/etc/slackware-version" ]; then
89 elif [ -e "$root_dir/etc/SuSE-release" ]; then
91 elif [ -e "$root_dir/etc/gentoo-release" ]; then
94 warn "Unknown distro."
98 get_partition_table() {
100 if ! output="$("$PARTED" -s -m "$dev" print)"; then
101 log_error "Unable to read partition table for device \`${dev}'"
107 get_partition_table_type() {
110 local dev="$(sed -n 2p <<< "$ptable")"
112 IFS=':' read -ra field <<< "$dev"
117 get_partition_count() {
120 expr $(echo "$ptable" | wc -l) - 2
123 get_last_partition() {
126 echo "$ptable" | tail -1
129 is_extended_partition() {
133 id=$($SFDISK --print-id "$dev" "$part_num")
134 if [ "$id" = "5" ]; then
141 get_extended_partition() {
143 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
145 tail -n +3 <<< "$ptable" | while read line; do
146 part_num=$(cut -d':' -f1 <<< "$line")
147 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
155 get_logical_partitions() {
158 tail -n +3 <<< "$ptable" | while read line; do
159 part_num=$(cut -d':' -f1 <<< "$line")
160 if [ $part_num -ge 5 ]; then
168 get_last_primary_partition() {
170 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
173 if output=$(grep "^$i:" <<< "$ptable"); then
187 IFS=":;" read -ra fields <<< "$part"
188 local id=${fields[0]}
189 local start=${fields[1]}
190 local end=${fields[2]}
191 local size=${fields[3]}
192 local fs=${fields[4]}
193 local name=${fields[5]}
194 local flags=${fields[6]//,/ }
196 $PARTED -s -m $device mkpart "$ptype" $fs "$start" "$end"
197 for flag in $flags; do
198 $PARTED -s -m $device set "$id" "$flag" on
202 enlarge_partition() {
207 local new_end=$(get_last_free_sector "$device")
209 IFS=":;" read -ra fields <<< "$part"
213 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
214 new_part="$new_part":"${fields[$i]}"
216 new_part=${new_part:1}
218 # If this is an extended partition, removing it will also remove the
219 # logical partitions it contains. We need to save them for later.
220 if [ "$ptype" = "extended" ]; then
221 local table="$(get_partition_table "$device")"
222 local logical="$(get_logical_partitions "$table")"
226 $PARTED -s -m "$device" rm "$id"
227 create_partition "$device" "$new_part" "$ptype"
229 if [ "$ptype" = "extended" ]; then
230 # Recreate logical partitions
231 echo "$logical" | while read logical_part; do
232 create_partition "$device" "$logical_part" "logical"
237 get_last_free_sector() {
239 local last_line="$("$PARTED" -s -m "$dev" print free | tail -1)"
240 local ptype="$(echo "$last_line" | cut -d: -f 5)"
242 if [ "$ptype" = "free;" ]; then
243 echo "$last_line" | cut -d: -f 3
248 # if something fails here, it shouldn't call cleanup again...
251 if [ ${#CLEANUP[*]} -gt 0 ]; then
252 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
253 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
254 for i in $REVERSE_INDEXES; do
255 # If something fails here, it's better to retry it for a few times
256 # before we give up with an error. This is needed for kpartx when
257 # dealing with ntfs partitions mounted through fuse. umount is not
258 # synchronous and may return while the partition is still busy. A
259 # premature attempt to delete partition mappings through kpartx on a
260 # device that hosts previously mounted ntfs partition may fail with
261 # a `device-mapper: remove ioctl failed: Device or resource busy'
262 # error. A sensible workaround for this is to wait for a while and
264 local cmd=${CLEANUP[$i]}
265 $cmd || for interval in 0.25 0.5 1 2 4; do
266 echo "Command $cmd failed!"
267 echo "I'll wait for $interval secs and will retry..."
271 if [ "$?" != "0" ]; then
279 check_if_excluded() {
281 local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
282 if [ -n "${!exclude}" ]; then
283 warn "Task $PROGNAME was excluded and will not run."
292 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :