Linux fdisk command (changes partitions)
The fdisk command in Linux is a utility for creating, listing, altering, and deleting disk partitions. It is very robust and allows us to create a large list of partition types for Linux and different operating systems. fdisk works in text mode in the form of a friendly dialogue.
fdisk is capable of manipulating GPT, MBR, Sun, SGI, and BSD partition tables.
The fdisk -l option lists all disks and partitions found on the Linux system:
Units: sector of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
In this example above, the system has two disks xvda and xvdb. The first disk has an 8GB partition and a 1MB boot partition and uses the GPT system. The second 8GB disk has no partitions created.
In Cloud Computing systems, it is common for disks to use the nomenclature “xvd” to designate disks instead of “sd”.
To partition the disk, you must pass the block device you want to work with as a parameter to fdisk. You must have root permissions to change partitions.
In this example, fdisk will be able to partition the virtual disk xvdb.
The most common menu options are:
DOS (MBR)
a toggles the boot option
b edit the nested BSD disk label
c toggle the “compatibility” optionGeneric
d Delete a partition
F lists free unpartitioned partitions
l list known partition types
n add a new partition
p show the partition table
t changes the Partition typev check the partition table
i shows information about a partitionMiscellaneous
m shows this menu
u changes the units of the entries shown
; x additional functionality (for advanced users only)Script
I loads disk layout from an sfdisk script file
O dumps disk layout into an sfdisk script fileSave & ; exit
and save the table to disk and exit
without saving the changesCreate a new label
g create a new empty GPT partition table G creates a new partition table
G creates a new partition table SGI (IRIX) empty or creates a new empty DOS partition
To create a new MBR partition you must choose the “n” option on fdisk. Then you’ll have to choose whether the new partition will be primary, extended, or logical. If the partition is primary or extended, you must enter the partition number from 1 to 4. If the partition is logical, fdisk takes care of numbering for you.
In this example below, an 8GB primary partition was created using the MBR table:
The size of the partition must be chosen by typing the starting cylinder and the desired final cylinder or by the size in megabytes or gigabytes. For example, to create a 1-giga partition, you can type +1024M or +1G.
If you want to use GPT to create a partition, use the “g” option and then the “n” option.
By default, all partitions are created with the Linux type 83.
If you want another type of partition, such as swap, you must create the partition and then change its type with the “t” option.
See the types of partitions that Linux supports:
Since you have defined the partitions, it is necessary to write the data to the partition table with the “w” option.