Learn a Linux command every day (78): fdisk

Time:2022-2-17

Command introduction

The fdisk command is used to view disk usage and disk partitions. It can be used to create, delete and modify disk partitions.

Syntax format

disk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

Option description

-B < size > # sector size (512, 1024, 2048 or 4096)
-C [= < mode >] # compatibility mode: "Dos" or "nondos" (default)
-H # print this help text
-U [= < units >] # display units: "cylinders" or "sectors" (sectors, default)
-V # print program version
-C < number > # specifies the number of cylinders
-H < number > # specifies the number of heads
-S < number > # specifies the number of sectors per track

Application examples

Select the disk to operate on

[[email protected] ~]$ fdisk /dev/sdb

Enter m to view the executable commands

command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Enter p to list the current partition of the disk

Command (m for help): p
 
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           1        8001   8e  Linux LVM
/dev/sdb2               2          26      200812+  83  Linux

Enter D and select the partition to delete the existing partition

Command (m for help): d
Partition number (1-4): 1
 
Command (m for help): d
Selected partition 2

Check the partition and confirm that the partition has been deleted

Command (m for help): print
 
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help):

Enter n to create a new disk partition. First, create two main disk partitions

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
P # create primary partition
Partition number (1-4): 1 # partition number
First # cylinder (1-391, default 1): # partition start position
Using default value 1
Last # cylinder # or + size # or + sizem # or + sizek (1-391, default 391): end position of 100 # partition, unit: Sector
 
Command (m for help): n # create another partition
Command action
   e   extended
   p   primary partition (1-4)
p 
Partition number (1-4): 2 # partition number is 2
First cylinder (101-391, default 101):
Using default value 101
Last # cylinder or + size or + sizem or + sizek (101-391, default 391): + 200m # partition end position, unit: M

Confirm that the partition is established successfully

Command (m for help): p
 
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux

Create another logical partition

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
E # select extended partition
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391

Confirm that the extended partition is established successfully

Command (m for help): p
 
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended

Create two logical partitions on the extended partition

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
L # select logical partition
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M    
 
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391

Confirm that the logical partition is established successfully

Command (m for help): p
 
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended
/dev/sdb5             126         175      401593+  83  Linux
/dev/sdb6             176         391     1734988+  83  Linux
 
Command (m for help):

Save operation information

Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

After the partition is established, we also need to format the partition to use the disk in the system. This format command will be explained in detail in the following chapters.

Learn a Linux command every day (78): fdisk