Loading...

Knowledge Base

How to Create Disk Partitions on a Linux Server

When you want to divide server storage into separate partitions. This article explains how to create disk partitions on a Linux server.

A disk can be used as a single storage device or broken up into one or more partitions.

Disks are generally called /dev/sda, /dev/sdb, etc, in physical servers and /dev/vda, /dev/vdb, etc, in virtual machines.

Partitions get their names from the disk name itself and add a number starting at 1 (/dev/sda1, /dev/sda2, etc or /dev/vda1, /dev/vda2, etc).

A partition table is a special structure containing partitions organization.
Not recent disks use 512-byte sectors and the MBR partition table (MBR stands for Master Boot Record). This organization allows for 4 primary partitions only. If you want more than that, you need to create an extended partition (using one of the 4 primary slots), and then create logical partitions inside.

To avoid all these limitations, recent disks use 4096-byte sectors and the GPT partition table (GPT stands for GUID – Globally Unique IDentifier – Partition Table). 
There are some commands like ‘fdisk’, ‘gdisk’ and ‘parted’ that are really useful to manipulate disks and partitions.
 

The parted command


To start the parted command, type "parted" on a root SSH terminal
parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

To list all the disks and partitions, use:
(parted) print all
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  ext4         boot
 2      525MB   500GB  500GB  primary               lvm

Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start  End  Size  Type  File system  Flags

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg_root-lv_root: 497GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Number  Start  End    Size   File system  Flags
 1      0.00B  497GB  497GB  ext4

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg_root-lv_swap: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Number  Start  End     Size    File system     Flags
 1      0.00B  2147MB  2147MB  linux-swap(v1)

As you can see from the above result, /dev/sda is already partitioned and a disk called /dev/sdb without a partition but with an MBR partition table (Partition Table: msdos).

