Disk Management: Resizing ext4/XFS Partitions using fdisk and resize2fs

When you upgrade your Hovixa VPS plan to include more storage, the additional space is often added to the end of the block device as unallocated space. To utilize this capacity, you must perform a two-step process: first, modify the partition table to extend the boundaries of the partition, and second, grow the filesystem itself. This guide covers the technical execution for the two most common Linux filesystems: ext4 and XFS.

⚠️ CRITICAL: Data Integrity

Modifying partition tables carries inherent risk. Always ensure you have a fresh Hovixa Snapshot or external backup before proceeding. While these steps can typically be performed on a live system, a mistake in the sector offsets can lead to data loss.

1. Identifying the Target Device

Use the lsblk command to view your current disk layout. Identify the disk (usually /dev/vda) and the specific partition you wish to expand (e.g., /dev/vda1).

# View block devices and mount points
lsblk
    

2. Expanding the Partition with fdisk

fdisk is a dialogue-driven program used for creating and manipulating partition tables. To "resize" a partition, we technically delete the existing partition entry and recreate it with the exact same starting sector but a larger ending sector.

  1. Start fdisk on the disk: sudo fdisk /dev/vda
  2. Print the current table: Press p (Note the start sector of your partition).
  3. Delete the partition: Press d.
  4. Create a new partition: Press n. Use the same start sector as before. For the end sector, press Enter to use the default (max available).
  5. Remove Signature: If asked to remove the existing 'ext4' or 'xfs' signature, press N (No).
  6. Write changes: Press w.

3. Informing the Kernel

The Linux kernel may still be using the old partition table in memory. Force an update without rebooting using partprobe.

sudo partprobe /dev/vda
    

4. Resizing the Filesystem

The steps differ depending on the filesystem type. You can check your type using df -T.

Filesystem Tool Command
ext4 `resize2fs` `sudo resize2fs /dev/vda1`
XFS `xfs_growfs` `sudo xfs_growfs /` (use mount point)

For ext4:

The resize2fs utility expands the filesystem to fill the newly enlarged partition. It works while the partition is mounted.

For XFS:

XFS filesystems are grown using the xfs_growfs command, which targets the mount point rather than the device path.

5. Technical Implementation Details

  • GPT vs MBR: fdisk works well for MBR. For GPT partition tables (common on modern 2TB+ drives), gdisk or parted is the preferred tool.
  • LVM (Logical Volume Manager): If your Hovixa VPS uses LVM, the process involves pvresize, lvextend, and then the filesystem resize. Most standard VPS templates use raw partitions for simplicity.
  • Online Resizing: Both resize2fs and xfs_growfs support online resizing. You do not need to unmount your web server's root partition to reclaim the space.

Sysadmin Advice: After resizing, run **df -h** to verify the new available space. If the size hasn't changed, check **dmesg** for any errors related to the partition table re-read.

Răspunsul a fost util? 0 utilizatori au considerat informația utilă (0 Voturi)