parted Command: Creating GPT Partitions and Using mkpart and resizepart
The parted command is a Linux command that creates a partition table on a disk and creates, deletes or resizes partitions. On an AlmaLinux 9 test server, a GPT partition table and 2 partitions were created on a 4 GiB practice disk and taken through deletion and extension, and on a 3 TiB disk, a partition that could not be created with MBR was created by changing to GPT.
The commands below were run under the root account of an AlmaLinux 9 test server, and the output is exactly as received from GNU Parted 3.5. Image files of 4 GiB and 3 TiB made with truncate were attached to the loop devices /dev/loop0 and /dev/loop1 and used as practice disks. Both images are sparse files that take up only as much space as has been written to them, so even the 3 TiB image could be created while using almost no actual space. The mount point /mnt/data was created first with mkdir, and the connection address shown in the simulator is an example value.
1️⃣ Basic Usage of the parted Command
parted is run with a device name and a command written after it. The partition layout of the disks attached to the server was checked first with the -l option.
parted -lModel: Virtio Block Device (virtblk)
Disk /dev/vda: 26.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 274MB 273MB fat16 EFI System Partition boot, esp
2 274MB 26.8GB 26.6GB ext4
The test server's disk /dev/vda appeared. The upper part shows the disk model and size, the sector size and the partition table type, and the lower part shows one line per partition with its number, start position, end position, size, file system, name and flags. The practice loop devices did not appear in this list, so they were checked by giving the device name directly.
The partition table of a single device is printed with the print command. For a one-line run, the -s option was added so that it finishes without confirmation questions.
parted -s /dev/loop0 printError: /dev/loop0: unrecognised disk label
Model: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: The new disk had no partition table, so an [unrecognised disk label] error appeared and the Partition Table line was unknown. The exit code was 1.
Running it with only the device name enters interactive mode. Commands are typed one at a time at the (parted) prompt, and quit exits. In interactive mode, print, [unit MiB] and print were entered in that order.
parted /dev/loop0GNU Parted 3.5
Using /dev/loop0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: /dev/loop0: unrecognised disk label
Model: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted) unit MiB
(parted) print
Error: /dev/loop0: unrecognised disk label
Model: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted) quitAfter [unit MiB] was entered, the size on the Disk line changed from 4295MB to 4096MiB. MB is calculated in units of 1000 and MiB in units of 1024. Interactive mode and the one-line run use the same commands, and this article uses the one-line run, which makes it easy to check the result of each command.
2️⃣ Creating GPT Partitions with parted mkpart
On a new disk, a partition table is created first with the mklabel command. The partition table type was set to gpt.
parted -s /dev/loop0 mklabel gptparted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
The Partition Table line changed to gpt, and the partition list is empty. A partition is created by giving the mkpart command a name, a file system type, a start position and an end position, in that order. The name was set to data, the start to 1MiB and the end to 50% of the disk.
parted -s /dev/loop0 mkpart data ext4 1MiB 50%lsblk /dev/loop0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4G 0 loop
`-loop0p1 259:0 0 2G 0 part mkpart finished without printing anything, and lsblk showed the 2 GiB loop0p1 right away. parted writes to the disk as soon as a command is run, and there is no separate save command.
This is how it differs from tools that collect changes and write them only when w is entered, such as the fdisk command, gdisk command. In parted, even a wrongly entered command is applied right away, so check the device name again before running a command.
In the same way, a backup partition was created in the remaining half and checked with print.
parted -s /dev/loop0 mkpart backup xfs 50% 100%parted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2147MB 2146MB data
2 2147MB 4294MB 2146MB backup
2 partitions appeared, and the File system column is empty. The ext4 and xfs given to mkpart are values that record the partition type, and the file system has to be created separately with mkfs.
The start and end positions can be checked in another unit by changing the unit with the unit command. They were printed in MiB and in sectors (s).
parted -s /dev/loop0 unit MiB printModel: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1.00MiB 2048MiB 2047MiB data
2 2048MiB 4095MiB 2047MiB backup
parted -s /dev/loop0 unit s printModel: Loopback device (loopback)
Disk /dev/loop0: 8388608s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 4194303s 4192256s data
2 4194304s 8386559s 4192256s backup
Partition 1 starts at 1.00MiB, which is 2048s in sectors. Since the sector size is 512B, 2048 sectors are 1 MiB. The boundary given as 50% was aligned to 2048MiB.
Whether the start position of a partition is aligned for the disk is checked with the align-check command. With -s, align-check prints no result, so it was run without -s.
parted /dev/loop0 align-check optimal 11 aligned[1 aligned] appeared, confirming that partition 1 is aligned by the optimal standard. Partition 2, which starts at 50%, also gave [2 aligned] with the same command. The space left on the disk is checked with [print free].
parted -s /dev/loop0 print freeModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2147MB 2146MB data
2 2147MB 4294MB 2146MB backup
4294MB 4295MB 1032kB Free Space
There is no free space between the two partitions, and only about 1 MiB of Free Space remains at the very start and the very end of the disk.
3️⃣ Deleting Partitions and Extending Their Size with parted
Partitions can be deleted or resized even after they are created, but a mounted partition is not changed right away. To check this behavior, an ext4 file system was created on partition 1 with mkfs.ext4, and mount was used to attach it to /mnt/data. The -q of mkfs.ext4 is an option that suppresses the progress output.
mkfs.ext4 -q /dev/loop0p1mount /dev/loop0p1 /mnt/datadf -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 2.0G 24K 1.9G 1% /mnt/dataThe 2 GiB partition was mounted with a size of 2.0G. A partition is deleted by giving its number to the rm command. rm was run on the mounted partition 1.
parted -s /dev/loop0 rm 1Warning: Partition /dev/loop0p1 is being used. Are you sure you want to continue?A [Partition /dev/loop0p1 is being used] warning appeared and it ended with exit code 1. The warning is a question asking whether to continue, but it cannot be answered with the -s option, so the partition was not deleted. The same command was run on partition 2, which was not in use, and the result was checked with print.
parted -s /dev/loop0 rm 2parted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2147MB 2146MB ext4 data
rm ran without a confirmation question, and print showed only partition 1. The deleted area appeared in [print free] as 2147MB of Free Space.
The size of a partition is changed by giving the resizepart command its number and a new end position. To extend partition 1 to the end of the disk, the end position was set to 100%.
parted -s /dev/loop0 resizepart 1 100%Warning: Partition /dev/loop0p1 is being used. Are you sure you want to continue?While it was mounted, the same warning as with rm appeared and nothing was done. After unmounting with umount, it was run again.
umount /mnt/dataparted -s /dev/loop0 resizepart 1 100%parted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 4295MB 4294MB ext4 data
The End of partition 1 changed from 2147MB to 4295MB, and the Size from 2146MB to 4294MB. It was mounted again and the capacity was checked with df.
mount /dev/loop0p1 /mnt/datadf -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 2.0G 24K 1.9G 1% /mnt/dataThe partition was extended to 4 GiB, but the Size in df is still 2.0G. This is because resizepart changes only the end position of the partition and does not change the file system inside it. The ext4 file system was extended to fit the partition size with resize2fs.
resize2fs /dev/loop0p1resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/loop0p1 is mounted on /mnt/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/loop0p1 is now 1048315 (4k) blocks long.
df -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 3.9G 24K 3.8G 1% /mnt/dataThe Size in df changed to 3.9G. Conversely, setting the end position earlier than the current one shrinks the partition. The partition was unmounted and the end position was set to 50%.
umount /mnt/dataparted -s /dev/loop0 resizepart 1 50%Warning: Shrinking a partition can cause data loss, are you sure you want to continue?A warning appeared that shrinking the partition can cause data loss, and with the -s option it was not carried out, so the partition size stayed the same.
The XFS file system, widely used on the RHEL family, is extended with xfs_growfs instead of resize2fs. A newly made 4 GiB practice image was attached and a GPT partition table was created on it, then a data partition was created with mkpart with the file system type set to xfs and formatted with mkfs.xfs. Next, the partition was extended to 100% with resizepart, and after mounting, the capacity before and after running xfs_growfs was checked with df.
parted -s /dev/loop0 mkpart data xfs 1MiB 50%mkfs.xfs -q /dev/loop0p1parted -s /dev/loop0 resizepart 1 100%mount /dev/loop0p1 /mnt/datadf -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 2.0G 47M 1.9G 3% /mnt/dataxfs_growfs /mnt/datameta-data=/dev/loop0p1 isize=512 agcount=4, agsize=131008 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=524032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 524032 to 1048315df -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 4.0G 61M 3.9G 2% /mnt/dataThe Size in df changed from 2.0G to 4.0G, and the last line of the xfs_growfs output showed the data blocks changing from 524032 to 1048315. xfs_growfs is run with the mount point, and while the file system was unmounted, the [xfs_growfs: /mnt/data is not a mounted XFS filesystem] error appeared and it was not extended.
4️⃣ parted -s Scripts and Disks Larger Than 2 TiB
-s is the option that makes parted finish without questions, so it is used in shell scripts. On the 3 TiB disk /dev/loop1, the partition size that MBR can create was checked first, and then several commands were run on one line. An MBR partition table was created first. In parted, MBR is written as msdos. With msdos, the first value of mkpart is one of primary, extended or logical instead of a name.
parted -s /dev/loop1 mklabel msdosparted -s /dev/loop1 mkpart primary ext4 1MiB 100%Error: partition length of 6442448896 sectors exceeds the msdos-partition-table-imposed maximum of 4294967295Specifying the whole disk as a single partition produced an error. It says that the partition length of 6442448896 sectors exceeds 4294967295 sectors, the maximum for an msdos partition table. Calculated with a sector size of 512B, this maximum corresponds to 2 TiB. The end position was lowered to 2TiB and it was run again.
parted -s /dev/loop1 mkpart primary ext4 1MiB 2TiBparted -s /dev/loop1 unit GiB print freeModel: Loopback device (loopback)
Disk /dev/loop1: 3072GiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
0.00GiB 0.00GiB 0.00GiB Free Space
1 0.00GiB 2048GiB 2048GiB primary
2048GiB 3072GiB 1024GiB Free Space
The 2048GiB partition was created, but the 1024GiB after it was left as Free Space. With MBR a single partition cannot exceed 2 TiB, so GPT is used for disks larger than 2 TiB.
The partition table was changed to GPT. On a disk that already has a partition table, mklabel in interactive mode asks whether to continue.
parted /dev/loop1GNU Parted 3.5
Using /dev/loop1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
Warning: The existing disk label on /dev/loop1 will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted) quit
Information: You may need to update /etc/fstab.A warning appeared that the existing partition table would be deleted and all data on the disk would be lost, and the change was made after Yes was entered. Then, as when MBR gave the error, mkpart was run with the whole disk as a single partition and the result was checked with print.
parted -s /dev/loop1 mkpart data ext4 0% 100%parted -s /dev/loop1 unit TiB printModel: Loopback device (loopback)
Disk /dev/loop1: 3.00TiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 0.00TiB 3.00TiB 3.00TiB data
The 3.00TiB partition was created without errors. This time, mklabel, which creates a new partition table, and two mkpart commands that split the disk in half were written one after another on one line and run.
parted -s /dev/loop1 mklabel gpt mkpart data ext4 0% 50% mkpart backup xfs 50% 100%parted -s /dev/loop1 printModel: Loopback device (loopback)
Disk /dev/loop1: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1649GB 1649GB data
2 1649GB 3299GB 1649GB backup
It ran straight away without the mklabel warning seen in interactive mode, and 2 partitions of 1649GB were created. When reading values in a script, the -m option is added to print in a colon-separated format.
parted -s -m /dev/loop1 unit MiB printBYT;
/dev/loop1:3145728MiB:loopback:512:512:gpt:Loopback device:;
1:1.00MiB:1572864MiB:1572863MiB::data:;
2:1572864MiB:3145727MiB:1572863MiB::backup:;For each partition, the number, start, end, size, file system, name and flags appear on one line separated by colons. Even with -s, rm and resizepart on a mounted partition and shrinking a partition were not carried out and ended with exit code 1, so scripts check the exit code.
🔲 parted Command Options
| Option | Copy | Description |
|---|---|---|
| -l--list | Print the partition layout of all disks | |
| -s--script | Run without asking the user questions | |
| -m--machine | Print in a colon-separated format | |
| Print the partition table. With free, also print free space | ||
| unit | Set the unit for printing positions and sizes | |
| mklabel | Create a partition table. The existing partition table is deleted | |
| mkpart | Create a partition by giving a name, file system type, start and end | |
| align-check | Check the alignment of a partition's start position | |
| rm | Delete a partition by giving its number | |
| resizepart | Change the end position of a partition. The file system is not changed | |
| rescue | Search for deleted partitions between a start and an end | |
| name | Change a partition's name |
| Option | Copy | Description |
|---|---|---|
| -h--help | Prints help | |
| -v--version | Print the version | |
| -j--json | Print in JSON format | |
| -f--fix | Answer fix instead of stopping when a question appears in script mode | |
| -a--align | Set the alignment type for new partitions (none, cyl, min, opt) | |
| help | Print the list of commands or help for a given command | |
| mktable | Same command as mklabel | |
| select | Select the device to edit | |
| set | Change a partition flag to on or off | |
| toggle | Toggle the state of a partition flag | |
| type | Set the partition type ID or UUID | |
| disk_set | Change a disk flag | |
| disk_toggle | Toggle the state of a disk flag | |
| version | Print version and copyright information |
🔲 parted Command Simulator
In the simulator below, the one-line run commands from this article can be entered with the chips or the keyboard. Pressing the chips in order from the left creates 2 partitions on /dev/loop0, takes them through deletion and extension, and then shows the 2 TiB error on /dev/loop1.
5️⃣ Practical Examples of the parted Command
The commands in this section were also run on the practice disk of the same test server. The same commands can also be entered directly in the parted command simulator.
🔲 parted Example: Partitioning a New Disk
This is the case of using an entire data disk added to a server as a single partition. A new 4 GiB practice image was made and attached, then a GPT partition table was created on this disk, which had no partitions, and a partition was created with a start of 0% and an end of 100%.
lsblk /dev/loop0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4G 0 loop parted -s /dev/loop0 mklabel gptparted -s /dev/loop0 mkpart data ext4 0% 100%parted /dev/loop0 align-check optimal 11 alignedlsblk /dev/loop0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4G 0 loop
`-loop0p1 259:0 0 4G 0 part align-check gave [1 aligned], and lsblk showed loop0p1 with a size of 4G. Next, a file system was created and mounted.
mkfs.ext4 -q /dev/loop0p1mount /dev/loop0p1 /mnt/datalsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS /dev/loop0NAME SIZE TYPE FSTYPE MOUNTPOINTS
loop0 4G loop
`-loop0p1 4G part ext4 /mnt/datadf -h /mnt/dataFilesystem Size Used Avail Use% Mounted on
/dev/loop0p1 3.9G 24K 3.7G 1% /mnt/datalsblk showed ext4 in the FSTYPE column and /mnt/data in the MOUNTPOINTS column, and the Size in df was 3.9G.
This is the case of adding a partition in the remaining space of a disk that already has partitions. On the 3 TiB disk, partition 2 was deleted with rm so that only the first half had a partition left. First, the location of the free space was checked with [print free].
parted -s /dev/loop1 print freeModel: Loopback device (loopback)
Disk /dev/loop1: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 1649GB 1649GB data
1649GB 3299GB 1649GB Free Space
parted -s /dev/loop1 mkpart backup xfs 50% 100%parted -s /dev/loop1 printModel: Loopback device (loopback)
Disk /dev/loop1: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1649GB 1649GB data
2 1649GB 3299GB 1649GB backup
lsblk /dev/loop1NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop1 7:1 0 3T 0 loop
|-loop1p1 259:0 0 1.5T 0 part
`-loop1p2 259:2 0 1.5T 0 part A backup partition was created at the 1649GB position that had appeared as Free Space, and lsblk showed 2 partitions of 1.5T.
🔲 Common Mistakes When Using parted
The mistake to watch for most is running it without a device name. When run without a device name, parted selects the first disk it finds.
partedGNU Parted 3.5
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) quit[Using /dev/vda] appeared, and the root disk of the test server was selected. Commands entered in this state are written straight to the root disk, so it was exited with quit. Always give the device name, and in interactive mode check the Using line first.
With -s, mklabel runs without asking even on a disk that has partitions. mklabel was run on a disk where a file system had been created and then unmounted.
lsblk /dev/loop0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4G 0 loop
`-loop0p1 259:1 0 4G 0 part parted -s /dev/loop0 mklabel gptlsblk /dev/loop0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4G 0 loop It finished without a warning, and loop0p1 disappeared from lsblk. When the partition was mounted, the same command ended with an error.
parted -s /dev/loop0 mklabel gptError: Partition(s) on /dev/loop0 are being used.A [Partition(s) on /dev/loop0 are being used] error appeared and the partition table was not changed. This error does not appear on a disk that has been unmounted, so before running mklabel, check the partition list of the target disk with print.
Writing the start position directly as a sector number can produce an alignment warning. On a GPT disk with all partitions deleted, the start was set to 34s.
parted -s /dev/loop1 mkpart data ext4 34s 100%Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s != 0sparted /dev/loop1 align-check optimal 11 not aligned: 34s % 2048s != 0sA warning appeared, but the exit code was 0 and the partition was created anyway. The align-check result was [1 not aligned]. Partitions whose start was given as 1MiB or 0% were all [1 aligned], so use these units instead of sector numbers.
Another mistake is running only mkpart and mounting without creating a file system.
mount /dev/loop0p1 /mnt/datamount: /mnt/data: wrong fs type, bad option, bad superblock on /dev/loop0p1, missing codepage or helper program, or other error.A [wrong fs type, bad option, bad superblock] error appeared. Writing ext4 in mkpart does not create a file system, so run mkfs.ext4 first.
🔲 Checking the Change in parted Output
The change commands of parted print nothing even when they succeed, so the result was checked by comparing the print output before and after running them. This is print right after making the whole disk a single partition.
parted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 4294MB 4293MB data
The Start column is 1049kB, so the start position given as 0% was aligned to 1 MiB. The File system column is empty. After [mkfs.ext4 -q /dev/loop0p1] was run, the same command was run again.
parted -s /dev/loop0 printModel: Loopback device (loopback)
Disk /dev/loop0: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 4294MB 4293MB ext4 data
The File system column, which was empty before mkfs.ext4 was run, changed to ext4.
In this article, parted run on one line with -s ended with exit code 0 on success and 1 when an error appeared or it stopped at a warning. mkpart that gave only an alignment warning returned 0.
🔢 FAQ & Recommended Content
Linux · Disk
Linux blkid Command | UUID vs GUID
How to use the blkid command to check the file system type and UUID of block devices.
Linux · Disk
Swap Memory setup: 2 methods, partition & file | swapon, mkswap commands
How to create swap, which uses disk space like memory, in two ways, as a partition and as a file, and enable it with mkswap and swapon.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.