Now Reading
Parted commands in Linux with examples
0

Parted commands in Linux with examples

Linux ‘Parted’ Commands to Create, Delete and Rescue Disk Partitions

Parted is a command line tool that helps to manage the hard disk partitions. The Linux ‘Parted’ Commands that are used to Create, delete and Rescue Disk Partitions is explained in this article.

To Install Parted on Linux

Parted is a pre-installed tool and if it is not available install it by using the following commands.

On Fedora 22+ versions

# dnf install parted

On Debian/Ubuntu systems

$ sudo apt-get install parted

On RHEL/CentOS and Fedora

[root@linuxhelp ~]# yum install parted -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package parted.x86_64 0:3.1-17.el7 will be updated
---> Package parted.x86_64 0:3.1-23.el7 will be an update
.
.
.
  Verifying  : parted-3.1-17.el7.x86_64                                                                                                 2/2 

Updated:
  parted.x86_64 0:3.1-23.el7                                                                                                                

Complete!

To use parted command to manage partition

Run the following command, to manage partition. If you run parted command without any argument means it will take the default first hard disk of your system.

[root@linuxhelp ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

To List Linux Disk Partitions

Run the following print command to list the partition of the specific hard disk and model.

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

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  525MB   524MB   primary  xfs             boot
 2      525MB   2622MB  2097MB  primary  linux-swap(v1)
 3      2622MB  21.5GB  18.9GB  primary  xfs

To exit from parted type quit command.

(parted) quit                                                             
[root@linuxhelp ~]#

To use parted on specific hard drive

Now run the parted command with the hard disk path to manage the partition on the particular hard disk.

[root@linuxhelp ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)                                    
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 
(parted)

Above example shows the hard drive currently has no partition created.

To Create Primary or Logical Partition in Linux

To generate primary or logical disk partitions use parted. Check for the disk before starting partition using print statement.

Now create a new label for the hard disk.

(parted) mklabel msdos

Use mkpart to create partition. The units are in MB so to generate 10 GB start the partition from 1 to 10000.

(parted) mkpart                                                           
Partition type?  primary/extended? primary                                
File system type?  [ext2]? ext4                                           
Start? 1                                                                  
End? 10000
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.0GB  9999MB  primary

Now exit from parted and run the following command to format partition in ext4 file system.

[root@linuxhelp ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
610800 inodes, 2441216 blocks
122060 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
75 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Important: While executing the above command select the correct disk where the partition to be done.

Now verify our results, by printing the partition table on our secondary disk. Under file system column, ext4 or the file system type that you have decided to use for your partition:

[root@linuxhelp ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.0GB  9999MB  primary  ext4

To Change Linux Partition Flag

The various flags that supports Partition are as follows.

lvm
lba
legacy_boot
swap
hidden
raid
irst
esp
palo
boot
root

Use ‘set‘ statement inside parted to alter flag settings.

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

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.0GB  9999MB  primary  ext4         boot

It sets the value of boot flag to ‘on‘ in the partition 1. Check the resultant by viewing print command.

To Delete Linux Partition

Use rm command to remove the disk partitions. First get the number of the disk by running print command.

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

Number  Start  End  Size  Type  File system  Flags

To Rescue Linux Disk Partition

rescue‘ command is used to recover the lost partition within the starting and end point and stores them.

(parted) rescue
Start? 1                                                                  
End? 10000                                                                
searching for file systems... 1%        
(time left 02:20)Information: 
A ext4 primary partition was found at 1049kB -> 10.0GB.  
Do you want to add it to the partition table?Yes/No/Cancel? yes                                                      
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.0GB  9999MB  primary  ext4
boot

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response