List, create, delete partitions on MBR and GPT disks

Check our current situation:

# fdisk -l

Check the free space of our disks:

# parted /dev/sda unit MB print free

Note: If you don’t have free space, it is better to add a second disk.

# fdisk -l
Disk /dev/vda: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d3dcc

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 16777215 7339008 8e Linux LVM

Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The fdisk command shows us the new and empty disk (/dev/vdb).

To begin, we chose the vdb disk:

m = menu or help
note: fdisk command is used to work with MBR disks.           
gdisk command is used to work with GPT disks.

How to create a partition table:

# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
 
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa15b74e0.
 
Command (m for help): p  <--(print the partition list)
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n <--(Creating the first partition)
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): <--(Enter to choose a primary partition)
Using default response p
Partition number (1-4, default 1): <--(Enter to choose a partition number)
First sector (2048-4404019, default 2048): <--(Enter to choose the default first sector of the disk/partition)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4404019, default 4404019): +250M <--(Size according to your requirements)
Partition 1 of type Linux and of size 250 MiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      514047      256000   83  Linux
 
Command (m for help): n <--(Creating the 2° partition)
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (514048-4404019, default 514048):
Using default value 514048
Last sector, +sectors or +size{K,M,G} (514048-4404019, default 4404019): +250M
Partition 2 of type Linux and of size 250 MiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      514047      256000   83  Linux
/dev/vdb2          514048     1026047      256000   83  Linux
 
Command (m for help): n <--(Creating the 3° partition)
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (1026048-4404019, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-4404019, default 4404019): +250M
Partition 3 of type Linux and of size 250 MiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      514047      256000   83  Linux
/dev/vdb2          514048     1026047      256000   83  Linux
/dev/vdb3         1026048     1538047      256000   83  Linux
 
Command (m for help): n <--(Creating the 4° partition. Note this is extended)
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e):
Using default response e
Selected partition 4
First sector (1538048-4404019, default 1538048):
Using default value 1538048
Last sector, +sectors or +size{K,M,G} (1538048-4404019, default 4404019):
Using default value 4404019
Partition 4 of type Extended and of size 1.4 GiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      514047      256000   83  Linux
/dev/vdb2          514048     1026047      256000   83  Linux
/dev/vdb3         1026048     1538047      256000   83  Linux
/dev/vdb4         1538048     4404019     1432986    5  Extended
 
Command (m for help): n <--(Creating the first logical partition)
All primary partitions are in use
Adding logical partition 5
First sector (1540096-4404019, default 1540096):
Using default value 1540096
Last sector, +sectors or +size{K,M,G} (1540096-4404019, default 4404019): +250M
Partition 5 of type Linux and of size 250 MiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa15b74e0
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      514047      256000   83  Linux
/dev/vdb2          514048     1026047      256000   83  Linux
/dev/vdb3         1026048     1538047      256000   83  Linux
/dev/vdb4         1538048     4404019     1432986    5  Extended
/dev/vdb5         1540096     2052095      256000   83  Linux
 
Command (m for help): w <--(saving the partitions table)
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
 
# parted /dev/vdb unit MB print free <--(checking the free space)
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2255MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
 
Number  Start   End     Size    Type      File system  Flags
        0.03MB  1.05MB  1.02MB            Free Space
 1      1.05MB  263MB   262MB   primary
 2      263MB   525MB   262MB   primary
 3      525MB   787MB   262MB   primary
 4      787MB   2255MB  1467MB  extended
        788MB   788MB   0.57MB            Free Space
 5      789MB   1051MB  262MB   logical
        1051MB  2255MB  1204MB            Free Space
 
# partprobe -s  <--(this command inform the OS of partition table changes)
/dev/vda: msdos partitions 1 2
/dev/vdb: msdos partitions 1 2 3 4 <5>

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *