Skip to main content
Engineering LibreTexts

04-A.7: Disk Setup Process - fdisk

  • Page ID
    26841
  • Setting Up a Disk

    In this section, we look at several of the command line tools that can be used to prepare a disk for use. Take note - you only need to run EITHER fdisk, or parted - you do NOT need to run them both, as they do the same thing to the disk. They are alternatives to one another.

    The fdisk Command

    The fdisk command, also known as "format disk," is a dialog-driven command in Linux used for creating and manipulating disk partition table. It is used for the view, create, delete, change, resize, copy and move partitions on a hard drive using the dialog-driven interface.

    fdisk allows you to create a maximum of four Primary partitions, and the number of Logical partitions depends on the size of the hard disk you are using. It allows the user:

    • To create space for new partitions.
    • To organize space for new drives.
    • To Re-organize old drives.
    • To copy or move data to new disks(partitions).

    Syntax:

    fdsik [ OPTIONS ] device
    
    or
    
    fdisk -l [device...]

    Command Options:

    Options Option Meaning
    -b sectorsize Specify the sector size of the disk. Valid values are 512, 1024, 2048 or 4096. (Recent kernels know the sector size. Use this only on old kernels or to override the kernel's ideas.) Since util-linux-ng 2.17 fdisk differentiates between logical and physical sector size, this option changes both sector sizes to sectorsize.
    -h Print help and then exit.
    -c Switch off DOS-compatible mode. (Recommended)
    -C cyls Specify the number of cylinders of the disk. I have no idea why anybody would want to do so.
    -H heads Specify the number of heads of the disk. (Not the physical number, of course, but the number used for partition tables.) Reasonable values are 255 and 16.
    -S sects Specify the number of sectors per track of the disk. (Not the physical number, of course, but the number used for partition tables.) A reasonable value is 63.
    -l List the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that exists) are used.
    -u When listing partition tables, give sizes in sectors instead of cylinders.
    -s partition The size of the partition (in blocks) is printed on the standard output.

    Below is an example of running the utility using /dev/sda. We will create a single partition that will occupy the entire disk.

    1) Initiate fdisk with the following command:

      sudo fdisk /dev/sda 

    2) fdisk will display the following menu:

      Command (m for help): m <enter>
      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)
    
      Command (m for help):

    3) We want to add a new partition. Type "n" and press enter.

      Command action
       e   extended
       p   primary partition (1-4)

    4) We want a primary partition. Enter "p" and enter.

      Partition number (1-4):

    5) Since this will be the only partition on the drive, number 1. Enter "1" and enter.

      Command (m for help):

    If it asks about the first cylinder, just type "1" and enter. (We are making one partition to use the whole disk, so it should start at the beginning.)

    6) Now that the partition is entered, choose option "w" to write the partition table to the disk. Type "w" and enter.

      The partition table has been altered!

    7) If all went well, you now have a properly partitioned hard drive that's ready to be formatted. Since this is the first partition, Linux will recognize it as /dev/sda1, while the disk that the partition is on is still /dev/sda.

    Adapted from:
    "Installing A New Hard Drive" by Robert Unverzagt, The Community Help Wiki is licensed under CC BY-SA 4.0
    "fdisk command in Linux with examples" by Mandeep_Sheoran, Geeks for Geeks is licensed under CC BY-SA 4.0

    • Was this article helpful?