Partitioning Disks by Using fdisk
To create and manage hard disks that use MBRs, use the fdisk command. Or, you can use the cfdisk utility, which is a text-based, graphical version of fdisk.
Before running fdisk, complete the following requirements first:
-
Unmount any mounted partition on the disk.
-
Disable any partition that's being used as swap space by using the swapoff command.
-
Backup the data on the disk to be configured.
fdisk can be used either interactively or directly with command line options and arguments.
Note:
The two modes can differ in the options they support to perform specific actions. To list supported options while in interactive mode, enter m at the mode's prompt. For supported options in the command line mode, type:
fdisk -h
To run the fdisk command interactively, specify only the name of the disk device as an argument, for example:
sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.40.2) Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help):
The following commands are useful for managing partitions:
- p
-
Displays the current partition table.
- n
-
Initiates the process for creating new partitions.
- t
-
Changes the partition type.
Tip:
To list all the supported partition types, enter l.
- w
-
Commits changes you made to the partition table, then exits the interactive session.
- q
-
Disregards any configuration changes you made and exits the session.
- m
-
Displays all the supported commands in the interactive mode.
For more information, see the cfdisk(8)
and
fdisk(8)
manual pages.
Displaying the Partition Table
To display the partition table, enter p at the fdisk
prompt, for example:
Command (m for help): p
Disk /dev/sda: 36.5 GiB, 39191576576 bytes, 76546048 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 Disklabel type: dos Disk identifier: 0x67fb0c7a Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1026047 1024000 500M 83 Linux /dev/sda2 1026048 76546047 75520000 36G 8e Linux LVM Command (m for help):
The output contains device information summary such as disk size, disklabel type, and partition details. The partition details are specified under the following field names:
-
Device
-
Lists the current partitions on the device.
-
Boot
-
Identifies the boot partition with an asterisk (
*
). This partition contains the files that the GRUB bootloader needs to boot the system. Only one partition can be bootable. Start
andEnd
-
Lists the start and end offsets in sectors that mark a sector's boundaries. All partitions are aligned on one-megabyte boundaries.
-
Sectors
-
Displays sector sizes.
-
Size
-
Displays partition sizes.
Id
andType
-
Indicates a representative number and its corresponding representative number.
Oracle Linux typically supports the following types:
-
5 Extended
-
An extended partition that can contain up to four logical partitions.
-
82 Linux swap
-
Swap space partition.
-
83 Linux
-
Linux partition for a file system that's not managed by LVM. This is the default partition type.
-
8e Linux LVM
-
Linux partition that's managed by LVM.
-
Creating Partitions
The following example shows how to use the different fdisk
interactive commands to partition a disk. 2 partitions are created on
/dev/sdb
. The first partition is assigned 2 GB while the second partition
uses all the remaining disk space.
sudo fdisk /dev/sdb
The command runs a menu-based system where you must select the appropriate responses to configure the partition. Example inputs are displayed in the following interactive session:
... 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-32767999, default 2048): <Enter> Last sector, +sectors or +size{K,M,G,T,P} (2048-32767999, default 32767999): +2G Created a new partition 1 of type 'Linux' and of size 2 GiB. 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-32767999, default 4196352): <Enter> Last sector, +sectors or +size{K,M,G,T,P} (4196352-32767999, default 32767999): <Enter> Created a new partition 2 of type 'Linux' and of size 13.6 GiB. Command (m for help): p Disk /dev/sdb: 15.6 GiB, 16777216000 bytes, 32768000 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 Disklabel type: dos Disk identifier: 0x460247f0 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 4196351 4194304 2G 83 Linux /dev/sdb2 4196352 32767999 28571648 13.6G 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.