In this tutorial, you are going to learn how to create partitions using
parted command in Linux.
parted is a program to manipulate disk partitions. It supports multiple partition table formats, including MS-DOS and GPT. It is useful for creating space for new operating systems, re-organising disk usage, and copying data to new hard disks.
On any operating system, a disk needs to be partitioned before it can be used. A partition is a logical subset of the physical disk, and information about partitions are stored in a partition table. This table includes information about the first and last sectors of the partition and its type, and further details on each partition.
Create Partitions using parted command in Linux
Using this utility
parted you can create, delete, restore and resize partitions.
Understanding MBR and GPT
There are two main ways of storing partition information on hard disks. The first one is MBR (Master Boot Record), and the second one is GPT (GUID Partition Table).
This is a remnant from the early days of MS-DOS (more specifically, PC-DOS 2.0 from 1983) and for decades was the standard partitioning scheme on PCs. The partition table is stored on the first sector of a disk, called the Boot Sector, along with a boot loader, which on Linux systems is usually the GRUB bootloader. But MBR has a series of limitations that hinder its use on modern systems, like the inability to address disks of more than 2 TB in size, and the limit of only 4 primary partitions per disk.
A partitioning system that addresses many of the limitations of MBR. There is no practical limit on disk size, and the maximum number of partitions are limited only by the operating system itself. It is more commonly found on more modern machines that use UEFI instead of the old PC BIOS.
Create Partitions using parted command in Linux
parted is a very powerful partition editor (hence the name) that can be used to create, delete, resize and rescue. It can work with both GPT and MBR disks, and cover almost all of your disk management needs. There are many graphical front-ends that make working with
parted much easier, like GParted for GNOME-based desktop environments and the KDE Partition Manager for KDE Desktops.
gdisk, parted makes changes to the disk immediately after the command is issued, without waiting for another command to write the changes to disk. When practicing, it is wise to do so on an empty or spare disk or flash drive, so there is no risk of data loss should you make a mistake.
DEVICE is the device you want to manage (
sudo parted /dev/sdb
GNU Parted 3.3 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
NOTE; Be careful! If you do not specify a device,
parted will automatically select the primary disk (usually
/dev/sda) to work with.
select command to switch to a different disk than the one specified on the command line.
(parted) select /dev/sda Using /dev/sda (parted)
(parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 26.8GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 26.8GB 26.8GB primary ext4 boot
You also can get a list of all block devices connected to your system using
print devices command;
(parted) print devices /dev/sdb (16.6GB) /dev/sda (26.8GB)
Checking for Free Space
print free command to know how much free space there is in each one of them;
(parted) print free Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 26.8GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1024B 1049kB 1048kB Free Space 1 1049kB 26.8GB 26.8GB primary ext4 boot 26.8GB 26.8GB 1049kB Free Space
Creating a Partition Table on an Empty Disk using parted
To create a partition table on an empty disk, use the
mklabel command, followed by the partition table type that you want to use. There are many supported partition table types, but the main types you should know of are
msdos which is used here to refer to an MBR partition table, and
gpt to refer to a GPT partition table.
Create an MBR partition table;
(parted) mklabel msdos
Create a GPT partition table;
(parted) mklabel gpt
Create Partition using parted command in Linux
mkpart command to create a partition.
mkpart PARTTYPE FSTYPE START END
PARTTYPE: Is the partition type, which can be
extendedin case an MBR partition table is used.
FSTYPE: Specifies which filesystem will be used on this partition. Note that
partedwill not create the filesystem. It just sets a flag on the partition which tells the OS what kind of data to expect from it.
START: Specifies the exact point on the device where the partition begins. You can use different units to specify this point.
2scan be used to refer to the second sector of the disk, while
1mrefers to the beginning of the first megabyte of the disk. Other common units are
%(percentage of the disk).
END: Specifies the end of the partition. Note that this is not the size of the partition, this is the point on the disk where it ends. For example, if you specify
100mthe partition will end 100 MB after the start of the disk. You can use the same units as in the
(parted) mkpart primary ext4 1m 6g (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.6GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 6000MB 5999MB ext4 primary
The above output creates a primary partition of type
ext4, starting at the first megabyte of the disk, and ending after the 6th gigabyte.
Removing a Partition with parted command
To remove a partition, use the command
rm followed by the partition number, which you can display using the
rm 1 would remove the second partition on the currently selected disk.
(parted) rm 1
We can use
(parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.6GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags
In the above output, partition 1 has been removed so we don’t have any existing partition.
Recovering Partitions with parted command
parted can recover a deleted partition, to recover it, you can use the
rescue START END
START is the approximate location where the partition started, and
END the approximate location where it ended. We can recover the deleted partition 1 above.
parted will scan the disk in search of partitions, and offer to restore any that are found. In the example above the partition 1 started at 1 MB and ended at 6 GB.
(parted) rescue 990k 6010m Information: A ext4 primary partition was found at 1MB -> 6000MB. Do you want to add it to the partition table? Yes/No/Cancel? y
Displaying parted Help
To dispaly help, use
(parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END resizepart NUMBER END resize partition NUMBER rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted
This marks the end of our tutorial on create Partitions using parted command in Linux. We hope this tutorial was of help to you.
For further understanding of this utility, you can refer to its
Check If a Package is Installed on Linux
How To List Running Services on Linux
How To Check Memory Usage on Linux