Partition

From LQWiki
Jump to navigation Jump to search

A partition is a logical section of a hard disk and a practical way for users to divide their disks up into parts that behave as independent, separate devices, which in reality reside on one hard disk. A partition is technically defined in the partition table of a hard disk.

Why partition?

Partitioning has several advantages and disadvantages.

Multiple-boot

Most practically, partitioning allows you to set up different operating systems on a single disk. If you have successfully installed any multiple-boot system, say, with Linux, partitioning allows the different operating systems to coexist and not interfere.

Managing hard disk space

Partitioning also alleviates the problem of managing hard disk space, especially within a multi-user system. For example, when a multi-user system is set up, the home directories and the system directories often reside on different partitions. Without partitioning the disk in this way, or without setting up adequate disk quota systems, a user can fill up the partition and render the system unusable. Separating respective directories by usage into partitions can assist in alleviating space issues.

Problems with partitioning

Partitioning a disk is a rather low-level operation, and if it fails, valuable data can get lost. Performing partitioning requires a high grade of technical proficiency and accuracy without the use of the right tools.

Partition Topology

Types of partitions

Traditional DOS/MBR disks contain three types of partitions: "Primary", "Extended" and "Logical" partitions. The basic types are Primary and Logical; Extended partitions are actually a special case of a Primary partition. A DOS disk can have a maximum of 4 primary partitions, of which one (but not more than than one) can be an extended partition. A number of logical partitions (also called "Logical Disks") can be defined within the extended partition, which acts purely as a container for them. Primary and logical partitions are addressable for disk space and can therefore contain data, whereas an extended partition has only a nominal entry in the partition table, representing the set of logical partitions which it contains.

DOS and Windows make a distinction between primary and logical partitions. Only a primary partition can be booted, and consequently the Windows operating system must reside on a primary partition. Logical disks are used only for user data. Linux makes no such distinction, and Linux bootloaders such as GRUB will boot to any type of partition that can contain a filesystem. Another difference is that Windows requires the partition containing the OS to carry a special "boot" flag. Linux does not require this flag and Linux bootloaders ignore it.

Modern GPT disks can have a considerable number of primary partitions and therefore do not need to use extended partitions or logical disks at all.

Namespace

Windows uses drive letters for all addressable partitions, starting from "C" (C,D,E,...).This is because "A" and "B" were originally used for floppy disks. Traditionally Linux referred to IDE hard drives according to their positions on the motherboard: the master and slave drives on the first controller were hda and hdb; those on the second controller were hdc and hdd. The 4 primary partitions were then referred to by numbers, for example hda1, hda2, hda3 and hda4. If there was an extended partition, the numbers of the logical partitions inside it always started with 5. If SCSI drives were attached, they were referred to as sda, etc., the letters being assigned in the order in which the kernel detected the drives.

Modern kernels no longer distinguish between these two types of drive but treat all hard drives as scsi. Consequently all modern drive designations start with "sd". A program called udev communicates with the kernel and renames drives as they are detected according to stored rules, so that nomenclature is consistent over boots as it was under the traditional system. Because modern hard drives are quite large, there is now usually only a single hard drive in a computer and this will be called sda. If it is a DOS drive, its partitions will be numbered sda1 to sda4, with logical disks starting at sda5. If it is a GPT drive, all the partitions will be numbered continuously in the order in which they appear in the partition table.

Limitations

In Windows the total number of partitions is limited by the 26 letters of the alphabet. Linux permits 63 partitions in an IDE disk but only 15 in an SCSI or Sata disk. Some operating system, like Windows, can only be booted from a primary partition, Linux can be booted from either a primary or a logical partition.

Practical

How do I create partitions?

On Linux systems, partitions are managed by programs such as fdisk or cfdisk. Only unmounted disks should be partitioned. It is very dangerous to repartition a running system, and any changes in partitioning can render the data on the affected areas of the disk unusable. You will almost never have to partition unless you are installing a new operating system, or a new hard drive.

What partitions do I need?

Read the Partitioning Guidelines for suggestions on how to divide up your available disk space.

Older versions of LILO required that the boot information for the kernel reside on a partition near the "front" of the disk. Newer versions have eliminated this requirement.

How do I see the partitions on my computer?

You can look at the partition table of the device /dev/hda with:

fdisk -l /dev/hda

You can see all partitions (of all devices) known by the kernel with

hwinfo --partition

You can print the filesystem types in use with:

df -hT

Find out a partition's size

To find out a partition's size in bytes, use fdisk. Using fdisk on a partition will not print you a partition table, obviously, but it will print you the partition's size like this:

fdisk -l /dev/sdc1

Disk /dev/sdc1: 80.0 GB, 80023716864 bytes
255 heads, 63 sectors/track, 9728 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

     Device Boot      Start         End      Blocks   Id  System

In this example, we find out /dev/sdc1 has a size of 80.0 GB.

Sizes expressed in bytes are often difficult to appreciate. Using fdisk -lh /dev/sdc1 will give you the size in "humanly appropriate" units such as megabytes or gigabytes, depending on the size.

See also