| 1 |
#!/bin/sh |
| 2 |
# shellcheck disable=SC2015,SC2162,SC2166,SC2317,SC3043 |
| 3 |
# Copyright (C) 2011 Canonical Ltd. |
| 4 |
# Copyright (C) 2013 Hewlett-Packard Development Company, L.P. |
| 5 |
# |
| 6 |
# Authors: Scott Moser <[email protected]> |
| 7 |
# Juerg Haefliger <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, version 3 of the License. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
# the fudge factor. if within this many bytes dont bother |
| 22 |
FUDGE=${GROWPART_FUDGE:-$((1024*1024))} |
| 23 |
TEMP_D="" |
| 24 |
RESTORE_FUNC="" |
| 25 |
RESTORE_HUMAN="" |
| 26 |
VERBOSITY=0 |
| 27 |
DISK="" |
| 28 |
PART="" |
| 29 |
PT_UPDATE=false |
| 30 |
DRY_RUN=0 |
| 31 |
FLOCK_DISK_FD="" |
| 32 |
RESIZE_RESULT="" |
| 33 |
|
| 34 |
SFDISK_VERSION="" |
| 35 |
SFDISK_2_26="22600" |
| 36 |
SFDISK_V_WORKING_GPT="22603" |
| 37 |
MBR_BACKUP="" |
| 38 |
GPT_BACKUP="" |
| 39 |
_capture="" |
| 40 |
|
| 41 |
error() { |
| 42 |
echo "$@" 1>&2 |
| 43 |
} |
| 44 |
|
| 45 |
fail() { |
| 46 |
[ $# -eq 0 ] || echo "FAILED:" "$@" |
| 47 |
exit 2 |
| 48 |
} |
| 49 |
|
| 50 |
nochange() { |
| 51 |
RESIZE_RESULT="NOCHANGE" |
| 52 |
echo "NOCHANGE:" "$@" |
| 53 |
return 1 |
| 54 |
} |
| 55 |
|
| 56 |
changed() { |
| 57 |
RESIZE_RESULT="CHANGED" |
| 58 |
echo "CHANGED:" "$@" |
| 59 |
return 0 |
| 60 |
} |
| 61 |
|
| 62 |
change() { |
| 63 |
RESIZE_RESULT="CHANGE" |
| 64 |
echo "CHANGE:" "$@" |
| 65 |
return 0 |
| 66 |
} |
| 67 |
|
| 68 |
cleanup() { |
| 69 |
if [ -n "${RESTORE_FUNC}" ]; then |
| 70 |
error "***** WARNING: Resize failed, attempting to revert ******" |
| 71 |
if ${RESTORE_FUNC} ; then |
| 72 |
error "***** Restore appears to have gone OK ****" |
| 73 |
else |
| 74 |
error "***** Restore FAILED! ******" |
| 75 |
if [ -n "${RESTORE_HUMAN}" -a -f "${RESTORE_HUMAN}" ]; then |
| 76 |
error "**** original table looked like: ****" |
| 77 |
cat "${RESTORE_HUMAN}" 1>&2 |
| 78 |
else |
| 79 |
error "We seem to have not saved the partition table!" |
| 80 |
fi |
| 81 |
fi |
| 82 |
unlock_disk_and_settle "$DISK" |
| 83 |
fi |
| 84 |
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}" |
| 85 |
} |
| 86 |
|
| 87 |
debug() { |
| 88 |
local level="$1" |
| 89 |
shift |
| 90 |
[ "${level}" -gt "${VERBOSITY}" ] && return |
| 91 |
if [ "${DEBUG_LOG}" ]; then |
| 92 |
echo "$@" >>"${DEBUG_LOG}" |
| 93 |
else |
| 94 |
error "$@" |
| 95 |
fi |
| 96 |
} |
| 97 |
|
| 98 |
debugcat() { |
| 99 |
local level="$1" |
| 100 |
shift; |
| 101 |
[ "${level}" -gt "$VERBOSITY" ] && return |
| 102 |
if [ "${DEBUG_LOG}" ]; then |
| 103 |
cat "$@" >>"${DEBUG_LOG}" |
| 104 |
else |
| 105 |
cat "$@" 1>&2 |
| 106 |
fi |
| 107 |
} |
| 108 |
|
| 109 |
mktemp_d() { |
| 110 |
# just a mktemp -d that does not need mktemp if its not there. |
| 111 |
_RET=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX" 2>/dev/null) && |
| 112 |
return |
| 113 |
_RET=$(umask 077 && t="${TMPDIR:-/tmp}/${0##*/}.$$" && |
| 114 |
mkdir "${t}" && echo "${t}") |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
Usage() { |
| 119 |
cat <<EOF |
| 120 |
${0##*/} disk partition |
| 121 |
rewrite partition table so that partition takes up all the space it can |
| 122 |
options: |
| 123 |
-h | --help print Usage and exit |
| 124 |
--free-percent F resize so that specified percentage F of the disk is |
| 125 |
not used in total (not just by this partition). This |
| 126 |
is useful for consumer SSD or SD cards where a small |
| 127 |
percentage unallocated can improve device lifetime. |
| 128 |
--fudge F if part could be resized, but change would be less |
| 129 |
than 'F' bytes, do not resize (default: ${FUDGE}) |
| 130 |
-N | --dry-run only report what would be done, show new 'sfdisk -d' |
| 131 |
-v | --verbose increase verbosity / debug |
| 132 |
-u | --update R update the the kernel partition table info after |
| 133 |
growing this requires kernel support and |
| 134 |
'partx --update' |
| 135 |
R is one of: |
| 136 |
- 'auto' : [default] update partition if possible |
| 137 |
- 'force' : try despite sanity checks (fail on |
| 138 |
failure) |
| 139 |
- 'off' : do not attempt |
| 140 |
- 'on' : fail if sanity checks indicate no |
| 141 |
support |
| 142 |
|
| 143 |
Example: |
| 144 |
- ${0##*/} /dev/sda 1 |
| 145 |
Resize partition 1 on /dev/sda |
| 146 |
|
| 147 |
- ${0##*/} --free-percent=10 /dev/sda 1 |
| 148 |
Resize partition 1 on /dev/sda so that 10% of the disk is unallocated |
| 149 |
EOF |
| 150 |
} |
| 151 |
|
| 152 |
bad_Usage() { |
| 153 |
Usage 1>&2 |
| 154 |
error "$@" |
| 155 |
exit 2 |
| 156 |
} |
| 157 |
|
| 158 |
lock_disk() { |
| 159 |
local disk="$1" |
| 160 |
# flock the target disk to protect against udev actions while modifying. |
| 161 |
# https://systemd.io/BLOCK_DEVICE_LOCKING/ |
| 162 |
[ "${DRY_RUN}" = 0 ] || return |
| 163 |
|
| 164 |
# only lock block devices, files do not need a lock |
| 165 |
[ -b "${disk}" ] || return |
| 166 |
|
| 167 |
# The FD values are hard-coded per /bin/sh requirement for using exec |
| 168 |
# to open paths with specific fd values; man (1) sh on "Redirections" |
| 169 |
FLOCK_DISK_FD=9 |
| 170 |
debug 1 "FLOCK: try exec open fd 9, on failure exec exits this program" |
| 171 |
exec 9<>"$disk" |
| 172 |
|
| 173 |
# Do not use --nonblock or --timeout as udev may be already processing |
| 174 |
# the disk and we must wait until it has released the disk to |
| 175 |
# proceed. Failure to obtain exclusive lock is fatal to growpart. |
| 176 |
rq flock flock -x $FLOCK_DISK_FD || |
| 177 |
fail "Error while obtaining exclusive lock on $DISK" |
| 178 |
debug 1 "FLOCK: $disk: obtained exclusive lock" |
| 179 |
} |
| 180 |
|
| 181 |
unlock_disk_and_settle() { |
| 182 |
# unlock_disk(disk, settle) |
| 183 |
local disk="$1" |
| 184 |
local settle="${2-1}" |
| 185 |
# release the lock on a disk if locked. When a disk is locked, |
| 186 |
# FLOCK_DISK_FD is set to the hard-coded value of 9. |
| 187 |
# After unlocking run udevadm settle (if installed) as the disk has |
| 188 |
# likely been changed. |
| 189 |
[ "${DRY_RUN}" = 0 ] || return |
| 190 |
[ -n "${FLOCK_DISK_FD}" ] || return |
| 191 |
|
| 192 |
debug 1 "FLOCK: ${disk}: releasing exclusive lock" |
| 193 |
exec 9>&- |
| 194 |
[ "${settle}" = 1 ] && has_cmd udevadm && udevadm settle |
| 195 |
FLOCK_DISK_FD="" |
| 196 |
} |
| 197 |
|
| 198 |
sfdisk_restore_legacy() { |
| 199 |
sfdisk --no-reread "${DISK}" -I "${MBR_BACKUP}" |
| 200 |
} |
| 201 |
|
| 202 |
sfdisk_restore() { |
| 203 |
# files are named: sfdisk-<device>-<offset>.bak |
| 204 |
local f="" offset="" fails=0 |
| 205 |
for f in "${MBR_BACKUP}"*.bak; do |
| 206 |
[ -f "$f" ] || continue |
| 207 |
offset=${f##*-} |
| 208 |
offset=${offset%.bak} |
| 209 |
[ "$offset" = "$f" ] && { |
| 210 |
error "WARN: confused by file $f"; |
| 211 |
continue; |
| 212 |
} |
| 213 |
dd "if=$f" "of=${DISK}" seek="$offset" bs=1 conv=notrunc || |
| 214 |
{ error "WARN: failed restore from $f"; fails=$((fails+1)); } |
| 215 |
done |
| 216 |
return "$fails" |
| 217 |
} |
| 218 |
|
| 219 |
sfdisk_worked_but_blkrrpart_failed() { |
| 220 |
local ret="$1" output="$2" |
| 221 |
# exit code found was just 1, but dont insist on that |
| 222 |
#[ $ret -eq 1 ] || return 1 |
| 223 |
# Successfully wrote the new partition table |
| 224 |
if grep -qi "Success.* wrote.* new.* partition" "$output"; then |
| 225 |
grep -qi "BLKRRPART: Device or resource busy" "$output" |
| 226 |
return |
| 227 |
# The partition table has been altered. |
| 228 |
elif grep -qi "The.* part.* table.* has.* been.* altered" "$output"; then |
| 229 |
# Re-reading the partition table failed |
| 230 |
grep -qi "Re-reading.* partition.* table.* failed" "$output" |
| 231 |
return |
| 232 |
fi |
| 233 |
return "$ret" |
| 234 |
} |
| 235 |
|
| 236 |
get_sfdisk_version() { |
| 237 |
# set SFDISK_VERSION to MAJOR*10000+MINOR*100+MICRO |
| 238 |
local out oifs="$IFS" ver="" |
| 239 |
[ -n "$SFDISK_VERSION" ] && return 0 |
| 240 |
[ -n "$SFDISK" ] || { |
| 241 |
SFDISK_VERSION=0 |
| 242 |
return 0 |
| 243 |
} |
| 244 |
# expected output: sfdisk from util-linux 2.25.2 |
| 245 |
out=$(LANG=C sfdisk --version) || |
| 246 |
{ error "failed to get sfdisk version"; return 1; } |
| 247 |
# shellcheck disable=SC2086 |
| 248 |
set -- $out |
| 249 |
ver=$4 |
| 250 |
case "$ver" in |
| 251 |
[0-9]*.[0-9]*.[0-9]|[0-9].[0-9]*) |
| 252 |
# shellcheck disable=SC2086 |
| 253 |
{ IFS="."; set -- $ver; IFS="$oifs"; } |
| 254 |
SFDISK_VERSION=$(($1*10000+$2*100+${3:-0})) |
| 255 |
return 0;; |
| 256 |
*) error "unexpected output in sfdisk --version [$out]" |
| 257 |
return 1;; |
| 258 |
esac |
| 259 |
} |
| 260 |
|
| 261 |
get_diskpart_path() { |
| 262 |
# get_diskpart_path(disk, part_number) |
| 263 |
# return the path to the partition device on disk |
| 264 |
# |
| 265 |
# Convert inputs disk and part into a path to a partition device path |
| 266 |
# handle both block devices or files. |
| 267 |
# e.g. get_diskpart_path /dev/loop0 7 returns /dev/loop0p7 |
| 268 |
local disk="$1" |
| 269 |
local part="$2" |
| 270 |
local dpart="" |
| 271 |
|
| 272 |
dpart="${disk}${part}" # disk and partition number |
| 273 |
if [ -b "$disk" ]; then |
| 274 |
if [ -b "${disk}p${part}" -a "${disk%[0-9]}" != "${disk}" ]; then |
| 275 |
# for block devices that end in a number (/dev/nbd0) |
| 276 |
# the partition is "<name>p<partition_number>" (/dev/nbd0p1) |
| 277 |
dpart="${disk}p${part}" |
| 278 |
elif [ "${disk#/dev/loop[0-9]}" != "${disk}" ]; then |
| 279 |
# for /dev/loop devices, sfdisk output will be <name>p<number> |
| 280 |
# format also, even though there is not a device there. |
| 281 |
dpart="${disk}p${part}" |
| 282 |
fi |
| 283 |
else |
| 284 |
case "$disk" in |
| 285 |
# sfdisk for files ending in digit to <disk>p<num>. |
| 286 |
*[0-9]) dpart="${disk}p${part}";; |
| 287 |
esac |
| 288 |
fi |
| 289 |
_RET="$dpart" |
| 290 |
} |
| 291 |
|
| 292 |
resize_sfdisk() { |
| 293 |
local humanpt="${TEMP_D}/recovery" |
| 294 |
local mbr_backup="${TEMP_D}/orig.save" |
| 295 |
local restore_func="" |
| 296 |
local format="$1" |
| 297 |
|
| 298 |
local change_out="${TEMP_D}/change.out" |
| 299 |
local dump_out="${TEMP_D}/dump.out" |
| 300 |
local new_out="${TEMP_D}/new.out" |
| 301 |
local dump_mod="${TEMP_D}/dump.mod" |
| 302 |
local tmp="${TEMP_D}/tmp.out" |
| 303 |
local mbr_max_512="4294967296" |
| 304 |
|
| 305 |
local pt_start pt_size pt_end max_end new_size change_info dpart |
| 306 |
local sector_num sector_size disk_size pt_size_bytes out |
| 307 |
local excess_sectors free_percent_sectors remaining_free_sectors |
| 308 |
|
| 309 |
LANG=C rqe sfd_list sfdisk --list --unit=S "$DISK" >"$tmp" || |
| 310 |
fail "failed: sfdisk --list $DISK" |
| 311 |
if [ "${SFDISK_VERSION}" -lt "${SFDISK_2_26}" ]; then |
| 312 |
# expected output contains: Units: sectors of 512 bytes, ... |
| 313 |
out=$(awk '$1 == "Units:" && $5 ~ /bytes/ { print $4 }' "$tmp") || |
| 314 |
fail "failed to read sfdisk output" |
| 315 |
if [ -z "$out" ]; then |
| 316 |
error "WARN: sector size not found in sfdisk output, assuming 512" |
| 317 |
sector_size=512 |
| 318 |
else |
| 319 |
sector_size="$out" |
| 320 |
fi |
| 321 |
local _w _cyl _w1 _heads _w2 _w3 t |
| 322 |
# show-size is in units of 1024 bytes (same as /proc/partitions) |
| 323 |
t=$(sfdisk --show-size "${DISK}") || |
| 324 |
fail "failed: sfdisk --show-size $DISK" |
| 325 |
disk_size=$((t*1024)) |
| 326 |
sector_num=$((disk_size/sector_size)) |
| 327 |
msg="disk size '$disk_size' not evenly div by sector size '$sector_size'" |
| 328 |
[ "$((disk_size%sector_size))" -eq 0 ] || |
| 329 |
error "WARN: $msg" |
| 330 |
restore_func=sfdisk_restore_legacy |
| 331 |
else |
| 332 |
# --list first line output: |
| 333 |
# Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors |
| 334 |
local _x |
| 335 |
read _x _x _x _x disk_size _x sector_num _x < "$tmp" |
| 336 |
sector_size=$((disk_size/sector_num)) |
| 337 |
restore_func=sfdisk_restore |
| 338 |
fi |
| 339 |
|
| 340 |
debug 1 "$sector_num sectors of $sector_size. total size=${disk_size} bytes" |
| 341 |
|
| 342 |
rqe sfd_dump sfdisk --unit=S --dump "${DISK}" >"${dump_out}" || |
| 343 |
fail "failed to dump sfdisk info for ${DISK}" |
| 344 |
RESTORE_HUMAN="$dump_out" |
| 345 |
|
| 346 |
{ |
| 347 |
echo "## sfdisk --unit=S --dump ${DISK}" |
| 348 |
cat "${dump_out}" |
| 349 |
} >"$humanpt" |
| 350 |
|
| 351 |
# shellcheck disable=SC2181 |
| 352 |
[ $? -eq 0 ] || fail "failed to save sfdisk -d output" |
| 353 |
RESTORE_HUMAN="$humanpt" |
| 354 |
|
| 355 |
debugcat 1 "$humanpt" |
| 356 |
|
| 357 |
sed -e 's/,//g; s/start=/start /; s/size=/size /' "${dump_out}" \ |
| 358 |
>"${dump_mod}" || |
| 359 |
fail "sed failed on dump output" |
| 360 |
|
| 361 |
get_diskpart_path "$DISK" "$PART" |
| 362 |
dpart="$_RET" |
| 363 |
|
| 364 |
pt_start=$(awk '$1 == pt { print $4 }' "pt=${dpart}" <"${dump_mod}") && |
| 365 |
pt_size=$(awk '$1 == pt { print $6 }' "pt=${dpart}" <"${dump_mod}") && |
| 366 |
[ -n "${pt_start}" ] && [ -n "${pt_size}" ] && |
| 367 |
pt_end=$((pt_size+pt_start-1)) || |
| 368 |
fail "failed to get start and end for ${dpart} in ${DISK}" |
| 369 |
|
| 370 |
# find the minimal starting location that is >= pt_end |
| 371 |
max_end=$(awk '$3 == "start" { if($4 >= pt_end && $4 < min) |
| 372 |
{ min = $4 } } END { printf("%s\n",min); }' \ |
| 373 |
min="${sector_num}" pt_end="${pt_end}" "${dump_mod}") && |
| 374 |
[ -n "${max_end}" ] || |
| 375 |
fail "failed to get max_end for partition ${PART}" |
| 376 |
# As sector numbering starts from 0 need to reduce value by 1. |
| 377 |
max_end=$((max_end - 1)) |
| 378 |
|
| 379 |
if [ "$format" = "gpt" ]; then |
| 380 |
# sfdisk respects 'last-lba' in input, and complains about |
| 381 |
# partitions that go past that. without it, it does the right thing. |
| 382 |
sed -i '/^last-lba:/d' "$dump_out" || |
| 383 |
fail "failed to remove last-lba from output" |
| 384 |
fi |
| 385 |
if [ "$format" = "dos" ]; then |
| 386 |
mbr_max_sectors=$((mbr_max_512*$((sector_size/512)))) |
| 387 |
if [ "$max_end" -gt "$mbr_max_sectors" ]; then |
| 388 |
max_end=$mbr_max_sectors |
| 389 |
fi |
| 390 |
[ $((disk_size/512)) -gt "$mbr_max_512" ] && |
| 391 |
debug 0 "WARNING: MBR/dos partitioned disk is larger than 2TB." \ |
| 392 |
"Additional space will go unused." |
| 393 |
fi |
| 394 |
|
| 395 |
local gpt_second_size="33" |
| 396 |
if [ "${max_end}" -gt "$((sector_num-gpt_second_size))" ]; then |
| 397 |
# if MBR, allow subsequent conversion to GPT without shrinking |
| 398 |
# the partition and safety net at cost of 33 sectors seems |
| 399 |
# reasonable. If GPT, we can't write there anyway. |
| 400 |
debug 1 "padding ${gpt_second_size} sectors for gpt secondary header" |
| 401 |
max_end=$((sector_num - gpt_second_size - 1)) |
| 402 |
fi |
| 403 |
|
| 404 |
# make partition sizes always even units of $align bytes. |
| 405 |
local align=$((1024*1024)) |
| 406 |
local sectors_per_align=$((align/sector_size)) max_size_align="" |
| 407 |
max_size_align=$((((max_end+1-pt_start)/sectors_per_align)*sectors_per_align)) |
| 408 |
max_end=$((max_size_align+pt_start-1)) |
| 409 |
|
| 410 |
if [ -n "${free_percent}" ]; then |
| 411 |
# shellcheck disable=SC2017 |
| 412 |
free_percent_sectors=$((sector_num/100*free_percent)) |
| 413 |
|
| 414 |
if [ "$format" = "dos" ]; then |
| 415 |
if [ $((disk_size/512)) -ge $((mbr_max_512+free_percent_sectors)) ]; then |
| 416 |
# If MBR partitioned disk larger than 2TB and |
| 417 |
# remaining space over 2TB boundary is greater |
| 418 |
# than the requested overprovisioning sectors |
| 419 |
# then do not change max_end. |
| 420 |
debug 1 "WARNING: Additional unused space on MBR/dos partitioned disk" \ |
| 421 |
"is larger than requested percent of overprovisioning." |
| 422 |
elif [ "$sector_num" -gt "$mbr_max_512" ]; then |
| 423 |
# If only some of the overprovisioning sectors |
| 424 |
# are over the 2TB boundary then reduce max_end |
| 425 |
# by the remaining number of overprovisioning |
| 426 |
# sectors. |
| 427 |
excess_sectors=$((sector_num-mbr_max_512)) |
| 428 |
remaining_free_sectors=$((free_percent_sectors - excess_sectors)) |
| 429 |
debug 1 "reserving ${remaining_free_sectors} sectors from MBR maximum for overprovisioning" |
| 430 |
max_end=$((max_end - remaining_free_sectors)) |
| 431 |
else |
| 432 |
# Shrink max_end to keep X% of whole disk unused |
| 433 |
# (for overprovisioning). |
| 434 |
debug 1 "reserving ${free_percent_sectors} sectors (${free_percent}%) for overprovisioning" |
| 435 |
max_end=$((max_end-free_percent_sectors)) |
| 436 |
fi |
| 437 |
max_end=$((((max_end/sectors_per_align)*sectors_per_align)-1)) |
| 438 |
|
| 439 |
if [ "${max_end}" -lt "${pt_end}" ]; then |
| 440 |
nochange "partition ${PART} could not be grown while leaving" \ |
| 441 |
"${free_percent}% (${free_percent_sectors} sectors) free on device" |
| 442 |
return |
| 443 |
fi |
| 444 |
else |
| 445 |
# Shrink max_end to keep X% of whole disk unused |
| 446 |
# (for overprovisioning). |
| 447 |
debug 1 "reserving ${free_percent_sectors} sectors (${free_percent}%) for overprovisioning" |
| 448 |
max_end=$((max_end-free_percent_sectors)) |
| 449 |
max_end=$((((max_end/sectors_per_align)*sectors_per_align)-1)) |
| 450 |
|
| 451 |
if [ "${max_end}" -lt "${pt_end}" ]; then |
| 452 |
nochange "partition ${PART} could not be grown while leaving" \ |
| 453 |
"${free_percent}% (${free_percent_sectors} sectors) free on device" |
| 454 |
return |
| 455 |
fi |
| 456 |
fi |
| 457 |
fi |
| 458 |
|
| 459 |
pt_size_bytes=$((pt_size*sector_size)) |
| 460 |
|
| 461 |
debug 1 "max_end=${max_end} tot=${sector_num} pt_end=${pt_end}" \ |
| 462 |
"pt_start=${pt_start} pt_size=${pt_size} pt_size_bytes=${pt_size_bytes}" |
| 463 |
[ "${pt_end}" -eq "${max_end}" ] && { |
| 464 |
nochange "partition ${PART} size is ${pt_size_bytes} bytes (${pt_size} sectors of ${sector_size}). it cannot be grown" |
| 465 |
return |
| 466 |
} |
| 467 |
[ "$((pt_end+(FUDGE/sector_size)))" -gt "${max_end}" ] && { |
| 468 |
nochange "partition ${PART} could only be grown by" \ |
| 469 |
"$((max_end-pt_end)) [fudge=$((FUDGE/sector_size))]" |
| 470 |
return |
| 471 |
} |
| 472 |
|
| 473 |
# Now, change the size for this partition in ${dump_out} to be the |
| 474 |
# new size. |
| 475 |
new_size=$((max_end - pt_start + 1)) |
| 476 |
sed "\|^\s*${dpart} |s/\(.*\)${pt_size},/\1${new_size},/" "${dump_out}" \ |
| 477 |
>"${new_out}" || |
| 478 |
fail "failed to change size in output" |
| 479 |
|
| 480 |
change_info="partition=${PART} start=${pt_start}" |
| 481 |
change_info="${change_info} old: size=${pt_size} end=${pt_end}" |
| 482 |
change_info="${change_info} new: size=${new_size} end=${max_end}" |
| 483 |
if [ "${DRY_RUN}" -ne 0 ]; then |
| 484 |
echo "CHANGE: ${change_info}" |
| 485 |
{ |
| 486 |
echo "# === old sfdisk -d ===" |
| 487 |
cat "${dump_out}" |
| 488 |
echo "# === new sfdisk -d ===" |
| 489 |
cat "${new_out}" |
| 490 |
} 1>&2 |
| 491 |
exit 0 |
| 492 |
fi |
| 493 |
|
| 494 |
MBR_BACKUP="${mbr_backup}" |
| 495 |
LANG=C sfdisk --no-reread "${DISK}" --force \ |
| 496 |
-O "${mbr_backup}" <"${new_out}" >"${change_out}" 2>&1 |
| 497 |
ret=$? |
| 498 |
[ "$ret" -eq 0 ] || RESTORE_FUNC="${restore_func}" |
| 499 |
|
| 500 |
if [ "$ret" -eq 0 ]; then |
| 501 |
debug 1 "resize of ${DISK} returned 0." |
| 502 |
if [ "$VERBOSITY" -gt 2 ]; then |
| 503 |
sed 's,^,| ,' "${change_out}" 1>&2 |
| 504 |
fi |
| 505 |
elif $PT_UPDATE && |
| 506 |
sfdisk_worked_but_blkrrpart_failed "$ret" "${change_out}"; then |
| 507 |
# if the command failed, but it looks like only because |
| 508 |
# the device was busy and we have pt_update, then go on |
| 509 |
debug 1 "sfdisk failed, but likely only because of blkrrpart" |
| 510 |
else |
| 511 |
error "attempt to resize ${DISK} failed. sfdisk output below:" |
| 512 |
sed 's,^,| ,' "${change_out}" 1>&2 |
| 513 |
fail "failed to resize" |
| 514 |
fi |
| 515 |
|
| 516 |
rq pt_update pt_update "$DISK" "$PART" || |
| 517 |
fail "pt_resize failed" |
| 518 |
|
| 519 |
RESTORE_FUNC="" |
| 520 |
|
| 521 |
changed "${change_info}" |
| 522 |
return |
| 523 |
|
| 524 |
# dump_out looks something like: |
| 525 |
## partition table of /tmp/out.img |
| 526 |
#unit: sectors |
| 527 |
# |
| 528 |
#/tmp/out.img1 : start= 1, size= 48194, Id=83 |
| 529 |
#/tmp/out.img2 : start= 48195, size= 963900, Id=83 |
| 530 |
#/tmp/out.img3 : start= 1012095, size= 305235, Id=82 |
| 531 |
#/tmp/out.img4 : start= 1317330, size= 771120, Id= 5 |
| 532 |
#/tmp/out.img5 : start= 1317331, size= 642599, Id=83 |
| 533 |
#/tmp/out.img6 : start= 1959931, size= 48194, Id=83 |
| 534 |
#/tmp/out.img7 : start= 2008126, size= 80324, Id=83 |
| 535 |
} |
| 536 |
|
| 537 |
gpt_restore() { |
| 538 |
sgdisk -l "${GPT_BACKUP}" "${DISK}" |
| 539 |
} |
| 540 |
|
| 541 |
resize_sgdisk() { |
| 542 |
GPT_BACKUP="${TEMP_D}/pt.backup" |
| 543 |
|
| 544 |
local pt_info="${TEMP_D}/pt.info" |
| 545 |
local pt_pretend="${TEMP_D}/pt.pretend" |
| 546 |
local pt_data="${TEMP_D}/pt.data" |
| 547 |
local out="${TEMP_D}/out" |
| 548 |
|
| 549 |
local dev="disk=${DISK} partition=${PART}" |
| 550 |
|
| 551 |
local pt_start pt_end pt_size last pt_max code guid name new_size |
| 552 |
local change_info sector_size pt_size_bytes |
| 553 |
|
| 554 |
# Dump the original partition information and details to disk. This is |
| 555 |
# used in case something goes wrong and human interaction is required |
| 556 |
# to revert any changes. |
| 557 |
rqe sgd_info sgdisk "--info=${PART}" --print "${DISK}" >"${pt_info}" || |
| 558 |
fail "${dev}: failed to dump original sgdisk info" |
| 559 |
RESTORE_HUMAN="${pt_info}" |
| 560 |
|
| 561 |
# support reading old format (<1.0.2) |
| 562 |
# Logical sector size: <VAL> bytes |
| 563 |
# and newer format >= 1.0.2 |
| 564 |
# Sector size (logical): <VAL> bytes |
| 565 |
# Sector size (logical/physical): <VAL>/<PVAL> bytes |
| 566 |
sector_size=$(awk ' |
| 567 |
$0 ~ /^Logical sector size:.*bytes/ { print $4; exit(0); } |
| 568 |
$0 ~ /^Sector size \(logical\):/ { print $4; exit(0); } |
| 569 |
$0 ~ /^Sector size \(logical\/physical\):/ { |
| 570 |
sub(/\/.*/, "", $4); print $4; exit(0); }' \ |
| 571 |
"$pt_info") && [ -n "$sector_size" ] || { |
| 572 |
sector_size=512 |
| 573 |
error "WARN: did not find sector size, assuming 512" |
| 574 |
} |
| 575 |
|
| 576 |
debug 1 "$dev: original sgdisk info:" |
| 577 |
debugcat 1 "${pt_info}" |
| 578 |
|
| 579 |
# Pretend to move the backup GPT header to the end of the disk and dump |
| 580 |
# the resulting partition information. We use this info to determine if |
| 581 |
# we have to resize the partition. |
| 582 |
rqe sgd_pretend sgdisk --pretend --move-second-header \ |
| 583 |
--print "${DISK}" >"${pt_pretend}" || |
| 584 |
fail "${dev}: failed to dump pretend sgdisk info" |
| 585 |
|
| 586 |
debug 1 "$dev: pretend sgdisk info" |
| 587 |
debugcat 1 "${pt_pretend}" |
| 588 |
|
| 589 |
# Extract the partition data from the pretend dump |
| 590 |
awk 'found { print } ; $1 == "Number" { found = 1 }' \ |
| 591 |
"${pt_pretend}" >"${pt_data}" || |
| 592 |
fail "${dev}: failed to parse pretend sgdisk info" |
| 593 |
|
| 594 |
# Get the start and end sectors of the partition to be grown |
| 595 |
pt_start=$(awk '$1 == '"${PART}"' { print $2 }' "${pt_data}") && |
| 596 |
[ -n "${pt_start}" ] || |
| 597 |
fail "${dev}: failed to get start sector" |
| 598 |
pt_end=$(awk '$1 == '"${PART}"' { print $3 }' "${pt_data}") && |
| 599 |
[ -n "${pt_end}" ] || |
| 600 |
fail "${dev}: failed to get end sector" |
| 601 |
# Start and end are inclusive, start 2048 end 2057 is length 10. |
| 602 |
pt_size="$((pt_end - pt_start + 1))" |
| 603 |
|
| 604 |
# Get the last usable sector |
| 605 |
last=$(awk '/last usable sector is/ { print $NF }' \ |
| 606 |
"${pt_pretend}") && [ -n "${last}" ] || |
| 607 |
fail "${dev}: failed to get last usable sector" |
| 608 |
|
| 609 |
# Find the maximal end sector that is >= pt_end |
| 610 |
pt_max=$(awk '{ if ($2 >= pt_end && $2 < min) { min = $2 } } END \ |
| 611 |
{ print min-1 }' min="${last}" pt_end="${pt_end}" \ |
| 612 |
"${pt_data}") && [ -n "${pt_max}" ] || |
| 613 |
fail "${dev}: failed to find max end sector" |
| 614 |
|
| 615 |
pt_size_bytes=$((pt_size*sector_size)) |
| 616 |
|
| 617 |
# make partition sizes always even units of $align bytes. |
| 618 |
local align=$((1024*1024)) |
| 619 |
local sectors_per_align=$((align/sector_size)) max_size_align="" |
| 620 |
max_size_align=$((((pt_max+1-pt_start)/sectors_per_align)*sectors_per_align)) |
| 621 |
pt_max=$((max_size_align+pt_start-1)) |
| 622 |
|
| 623 |
debug 1 "${dev}: pt_start=${pt_start} pt_end=${pt_end}" \ |
| 624 |
"pt_size=${pt_size} pt_max=${pt_max} last=${last} pt_size_bytes=${pt_size_bytes}" |
| 625 |
|
| 626 |
# Check if the partition can be grown |
| 627 |
[ "${pt_end}" -eq "${pt_max}" ] && { |
| 628 |
nochange "${dev}: size=${pt_size_bytes} bytes (${pt_size} sectors of ${sector_size}), it cannot be grown" |
| 629 |
return |
| 630 |
} |
| 631 |
[ "$((pt_end + FUDGE/sector_size))" -gt "${pt_max}" ] && { |
| 632 |
nochange "${dev}: could only be grown by" \ |
| 633 |
"$((pt_max - pt_end)) [fudge=$((FUDGE/sector_size))]" |
| 634 |
return |
| 635 |
} |
| 636 |
|
| 637 |
# The partition can be grown if we made it here. Get some more info |
| 638 |
# about it so we can do it properly. |
| 639 |
# FIXME: Do we care about the attribute flags? |
| 640 |
code=$(awk '/^Partition GUID code:/ { print $4 }' "${pt_info}") |
| 641 |
guid=$(awk '/^Partition unique GUID:/ { print $4 }' "${pt_info}") |
| 642 |
name=$(awk '/^Partition name:/ { gsub(/'"'"'/, "") ; \ |
| 643 |
if (NF >= 3) print substr($0, index($0, $3)) }' "${pt_info}") |
| 644 |
[ -n "${code}" -a -n "${guid}" ] || |
| 645 |
fail "${dev}: failed to parse sgdisk details" |
| 646 |
|
| 647 |
debug 1 "${dev}: code=${code} guid=${guid} name='${name}'" |
| 648 |
local wouldrun="" |
| 649 |
[ "$DRY_RUN" -ne 0 ] && wouldrun="would-run" |
| 650 |
|
| 651 |
# Calculate the new size of the partition |
| 652 |
new_size=$((pt_max - pt_start + 1)) |
| 653 |
change_info="partition=${PART} start=${pt_start}" |
| 654 |
change_info="${change_info} old: size=${pt_size} end=${pt_end}" |
| 655 |
change_info="${change_info} new: size=${new_size} end=${pt_max}" |
| 656 |
|
| 657 |
# Backup the current partition table, we're about to modify it |
| 658 |
rq sgd_backup ${wouldrun:+"$wouldrun"} sgdisk "--backup=${GPT_BACKUP}" "${DISK}" || |
| 659 |
fail "${dev}: failed to backup the partition table" |
| 660 |
|
| 661 |
# Modify the partition table. We do it all in one go (the order is |
| 662 |
# important!): |
| 663 |
# - move the GPT backup header to the end of the disk |
| 664 |
# - delete the partition |
| 665 |
# - recreate the partition with the new size |
| 666 |
# - set the partition code |
| 667 |
# - set the partition GUID |
| 668 |
# - set the partition name |
| 669 |
rq sgdisk_mod ${wouldrun:+"$wouldrun"} sgdisk --move-second-header "--delete=${PART}" \ |
| 670 |
"--new=${PART}:${pt_start}:${pt_max}" \ |
| 671 |
"--typecode=${PART}:${code}" \ |
| 672 |
"--partition-guid=${PART}:${guid}" \ |
| 673 |
"--change-name=${PART}:${name}" "${DISK}" && |
| 674 |
rq pt_update ${wouldrun:+"$wouldrun"} pt_update "$DISK" "$PART" || { |
| 675 |
RESTORE_FUNC=gpt_restore |
| 676 |
fail "${dev}: failed to repartition" |
| 677 |
} |
| 678 |
|
| 679 |
# Dry run |
| 680 |
[ "${DRY_RUN}" -ne 0 ] && { |
| 681 |
change "${change_info}" |
| 682 |
return |
| 683 |
} |
| 684 |
|
| 685 |
changed "${change_info}" |
| 686 |
return |
| 687 |
} |
| 688 |
|
| 689 |
kver_to_num() { |
| 690 |
local kver="$1" maj min mic |
| 691 |
|
| 692 |
# Canonicalize the kernel version |
| 693 |
kver=${kver%%[!0-9.]*}.0.0 |
| 694 |
|
| 695 |
maj=${kver%%[!0-9]*} |
| 696 |
kver=${kver#*.} |
| 697 |
min=${kver%%[!0-9]*} |
| 698 |
kver=${kver#*.} |
| 699 |
mic=${kver%%[!0-9]*} |
| 700 |
_RET=$((maj*1000*1000+min*1000+mic)) |
| 701 |
} |
| 702 |
|
| 703 |
kver_cmp() { |
| 704 |
local op="$2" n1="" n2="" |
| 705 |
kver_to_num "$1" |
| 706 |
n1="$_RET" |
| 707 |
kver_to_num "$3" |
| 708 |
n2="$_RET" |
| 709 |
test $n1 "$op" $n2 |
| 710 |
} |
| 711 |
|
| 712 |
rq() { |
| 713 |
# runquieterror(label, command) |
| 714 |
# gobble stderr of a command unless it errors |
| 715 |
local label="$1" ret="" efile="" |
| 716 |
efile="$TEMP_D/$label.err" |
| 717 |
shift; |
| 718 |
|
| 719 |
local rlabel="running" |
| 720 |
[ "$1" = "would-run" ] && rlabel="would-run" && shift |
| 721 |
|
| 722 |
local cmd="" x="" |
| 723 |
for x in "$@"; do |
| 724 |
[ "${x#* }" != "$x" -o "${x#* \"}" != "$x" ] && x="'$x'" |
| 725 |
cmd="$cmd $x" |
| 726 |
done |
| 727 |
cmd=${cmd# } |
| 728 |
|
| 729 |
debug 2 "${rlabel}[$label][$_capture]" "$cmd" |
| 730 |
[ "$rlabel" = "would-run" ] && return 0 |
| 731 |
|
| 732 |
if [ "${_capture}" = "erronly" ]; then |
| 733 |
"$@" 2>"$TEMP_D/$label.err" |
| 734 |
ret=$? |
| 735 |
else |
| 736 |
"$@" >"$TEMP_D/$label.err" 2>&1 |
| 737 |
ret=$? |
| 738 |
fi |
| 739 |
if [ $ret -ne 0 ]; then |
| 740 |
error "failed [$label:$ret]" "$@" |
| 741 |
cat "$efile" 1>&2 |
| 742 |
fi |
| 743 |
return $ret |
| 744 |
} |
| 745 |
|
| 746 |
rqe() { |
| 747 |
local _capture="erronly" |
| 748 |
rq "$@" |
| 749 |
} |
| 750 |
|
| 751 |
verify_ptupdate() { |
| 752 |
local input="$1" found="" reason="" kver="" |
| 753 |
|
| 754 |
# we can always satisfy 'off' |
| 755 |
if [ "$input" = "off" ]; then |
| 756 |
_RET="false"; |
| 757 |
return 0; |
| 758 |
fi |
| 759 |
|
| 760 |
if command -v partx >/dev/null 2>&1; then |
| 761 |
local out="" ret=0 |
| 762 |
out=$(partx --help 2>&1) |
| 763 |
ret=$? |
| 764 |
if [ $ret -eq 0 ]; then |
| 765 |
echo "$out" | grep -q -- --update || { |
| 766 |
reason="partx has no '--update' flag in usage." |
| 767 |
found="off" |
| 768 |
} |
| 769 |
else |
| 770 |
reason="'partx --help' returned $ret. assuming it is old." |
| 771 |
found="off" |
| 772 |
fi |
| 773 |
else |
| 774 |
reason="no 'partx' command" |
| 775 |
found="off" |
| 776 |
fi |
| 777 |
|
| 778 |
if [ -z "$found" ]; then |
| 779 |
if [ "$(uname)" != "Linux" ]; then |
| 780 |
reason="Kernel is not Linux per uname." |
| 781 |
found="off" |
| 782 |
fi |
| 783 |
fi |
| 784 |
|
| 785 |
if [ -z "$found" ]; then |
| 786 |
kver=$(uname -r) || debug 1 "uname -r failed!" |
| 787 |
|
| 788 |
if ! kver_cmp "${kver-0.0.0}" -ge 3.8.0; then |
| 789 |
reason="Kernel '$kver' < 3.8.0." |
| 790 |
found="off" |
| 791 |
fi |
| 792 |
fi |
| 793 |
|
| 794 |
if [ -z "$found" ]; then |
| 795 |
_RET="true" |
| 796 |
return 0 |
| 797 |
fi |
| 798 |
|
| 799 |
case "$input" in |
| 800 |
on) error "$reason"; return 1;; |
| 801 |
auto) |
| 802 |
_RET="false"; |
| 803 |
debug 1 "partition update disabled: $reason" |
| 804 |
return 0;; |
| 805 |
force) |
| 806 |
_RET="true" |
| 807 |
error "WARNING: ptupdate forced on even though: $reason" |
| 808 |
return 0;; |
| 809 |
esac |
| 810 |
error "unknown input '$input'"; |
| 811 |
return 1; |
| 812 |
} |
| 813 |
|
| 814 |
pt_update() { |
| 815 |
local dev="$1" part="$2" update="${3:-$PT_UPDATE}" |
| 816 |
if ! $update; then |
| 817 |
return 0 |
| 818 |
fi |
| 819 |
# partx only works on block devices (do not run on file) |
| 820 |
[ -b "$dev" ] || return 0 |
| 821 |
partx --update --nr "$part" "$dev" |
| 822 |
} |
| 823 |
|
| 824 |
has_cmd() { |
| 825 |
command -v "${1}" >/dev/null 2>&1 |
| 826 |
} |
| 827 |
|
| 828 |
resize_sgdisk_gpt() { |
| 829 |
resize_sgdisk gpt |
| 830 |
} |
| 831 |
|
| 832 |
resize_sgdisk_dos() { |
| 833 |
fail "unable to resize dos label with sgdisk" |
| 834 |
} |
| 835 |
|
| 836 |
resize_sfdisk_gpt() { |
| 837 |
resize_sfdisk gpt |
| 838 |
} |
| 839 |
|
| 840 |
resize_sfdisk_dos() { |
| 841 |
resize_sfdisk dos |
| 842 |
} |
| 843 |
|
| 844 |
get_table_format() { |
| 845 |
local out="" disk="$1" |
| 846 |
if has_cmd blkid && blkid -V | grep -q util-linux && |
| 847 |
out=$(blkid -o value -s PTTYPE "$disk") && |
| 848 |
[ "$out" = "dos" -o "$out" = "gpt" ]; then |
| 849 |
_RET="$out" |
| 850 |
return |
| 851 |
fi |
| 852 |
_RET="dos" |
| 853 |
if [ -z "$SFDISK" ]; then |
| 854 |
out=$(LANG=C sgdisk --print "$disk") || { |
| 855 |
error "Could not determine partition table format of $disk" \ |
| 856 |
"with 'sgdisk --print $disk'" |
| 857 |
return 1 |
| 858 |
} |
| 859 |
# looking for a message like: |
| 860 |
# Found invalid GPT and valid MBR; converting MBR to GPT format |
| 861 |
case "$out" in |
| 862 |
*\ valid\ MBR\ *) _RET="dos";; |
| 863 |
*) _RET="gpt";; |
| 864 |
esac |
| 865 |
return |
| 866 |
elif [ ${SFDISK_VERSION} -lt ${SFDISK_2_26} ] && |
| 867 |
out=$(sfdisk --id --force "$disk" 1 2>/dev/null); then |
| 868 |
if [ "$out" = "ee" ]; then |
| 869 |
_RET="gpt" |
| 870 |
else |
| 871 |
_RET="dos" |
| 872 |
fi |
| 873 |
return |
| 874 |
elif out=$(LANG=C sfdisk --list "$disk"); then |
| 875 |
out=$(echo "$out" | sed -e '/Disklabel type/!d' -e 's/.*: //') |
| 876 |
case "$out" in |
| 877 |
gpt|dos) _RET="$out";; |
| 878 |
*) error "WARN: unknown label $out";; |
| 879 |
esac |
| 880 |
fi |
| 881 |
} |
| 882 |
|
| 883 |
get_resizer() { |
| 884 |
local format="$1" user="${2:-auto}" |
| 885 |
|
| 886 |
case "$user" in |
| 887 |
sgdisk) _RET="resize_sgdisk_$format"; return;; |
| 888 |
sfdisk) _RET="resize_sfdisk_$format"; return;; |
| 889 |
auto) :;; |
| 890 |
*) error "unexpected value '$user' for growpart resizer"; return 1;; |
| 891 |
esac |
| 892 |
|
| 893 |
if [ "$format" = "dos" ]; then |
| 894 |
[ -n "$SFDISK" ] || { |
| 895 |
error "sfdisk is required for resizing dos/MBR partition table." |
| 896 |
return 1 |
| 897 |
} |
| 898 |
_RET="resize_sfdisk_dos" |
| 899 |
return 0 |
| 900 |
fi |
| 901 |
|
| 902 |
if [ "${SFDISK_VERSION}" -ge ${SFDISK_V_WORKING_GPT} ]; then |
| 903 |
# sfdisk 2.26.2 works for resize but loses type (LP: #1474090) |
| 904 |
_RET="resize_sfdisk_gpt" |
| 905 |
elif has_cmd sgdisk; then |
| 906 |
_RET="resize_sgdisk_$format" |
| 907 |
else |
| 908 |
error "no tools available to resize disk with '$format'" |
| 909 |
return 1 |
| 910 |
fi |
| 911 |
return 0 |
| 912 |
} |
| 913 |
|
| 914 |
maybe_lvm_resize() { |
| 915 |
local disk="$1" part="$2" partpath="" ret="" out="" wouldrun="" |
| 916 |
[ "$DRY_RUN" -ne 0 ] && wouldrun="would-run" |
| 917 |
|
| 918 |
has_cmd lvm || { |
| 919 |
debug 2 "No lvm command, cannot attempt lvm resize of disk '$disk' part '$part'" |
| 920 |
return 0 |
| 921 |
} |
| 922 |
get_diskpart_path "$1" "$2" || { |
| 923 |
error "could not determine partition path for disk '$DISK' part '$part'" |
| 924 |
return 1 |
| 925 |
} |
| 926 |
partpath="$_RET" |
| 927 |
|
| 928 |
# can't use rq or rqe here because of "not an lvm" exit code 5. |
| 929 |
set -- lvm pvs --nolocking --readonly -o pvname "$partpath" |
| 930 |
debug 2 "executing: $*" |
| 931 |
out=$("$@" 2>&1) |
| 932 |
ret=$? |
| 933 |
case "$ret" in |
| 934 |
5) debug 1 "$partpath is not an lvm pv"; return 0;; |
| 935 |
0) :;; |
| 936 |
*) error "failed to execute [$ret] '$*'" |
| 937 |
error "$out" |
| 938 |
return 1;; |
| 939 |
esac |
| 940 |
rq lvm_resize $wouldrun lvm pvresize "$partpath" || { |
| 941 |
error "Failed to resize lvm pv $partpath" |
| 942 |
return 1 |
| 943 |
} |
| 944 |
return 0 |
| 945 |
} |
| 946 |
|
| 947 |
pt_update="auto" |
| 948 |
resizer=${GROWPART_RESIZER:-"auto"} |
| 949 |
free_percent="" |
| 950 |
while [ $# -ne 0 ]; do |
| 951 |
cur=${1} |
| 952 |
next=${2} |
| 953 |
case "$cur" in |
| 954 |
-h|--help) |
| 955 |
Usage |
| 956 |
exit 0 |
| 957 |
;; |
| 958 |
--free-percent|--free-percent=*) |
| 959 |
if [ "${cur#--free-percent=}" != "$cur" ]; then |
| 960 |
next="${cur#--free-percent=}" |
| 961 |
else |
| 962 |
shift |
| 963 |
fi |
| 964 |
if [ "$next" -gt 0 ] 2>/dev/null && |
| 965 |
[ "$next" -lt 100 ] 2>/dev/null; then |
| 966 |
free_percent=$next |
| 967 |
else |
| 968 |
fail "unknown/invalid --free-percent option: $next" |
| 969 |
fi |
| 970 |
;; |
| 971 |
--fudge) |
| 972 |
FUDGE=${next} |
| 973 |
shift |
| 974 |
;; |
| 975 |
-N|--dry-run) |
| 976 |
DRY_RUN=1 |
| 977 |
;; |
| 978 |
-u|--update|--update=*) |
| 979 |
if [ "${cur#--update=}" != "$cur" ]; then |
| 980 |
next="${cur#--update=}" |
| 981 |
else |
| 982 |
shift |
| 983 |
fi |
| 984 |
case "$next" in |
| 985 |
off|auto|force|on) pt_update=$next;; |
| 986 |
*) fail "unknown --update option: $next";; |
| 987 |
esac |
| 988 |
;; |
| 989 |
-v|--verbose) |
| 990 |
VERBOSITY=$((VERBOSITY+1)) |
| 991 |
;; |
| 992 |
--) |
| 993 |
shift |
| 994 |
break |
| 995 |
;; |
| 996 |
-*) |
| 997 |
fail "unknown option ${cur}" |
| 998 |
;; |
| 999 |
*) |
| 1000 |
if [ -z "${DISK}" ]; then |
| 1001 |
DISK=${cur} |
| 1002 |
else |
| 1003 |
[ -z "${PART}" ] || fail "confused by arg ${cur}" |
| 1004 |
PART=${cur} |
| 1005 |
fi |
| 1006 |
;; |
| 1007 |
esac |
| 1008 |
shift |
| 1009 |
done |
| 1010 |
|
| 1011 |
[ -n "${DISK}" ] || bad_Usage "must supply disk and partition-number" |
| 1012 |
[ -n "${PART}" ] || bad_Usage "must supply partition-number" |
| 1013 |
|
| 1014 |
[ -e "${DISK}" ] || fail "${DISK}: does not exist" |
| 1015 |
|
| 1016 |
# cache the has_cmd return value. |
| 1017 |
has_cmd sfdisk && SFDISK=sfdisk || SFDISK="" |
| 1018 |
has_cmd sgdisk && SGDISK=sgdisk || SGDISK="" |
| 1019 |
|
| 1020 |
[ -n "$SGDISK" -o -n "$SFDISK" ] || |
| 1021 |
fail "Did not have sfdisk or sgdisk in PATH." |
| 1022 |
|
| 1023 |
get_sfdisk_version || fail |
| 1024 |
|
| 1025 |
# If $DISK is a symlink, resolve it. |
| 1026 |
# This avoids problems due to varying partition device name formats |
| 1027 |
# (e.g. "1" for /dev/sda vs "-part1" for /dev/disk/by-id/name) |
| 1028 |
if [ -L "${DISK}" ]; then |
| 1029 |
has_cmd readlink || |
| 1030 |
fail "${DISK} is a symlink, but 'readlink' command not available." |
| 1031 |
real_disk=$(readlink -f "${DISK}") || fail "unable to resolve ${DISK}" |
| 1032 |
debug 1 "${DISK} resolved to ${real_disk}" |
| 1033 |
DISK=${real_disk} |
| 1034 |
fi |
| 1035 |
|
| 1036 |
[ "${PART#*[!0-9]}" = "${PART}" ] || fail "partition-number must be a number" |
| 1037 |
|
| 1038 |
verify_ptupdate "$pt_update" || fail |
| 1039 |
PT_UPDATE=$_RET |
| 1040 |
|
| 1041 |
debug 1 "update-partition set to $PT_UPDATE" |
| 1042 |
|
| 1043 |
mktemp_d && TEMP_D="${_RET}" || fail "failed to make temp dir" |
| 1044 |
trap cleanup 0 # EXIT - some shells may not like 'EXIT' but are ok with 0 |
| 1045 |
|
| 1046 |
# get the ID of the first partition to determine if it's MBR or GPT |
| 1047 |
get_table_format "$DISK" || fail |
| 1048 |
format=$_RET |
| 1049 |
get_resizer "$format" "$resizer" || |
| 1050 |
fail "failed to get a resizer for format '$format'" |
| 1051 |
resizer=$_RET |
| 1052 |
if [ -n "$free_percent" ] && [ "$resizer" = "sgdisk" ]; then |
| 1053 |
fail "--free-percent is not supported with sgdisk resizer" |
| 1054 |
fi |
| 1055 |
|
| 1056 |
lock_disk "$DISK" |
| 1057 |
debug 1 "resizing $PART on $DISK using $resizer" |
| 1058 |
"$resizer" |
| 1059 |
ret=$? |
| 1060 |
|
| 1061 |
unlock_disk_and_settle "$DISK" |
| 1062 |
|
| 1063 |
if [ "$RESIZE_RESULT" = "CHANGED" -o "$RESIZE_RESULT" = "CHANGE" ]; then |
| 1064 |
maybe_lvm_resize "$DISK" "$PART" || fail "lvm resize failed." |
| 1065 |
fi |
| 1066 |
|
| 1067 |
exit $ret |
| 1068 |
|
| 1069 |
# vi: ts=4 noexpandtab |