Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| #!/bin/sh | |
| # Environment variables that this script relies upon: | |
| # - root | |
| _info() { | |
| echo "growroot: $*" | |
| } | |
| _warning() { | |
| echo "growroot Warning: $*" >&2 | |
| } | |
| # This will drop us into an emergency shell | |
| _fatal() { | |
| echo "growroot Fatal: $*" >&2 | |
| exit 1 | |
| } | |
| _growroot() { | |
| # Compute root-device | |
| if [ -z "${root##*mapper*}" ] | |
| then | |
| set -- "${root##*mapper/}" | |
| VOLGRP=${1%-*} | |
| ROOTVOL=${1#*-} | |
| rootdev=$(readlink -f $(pvs --noheadings | awk '/'${VOLGRP}'/{print $1}')) | |
| _info "'/' is hosted on an LVM2 volume: setting \$rootdev to ${rootdev}" | |
| else | |
| # Remove 'block:' prefix and find the root device | |
| rootdev=$(readlink -f "${root#block:}") | |
| fi | |
| # root arg was nulled at some point... | |
| if [ -z "${rootdev}" ] ; then | |
| _warning "unable to find root device" | |
| return | |
| fi | |
| # If the basename of the root device (ie 'xvda1', 'sda1', 'vda') exists | |
| # in /sys/block/ then it is a block device, not a partition | |
| if [ -e "/sys/block/${rootdev##*/}" ] ; then | |
| _info "${rootdev} is not a partition" | |
| return | |
| fi | |
| # Check if the root device is a partition (name ends with a digit) | |
| if [ "${rootdev%[0-9]}" = "${rootdev}" ] ; then | |
| _warning "${rootdev} is not a partition" | |
| return | |
| fi | |
| # Remove all numbers from the end of rootdev to get the rootdisk and | |
| # partition number | |
| rootdisk=${rootdev} | |
| while [ "${rootdisk%[0-9]}" != "${rootdisk}" ] ; do | |
| rootdisk=${rootdisk%[0-9]} | |
| done | |
| partnum=${rootdev#${rootdisk}} | |
| # Check if we need to strip a trailing 'p' from the rootdisk name (for | |
| # device names like /dev/mmcblk0p) | |
| tmp=${rootdisk%[0-9]p} | |
| if [ "${#tmp}" != "${#rootdisk}" ] ; then | |
| rootdisk=${rootdisk%p} | |
| fi | |
| # Do a growpart dry run and exit if it fails or doesn't have anything | |
| # to do | |
| if ! out=$(growpart --dry-run "${rootdisk}" "${partnum}") ; then | |
| _info "${out}" | |
| return | |
| fi | |
| # Wait for any of the initial udev events to finish otherwise growpart | |
| # might fail | |
| udevadm settle --timeout=30 | |
| # Resize the root partition | |
| if out=$(growpart --update off "${rootdisk}" "${partnum}" 2>&1) ; then | |
| _info "${out}" | |
| else | |
| _warning "${out}" | |
| _warning "growpart failed" | |
| fi | |
| # Wait for the partition re-read events to complete so that the root | |
| # partition is available for mounting | |
| udevadm settle --timeout=30 | |
| # Add the root partition if it didn't come back on its own | |
| if ! [ -e "${rootdev}" ] ; then | |
| partx --add --nr "${partnum}" "${rootdisk}" || \ | |
| _warning "failed to add root device ${rootdev}" | |
| udevadm settle --timeout=30 | |
| fi | |
| } | |
| _growroot | |
| # vi: ts=4 noexpandtab |