Expanding a Linux Partition Using parted
The parted utility is a partition editing tool that is available on most Linux distributions. It can create and edit both MBR partition tables and GPT partition tables. Some versions of parted (newer than version 2.1) have limited support for GPT partition tables and they may cause boot issues if their version of parted is used to modify boot volumes. You can check your version of parted with the parted --version
command.
If you are expanding a partition that resides on a GPT partitioned device, you should choose to use the gdisk utility instead. If you’re not sure which disk label type your volume uses, you can check it with the sudo fdisk -l command.
To expand a Linux partition using parted
- Identify the device that contains the partition that you want to expand. Use the lsblk command to list all devices and partitions attached to the instance.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvdf 202:80 0 100G 0 disk └─xvdf1 202:81 0 8G 0 part /mnt xvda1 202:1 0 30G 0 disk /
In this example, the
xvdf
device has 100 GiB of available storage and it contains an 8 GiB partition. - Unmount the partition if it is mounted. Run the umount command with the value of
MOUNTPOINT
from thelsblk command. In this example, theMOUNTPOINT
value for the partition is/mnt
.$ umount /mnt
- Take a snapshot of your volume (unless you just took one in the previous procedure). It can be easy to corrupt or lose your data in the following procedures. If you have a fresh snapshot, you can always start over in case of a mistake and your data will still be safe. For more information, see Creating an Amazon EBS Snapshot.
- Run the parted command on the device (and not the partition on the device). Remember to add the
/dev/
prefix to the name that lsblk outputs.$ parted /dev/xvdf GNU Parted 2.1 Using /dev/xvdf Welcome to GNU Parted! Type 'help' to view a list of commands.
- Change the parted units of measure to sectors.
(parted) unit s
- Run the print command to list the partitions on the device. For certain partition table types, you might be prompted to repair the partition table for the larger volume size. Answer ‘Ignore’ to any questions about fixing the existing partition table; you will create a new table later.
(parted) print
- If you receive the following message, enter ‘Ignore’ to prevent the backup GPT location from changing.
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)? Fix/Ignore/Cancel? Ignore
- If you receive the following message, enter ‘Ignore’ again to keep the space on the drive the same.
Warning: Not all of the space available to /dev/xvdf appears to be used, you can fix the GPT to use all of the space (an extra 46137344 blocks) or continue with the current setting? Fix/Ignore? Ignore
- If you receive the following message, enter ‘Ignore’ to prevent the backup GPT location from changing.
- Examine the output for the total size of the disk, the partition table type, the number of the partition, the start point of the partition, and any flags, such as
boot
. Forgpt
partition tables, note the name of the partition; formsdos
partition tables, note theType
field (primary
orextended
). These values are used in the upcoming steps.The following is agpt
partition table example.Model: Xen Virtual Block Device (xvd) Disk /dev/xvdf: 209715200s Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 128 2048s 4095s 2048s BIOS Boot Partition bios_grub 1 4096s 16777182s 16773087s ext4 Linux
The following is an
msdos
partition table example.Model: Xen Virtual Block Device (xvd) Disk /dev/xvdg: 104857600s Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 2048s 35649535s 35647488s primary ext3
Delete the partition entry for the partition using the number (1) from the previous step.
(parted) rm 1
- Create a new partition that extends to the end of the volume.(For the
gpt
partition table example)
Note the start point and name of partition 1 above.
For thegpt
example, there is a start point of 4096s, and the nameLinux
.
Run the mkpart command with the start point of partition 1, the name, and 100% to use all of the available space.(parted) mkpart Linux 4096s 100%
(For the
msdos
partition table example)
Note the start point and the partition type of partition 1 above.
For themsdos
example, there is a start point of 2048s and a partition type ofprimary
.
Run the mkpart command with a primary partition type, the start point of partition 1, and 100% to use all of the available space.(parted) mkpart primary 2048s 100%
- Run the print command again to verify your partition.(For the
gpt
partition table example)(parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdf: 209715200s Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 128 2048s 4095s 2048s BIOS Boot Partition bios_grub 1 4096s 209713151s 209709056s ext4 Linux
(For the
msdos
partition table example)(parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdg: 104857600s Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 2048s 104857599s 104855552s primary ext3
Check to see that any flags that were present earlier are still present for the partition that you expanded. In some cases the
boot
flag may be lost. If a flag was dropped from the partition when it was expanded, add the flag with the following command, substituting your partition number and the flag name. For example, the following command adds theboot
flag to partition 1.(parted) set 1 boot on
You can run the print command again to verify your change.
- Run the quit command to exit parted.
(parted) quit
Note
Because you removed a partition and added a partition, parted may warn that you may need to update
/etc/fstab
. This is only required if the partition number changes. - Check the file system to make sure there are no errors (this is required before you may extend the file system). Note the file system type from the previous print commands. Choose one of the commands below based on your file system type; if you are using a different file system, consult the documentation for that file system to determine the correct check command.(For
ext3
orext4
file systems)$ e2fsck -f /dev/xvdf1 e2fsck 1.42.3 (14-May-2012) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /: 31568/524288 files (0.4% non-contiguous), 266685/2096635 blocks
(For
xfs
file systems)$ sudo xfs_repair /dev/xvdf1 Phase 1 - find and verify superblock... Phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk Phase 3 - for each AG... - scan and clear agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - agno = 2 - agno = 3 - process newly discovered inodes... Phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 - agno = 2 - agno = 3 Phase 5 - rebuild AG headers and trees... - reset superblock... Phase 6 - check inode connectivity... - resetting contents of realtime bitmap and summary inodes - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... Phase 7 - verify and correct link counts... done
To extend a Linux file system
-
- Log in to your Linux instance using an SSH client. For more information about connecting to a Linux instance, see Connecting to Your Linux Instance Using SSH.
- Use the df -h command to report the existing file system disk space usage. In this example, /dev/xvda1 device has already been expanded to 70 GiB, but the operating system still sees only the original 7.9 GiB ext4 file system. Similarly, the /dev/xvdf device has been expanded to 100 GiB, but the operating system still only sees the original 1.0 GiB XFS file system.
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 8.0G 943M 6.9G 12% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvdf 1014M 33M 982M 4% /mnt
- Use a file system-specific command to resize each file system to the new volume capacity.For a Linux ext2, ext3, or ext4 file system, use the following command, substituting the device name to extend:
$ sudo resize2fs /dev/xvda1 resize2fs 1.42.3 (14-May-2012) Filesystem at /dev/xvda1 is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 5 Performing an on-line resize of /dev/xvda1 to 18350080 (4k) blocks. The filesystem on /dev/xvda1 is now 18350080 blocks long.
For an XFS file system, first install the XFS userspace tools:
$ sudo yum install xfsprogs
Then use the following command, substituting the mount point of the file system (XFS file systems must be mounted to resize them):
$ sudo xfs_growfs -d /mnt meta-data=/dev/xvdf isize=256 agcount=4, agsize=65536 blks = sectsz=512 attr=2 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 262144 to 26214400
Note
If you receive an xfsctl failed: Cannot allocate memory error, you may need to update the Linux kernel on your instance. For more information, refer to your specific operating system documentation.
If you receive a The filesystem is already nnnnnnn blocks long. Nothing to do! error, see Expanding a Linux Partition.
- Use the df -h command to report the existing file system disk space usage, which should now show the full 70 GiB on the ext4 file system and 100 GiB on the XFS file system:
# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 70G 951M 69G 2% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvdf 100G 45M 100G 1% /mnt