From charlesreid1

Gparted is a unix utility that can be used to deal with partitions and disks.

Most gparted commands require superuser permissions, so it can be started using the sudo parted command.

There are two interfaces: the GUI interface, and the command-line interface.

To start the GUI interface, run:

$ sudo gparted

To start the command-line interface, run:

$ sudo parted

(parted)


Command line usage

Showing disk information

The list of devices can be shown using the print command:

(parted) print all

Disk /dev/sda: 250GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End    Size    Type     File system  Flags
 1      32.3kB  839MB  839MB   primary  linux-swap        
 4      839MB   199GB  198GB   primary  ext3              
 3      199GB   225GB  26.2GB  primary  ext3              
 2      225GB   250GB  25.0GB  primary  ext3         boot 

Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  1000GB  1000GB  primary  fat32

This scenario has two disks in the system:

  • A 250 GB ext3 hard drive (this is the main hard drive, and has four partitions, with one main partition - the 839MB-199GB partition)
  • A 1 TB fat32 hard drive (this is an external drive, since there is no boot or swap partition)

The main hard drive is the device /dev/sda, and the external hard drive is /dev/sdb. Further external hard drives would be labeled /dev/sdc, /dev/sdd, etc.

The device location is important, since it can be used to mount disks like this:

# For fat32 drives, specify uid and gid to allow read/write permission
mount /dev/sdb1 /path/to/mount/point -o user,rw,uid=userid,gid=groupid

# For ext3 drives, use chown and chgrp to add read/write permission
mount /dev/sdc1 /path/to/mount/point -o user,rw
chown userid /path/to/mount/point
chgrp groupid /path/to/mount/point

The number in the list of each device, and in the name of each device in /dev, corresponds to the partition number.


Creating a new partition

There are a couple of commands that can be used to create partitions. A brief explanation of each is given here:

  • mkpart - creates a partition
  • mkpartfs - creates a partition, and a filesystem for each partition
  • mkfs - creates a filesystem for a partition

There are a couple ways to do this. The first is to create a partition using mkpartition, and then create a filesystem for that partition using mkfs or some other method.

The other (preferred) option is to use mkpartfs, which does both.

If you don't have any partitions available on the device, you will see something like this:

(parted) print free                                                       

Disk /dev/sdd1: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: loop

Number  Start   End     Size    File system  Flags
        0.00kB  500GB   500GB  Free Space

Command mkpartfs

To begin, the help for this command is:

(parted) help mkpartfs                                                    
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system

        PART-TYPE is one of: primary, logical, extended
        START and END are disk locations, such as 4GB or 10%.  
        Negative values count from the end of the disk.  For 
        example, -1s specifies exactly the last sector.

Let's say I'm formatting an external hard drive: partitioning an external hard drive to use for storing files and such. I can choose several different formats, depending on what my needs are.

Other non-supported filesystems include:

  • Ext3 - this is another format used by Linux (NOTE: this format is NOT compatible with parted! See below for use.)

To create a partition with one of the filesystems supported by parted (e.g. ext2):

(parted) mkpartfs 
File system type?  [ext2]? ext2
Start? 0GB
End? 500GB

This creates a 500GB ext2 partition. The start can also be specified using megabytes, or sectors.

Command mkpart

The help for this command is:

(parted) help mkpart
  mkpart PART-TYPE [FS-TYPE] START END     make a partition

        PART-TYPE is one of: primary, logical, extended
        FS-TYPE is one of: ext3, ext2, fat32, fat16, hfsx, hfs+, 
        hfs, jfs, linux-swap, ntfs, reiserfs, hp-ufs, sun-ufs, xfs, 
        apfs2, apfs1, asfs, amufs5, amufs4, amufs3, amufs2, amufs1, 
        amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2, affs1, 
        affs0
        START and END are disk locations, such as 4GB or 10%.  Negative
        values count from the end of the disk.  For example, -1s specifies 
        exactly the last sector.
        
        mkpart makes a partition without creating a new file system on 
        the partition.  FS-TYPE may be specified to set an appropriate 
        partition ID.

To create a partition using a filesystem not supported by parted, the mkpart command must be used to create the partition, while another command must be used to create the filesystem in the created partition.

To avoid specifying a filesystem type, the partition type, which may be one of "primary, extended, logical", must be "extended".

In order to create a 500GB partition, mkpart is used as follows:

1. Select the device to partition

(parted) select /dev/sdd

2. Print information about partitions and free space

(parted) print free

3. Create a partition (in this example, make a 500 GB partition)

(parted) mkpart
Partition type?  primary/extended? extended                               
Start? 0GB                                                                
End? 500GB    

If you select primary instead of extended, then parted will ask for a filesystem type.

Once the partition is created, you'll be able to see it using print free:

(parted) print

Disk /dev/sdd: 1500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      32.3kB  500GB   500GB   extended               lba  

Command mkfs

Once a partition has been created, a filesystem for it can be created using the parted command mkfs:

(parted) mkfs
Partition number? 1                                                       
File system?  [ext2]? ext2

This will then create an ext2 filesystem on the partition created with mkpart.

Command: mkfs.ext3

This command creates an ext3 filesystem in an existing partition. This requires that a partition is already created using mkpart.


Scripting with Gparted

Problems

Unfortunately one of the more useful features of parted is not usable from the script interface:

$ sudo parted -s print all
Error: Could not stat device print - No such file or directory.

This is discussed here: http://lists.alioth.debian.org/pipermail/parted-devel/2006-June/000148.html but I don't know what came of that discussion. (If you do know anything about this, please email me, root(at)charlesmartinreid.com).

In lieu of using this in a script, you can do this instead:

$ cat /proc/partitions 
major minor  #blocks  name

   8     0  244198584 sda
   8     1     819283 sda1
   8     2   24410767 sda2
   8     3   25599577 sda3
   8     4  193366372 sda4
   8    32  976762584 sdc
   8    33  976760001 sdc1
   8    16 1465136128 sdb
   8    17     204800 sdb1
   8    18  996147200 sdb2
   8    19  468782080 sdb3

While this doesn't give you as much information as parted's print all, it'sat least similar, and usable from a script.