How To Create a Partition Using “parted” Command

There are different options to create or manage partitions under Linux – Parted is one of them. This note describes its usage and the use case when creating a new partition table. Use parted interactively to enter commands one at a time. Include only the device as an argument to invoke interactive mode.

Creating a partition using parted

1. Select the hard disk to be partitioned

Select the disk on which the partition is being created, in the below example /dev/sdb is being partitioned. You can select the disk in below given two ways.

# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

or

# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdb                                                  
Using /dev/sdb
(parted)

It is possible to execute “help” to see the various options provided in parted command

2. Set partition table type

The following example creates a new partition table by using the mklabel command. The disk label type must be one of the following: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, or loop.

(parted) help mklabel                                                     
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)

 LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop

In the above output, it is possible to see that different LABEL-TYPEs are supported in parted. It is important to note that one will require to use msdos as LABEL-TYPE for generic Linux.

(parted) mklabel msdos                                                    
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
Yes/No? Yes

3. Check free space and existing partitions

To check free space and any existing partitions on the disk use the print sub-command. As you can see 21.5GB space is free on the disk and no partition is created yet on the disk.

(parted) print free                                                       
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type  File system  Flags
        32.3kB  21.5GB  21.5GB        Free Space
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

(parted)

4. Creating Primary or Logical Partition in Selected Disk Using mkpart

One can create either Primary or Logical partitions using mkpart command. Options can be primary and logical respectively. Example for both options are shown as below (we will create 2 partitions of 200MB each as primary and logical partition) :
a. Creating primary partition

(parted) mkpart primary                                                   
File system type?  [ext2]? ext4                                           
Start? 0                                                                  
End? 200MB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End    Size   Type     File system  Flags
 1      512B   200MB  200MB  primary

b. Creating logical partition

(parted) mkpart logical                                                   
parted: invalid token: logical
Partition type?  primary/extended? extended                               
Start? 201M                                                               
End? 402M
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End    Size   Type      File system  Flags
 1      512B   200MB  200MB  primary
 2      201MB  402MB  200MB  extended               lba
NOTE: Parted asks for the FS-Type while creating primary partition unless a logical partition is to be created.

Remove a Partition Using rm Command

One can also delete an existing partition using “rm” command, as shown in below example we have 2 partitions with number 1 and 2.

(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End    Size   Type      File system  Flags
 1      512B   200MB  200MB  primary
 2      201MB  402MB  200MB  extended               lba

To delete partition 2:

(parted) rm                                                               
Partition number? 2

Verify that you can see only the partition number 1 now.

(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End    Size   Type     File system  Flags
 1      512B   200MB  200MB  primary
Related Post