How to Resize Partition to Maximum Size Using Parted in Non-Interactive Mode: A Complete Guide


3 views

When working with cloud instances or virtual machines, you'll often need to resize partitions after expanding the underlying storage. While fdisk is commonly used, its non-interactive mode is challenging to implement reliably due to its requirement for user input.

The parted utility provides a cleaner solution for scripting scenarios. Its resizepart command can be used in non-interactive mode, but requires precise calculation of the maximum available space - which is exactly what we'll solve here.

Here's how to resize /dev/sda1 to maximum available space in one command:


parted /dev/sda resizepart 1 $(parted /dev/sda unit s print free | awk '/Free Space/{print $1}' | tail -n1)s

Let's break down what this does:

  1. Uses parted to examine free space in sectors (unit s)
  2. awk extracts the starting sector of each free space region
  3. tail -n1 gets the last (largest) free space segment
  4. The result is passed as the end position for resizepart

After resizing the partition, you'll typically need to resize the filesystem:

For ext2/3/4 filesystems:


resize2fs /dev/sda1

For XFS filesystems:


xfs_growfs /mount/point

For LVM physical volumes:


pvresize /dev/sda1

For production use, consider this more complete script:


#!/bin/bash
DISK="/dev/sda"
PARTITION=1

# Resize partition
END_SECTOR=$(parted $DISK unit s print free | awk '/Free Space/{print $1}' | tail -n1)
parted $DISK resizepart $PARTITION ${END_SECTOR}s

# Detect and resize filesystem
FS_TYPE=$(blkid -o value -s TYPE ${DISK}${PARTITION})
case $FS_TYPE in
    ext*) resize2fs ${DISK}${PARTITION} ;;
    xfs)  echo "Run 'xfs_growfs MOUNTPOINT' on mounted filesystem" ;;
    swap) swapon ${DISK}${PARTITION} && swapoff ${DISK}${PARTITION} && mkswap ${DISK}${PARTITION} ;;
    *)    echo "Unknown filesystem - manual intervention needed" ;;
esac
  • Always backup important data before partition operations
  • The partition must be the last one on disk for this to work
  • For LVM, you might need to resize the physical volume before logical volumes
  • Some filesystems require being unmounted for resizing

Another approach is to dump the partition table, modify it, and restore:


sfdisk -d /dev/sda > partitions.bak
sed -i "s/sda1 : start=.*, size=.*/sda1 : start=2048, size=+, type=83/" partitions.bak
sfdisk /dev/sda < partitions.bak

When automating disk operations in Linux, the parted utility is often more script-friendly than fdisk due to its support for non-interactive mode. However, resizing partitions to their maximum available space still presents challenges, particularly when working with cloud instances or virtual machines where disks may be dynamically resized.

The basic syntax for resizing a partition with parted is:

parted /dev/sda resizepart 1 [END]

Where [END] represents the new end position of the partition. The key challenge is determining this value automatically.

To resize to maximum capacity without manual intervention, we need to:

  1. Get the disk's total size
  2. Calculate optimal partition boundaries
  3. Execute the resize operation

Here's a complete solution:

#!/bin/bash
DISK="/dev/sda"
PARTITION_NUM=1

# Get disk size in sectors
DISK_SIZE=$(parted -s $DISK unit s print | awk '/^Disk/ {print $3}' | tr -d 's')

# Calculate end position (leave 1MB margin at end)
END_POS=$((DISK_SIZE - 2048))  # 2048 sectors = 1MB in 512b/sector drives

# Perform the resize
parted -s $DISK resizepart $PARTITION_NUM ${END_POS}s

For systems using different sector sizes or when working with very large disks, you might want to use megabytes instead:

DISK_SIZE_MB=$(parted -s $DISK unit MB print | awk '/^Disk/ {print $3}' | tr -d 'MB')
END_POS_MB=$((DISK_SIZE_MB - 1))  # Leave 1MB margin
parted -s $DISK resizepart $PARTITION_NUM ${END_POS_MB}MB

When working in automated environments, consider these additional factors:

  • Filesystem resizing after partition operation (resize2fs for ext*, xfs_growfs for XFS)
  • Partition alignment for optimal performance
  • Error handling for cases where the partition is in use

Here's an enhanced version with error checking:

#!/bin/bash
set -e

DISK="/dev/sda"
PARTITION_NUM=1

# Verify disk exists
[ -b $DISK ] || { echo "Disk $DISK not found"; exit 1; }

# Get disk size safely
DISK_SIZE=$(parted -s $DISK unit s print 2>/dev/null | awk '/^Disk/ {print $3}' | tr -d 's') || { echo "Failed to get disk size"; exit 1; }

# Calculate end position
END_POS=$((DISK_SIZE - 2048))

# Resize partition
if ! parted -s $DISK resizepart $PARTITION_NUM ${END_POS}s; then
    echo "Partition resize failed"
    exit 1
fi

echo "Partition resized successfully"

In cloud environments where disks are expanded after VM creation, you might use this in a startup script:

#!/bin/bash
# AWS/GCP/Azure disk expansion handler
GROWPART_DISK="/dev/nvme0n1"
GROWPART_NUM=1

# Resize partition
DISK_SIZE=$(parted -s $GROWPART_DISK unit s print | awk '/^Disk/ {print $3}' | tr -d 's')
END_POS=$((DISK_SIZE - 2048))
parted -s $GROWPART_DISK resizepart $GROWPART_NUM ${END_POS}s

# Resize filesystem
if grep -q "^${GROWPART_DISK}p${GROWPART_NUM}.*ext" /etc/fstab; then
    resize2fs "${GROWPART_DISK}p${GROWPART_NUM}"
elif grep -q "^${GROWPART_DISK}p${GROWPART_NUM}.*xfs" /etc/fstab; then
    xfs_growfs "${GROWPART_DISK}p${GROWPART_NUM}"
fi