1 Using Disk Partitions
All storage devices, from hard disks to solid state drives to SD cards, must be partitioned to become usable. A device must have at least one partition, although you can create several partitions on any device.
Partitioning divides a disk drive into one or more reserved areas called partitions. Information about these partitions are stored in the partition table on the disk drive. The OS treats each partition as a separate disk that can contain a file system.
You create more partitions to simplify backups, enhance system security, and meet other needs, such as setting up development sandboxes and test areas. You can add partitions to store data that frequently changes, such as user home directories, databases, and log file directories.
Disk Partitions in Oracle Linux
Oracle Linux requires one partition for the root file system. Further, two other partitions are typically reserved for swap space and the boot file system. On x86 and x86_64 systems, the system BIOS can 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 GRUB bootloader to access the kernel image and other files that are required to boot the system.
For hard disks with a master boot record (MBR), the partitioning scheme supports up to 4 primary partitions. In turn, a primary partition can further be divided 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. The GPT partition scheme doesn't use the concept of extended or logical partitions. If the disk's size is larger than 2 TB, use GPT to configure the device's partitions.
Note:
When you partition a block storage device, align the 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 would 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, solid state drives (SSDs), LUNs on storage arrays, and host RAID adapters.
Partitioning Disks by Using fdisk
To create and manage hard disks that use MBRs, use the fdisk command. Alternatively, 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.32.1) 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.
Partitioning Disks by Using parted
To create and manage hard disks that use GPTs, use the parted command. The command enables you to perform typical partition operations as fdisk. However, parted is more advanced because it supports a larger set of commands and more disk label types including GPT disks.
Before running parted, 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.
You can use parted either interactively or directly with command line arguments. To run parted interactively, specify only the name of the disk device as an argument, for example:
sudo parted /dev/sdb
GNU Parted 3.2 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
The following commands are useful for managing partitions:
-
Displays the current partition table.
- mklabel
-
Creates a partition type according to the label you choose.
- mkpart
-
Starts the process for creating new partitions.
- quit
-
Exits the session.
Note:
In interactive sessions, changes are committed to disk immediately. Unlike fdisk, the parted utility doesn't have an option for quitting without saving changes.
- help
-
Displays all the supported commands in the interactive mode.
Creating Partitions
The following example shows how to use the different parted commands to create 2 disk partitions. The first partition is assigned 2 GB while the second partition uses all the remaining disk space.
sudo parted /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:
GNU Parted 3.2 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.8GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? <Enter> Start? 1 End? 2GB (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.8GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 2000MB 1999MB primary ext2 lba (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? <Enter> Start? 2001 End? -0 (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.8GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 2000MB 1999MB primary ext2 lba 2 2001MB 16.8GB 14.8GB primary ext2 lba (parted) quit
Note:
Unless you specify otherwise, the size for the Start
and
End
offsets is in megabytes. To use another unit of measure, type the
value and the unit together, for example, 2GB. To assign all remaining disk space to the
partition, enter -0
for the End
offset as shown in the
example.
Customizing Labels
By default, parted creates msdos
-labeled partitions.
When partitioning with this label, you're also prompted for the partition type. Partition
types can be primary
, extended
, or logical
.
To use a different label, you would need to specify that label first with the mklabel command before creating the partition. Depending on the label, you would be prompted during the partitioning process for information, such as the partition name, as shown in the following example:
sudo parted /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:
GNU Parted 3.2 Using /dev/sdb 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/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.8GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags (parted) mkpart Partition name? []? Example File system type? [ext2]? linux-swap Start? 1 End? 2GB (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 16.8GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2000MB 1999MB linux-swap(v1) Example (parted) quit
To know which types of file systems and labels are supported by
parted, consult the GNU Parted User Manual at https://www.gnu.org/software/parted/manual/, or
enter info parted to view the online user manual. For more information,
see the parted(8)
manual page.
Automatic Device Mappings for Partitions and File Systems
Device mappings are handled automatically on Oracle Linux, by the kernel
and the udev service. In the case of disk devices, the kernel automatically
creates device mappings for disks and their partitions using a non
persistent naming scheme in the form of
/dev/sdxy
,
for example /dev/sda1
. A problem with this approach is that
the device naming scheme depends on the order in which devices are detected,
which means that device names can change easily. Typically, device names
change when changes to the boot process exist or when storage devices or
their related controllers fail.
Avoid using the non persistent disk or partition names when configuring mount points in
/etc/fstab
.
Udev
is a subsystem that works with the kernel to monitor hardware or device
changes and manages events related to changes. Storage devices, partitions,
and file systems are all allocated unique identifiers that the
udev
subsystem can read and use to automatically
configure device mappings that you can use to identify the device or
partition that you intend to work use. Device mappings for storage devices
managed by udev are stored in /dev/disks
and you can
identify a device by various identifiers, including the unique partition
UUID, file system UUID, or the partition label.
When configuring mount points in /etc/fstab
, use the file system UUID or, if
set, the partition label, for example:
UUID=8980b45b-a2ce-4df6-93d8-d1e72f3664a0 /boot xfs defaults 0 0 LABEL=home /home xfs defaults 0 0
Note that because file systems can span multiple devices, defining mount points against the file system UUID is preferable to using a partition UUID or label. File system UUIDs are assigned when the file system is created and is stored as part of the file system itself. If you copy the file system to a different device, the file system retains the same file system UUID. However, if you reformat a device by using the mkfs command, the device loses the file system UUID and a new UUID is generated.
Listing Device Mapping Information
You can use the lsblk command to list device information for any block device attached to the system:
Tip:
Use the-o +UUID
option to display the UUIDs for each device
and partition listed, or use the -f
option to get a display of important file
system information.
lsblk -f
Output might appear as follows, indicating a tree of file system mappings:
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT16 DDD4-C455 94.5M 5% /boot/efi ├─sda2 xfs 8980b45b-a2ce-4df6-93d8-d1e72f3664a0 1.5G 26% /boot └─sda3 LVM2_member LVM2 001 LzsEPR-Mnbk-kQZY-eV3n-9u1T-lFXZ-x7ANgD ├─ocivolume-root xfs 26029274-0a04-4dc5-b794-2576b9a16884 28.6G 25% / └─ocivolume-oled xfs 3cbc8301-6f0e-4947-bbe6-f3669b9e6985 9.9G 1% /var/oled
You can also use the udevadm info command to obtain information about any udev mappings on the system. For example:
sudo udevadm info /dev/sda3
Output might appear as follows, listing all the device links that udev
has
created for the device and any other information that udev
has on the
device:
P: /devices/pci0000:00/0000:00:04.0/virtio1/host2/target2:0:0/2:0:0:1/block/sda/sda3 N: sda3 L: 0 S: disk/by-partuuid/18b918a1-16ca-4a07-91c6-455c6dc59fac S: oracleoci/oraclevda3 S: disk/by-id/wwn-0x60170f5736a64bd7accb6a5e66fe70ee-part3 S: disk/by-path/pci-0000:00:04.0-scsi-0:0:0:1-part3 S: disk/by-id/scsi-360170f5736a64bd7accb6a5e66fe70ee-part3 S: disk/by-id/lvm-pv-uuid-LzsEPR-Mnbk-kQZY-eV3n-9u1T-lFXZ-x7ANgD E: DEVPATH=/devices/pci0000:00/0000:00:04.0/virtio1/host2/target2:0:0/2:0:0:1/block/sda/sda3 E: DEVNAME=/dev/sda3 E: DEVTYPE=partition E: DISKSEQ=9 E: PARTN=3 E: MAJOR=8 E: MINOR=3 E: SUBSYSTEM=block E: USEC_INITIALIZED=20248855 E: ID_SCSI=1 E: ID_VENDOR=ORACLE E: ID_VENDOR_ENC=ORACLE\x20\x20 E: ID_MODEL=BlockVolume E: ID_MODEL_ENC=BlockVolume\x20\x20\x20\x20\x20 E: ID_REVISION=1.0 E: ID_TYPE=disk E: ID_SERIAL=360170f5736a64bd7accb6a5e66fe70ee E: ID_SERIAL_SHORT=60170f5736a64bd7accb6a5e66fe70ee E: ID_WWN=0x60170f5736a64bd7 E: ID_WWN_VENDOR_EXTENSION=0xaccb6a5e66fe70ee E: ID_WWN_WITH_EXTENSION=0x60170f5736a64bd7accb6a5e66fe70ee E: ID_BUS=scsi E: ID_PATH=pci-0000:00:04.0-scsi-0:0:0:1 E: ID_PATH_TAG=pci-0000_00_04_0-scsi-0_0_0_1 E: ID_PART_TABLE_UUID=a0b1f7d8-e84b-461f-a016-c3fcfed369c3 E: ID_PART_TABLE_TYPE=gpt E: ID_SCSI_INQUIRY=1 E: ID_FS_UUID=LzsEPR-Mnbk-kQZY-eV3n-9u1T-lFXZ-x7ANgD E: ID_FS_UUID_ENC=LzsEPR-Mnbk-kQZY-eV3n-9u1T-lFXZ-x7ANgD E: ID_FS_VERSION=LVM2 001 E: ID_FS_TYPE=LVM2_member E: ID_FS_USAGE=raid E: ID_PART_ENTRY_SCHEME=gpt E: ID_PART_ENTRY_UUID=18b918a1-16ca-4a07-91c6-455c6dc59fac E: ID_PART_ENTRY_TYPE=e6d6d379-f507-44c2-a23c-238f2a3df928 E: ID_PART_ENTRY_NUMBER=3 E: ID_PART_ENTRY_OFFSET=4401152 E: ID_PART_ENTRY_SIZE=100456415 E: ID_PART_ENTRY_DISK=8:0 E: SCSI_TPGS=0 E: SCSI_TYPE=disk E: SCSI_VENDOR=ORACLE E: SCSI_VENDOR_ENC=ORACLE\x20\x20 E: SCSI_MODEL=BlockVolume E: SCSI_MODEL_ENC=BlockVolume\x20\x20\x20\x20\x20 E: SCSI_REVISION=1.0 E: SCSI_IDENT_LUN_NAA_REGEXT=60170f5736a64bd7accb6a5e66fe70ee E: UDISKS_IGNORE=1 E: DEVLINKS=/dev/disk/by-partuuid/18b918a1-16ca-4a07-91c6-455c6dc59fac /dev/oracleoci/oraclevda3 /dev/disk/by-id/wwn-0x60170f5736a64bd7accb6a5e66fe70ee-part3 /dev/disk/by-path/pci-0000:00:04.0-scsi-0:0:0:1-part3 /dev/disk/by-id/scsi-360170f5736a64bd7accb6a5e66fe70ee-part3 /dev/disk/by-id/lvm-pv-uuid-LzsEPR-Mnbk-kQZY-eV3n-9u1T-lFXZ-x7ANgD E: TAGS=:systemd: E: CURRENT_TAGS=:systemd:
Manually Mapping Partition Tables to Devices
The kpartx utility maps to device files the
partitions of any block device or file that contains a
partition table. The command reads the partition table,
creates device files for the partitions, and stores the device
files in /dev/mapper
. Each device file
represents a disk volume or a disk partition on a device or
within an image file.
For more information, see the kpartx(8)
manual page.
Creating Device Mappings by Using kpartx
The -a option creates the device mappings. The following example uses the disk partitions that were created in Creating Partitions as basis for creating the mapping. The example begins by showing the partition table:
-
Display the partition table.
sudo fdisk -l /dev/sdb
... Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 3907583 3905536 1.9G 83 Linux /dev/sdb2 3907584 32767999 28860416 13.8G 83 Linux
-
Map the partitions.
sudo kpartx -av /dev/sdb
add map sdb1 (253:2): 0 3905536 linear 8:16 2048 add map sdb2 (253:3): 0 28860416 linear 8:16 3907584
-
Display
/dev/mapper
contents.ls /dev/mapper
control sdb1 sdb2 vg_main-lv_root vg_main-lv_swap
Listing Partition Mappings For a Device by Using kpartx
To list the partitions in the device, use the -l option.
In the following example, the first column of the output identifies the device files in
/dev/mapper
.
sudo kpartx -l /dev/sdb
sdb1 : 0 3905536 /dev/sdb 2048 sdb2 : 0 28860416 /dev/sdb 3907584
The kpartx command also works with image files such as an
installation image. For example, for an image file system.img
, you can
do the following:
sudo kpartx -a system.img
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
The output of the previous command shows that the drive image contains four partitions.