rocky-linux-generic-images/scripts/expand-rootfs.sh

43 lines
1.4 KiB
Bash

#!/usr/bin/env bash
ROOT_DEVICE=$(mount | grep '/ ' | awk '{print $1}') # "/dev/sda3"
DISK_NAME="/dev/$(lsblk -ls "${ROOT_DEVICE}" | tail -n 1 | awk '{print $1}')" # "/dev/sda"
CUT_LENGTH=$(( ${#DISK_NAME} + 1 )) # how much to cut from "/dev/sda3" to get "3"
if [[ "${DISK_NAME}" =~ "nvme" || "${DISK_NAME}" =~ "mmcblk" ]]; then
# Add an extra character to cut since NVMe and MMC-block devices
# have a partition number _after_ a 'p'
CUT_LENGTH=$(( CUT_LENGTH + 1 ))
fi
PARTITION_NUMBER=$(echo "${ROOT_DEVICE}" | cut -c "${CUT_LENGTH}-") # get "3" from "/dev/sda3"
if [[ -z "${ROOT_DEVICE}" || -z "${DISK_NAME}" || -z "${CUT_LENGTH}" || -z "${PARTITION_NUMBER}" ]]; then
>&2 echo "$0: error: unable to detect root device"
exit 1
fi
if ! command -v growpart > /dev/null; then
>&2 echo "$0: error: unable to find command 'growpart'"
exit 1
fi
if ! command -v resize2fs > /dev/null; then
>&2 echo "$0: error: unable to find command 'resize2fs'"
exit 1
fi
if ! command -v e2fsck > /dev/null; then
>&2 echo "$0: error: unable to find command 'e2fsck'"
exit 1
fi
if [[ ${EUID} -ne 0 || ${UID} -ne 0 ]]; then
>&2 echo "$0: error: please run this script as root"
exit 1
fi
set -x
growpart --verbose "${DISK_NAME}" "${PARTITION_NUMBER}"
e2fsck -D -f -F -p -v -y "${ROOT_DEVICE}"
resize2fs -f -F -p "${ROOT_DEVICE}"
e2fsck -D -f -F -p -v -y "${ROOT_DEVICE}"