From b08dcfa77394bf14bf6dac76bc8701592a832a82 Mon Sep 17 00:00:00 2001
From: Tim Emiola <temiola@google.com>
Date: Wed, 4 Feb 2015 02:47:36 -0800
Subject: [PATCH] Update docker creation script to use the full boot disk size

- automates the disk partitioning described in
  https://cloud.google.com/compute/docs/disks/persistent-disks#repartitionrootpd

Removes an unnecessary semi-colon
---
 .../new_grpc_docker_builder_on_startup.sh     | 75 +++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/tools/gce_setup/new_grpc_docker_builder_on_startup.sh b/tools/gce_setup/new_grpc_docker_builder_on_startup.sh
index 4b20a8be7d..cfd05415a0 100755
--- a/tools/gce_setup/new_grpc_docker_builder_on_startup.sh
+++ b/tools/gce_setup/new_grpc_docker_builder_on_startup.sh
@@ -41,7 +41,82 @@ _source_gs_script() {
   source $script_path
 }
 
+# Args:
+#   $1: numerator
+#   $2: denominator
+#   $3: threshold (optional; defaults to $THRESHOLD)
+#
+# Returns:
+#   1 if (numerator / denominator > threshold)
+#   0 otherwise
+_gce_disk_cmp_ratio() {
+  local DEFAULT_THRESHOLD="1.1"
+  local numer="${1}"
+  local denom="${2}"
+  local threshold="${3:-${DEFAULT_THRESHOLD}}"
+
+  if `which python > /dev/null 2>&1`; then
+    python -c "print(1 if (1. * ${numer} / ${denom} > ${threshold}) else 0)"
+  else
+    echo "Can't find python; calculation not done." 1>&2
+    return 1
+  fi
+}
+
+# Repartitions the disk or resizes the file system, depending on the current
+# state of the partition table.
+#
+# Automates the process described in
+# - https://cloud.google.com/compute/docs/disks/persistent-disks#repartitionrootpd
+_gce_disk_maybe_resize_then_reboot() {
+  # Determine the size in blocks, of the whole disk and the first partition.
+  local dev_sda="$(fdisk -s /dev/sda)"
+  local dev_sda1="$(fdisk -s /dev/sda1)"
+  local dev_sda1_start="$(sudo fdisk -l /dev/sda | grep /dev/sda1 | sed -e 's/ \+/ /g' | cut -d' ' -f 3)"
+
+  # Use fdisk to
+  # - first see if the partion 1 is using as much of the disk as it should
+  # - then to resize the partition if it's not
+  #
+  # fdisk(1) flags:
+  # -c: disable DOS compatibility mode
+  # -u: change display mode to sectors (from cylinders)
+  #
+  # fdisk(1) commands:
+  # d: delete partition (automatically selects the first one)
+  # n: new partition
+  # p: primary
+  # 1: partition number
+  # $dev_sda1_start: specify the value for the start sector, the default may be incorrect
+  # <1 blank lines>: accept the defaults for end sectors
+  # w: write partition table
+  if [ $(_gce_disk_cmp_ratio "${dev_sda}" "${dev_sda1}") -eq 1 ]; then
+    echo "$FUNCNAME: Updating the partition table to use full ${dev_sda} instead ${dev_sda1}"
+    cat <<EOF | fdisk -c -u /dev/sda
+d
+n
+p
+1
+$dev_sda1_start
+
+w
+EOF
+    echo "$FUNCNAME: ... updated the partition table"
+    shutdown -r now
+    return 0
+  fi
+
+  # After repartitioning, use resize2fs to expand sda1.
+  local df_size="$(df -B 1K / | grep ' /$' | sed -e 's/ \+/ /g' | cut -d' ' -f 2)"
+  if [ $(_gce_disk_cmp_ratio "${dev_sda}" "${df_size}") -eq 1 ]; then
+    echo "$FUNCNAME: resizing the partition to make full use of it"
+    resize2fs /dev/sda1
+    echo "$FUNCNAME: ... resize completed"
+  fi
+}
+
 main() {
+    _gce_disk_maybe_resize_then_reboot
     local script_attr='shared_startup_script_url'
     _source_gs_script $script_attr || {
       echo "halting, script 'attributes/$script_attr' could not be sourced"
-- 
GitLab