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_last_partition_id() {
100 if ! output="$("$PARTED" -s -m "$dev" print)"; then
101 log_error "Unable to read partition table for device \`${dev}'"
104 last_line=$(tail -1 <<< "$output")
106 echo $(cut -d: -f1 <<< "$last_line")
109 get_partition_table() {
111 if ! output="$("$PARTED" -s -m "$dev" unit s print)"; then
112 log_error "Unable to read partition table for device \`${dev}'"
118 get_partition_table_type() {
121 local dev="$(sed -n 2p <<< "$ptable")"
123 IFS=':' read -ra field <<< "$dev"
128 get_partition_count() {
131 expr $(echo "$ptable" | wc -l) - 2
134 get_partition_by_id() {
138 grep "^$id:" <<< "$ptable"
141 get_last_partition() {
144 echo "$ptable" | tail -1
147 is_extended_partition() {
151 id=$($SFDISK --print-id "$dev" "$part_num")
152 if [ "$id" = "5" ]; then
159 get_extended_partition() {
161 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
163 tail -n +3 <<< "$ptable" | while read line; do
164 part_num=$(cut -d':' -f1 <<< "$line")
165 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
173 get_logical_partitions() {
176 tail -n +3 <<< "$ptable" | while read line; do
177 part_num=$(cut -d':' -f1 <<< "$line")
178 if [ $part_num -ge 5 ]; then
186 get_last_primary_partition() {
188 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
191 if output=$(grep "^$i:" <<< "$ptable"); then
205 IFS=":;" read -ra fields <<< "$part"
206 local id="${fields[0]}"
207 local start="${fields[1]}"
208 local end="${fields[2]}"
209 local size="${fields[3]}"
210 local fs="${fields[4]}"
211 local name="${fields[5]}"
212 local flags="${fields[6]//,/ }"
214 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
215 for flag in $flags; do
216 $PARTED -s -m $device set "$id" "$flag" on
220 enlarge_partition() {
226 if [ -z "$new_end" ]; then
227 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
231 IFS=":;" read -ra fields <<< "$part"
235 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
236 new_part="$new_part":"${fields[$i]}"
238 new_part=${new_part:1}
240 # If this is an extended partition, removing it will also remove the
241 # logical partitions it contains. We need to save them for later.
242 if [ "$ptype" = "extended" ]; then
243 local table="$(get_partition_table "$device")"
244 local logical="$(get_logical_partitions "$table")"
248 $PARTED -s -m "$device" rm "$id"
249 create_partition "$device" "$new_part" "$ptype"
251 if [ "$ptype" = "extended" ]; then
252 # Recreate logical partitions
253 echo "$logical" | while read logical_part; do
254 create_partition "$device" "$logical_part" "logical"
259 get_last_free_sector() {
263 if [ -n "$unit" ]; then
267 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
268 local ptype="$(cut -d: -f 5 <<< "$last_line")"
270 if [ "$ptype" = "free;" ]; then
276 # if something fails here, it shouldn't call cleanup again...
279 if [ ${#CLEANUP[*]} -gt 0 ]; then
280 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
281 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
282 for i in $REVERSE_INDEXES; do
283 # If something fails here, it's better to retry it for a few times
284 # before we give up with an error. This is needed for kpartx when
285 # dealing with ntfs partitions mounted through fuse. umount is not
286 # synchronous and may return while the partition is still busy. A
287 # premature attempt to delete partition mappings through kpartx on
288 # a device that hosts previously mounted ntfs partition may fail
289 # with a `device-mapper: remove ioctl failed: Device or resource
290 # busy' error. A sensible workaround for this is to wait for a
291 # while and then try again.
292 local cmd=${CLEANUP[$i]}
293 $cmd || for interval in 0.25 0.5 1 2 4; do
294 echo "Command $cmd failed!"
295 echo "I'll wait for $interval secs and will retry..."
299 if [ "$?" != "0" ]; then
307 check_if_excluded() {
309 local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
310 if [ -n "${!exclude}" ]; then
311 warn "Task $PROGNAME was excluded and will not run."
321 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :