18.1 About Disk Partitions

18.1.1 Managing Partition Tables Using fdisk
18.1.2 Managing Partition Tables Using parted
18.1.3 Mapping Partition Tables to Devices

Partitioning a disk drive divides it into one or more reserved areas (partitions) and stores information about these partitions in the partition table on the disk The operating system treats each partition as a separate disk that can contain a file system.

Oracle Linux requires one partition for the root file system. It is usual to use two other partitions for swap space and the boot file system. On x86 and x86_64 systems, the system BIOS can usually access only the first 1024 cylinders of the disk at boot time. Configuring a separate boot partition in this region on the disk allows the GRUB bootloader to access the kernel image and other files that are required to boot the system.

You can create additional partitions to simplify backups, to enhance system security, and to meet other needs, such as setting up development sandboxes and test areas. Data that frequently changes, such as user home directories, databases, and log file directories, is typically assigned to separate partitions to facilitate backups.

The partitioning scheme for hard disks with a master boot record (MBR) allows you to create up to four primary partitions. If you need more than four partitions, you can divide one of the primary partitions into up to 11 logical partitions. The primary partition that contains the logical partitions is known as an extended partition. The MBR scheme supports disks up to 2 TB in size.

On hard disks with a GUID Partition Table (GPT), you can configure up to 128 partitions and there is no concept of extended or logical partitions. You should configure a GPT if the disk is larger than 2 TB.

You can create and manage MBRs by using the fdisk command. If you want to create a GPT, use parted instead.

Note

When partitioning a block storage device, align primary and logical partitions on one-megabyte (1048576 bytes) boundaries. If partitions, file system blocks, or RAID stripes are incorrectly aligned and overlap the boundaries of the underlying storage's sectors or pages, the device controller has to modify twice as many sectors or pages than if correct alignment is used. This recommendation applies to most block storage devices, including hard disk drives (spinning rust), solid state drives (SSDs), LUNs on storage arrays, and host RAID adapters.

18.1.1 Managing Partition Tables Using fdisk

Caution

If any partition on the disk to be configured using fdisk is currently mounted, unmount it before running fdisk on the disk. Similarly, if any partition is being used as swap space, use the swapoff command to disable the partition.

Before running fdisk on a disk that contains data, first back up the data on to another disk or medium.

You cannot use fdisk to manage a GPT hard disk.

You can use the fdisk utility to create a partition table, view an existing partition table, add partitions, and delete partitions. Alternatively, you can also use the cfdisk utility, which is a text-based, graphical version of fdisk.

You can use fdisk interactively or you can use command-line options and arguments to specify partitions. When you run fdisk interactively, you specify only the name of the disk device as an argument, for example:

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):

If you disable DOS-compatibility mode, fdisk aligns partitions on one-megabyte boundaries. It is recommended that you turn off DOS-compatibility mode and use display units of 512-byte sectors by specifying the -c and -u options or by entering the c and u commands.

Enter c to switch off DOS-compatibility mode, u to use sectors, and p to display the partition table:

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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 identifier: 0x0002a95d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    83886079    41430016   8e  Linux LVM

The example output shows that /dev/sda is a 42.9 GB disk. As modern hard disks support logical block addressing (LBA), any information about the numbers of heads and sectors per track is irrelevant and probably fictitious. The start and end offsets of each partition from the beginning of the disk are shown in units of sectors. The partition table is displayed after the device summary, and shows:

Device

The device that corresponds to the partition.

Boot

Specifies * if the partition contains the files that the GRUB bootloader needs to boot the system. Only one partition can be bootable.

Start and End

The start and end offsets in sectors. All partitions are aligned on one-megabyte boundaries.

Blocks

The size of the partition in one-kilobyte blocks.

Id and System

The partition type. The following partition types are typically used with Oracle Linux:

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 is not managed by LVM. This is the default partition type.

8e Linux LVM

Linux partition that is managed by LVM.

The n command creates a new partition. For example, to create partition table entries for two Linux partitions on /dev/sdc, one of which is 5 GB in size and the other occupies the remainder of the disk:

# fdisk -cu /dev/sdc
...
Command (m for help): n
Command action
    e    extended
    p    primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-25165823, default 2048):  2048
Last sector, +sectors or +size{K,M,G} (2048-25165823, default 25165823): +5G

Command (m for help): n
Command action
    e    extended
    p    primary partition (1-4)
