On Linux operating systems, the cfdisk command manipulates or displays information about the disk partition table. It incorporates all the functionality of fdisk, but with a text-based “graphical” interface.

Description

cfdisk is a program for partitioning any hard drive. Typical values of the device argument are:

  • Description

  • Syntax

  • Examples

  • Related commands

  • Linux commands help

  • /dev/hda [default]

  • /dev/hdb

  • /dev/sda

  • /dev/sdb

  • /dev/sdc

  • /dev/sdd

To write the partition table, cfdisk needs something called the “geometry” of the disk: the number of “heads” and the number of “sectors per track”. Linux does not use any geometry, so if the disk is not accessed by other operating systems, you can safely accept the defaults that cfdisk chooses for you. The geometry used by cfdisk is found as follows: first the partition table is examined, to see what geometry was used by the previous program that changed it. If the partition table is empty, or contains garbage, or does not point at a consistent geometry, the kernel is asked for advice. If nothing works, 255 heads and 63 sectors/track is assumed. The geometry can be overridden on the command line or by use of the “g” command. When partitioning an empty large modern disk, picking 255 heads and 63 sectors/track is always a good idea. There is no need to set the number of cylinders since cfdisk knows the disk size.

Next, cfdisk tries to read the current partition table from the disk drive. If it cannot figure out the partition table, an error is displayed and the program will exit. This might also be caused by incorrect geometry information, and can be overridden on the command line. Another way around this problem is with the -z option. This option ignores the partition table on the disk.

The main display is composed of four sections, from top to bottom: the header, the partitions, the command line and a warning line. The header contains the program name and version number followed by the disk drive and its geometry. The partitions section always displays the current partition table. The command line is the place where commands and text are entered. The available commands are usually displayed in brackets. The warning line is usually empty except when there is important information to show. The current partition is highlighted with reverse video (or an arrow if the -a option is given). All partition specific commands apply to the current partition.

The format of the partition table in the partitions section is, from left to right: Name, Flags, Partition Type, Filesystem Type and Size. The name is the partition device name. The flags can be Boot, which designates a bootable partition, or NC, which stands for “Not Compatible with DOS or OS/2”. DOS, OS/2, and possibly other operating systems require the first sector of the first partition on the disk and all logical partitions to begin on the second head. This wastes the second through the last sector of the first track of the first head (the first sector is taken by the partition table itself). cfdisk lets you recover these “lost” sectors with the maximize command (m).

A warning about DOS 6.x

The DOS 6.x FORMAT command looks for some information in the first sector of the data area of the partition, and treats this information as more reliable than the information in the partition table. DOS FORMAT expects DOS FDISK to clear the first 512 bytes of the data area of a partition whenever a size change occurs. DOS FORMAT looks at this extra information even if the /U flag is given; this is considered a bug in DOS FORMAT and DOS FDISK.

fdisk and some early versions of DOS create all partitions with the number of sectors already maximized. For more information, see the maximize command below. The partition type can be one of Primary or Logical. For unallocated space on the drive, the partition type can also be Pri/Log, or empty (if the space is unusable). The filesystem type section displays the name of the filesystem used on the partition, if known. If it is unknown, then Unknown and the hex value of the filesystem type are displayed. A special case occurs when there are sections of the disk drive that cannot be used (because all the primary partitions are used). When this is detected, the filesystem type is displayed as Unusable. The size field displays the size of the partition in megabytes (by default). It can also display the size in sectors and cylinders (see the change units command below). If an asterisk (*) appears after the size, this indicates the partition is not aligned on cylinder boundaries.

The bottom line is that if you use cfdisk or fdisk to change the size of a DOS partition table entry, then you must also use dd to zero the first 512 bytes of that partition before using DOS FORMAT to format the partition. For example, if you were using cfdisk to make a DOS partition table entry for /dev/hda1, then (after exiting fdisk or cfdisk and rebooting Linux so that the partition table information is valid) you would use the command

dd if=/dev/zero of=/dev/hda1 bs=512 count=1

…to zero the first 512 bytes of the partition. Note:

For best results, always use an OS-specific partition table program. For example, make DOS partitions with the DOS FDISK program and Linux partitions with the Linux fdisk or Linux cfdisk program.

When using the dd command, a small typo can make all the data on your disk useless.

Syntax

cfdisk [-agvz] [-c cylinders] [-h heads] [-s sectors-per-track] [-P opt] [device]

Commands

cfdisk commands can be entered by pressing the desired key (pressing Enter after the command is not necessary). Here is a list of the available commands:

All the commands can be entered with either upper or lowercase letters (except for Writes). When in a sub-menu or at a prompt to enter a file name, you can press Esc to return to the main command line.

  • There are only four slots available for primary partitions. Since there can be only one extended partition, which contains all the logical drives, all the logical drives must be contiguous (with no intervening primary partition).

The raw data format prints the sectors that would be written to disk if a write command is selected. First, the primary partition table is printed, followed by the partition tables associated with each logical partition. The data is printed in hex byte by byte with 16 bytes per line.

The partition table in sector order format prints the partition table ordered by sector number. The fields, from left to right, are the number of the partition, the partition type, the first sector, the last sector, the offset from the first sector of the partition to the start of the data, the length of the partition, the filesystem type (with the hex value in parenthesis), and the flags (with the hex value in parenthesis). In addition to the primary and logical partitions, free and unusable space is printed and the extended partition is printed before the first logical partition.

If a partition does not start or end on a cylinder boundary or if the partition length is not divisible by the cylinder size, an asterisk (*) is printed after the non-aligned sector number/count. This usually indicates that a partition was created by an operating system that either does not align partitions to cylinder boundaries or that used different disk geometry information. If you know the disk geometry of the other operating system, you could enter the geometry information with the change geometry command (g).

For the first partition on the disk and for all logical partitions, if the offset from the beginning of the partition is not equal to the number of sectors per track (i.e., the data does not start on the first head), a number sign (#) is printed after the offset. For the remaining partitions, if the offset is not zero, a number sign will be printed after the offset. This corresponds to the NC flag in the partitions section of the main display.

The partition table in raw format prints the partition table ordered by partition number. It will leave out all free and unusable space. The fields, from left to right, are the number of the partition, the flags (in hex), the starting head, sector, and cylinder, the filesystem ID (in hex), the ending head, sector, and cylinder, the starting sector in the partition and the number of sectors in the partition. The information in this table can be directly translated to the raw data format.

The partition table entries only have 10 bits available to represent the starting and ending cylinders. Thus, when the absolute starting (ending) sector number is on a cylinder greater than 1023, the maximal values for starting (ending) head, sector, and cylinder are printed. This is the method used by OS/2, and thus fixes the problems associated with OS/2’s fdisk rewriting the partition table when it is not in this format. Since Linux and OS/2 use absolute sector counts, the values in the starting and ending head, sector, and cylinder are not used.

Command-line options

Examples

sudo cfdisk

Run cfdisk, using the sudo command to execute the command with root privileges.

fdisk — A disk partitioning utility.mkfs — Build a Linux file system, usually a hard drive partition.