To select the /dev/sdb disk, type:
(parted) select /dev/sdb
Using /dev/sdb
To create a GPT partition table on the /dev/sdb disk, type:
(parted) mktable gpt
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? Y
(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start  End  Size  File system  Name  Flags
Note: Type mktable msdos to create a MBR partition table.
To create a primary partition with the ext4 type (here starting at 1MB and finishing at 400GB), type:
(parted) mkpart primary ext4 1MB 400GB

To check that the first partition is correctly aligned, type:
(parted) align-check optimal 1
1 aligned
To create a swap partition with a size of 2GB (here starting at 400GB and finishing at 402GB), type:
(parted) mkpart primary linux-swap 400GB 402GB
To print the result, type:
(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size    File system  Name     Flags
 1      1049kB  400GB  400GB   ext4         primary
 2      400GB   402GB  2000MB               primary

To set the first partition as bootable, type:
(parted) set 1 boot on
(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size    File system  Name     Flags
 1      1049kB  400GB  400GB   ext4         primary  boot
 2      400GB   402GB  2000MB               primary
Note: Type set 1 boot off to remove the bootable flag.

To remove the swap partition (here partition number 2), type:
(parted) rm 2
To exit the parted prompt, type:
(parted) quit
The changes are automatically saved when you quit ‘parted’.

To update the disk configuration seen by the kernel, type:
partprobe /dev/sdb
 

The gdisk Command


Install the gdisk package
yum install -y gdisk

Execute the gdisk command (here with the /dev/vda disk as a parameter):
gdisk /dev/vda
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
Type ? to display all the options:
Command (? for help): ?
b       back up GPT data to a file
c       change a partition's name
d       delete a partition
i       show detailed information on a partition
l       list known partition types
n       add a new partition
o       create a new empty GUID partition table (GPT)
p       print the partition table
q       quit without saving changes
r       recovery and transformation options (experts only)
s       sort partitions
t       change a partition's type code
v       verify disk
w       write table to disk and exit
x       extra functionality (experts only)
?       print this menu

Type p to print the partition table:
Command (? for help): p
Disk /dev/vda: 12582912 sectors, 6.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C6F7C323-530D-40B5-A985-241A1B181354
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 12582878
Partitions will be aligned on 2048-sector boundaries
Total free space is 1318845 sectors (644.0 MiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048        11266047   4.9 GiB     8E00  Linux LVM

Type n to create a new partition:
Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-12582878, default = 11266048) or {+-}size{KMGTP}: 34
Last sector (34-2047, default = 2047) or {+-}size{KMGTP}: 2047
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'
Type p to display the partition table:
Command (? for help): p
Disk /dev/vda: 12582912 sectors, 6.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C6F7C323-530D-40B5-A985-241A1B181354
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 12582878
Partitions will be aligned on 2048-sector boundaries
Total free space is 1316831 sectors (643.0 MiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048        11266047   4.9 GiB     8E00  Linux LVM
   3              34            2047   1007.0 KiB  8300  Linux filesystem

Type w to write the partition table to disk:
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/vda.
The operation has completed successfully.

To force the kernel to read the updated partition table, type:
partprobe
 

The fdisk Command

To list all the partitions, type:
fdisk -l

To create a primary partition on a disk (here /dev/vda), type:
fdisk /dev/vda
Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘n‘ (for new), type the partition number (between 1 and 4), the first sector and the size.
Finally, press ‘w‘ to save the partition table.

To delete a primary partition on a disk (here /dev/vda), type:
fdisk /dev/vda
Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘d‘ (for delete) and type the partition number (between 1 and 4).
Finally, press ‘w‘ to save the partition table.

To set the type of a primary partition (here /dev/vda3), type:
fdisk /dev/vda
Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘t‘ (for tag), type the partition number (here ‘3‘) and the partition type (83 for linux, 8e for Linux LVM, 82 for swap).
Finally, press ‘w‘ to save the partition table
.
To force the kernel to read the updated partition table, type:
partprobe

 

 
 

There are different types of partitions


Primary, Extended, and Logical partition types:

We can only create a maximum of 4 partitions on a hard disk drive. This is because of the fact that the MBR’s partition is very small in size and hence cannot hold information for more than 4 partitions. To overcome this behavior, the concept of Primary/Extended/Logical partitions was introduced.
  • Primary partition – This is the partition that comes by default.  If you only plan to create 4 partitions or less, then you only need to create a primary partition and you don’t have to worry about extended and logical partitions.
  • Extended partition – If you want to create more than 4 partitions on an HDD, then you can create one of the 4 partitions as an 'Extended'. You can create logical partitions inside an extended partition. You won't be able to store data inside an extended partition directly, you have to create a logical partition inside the extended partition. You can only create one extended partition per HDD.
  • Logical partition – Any partitions created inside an extended partition will be considered as Logical.

See the Partition table below that has 3 primary partitions:

(Here, we are using the ‘fdisk’ command to explain the procedure.)
Command (m for help): p

Disk /dev/sdb: 2206 MB, 2206806016 bytes, 4310168 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: 0x2f383010
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528       43007       10240   83  Linux
/dev/sdb3           43008       63487       10240   83  Linux

To create an extended partition,
Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e):     # notice that the default is now "e", which is nice. 
Using default response e
Selected partition 4
First sector (63488-4310167, default 63488):
Using default value 63488
Last sector, +sectors or +size{K,M,G} (63488-4310167, default 4310167): +1024M        # tip: leave this blank!
Partition 4 of type Extended and of size 1 GiB is set

Now if you create another partition on this same disk, the ‘fdisk’ command will automatically prompt and create a Logical partition.
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (65536-2160639, default 65536):
Using default value 65536
Last sector, +sectors or +size{K,M,G} (65536-2160639, default 2160639): +10M
Partition 5 of type Linux and of size 10 MiB is set

You can see the total partitions like below:
Command (m for help): p

Disk /dev/sdb: 2206 MB, 2206806016 bytes, 4310168 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: 0x2f383010

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528       43007       10240   83  Linux
/dev/sdb3           43008       63487       10240   83  Linux
/dev/sdb4           63488     2160639     1048576    5  Extended
/dev/sdb5           65536       86015       10240   83  Linux

You can see the partition types available by typing “l” in the ‘fdisk’ command prompt

The commonly used partitions types are listed below:
5/extended - an extended partition.
83/Linux - the default: installs a standard filesystem on it
8e/LVM - used for creating physical volumes
Swap - Used for creating virtual memory

To apply all the changes by saving the new partition table (using option "w") to the special internal MBR's partition:
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

To refresh the partition table, you can use the command,
partprobe
or, you can reboot the server.