#!/linuxSucks/Create Partitions & Format disk with fdisk

Jun 09, 2021

fdisk is a menu-driven command-line utility that allows you to create and manipulate partition tables on a hard disk.

fdisk is a dangerous tool and should be used with extreme caution. (root or users with sudo privileges can manipulate the partition tables)

List Partitions

To list the partition table of a device

sudo fdisk -l

fdiks with -l flag and no other argument will display all the driver

Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC  WDS200T1R0A
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 /dev/sdb: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: W800S 256GB SSD
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D283DB7D-0D7C-477E-BF41-930FF235C34D

Device         Start       End   Sectors   Size Type
/dev/sdb1       2048   1230847   1228800   600M EFI System
/dev/sdb2    1230848 106088447 104857600    50G Linux filesystem
/dev/sdb3  106088448 122865663  16777216     8G Linux swap
/dev/sdb4  122865664 500118158 377252495 179.9G Linux filesystem

To list the partitions for a drive run

sudo fdisk -l /dev/sdb

It will show something like

Disk /dev/sdb: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: W800S 256GB SSD
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D283DB7D-0D7C-477E-BF41-930FF235C34D

Device         Start       End   Sectors   Size Type
/dev/sdb1       2048   1230847   1228800   600M EFI System
/dev/sdb2    1230848 106088447 104857600    50G Linux filesystem
/dev/sdb3  106088448 122865663  16777216     8G Linux swap
/dev/sdb4  122865664 500118158 377252495 179.9G Linux filesystem

Creating Partition Table

To start partitioning the drive, run fdisk with the device name

sudo fdisk /dev/sdb

The command prompt will change, and the fdisk dialogue where you can type in commands will open:

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m

Type m to display all the options

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x435158be.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help):

Partition Table

The partition table you make, won’t take effect until you write them with the w command

You can exit the fdisk dialogue without saving the changes using the q command.

fdisk supports several partitioning schemes

  1. Use MBR to boot the disk in legacy BIOS mode.
  2. Use GPT to boot the disk in UEFI mode.
  3. The MBR standard supports creating a disk partition up to 2 TiB. If you have a disk of 2 TiB or larger, use GPT.

Enter g to create a new empty GPT partition table

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): g

The output will look something like

Created a new GPT disklabel (GUID: 4649EE36-3013-214E-961C-51A9187A7503).

## Create Partitions

Type n to create a new partition

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n

You’ll be prompted to enter the partition number. Hit “Enter” to use the default value (1)

Partition number (1-128, default 1):

Next, the command will ask you to specify the first sector. Generally it is always recommended to use the default values for the first value. Hit “Enter” to use the default value (2048)

First sector (2048-500118158, default 2048):

You’ll need to enter the last sector. You can use an absolute value for the last sector or relative value to the start sector, using the + symbol following by the partition size. The size can be specified in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P).

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-500118158, default 500118158): +100G

Created a new partition 1 of type ‘Linux filesystem’ and of size 100 GiB.

By default, the type of the new partition is set to “Linux filesystem”, which should be fine for most cases. If you want to change the type, press l to get a list of partition types and then press t to change the type.

Once done creating partitions, use the p command to display the new partition table

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

The output will be like

Disk /dev/sdb: 298.9 GiB, 320072933376 bytes, 625142448 sectors
Disk model: nal USB 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F8365250-AF58-F74E-B592-D56E3A5DEED1

Device         Start       End   Sectors   Size Type
/dev/sdb1       2048 209717247 209715200   100G Linux filesystem
/dev/sdb2  209717248 625142414 415425167 198.1G Linux filesystem

If you want to delete a partition, use the d command. Save the changes by running the w command:

The command will write the table to the disk and exit the fdisk menu

Format Partitions

To format both any partition (ext4)

sudo mkfs.ext4 -F /dev/sdb1
sudo mkfs.ext4 -F /dev/sdb2
sudo mkfs.ext4 -F /dev/sdb3

The output will be like

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 51928145 4k blocks and 12984320 inodes
Filesystem UUID: 63a3457e-c3a1-43f4-a0e6-01a7dbe7dfed
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872

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

Home  Linux  Notes  Blog Spot