1 Configuring Storage Devices

This chapter describes how to configure and manage disk partitions and swap spaces.

For additional information about Oracle Linux products featuring storage and storage management, refer to the following guides:

Working With Disk Partitions

Partitioning a disk drive involves dividing it into one or more reserved areas, called partitions, and then 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. Typically, two other partitions are used 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 enables the GRand Unified Bootloader (GRUB) to access the kernel image and any other files that are required to boot the system.

You can create additional partitions to do the following: simplify backups, enhance system security, as well as 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) enables 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 Master Boot Record (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 the parted command.

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), logical unit numbers (LUNs) on storage arrays, and host RAID adapters.

Managing Partition Tables With the fdisk Utility

Caution:

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

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

You cannot use the fdisk utility to manage a GPT hard disk.

Use the fdisk utility to do the following:

  • Create a partition table.

  • View an existing partition table.

  • Add and delete partitions.

Alternatively, you can use the cfdisk utility, which is a text-based, graphical version of the fdisk utility.

You can use the fdisk utility 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:

sudo 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):

Based on the strong recommendation in the output message, type c at the Command prompt to switch off DOS-compatibility mode, then u to use sectors, and the p command 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 previous 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 most likely 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 displays the following information:

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:

sudo 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 enables 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 type q instead, fdisk exits without committing the changes to disk.

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

Managing Partition Tables With the parted Program

Caution:

If any partition on the disk to be configured by using the parted program 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.

The parted program is more advanced than the fdisk utility, as it supports more disk label types, including GPT disks, and it implements a larger set of commands.

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

You can use the parted program to do the following:

  • Label a disk.

  • Create a partition table.

  • View an existing partition table.

  • Add, modify, and delete partitions.

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

sudo 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
(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 the following: 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 as the file system.

Unless you specify units such as GB for gigabytes, the start and end offsets of a partition are assumed to be in megabytes.

To specify the end of the disk for End, enter a value of -0.

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 the parted program, type quit.

Note:

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

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

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:

sudo 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:

sudo kpartx -a system.img
sudo 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:

sudo mkdir /mnt/sysimage
sudo mount /dev/mapper/loop0p1 /mnt/sysimage
sudo 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
...
sudo umount /mnt/sysimage

The -d option removes the device mappings:

sudo kpartx -d system.img
sudo ls /dev/mapper
control

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

About Swap Space

Oracle Linux uses swap space when your system does not have enough physical memory to store the text (code) and data pages that the processes are currently using. When your system needs more memory, it writes inactive pages to swap space on disk, freeing up physical memory. However, writing to swap space has a negative impact on system performance, so increasing swap space is not an effective solution to shortage of memory. Swap space is located on disk drives, which have much slower access times than physical memory. If your system often resorts to swapping, you should add more physical memory, not more swap space.

You can configure swap space on a swap file in a file system or on a separate swap partition. A dedicated swap partition is faster, but changing the size of a swap file is easier. Configure a swap partition if you know how much swap space your system requires. Otherwise, start with a swap file and create a swap partition when you know what your system requires.

Viewing Swap Space Usage

To view a system's usage of swap space, examine the contents of /proc/swaps:

cat /proc/swaps
Filename                Type        Size      Used   Priority
/dev/sda2               partition   4128760   388    -1
/swapfile               file        999992    0      -2

In this example, the system is using both a 4-gigabyte swap partition on /dev/sda2 and a one-gigabyte swap file, /swapfile. The Priority column shows that the system preferentially swaps to the swap partition rather than to the swap file.

You can also view /proc/meminfo or use utilities such as free, top, and vmstat to view swap space usage, for example:

grep Swap /proc/meminfo
SwapCached:          248 kB
SwapTotal:       5128752 kB
SwapFree:        5128364 kB
sudo free | grep Swap
Swap:      5128752        388    5128364

Creating and Using a Swap File

Note:

Configuring a swap file on a btrfs file system is not supported.

To create and use a swap file:

  1. Use the dd command to create a file of the required size (for example, one million one-kilobyte blocks):

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1000000
  2. Initialize the file as a swap file:

    sudo mkswap /swapfile
  3. Enable swapping to the swap file:

    sudo swapon /swapfile
  4. Add an entry to /etc/fstab for the swap file so that the system uses it following the next reboot:

    /swapfile       swap       swap       defaults       0 0

Creating and Using a Swap Partition

To create and use a swap partition:

  1. Use fdisk to create a disk partition of type 82 (Linux swap) or parted to create a disk partition of type linux-swap of the size that you require.

  2. Initialize the partition (for example, /dev/sda2) as a swap partition:

    sudo mkswap /dev/sda2
  3. Enable swapping to the swap partition:

    sudo swapon /swapfile
  4. Add an entry to /etc/fstab for the swap partition so that the system uses it following the next reboot:

    /dev/sda2       swap       swap       defaults       0 0

Removing a Swap File or Swap Partition

To remove a swap file or swap partition from use:

  1. Disable swapping to the swap file or swap partition, for example:

    sudo swapoff /swapfile
  2. Remove the entry for the swap file or swap partition from /etc/fstab.

  3. Optionally, remove the swap file or swap partition if you do not want to use it in future.