github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/contrib/cirrus/add_second_partition.sh (about)

     1  #!/bin/bash
     2  
     3  # N/B: This script could mega f*!@up your disks if run by mistake.
     4  #      it is left without the execute-bit on purpose!
     5  
     6  # $SLASH_DEVICE is the disk device to be f*xtuP
     7  SLASH_DEVICE="/dev/sda"  # Always the case on GCP
     8  
     9  # The unallocated space results from the difference in disk-size between VM Image
    10  # and runtime request.  The check_image.sh test includes a minimum-space check,
    11  # with the Image size set initially lower by contrib/cirrus/packer/libpod_images.yml
    12  NEW_PART_START="50%"
    13  NEW_PART_END="100%"
    14  
    15  set -eo pipefail
    16  
    17  source $(dirname $0)/lib.sh
    18  
    19  if [[ ! -r "/root" ]] || [[ -r "/root/second_partition_ready" ]]
    20  then
    21      echo "Warning: Ignoring attempted execution of $(basename $0)"
    22      exit 0
    23  fi
    24  
    25  [[ -n "type -P parted" ]] || \
    26      die 2 "The parted command is required."
    27  
    28  [[ ! -b ${SLASH_DEVICE}2 ]] || \
    29      die 5 "Found unexpected block device ${SLASH_DEVICE}2"
    30  
    31  PPRINTCMD="parted --script ${SLASH_DEVICE} print"
    32  FINDMNTCMD="findmnt --source=${SLASH_DEVICE}1 --mountpoint=/ --canonicalize --evaluate --first-only --noheadings"
    33  TMPF=$(mktemp -p '' $(basename $0)_XXXX)
    34  trap "rm -f $TMPF" EXIT
    35  
    36  if $FINDMNTCMD | tee $TMPF | egrep -q "^/\s+${SLASH_DEVICE}1"
    37  then
    38      echo "Repartitioning original partition table:"
    39      $PPRINTCMD
    40  else
    41      die 6 "Unexpected output from '$FINDMNTCMD': $(<$TMPF)"
    42  fi
    43  
    44  echo "Adding partition offset within unpartitioned space."
    45  parted --script --align optimal /dev/sda unit % mkpart primary "" "" "$NEW_PART_START" "$NEW_PART_END"
    46  
    47  echo "New partition table:"
    48  $PPRINTCMD
    49  
    50  echo "Growing ${SLASH_DEVICE}1 meet start of ${SLASH_DEVICE}2"
    51  growpart ${SLASH_DEVICE} 1
    52  
    53  FSTYPE=$(findmnt --first-only --noheadings --output FSTYPE ${SLASH_DEVICE}1)
    54  echo "Expanding $FSTYPE filesystem on ${SLASH_DEVICE}1"
    55  case $FSTYPE in
    56      ext*) resize2fs ${SLASH_DEVICE}1 ;;
    57      *) die 11 "Script $(basename $0) doesn't know how to resize a $FSTYPE filesystem." ;;
    58  esac
    59  
    60  # Must happen last - signals completion to other tooling
    61  echo "Recording newly available disk partition device into /root/second_partition_ready"
    62  echo "${SLASH_DEVICE}2" > /root/second_partition_ready