Objective
Learning how to manage partitions using the GNU parted partition manager on Linux.
Requirements
- Root permissions
Conventions
- # – requires given linux command to be executed with root privileges either
directly as a root user or by use ofsudo
command - $ – given linux command to be executed as a regular non-privileged user
Introduction
Partitions management is one of the most essentials and dangerous task to perform when working with operating systems. In this tutorial we will focus on the use of GNU parted and see how we can use it to create, resize and delete partitions from the command line interface. Parted can work both in interactive and non-interactive mode, the latter being particularly useful when we want to automate some operations or when commands must run in an unattended context, perhaps in a script or inside a kickstart
file.
Initializing a device with a partition table
The device I’m going to work on in this tutorial, it’s /dev/sdb
: the first thing we want to do is to let parted show the current state of this drive. To run in interactive mode
we must launch parted with root permissions, passing as an argument to the command, the path of device we want to operate on, in this case:
$ sudo parted /dev/sdb
The parted prompt will be opened:
GNU Parted 3.2 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
At, this point, as suggested onscreen, we can type help
, to receive a list of available commands. In this case, by the way, we want to visualize the current state of the drive, therefore we will use the print
command:
Error: /dev/sdb: unrecognised disk label Model: Generic- SD/MMC/MS PRO (scsi) Disk /dev/sdb: 7743MB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags:
As you can see, since /dev/sdb
doesn’t contain a partition table, parted show us just information about the disk model, total size and sector size. To be able to use the disk we need to initialize it, therefore we must create a partition table on it. The command which let us do that is mklabel
. If we don’t specify what kind of partition table we want to create, parted will ask us in the prompt:
(parted) mklabel New disk label type? msdos
In this case we create a traditional msdos partition table. Other valid values are “aix”, “amiga”, “bsd”, “dvh”, “gpt”, “loop”, “mac”, “pc98” and “sun”. As said before, we could have also specified the type of partition table as an argument to the mklabel command:
(parted) mklabel msdos
This is very similar to the command we want to use if we want to perform the same task, but in a non-interactive way. If the command must run in an unattended context, we should also provide the -s
option, (short for --script
): doing so, we will be sure that user intervention it’s never requested:
$ sudo parted -s /dev/sdb mklabel msdos
Creating a partition
Now, let’s create our first partition on the device: we must provide the partition type
, choosing between primary or extended, the filesystem type (optional), the partition starting point and the partition ending point. Again if not provided directly, those values will be requested interactively. The command to create a partition is mkpart
:
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? Start? 1MiB End? 1025MiB
One thing that should be clear its that even if parted asks for a filesystem type, it will never create one on the partition: the information is requested just to set the GUID
(Global Unique Identifier) of the partition.
We specified 1MiB
as the starting point for the partition, so that it will start at sector 2048
of the disk (1 sector is 512 Bytes, therefore 2048 * 512 = 1048576 Bytes = 1MiB). In this case we could also have used s
as a unit, which stands for sector
, providing directly the sector we want the partition to start from. The partition starting point is very important for alignment, but we will see about it later.
Since we wanted 1GiB partition (1024 MiB), we specified 1025 MiB as ending point, since the partitions starts at 1MiB. In case we wanted the partition to cover all the available space on the device, we could have just provided 100%
as a value. It’s also important to notice that when providing a partition starting or ending point is recommended to use binary units
such as MiB, or GiB. When running in non-interactive mode, the above command becomes:
$ sudo parted -s /dev/sdb mkpart primary 1MiB 1025MiB
If now run the print command again we can see the partition we just created:
(parted) print Model: Generic- SD/MMC/MS PRO (scsi) Disk /dev/sdb: 7743MB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 1075MB 1074MB primary
The number of the partition, its start and ending points plus its size and type are displayed. We can instruct parted to use a specific measurement unit when displaying those information. Say for example we want to use MiB
as a unit: we could use the unit
command to specify it and then run print
again:
(parted) unit MiB (parted) print Model: Generic- SD/MMC/MS PRO (scsi) Disk /dev/sdb: 7384MiB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1.00MiB 1025MiB 1024MiB primary
As you can see, the unit we specified are now used.
Checking a partition alignment
As we said before, the alignment of a partition, is a very important factor to optimize performances. In parted we can check two type of alignments, minimal
and optimal
. In minimal mode, the program checks that the partition respects the minimum alignment value to physical blocks, while in optimal mode, it checks if the partition is aligned to a multiple of the physical block size, to provide optimal performances. The command to use to perform those checks is align-check
:
(parted) align-check alignment type(min/opt) [optimal]/minimal? Partition number? 1 1 aligned
Once the command is run in interactive mode, we are prompted to provide the type of alignment we want to check (optimal is used by default) and the partition number (1). In this case parted confirmed that the partition is aligned correctly. The non-interactive version of the command is:
$ sudo parted -s /dev/sdb align-check optimal 1
Since we used the -s
flag again, we observed no output from the command, but we can know if it was successful, by checking its exit code:
$ echo $? 0
As you know the $?
variables contains the exit value of the last launched command, and since it is 0
, we know that the command itself was successful. When not providing the -s
option, the command returns the result of the check similarly to what happens in interactive mode:
$ sudo parted /dev/sdb align-check optimal 1 1 aligned
Resizing a partition
Resizing a partition is also a very dangerous operation, especially if the partition already contains a filesystem. Be aware that when changing the size of a partition, parted will never adapt the filesystem to it, therefore, especially when shrinking, you must use the dedicated tools to resize the filesystem in use first. The command used to perform a partition resize is resizepart
. Our partition size is currently 1 GiB; if for example, we would like to extend it to cover all the remaining space on the device, we would type:
(parted) resizepart Partition number? 1 End? [1075MB]? 100%
After we typed the resizepart
command, parted prompted us to provide the number of the partition and the value for its new end. In this case we provided 100%
, which is the shortest way to ensure that all remaining space on the device is covered. The non-interactive version of the command is:
sudo parted -s /dev/sdb resizepart 1 100%
Where, again, 1 is the partition number, and 100% its the new value for the partition ending point. If we run print
again, we can have a confirmation that the changes we made have been applied:
Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) unit MiB (parted) print Model: Generic- SD/MMC/MS PRO (scsi) Disk /dev/sdb: 7384MiB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1.00MiB 7384MiB 7383MiB primary
The partition now covers all the space on the device.
Removing a partition
Removing a partition is just as easy. Obviously we should perform such an operation with the greatest attention. The command to use in this case is rm
:
(parted) rm Partition number? 1
Again, since we didn’t provide the partition number directly, parted prompted us to provide the needed information. We could just have provided it directly, writing rm 1
. When running in non-interactive mode, the command becomes:
$ sudo parted -s /dev/sdb rm 1
As expected, after running the command, the partition doesn’t exist anymore:
(parted) print Model: Generic- SD/MMC/MS PRO (scsi) Disk /dev/sdb: 7743MB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags
Conclusions
Managing partitions is a dangerous task that should be performed with the maximum attention. Although many graphical tools exists on linux to accomplish the needed tasks (the most famous is probably Gparted which is based on parted itself), sometimes we need the simplicity and power of the command line. In such situations, parted is the right tool. As always, consulting the program manpage is always recommended. Have fun, and be careful!