How To Use Fdisk Command (Create Disk Partitions).
In this article, we will talk about the fdisk command.
The first thing you need to do after installing a new SSD or hard disk is to partition it. A drive needs to have at least one partition before you can format it and store files on it.
Be aware that fdisk is a dangerous tool and should be used with extreme caution. Only root or users with sudo privileges can manipulate the partition tables.
To list the partition table of a device, invoke the fdisk command with the -l option, followed by the device name. For example to list the /dev/sda partition table and partitions you would run:
When no device is given as an argument, fdisk will print partition tables of all devices listed in the /proc/partitions file:
Creating Partition Table
To start partitioning the drive, run fdisk with the device name. In this example we’ll work on /dev/vda2:
The command prompt will change, and the fdisk dialogue where you can type in commands will open:
To get a list of all available commands enter m
If you are partitioning a new drive, before starting to create partitions first, you need to create a partition table. Skip this step if the device already has a partition table and you want to keep it.
fdisk supports several partitioning schemes. MBR and GPT are the two most popular partition scheme standards, that store the partitioning information on a drive in a different way. GPT is a newer standard allowing and has many advantages over MBR. The main points to consider when choosing what partitioning standard to use:
Use MBR to boot the disk in legacy BIOS mode.
Use GPT to boot the disk in UEFI mode.
The MBR standard supports creating a disk partition up to 2 TiB. If you have a disk of 2 TiB or larger, use GPT.
MBR has a limit of 4 primary partitions. If you need more partitions, one of the primary partitions can be set as an extended partition and hold additional logical partitions. With GPT, you can have up to 128 partitions. GPT doesn’t support extended or logical partitions.
In this example, we will use a GPT partition table.
Enter g to create a new empty GPT partition table:
The output will look something like this:
The next step is to create the new partitions.
We will create two partitions. The first one with a size of 10 GiB and the second one will take the rest of the disk space.
Run the n command to create a new partition:
You’ll be prompted to enter the partition number. Hit “Enter” to use the default value (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):
On the next prompt, 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).
Enter +10G to set the partition size to 10 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.
Let’s create the second partition that will take the rest of the disk space:
Use the default values for the partition number, first and last sectors. This will create a partition that will use all available space on the disk.
Once done creating partitions, use the p command to display the new partition table:
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.
The kernel will read the device partition table without the need to reboot the system.
Activating the Partitions
Now that the partitions have been created, the next step is to format the partitions and mount them to the system’s directory tree.
We’ll format both partitions to ext4:
In this example, will mount the partitions to /mnt/audio and /mnt/video directories.
Create the mount points with mkdir :
Mount the new partition:
Partitions will stay mounted until you unmount it or shutdown the machine. To automatically mount a partition when your Linux system starts up, define the mount in the /etc/fstab file.
That’s it! You can now use the new partitions to store your files.
fdisk is a command-line tool for creating partition schemes. For more information about the fdisk command, type man fdisk in your terminal.