Skip to content

Commit

Permalink
Interim fix fo EL7 GPT header move race condition with systemd-fsck. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Marano authored Apr 28, 2020
1 parent 84bb53e commit 0c97a9e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
6 changes: 6 additions & 0 deletions packages/gce-disk-expand/packaging/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
gce-disk-expand (1:20200428.00-g1) stable; urgency=medium

* Fix potential races in disk expand logic with GPT disks.

-- Google Cloud Team <[email protected]> Tue, 28 Apr 2020 10:24:07 -0700

gce-disk-expand (1:20190708.00-g1) stable; urgency=medium

* Update disk expand to fix bugs with GPT.
Expand Down
2 changes: 1 addition & 1 deletion packages/gce-disk-expand/packaging/setup_deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

NAME="gce-disk-expand"
VERSION="20190708.00"
VERSION="20200428.00"

working_dir=${PWD}
if [[ $(basename "$working_dir") != $NAME ]]; then
Expand Down
2 changes: 1 addition & 1 deletion packages/gce-disk-expand/packaging/setup_rpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

NAME="gce-disk-expand"
VERSION="20190708.00"
VERSION="20200428.00"

rpm_working_dir=/tmp/rpmpackage/
working_dir=${PWD}
Expand Down
38 changes: 27 additions & 11 deletions packages/gce-disk-expand/src/expandfs-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

kmsg() {
echo "gce-disk-expand: $@" > /dev/kmsg
}

resize_filesystem() {
local disk="$1" fs_type=""

Expand All @@ -23,22 +27,22 @@ resize_filesystem() {

case "${fs_type}" in
xfs)
echo "XFS filesystems must be mounted to be resized, deferring."
kmsg "XFS filesystems must be mounted to be resized, deferring."
echo "true" > /tmp/xfs_resize
return 1
;;
ext*)
if ! out=$(e2fsck -pf "$disk"); then
local ret=$?
echo "Calling e2fsck \"${disk}\" failed: ${out} exit code ${ret}"
kmsg "Calling e2fsck \"${disk}\" failed: ${out} exit code ${ret}"
fi
if ! out=$(resize2fs "$disk"); then
echo "Calling resize2fs \"${disk}\" failed: ${out}"
kmsg "Calling resize2fs \"${disk}\" failed: ${out}"
return 1
fi
;;
*)
echo "Unsupported filesystem type ${fs_type}, unable to expand size."
kmsg "Unsupported filesystem type ${fs_type}, unable to expand size."
return 1
;;
esac
Expand All @@ -47,14 +51,15 @@ resize_filesystem() {
blkid_get_fstype() (
local root="$1"

kmsg "Getting fstype for $root with blkid."
if ! out=$(blkid -o udev "$root"); then
echo "Detecting fstype by blkid failed: ${out}"
kmsg "Detecting fstype by blkid failed: ${out}"
return 1
fi

eval "$out"
if [ -z "$ID_FS_TYPE" ]; then
echo "No ID_FS_TYPE from blkid."
kmsg "No ID_FS_TYPE from blkid."
return 1
fi
echo $ID_FS_TYPE
Expand All @@ -64,6 +69,7 @@ sgdisk_get_label() {
local root="$1"
[ -z "$root" ] && return 0

kmsg "Getting $root label with sgdisk."
if sgdisk -p "$root" | grep -q "Found invalid GPT and valid MBR"; then
echo "mbr"
else
Expand All @@ -78,7 +84,15 @@ 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.
Expand All @@ -87,7 +101,7 @@ split_partition() {
[ -z "$root" ] && return 0

if [ -e /sys/block/${root##*/} ]; then
echo "Root is not a partition, skipping partition resize."
kmsg "Root is not a partition, skipping partition resize."
return 1
fi

Expand All @@ -104,19 +118,20 @@ split_partition() {
parted_needresize() {
local disk="$1" partnum="$2" disksize="" partend=""
if [ -z "$disk" ] || [ -z "$partnum" ]; then
echo "invalid args to parted_needresize"
kmsg "invalid args to parted_needresize"
return 1
fi

kmsg "Check if $disk partition $partnum needs resize with parted."
if ! out=$(parted -sm "$disk" unit b print 2>&1); then
echo "Failed to get disk details: ${out}"
kmsg "Failed to get disk details: ${out}"
return 1
fi

udevadm settle

if ! printf "$out" | sed '$!d' | grep -q "^${partnum}:"; then
echo "Root partition is not final partition on disk. Not resizing."
kmsg "Root partition is not final partition on disk. Not resizing."
return 1
fi

Expand All @@ -137,8 +152,9 @@ parted_resizepart() {
local disk="$1" partnum="$2"
[ -z "$disk" -o -z "$partnum" ] && return

kmsg "Resizing $disk partition $partnum with parted."
if ! out=$(parted -sm "$disk" -- resizepart $partnum -1 2>&1); then
echo "Unable to resize ${disk}${partnum}: ${out}"
kmsg "Unable to resize ${disk}${partnum}: ${out}"
return 1
fi
udevadm settle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
# logged, rather than causing end of execution. Note that error handling in the
# main() function always calls return 0

kmsg() {
echo "expand_rootfs: $@" > /dev/kmsg
}

main() {
local disk="" partnum="" fs_type="" rootdev=""
Expand Down Expand Up @@ -69,7 +66,7 @@ main() {
fi

if ! out=$(resize_filesystem "$rootdev"); then
kmsg "Failed to resize filesystem: ${out}"
kmsg "Not resizing filesystem: ${out}"
return
fi
}
Expand Down

0 comments on commit 0c97a9e

Please sign in to comment.