fdisk Command
fdisk
is a command that allows you to make changes to the partition table of your disk. Block devices can be divided into logical units called "partition". Information such as in which sector the partitions start and in which sector they end are kept on the "partition table". This is where fdisk
comes into play.
"Disk partitioning operations on Linux" require "root" privilege.
Listing Disks on Linux
You can use fdisk -l
to list disks and their partitions:
root@gnuadmin:~# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0xa422b442
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 62912511 61911042 29.5G 5 Extended
/dev/sda5 1001472 62912511 61911040 29.5G 8e Linux LVM
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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/mapper/debian--vg-root: 28.56 GiB, 30668750848 bytes, 59899904 sectors
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/mapper/debian--vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
According to the output; there are two disks in the system: /dev/sda
and /dev/sdb
. Some of the "sda" disk is used for "LVM". The disks /dev/mapper
at the bottom indicate these "LVM partitions".
Disk Partitioning on Linux
Everything you do with fdisk
is only kept in memory until you write it to disk. If you think you did something wrong, you can exit the program without writing it to disk. Your disk will not be affected.
Make sure you are partitioning on the correct disk. Otherwise data loss may occur! In this example, we will use the /dev/sdb
disk.
Interactive Partitioning With fdisk
You can start the interactive process using the command fdisk disname
(fdisk /dev/sdb for our example):
With "m", you can get help and see a list of commands you can use:
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
We should learn this part well. Now, let's create a 2 GiB FAT32 partition, 2 GiB NTFS and 6 GiB ext4 partition on our 10 GiB disk:
fdisk FAT32 partition
Let's run the fdisk /dev/sdb
command and follow the steps:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x830a162b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
Note the "p" output before and after creating the partition. In the second output, you will see that a "Linux" type partition (/dev/sdb1) has been created. Now let's convert this partition to "FAT32 (LBA)" type. We will use the "0c" code for this. You can find the full fdisk
partition codes at the end of the page.
Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 0c
Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x830a162b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G c W95 FAT32 (LBA)
As you can see, we specified that we want to change the partition type with "t" and changed it to "W95 FAT32 (LBA)" type with "0c" .
fdisk NTFS partition
There is no difference in the partition creation process. For type we will select "07" (HPFS/NTFS/exFAT):
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-20971519, default 4196352):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-20971519, default 20971519): +2G
Created a new partition 2 of type 'Linux' and of size 2 GiB.
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 07
Changed type of partition 'Linux' to 'HPFS/NTFS/exFAT'.
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x830a162b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G c W95 FAT32 (LBA)
/dev/sdb2 4196352 8390655 4194304 2G 7 HPFS/NTFS/exFAT
fdisk ext4 partition
fdisk
creates a partition of type "Linux" by default. This will suffice for "ext4":
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (8390656-20971519, default 8390656):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (8390656-20971519, default 20971519):
Created a new partition 3 of type 'Linux' and of size 6 GiB.
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x830a162b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G c W95 FAT32 (LBA)
/dev/sdb2 4196352 8390655 4194304 2G 7 HPFS/NTFS/exFAT
/dev/sdb3 8390656 20971519 12580864 6G 83 Linux
What To Do After Partitioning
List partitions with "p". If everything looks correct, write the changes to disk with "w" and exit:
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VBOX HARDDISK
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: dos
Disk identifier: 0x830a162b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G c W95 FAT32 (LBA)
/dev/sdb2 4196352 8390655 4194304 2G 7 HPFS/NTFS/exFAT
/dev/sdb3 8390656 20971519 12580864 6G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
When you list your disks again, you are expected to see your partitions. Do not forget that; just because you have partitioned the disks does not mean that you can use these partitions. You have to format (create a filesystem) these partitions using different tools.
fdisk Partition Codes
00 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
01 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
02 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
03 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
04 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
05 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
06 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
07 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
08 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
09 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
0a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
0b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
0c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Linux extended
0e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
0f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT