From Beginner to Expert: Step-by-Step Guide to Partition Adjustment! ๐
Expanding the virtual machine disk from 1T to 2T and allocating all new space to the root directory (/)
Preparation:
- System: AlmaLinux (default uses LVM partition management, flexible and powerful!).
- File System: AlmaLinux defaults to XFS, some may be ext4 (I will teach you how to confirm).
- Permissions: Root permissions are required (use
<span>sudo</span>or log in as root directly). - Tools: Ensure
<span>parted</span>,<span>lvm2</span>,<span>xfsprogs</span>(or<span>e2fsprogs</span>) are installed (all are default in AlmaLinux).
Step 0: Check Current Disk Status
Run the following command to understand your disk situation, just like giving your system a health check! ๐ฉบ
df -h
Example Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/almalinux-root 470G 154G 317G 33% /
/dev/vda2 960M 255M 706M 27% /boot
/dev/mapper/almalinux-home 538G 11G 527G 2% /home
...
Now let’s take a look at the partition details:
fdisk -l
Example Output:
Disk /dev/vda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Device Start End Sectors Size Type
/dev/vda1 2048 4095 2048 1M BIOS boot
/dev/vda2 4096 2101247 2097152 1G Linux extended boot
/dev/vda3 2101248 2147481599 2145380352 1023G Linux LVM
...
๐ Tip: /dev/vda3 is the LVM partition responsible for managing the root, home, and other logical volumes. The key to expansion lies here!
Step 1: Confirm File System Type (Super Important!)
AlmaLinux defaults to using the XFS file system, but it still needs to be confirmed, as the commands for XFS and ext4 are different.
Method 1: Use the df Command (Most Direct)
df -Th /
# -T: Display file system type
# /: Specify the root partition
Example Output:
# XFS
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/almalinux-root xfs 470G 154G 317G 33% /
# ext4
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/almalinux-root ext4 470G 154G 317G 33% /
Method 2: Use the lsblk Command
lsblk -f /dev/mapper/almalinux-root
# lsblk can list block devices and file system information
# -f: Display file system information
Example Output:
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
almalinux-root xfs aec3e27.... 317G 11% /
# The FSTYPE field indicates the file system type (e.g., xfs or ext4)
Method 3: Use the file Command
file -sL /dev/mapper/almalinux-root
# Determine by checking the attributes of the device file
# -s: Process special files (like block devices)
# -L: Follow symbolic links
Example Output:
# XFS
/dev/mapper/almalinux-root: SGI XFS filesystem data
# ext4
/dev/mapper/almalinux-root: Linux rev 1.0 ext4 filesystem
โ ๏ธ If it is ext4: Don’t worry, the subsequent steps will indicate the differences. ๐
Step 2: Fix the GPT Partition Table (Must Do After Expanding Disk)
Assuming you have expanded the disk from
<span>1T </span>to<span>2T</span>in virtualization software (such as<span>VMware </span>,<span>VirtualBox</span>,<span>Hyper-V</span>). Running<span>fdisk -l</span>may show a warning:
[root@Hadesr ~]# fdisk -l
GPT PMBR size mismatch (2147483647 != 4294967295) will be corrected by write.
The backup GPT table is not on the end of the device.
Disk /dev/vda: 2 TiB, 2199023255552 bytes, 4294967296 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 52D120D6-9132-40D8-9FA0-0D6FE9C26EB1
Device Start End Sectors Size Type
/dev/vda1 2048 4095 2048 1M BIOS boot
/dev/vda2 4096 2101247 2097152 1G Linux extended boot
/dev/vda3 2101248 2147481599 2145380352 1023G Linux LVM
# Successfully expanded the virtual machine disk to 2TiB (4294967296 sectors), but the partition table has not been updated, /dev/vda3 is still at 1023G.
# Need to resolve the GPT partition table mismatch and expand vda3
Don’t worry! Use <span>parted</span> to fix: let the system automatically correct the GPT table
parted /dev/vda
After entering interactive mode:
- Type
<span>print</span>(prompt whether to fix). - Type
<span>fix</span>(automatically fix the GPT partition table mismatch).
Step 3: Expand the /dev/vda3 Partition
Continue in the <span>parted</span> interactive mode:
(parted) unit s # Switch unit to sectors (ensure consistency with disk information)
(parted) print # View current partition table, confirm the End sector of vda3
(parted) resizepart 3 # Adjust the 3rd partition (vda3)
(parted) Yes # Confirm modification
(parted) 4294965247 # Set new end sector (maximum sector of 2TB disk)
(parted) print # Verify if the End of vda3 has changed to 4294965247, Size is close to 2TB
(parted) quit # Exit parted
๐ Why use 4294965247? The total sectors of a 2T disk is 4294967296, leaving the last few sectors for the GPT backup table.
Step 4: Expand the LVM Physical Volume (PV)
Let LVM recognize the new space:
pvresize /dev/vda3
# Expand physical volume
pvs
# Verify: Check if PV Size has changed to about 2TiB
Step 5: Expand the Root Logical Volume (LV) to Over 1TB
First, check the available space in the volume group: confirm there is enough space
vgs
Expand the logical volume (example: expand to<span>1.5TB</span>):
lvextend -L 1.5T /dev/mapper/almalinux-root
Or use all free space (fill up to<span>2TB</span> minus other partitions)
lvextend -l +100%FREE /dev/mapper/almalinux-root
Step 6: Expand the File System (Key!)
Execute the command based on the root partition file system type:<span>AlmaLinux </span> defaults to <span>XFS</span>
If it is XFS (recommended):
xfs_growfs /dev/mapper/almalinux-root
If it is ext4 (need to confirm first):
resize2fs /dev/mapper/almalinux-root
Step 7: Verify Expansion Results โ
df -Th /
# Check if the root partition size has expanded to over 1TB
lsblk /dev/vda
# Confirm if vda3 size is about 2TiB
fdisk -l
Fixing the GPT table is a necessary step; otherwise, partition expansion may fail.
Example Output:
[root@Hadesr ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/almalinux-root xfs 1.5T 161G 1.4T 11% /
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 13G 9.9M 13G 1% /run
/dev/vda2 xfs 960M 255M 706M 27% /boot
/dev/mapper/almalinux-home xfs 538G 11G 527G 2% /home
tmpfs tmpfs 6.3G 16K 6.3G 1% /run/user/0
# The root partition (/) has been expanded to 1.5TB.
# Through df -Th, it can be seen that /dev/mapper/almalinux-root has expanded from the original 470G to 1.5T, with a usage rate of 11%, and ample remaining space (1.4T).
[root@Hadesr ~]# lsblk /dev/vda
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 252:0 0 2T 0 disk
โโvda1 252:1 0 1M 0 part
โโvda2 252:2 0 1G 0 part /boot
โโvda3 252:3 0 2T 0 part
โโalmalinux-root 253:0 0 1.5T 0 lvm /
โโalmalinux-swap 253:1 0 15.7G 0 lvm [SWAP]
โโalmalinux-home 253:2 0 537.3G 0 lvm /home
# The entire virtual disk /dev/vda has been expanded to 2TB;
# The partition /dev/vda3 has been successfully expanded to 2TB (including all LVM logical volumes);
# LVM logical volume allocation is reasonable: root (1.5T), swap (15.7G), home (537.3G), total capacity matches vda3 (2TB).
๐ Congratulations! Operation successful, the root partition is now huge, with plenty of space! ๐ The entire process (Expanding Virtual Disk โ Fixing GPT Partition Table โ Expanding vda3 โ LVM Expansion โ File System Expansion) has been completed! ๐