p
Partition number (1-4): 2
First sector (10487808-25165823, default 10487808): <Enter>
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-25165823, default 25165823): <Enter>
Using default value 25165823

Command (m for help): p

Disk /dev/sdc: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders, total 25165824 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 identifier: 0xe6d3c9f6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10487807     5242880   83  Linux
/dev/sdc2        10487808    25165823     7339008   83  Linux

The t command allows you to change the type of a partition. For example, to change the partition type of partition 2 to Linux LVM:

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e

Command (m for help): p
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10487807     5242880   83  Linux
/dev/sdc2        10487808    25165823     7339008   8e  Linux LVM

After creating the new partition table, use the w command to write the table to the disk and exit fdisk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

If you enter q instead, fdisk exits without committing the changes to disk.

For more information, see the cfdisk(8) and fdisk(8) manual pages.

18.1.2 Managing Partition Tables Using parted

Caution

If any partition on the disk to be configured using parted is currently mounted, unmount it before running parted on the disk. Similarly, if any partition is being used as swap space, use the swapoff command to disable the partition.

Before running parted on a disk that contains data, first back up the data on to another disk or medium.

You can use the parted utility to label a disk, create a partition table, view an existing partition table, add partitions, change the size of partitions, and delete partitions. parted is more advanced than fdisk as it supports more disk label types, including GPT disks, and it implements a larger set of commands.

You can use parted interactively or you can specify commands as arguments. When you run parted interactively, you specify only the name of the disk device as an argument, for example:

# parted /dev/sda
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

The print command displays the partition table:

(parted) print                                                            
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  ext4         boot
 2      525MB   42.9GB  42.4GB  primary               lvm

The mklabel command creates a new partition table:

# parted /dev/sdd
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/sdd will be destroyed
and all data on this disk will be lost. Do you want to continue?
Yes/No? y

Typically, you would set the disk label type to gpt or msdos for an Oracle Linux system, depending on whether the disk device supports GPT. You are prompted to confirm that you want to overwrite the existing disk label.

The mkpart command creates a new partition:

(parted) mkpart
Partition name? []? <Enter>
File system type? [ext2]? ext4
Start? 1
End? 5GB

For disks with an msdos label, you are also prompted to enter the partition type, which can be primary, extended, or logical. The file system type is typically set to one of fat16, fat32, ext4, or linux-swap for an Oracle Linux system. If you are going to create an btrfs, ext*, ocfs2, or xfs file system on the partition, specify ext4. Unless you specify units such as GB for gigabytes, the start and end offsets of a partition are assumed to be in megabytes.

To display the new partition, use the print command:

(parted) print
Number   Start   End     Size    File system  Name  Flags
1        1049kB  5000MB  4999MB  ext4

To exit parted, enter quit.

Note

parted commands such as mklabel and mkpart commit the changes to disk immediately. Unlike fdisk, you do not have the option of quitting without saving your changes.

For more information, see the parted(8) manual page or enter info parted to view the online user manual.

18.1.3 Mapping Partition Tables to Devices

You can use the kpartx utility to map the partitions of any block device or file that contains a partition table and partition images. kpartx reads the partition table and creates device files for the partitions in /dev/mapper. Each device file represents a disk volume or a disk partition on a device or within an image file.

The -l option lists any partitions that it finds, for example in an installation image file:

# kpartx –l system.img
loop0p1 : 0 204800 /dev/loop0 2048
loop0p2 : 0 12288000 /dev/loop0 206848
loop0p3 : 0 4096000 /dev/loop0 212494848
loop0p4 : 0 2 /dev/loop0 16590848

This output shows that the drive image contains four partitions, and the first column are the names of the device files that can be created in /dev/mapper.

The -a option creates the device mappings:

# kpartx –a system.img
# ls /dev/mapper
control  loop0p1  loop0p2  loop0p3  loop0p4

If a partition contains a file system, you can mount it and view the files that it contains, for example:

# mkdir /mnt/sysimage
# mount /dev/mapper/loop0p1 /mnt/sysimage
# ls /mnt/sysimage
config-2.6.32-220.el6.x86_64
config-2.6.32-300.3.1.el6uek.x86_64
efi
grub
initramfs-2.6.32-220.el6.x86_64.img
initramfs-2.6.32-300.3.1.el6uek.x86_64.img
...
# umount /mnt/sysimage

The -d option removes the device mappings:

# kpartx –d system.img
# ls /dev/mapper
control

For more information, see the kpartx(8) manual page.