From 4dcd267dfa76d960d1e511abb242690b1c43b128 Mon Sep 17 00:00:00 2001 From: Liam Hopkins Date: Tue, 28 Apr 2020 17:42:00 -0700 Subject: use flock in EL disk expand (#892) --- packages/gce-disk-expand/src/expandfs-lib.sh | 10 ---- .../modules.d/50expand_rootfs/expand_rootfs.sh | 62 +++++++++++++--------- 2 files changed, 36 insertions(+), 36 deletions(-) (limited to 'packages') diff --git a/packages/gce-disk-expand/src/expandfs-lib.sh b/packages/gce-disk-expand/src/expandfs-lib.sh index 7a26330..657dcfa 100755 --- a/packages/gce-disk-expand/src/expandfs-lib.sh +++ b/packages/gce-disk-expand/src/expandfs-lib.sh @@ -84,15 +84,8 @@ sgdisk_fix_gpt() { local label=$(sgdisk_get_label "$disk") [ "$label" != "gpt" ] && return - # TODO Find a better solution than sleep. - # Add sleeps to allow this operation to fully complete. On some systems, other - # operations such as systemd-fsck tasks can collide and fail. - udevadm settle - sleep 1 kmsg "Moving GPT header for $disk with sgdisk." sgdisk --move-second-header "$disk" - udevadm settle - sleep 1 } # Returns "disk:partition", supporting multiple block types. @@ -128,8 +121,6 @@ parted_needresize() { return 1 fi - udevadm settle - if ! printf "$out" | sed '$!d' | grep -q "^${partnum}:"; then kmsg "Root partition is not final partition on disk. Not resizing." return 1 @@ -157,5 +148,4 @@ parted_resizepart() { kmsg "Unable to resize ${disk}${partnum}: ${out}" return 1 fi - udevadm settle } diff --git a/packages/gce-disk-expand/src/usr/share/dracut/modules.d/50expand_rootfs/expand_rootfs.sh b/packages/gce-disk-expand/src/usr/share/dracut/modules.d/50expand_rootfs/expand_rootfs.sh index 6e1be71..41cc6da 100755 --- a/packages/gce-disk-expand/src/usr/share/dracut/modules.d/50expand_rootfs/expand_rootfs.sh +++ b/packages/gce-disk-expand/src/usr/share/dracut/modules.d/50expand_rootfs/expand_rootfs.sh @@ -22,9 +22,8 @@ # logged, rather than causing end of execution. Note that error handling in the # main() function always calls return 0 - main() { - local disk="" partnum="" fs_type="" rootdev="" + local rootdev="" disk="" partnum="" # Remove 'block:' prefix and find the root device. if ! rootdev=$(readlink -f "${root#block:}") || [ -z "${rootdev}" ]; then @@ -40,36 +39,47 @@ main() { disk=${out%:*} partnum=${out#*:} - if ! parted_needresize "$disk" "$partnum"; then - kmsg "Disk ${rootdev} doesn't need resizing" - return - fi + ( + # If we can't obtain an exclusive lock on FD 9 (which is associated in this + # subshell with the root device we're modifying), then exit. This is needed + # to prevent systemd from issuing udev re-enumerations and fsck calls before + # we're done. See https://systemd.io/BLOCK_DEVICE_LOCKING/ - if ! parted --help | grep -q 'resizepart'; then - kmsg "No 'resizepart' command in this parted" - return - fi + if ! flock -n 9; then + kmsg "couldn't obtain lock on ${rootdev}" + exit + fi - kmsg "Resizing disk ${rootdev}" - # Reset the counter for failed job starts to prevent overrunning the default - # start burst limits for systemd-fsck-root.service. - systemctl reset-failed + if ! parted_needresize "$disk" "$partnum"; then + kmsg "Disk ${rootdev} doesn't need resizing" + exit + fi - # First, move the secondary GPT to the end. - if ! out=$(sgdisk_fix_gpt "$disk"); then - kmsg "Failed to fix GPT: ${out}" - fi + if ! parted --help | grep -q 'resizepart'; then + kmsg "No 'resizepart' command in this parted" + exit + fi - if ! out=$(parted_resizepart "$disk" "$partnum"); then - kmsg "Failed to resize partition: ${out}" - return - fi + kmsg "Resizing disk ${rootdev}" - if ! out=$(resize_filesystem "$rootdev"); then - kmsg "Not resizing filesystem: ${out}" - return - fi + # First, move the secondary GPT to the end. + if ! out=$(sgdisk_fix_gpt "$disk"); then + kmsg "Failed to fix GPT: ${out}" + fi + + if ! out=$(parted_resizepart "$disk" "$partnum"); then + kmsg "Failed to resize partition: ${out}" + exit + fi + + if ! out=$(resize_filesystem "$rootdev"); then + kmsg "Failed to resize filesystem: ${out}" + exit + fi + ) 9<$rootdev } . /lib/expandfs-lib.sh +udevadm settle main +udevadm settle -- cgit v1.2.1