Understanding MBR and GPT Partitions
To effectively utilize a hard drive, partitioning is essential. Operating systems vary in their approach to partitioning: some consolidate everything into a single partition, whereas others, like Linux, typically utilize multiple partitions on a single disk. Employing multiple partitions offers several advantages:
- Data Organization: Facilitates better organization of different types of data.
- Mount Options: Allows specific mount options to optimize security and performance.
- Backup Strategy: Simplifies backup procedures by enabling selective backup of OS components.
- Fault Tolerance: Prevents system crashes if one partition fills up by maintaining usability of other partitions.
Note: Instead of multiple partitions, you can opt for LVM logical volumes or Stratis file systems. These advanced storage management topics are covered in Chapter 15, “Managing Advanced Storage.”
Understanding the MBR Partitioning Scheme
When personal computers emerged in the early 1980s, the Master Boot Record (MBR) partitioning scheme was developed to define hard disk layouts. During boot, the BIOS loaded to access hardware devices, then read the bootable disk device where the MBR was allocated. The MBR comprises essential components for system startup, including a boot loader and a partition table.
Initially, PCs supported various operating systems like MS-DOS/PC-DOS, PC/IX (IBM’s UNIX for 8086 PCs), CPM86, and MPM86, each allocated a portion of the disk via partitions. The MBR, limited to the first 512 bytes of a hard drive, contained an OS boot loader (e.g., GRUB 2) and a partition table, capable of defining up to four primary partitions due to its 64-byte partition table size and 32-bit partition size data.
To accommodate more than four partitions, an extended partition was introduced within the MBR, allowing creation of logical partitions. This extended the total number of addressable partitions to 15 for Linux kernels.
Understanding the Need for GPT Partitioning
Modern computer systems and their larger disk capacities outgrew MBR’s limitations, necessitating a new partitioning scheme: the GUID Partition Table (GPT). GPT partitions are essential for systems using the Unified Extensible Firmware Interface (UEFI) instead of BIOS. They offer significant improvements:
- Increased Partition Size: Supports up to 8 zebibytes (ZiB), equivalent to 1024 × 1024 × 1024 × 1024 gibibytes.
- Partition Limit: Allows creation of up to 128 partitions.
- No Size Limitation: Eliminates the 2 TiB partition size restriction.
- Simplified Partition Management: No distinction between primary, extended, or logical partitions.
- Reliability: Automatically creates a backup GUID partition table at the disk’s end, reducing single points of failure.
Understanding Storage Measurement Units
Storage capacity is measured using various units: megabyte (MB) and mebibyte (MiB). While MB denotes a decimal thousand, MiB represents a binary 1024. Modern Linux distributions standardize on binary units (MiB), ensuring consistency in storage reporting and calculation.
For historical context, older systems sometimes used mixed units, causing confusion between decimal and binary interpretations (e.g., “1.44 MB” floppy disks).
Managing Partitions and File Systems
RHEL supports both MBR and GPT partitioning schemes, each requiring specific utilities like fdisk
for MBR and gdisk
for GPT. These tools facilitate partition creation and management tailored to different partition types and disk devices.
By understanding these partitioning schemes and measurement units, you can effectively manage storage resources in a modern computing environment, ensuring compatibility and efficiency across various systems and hardware configurations.
Using Extended and Logical Partitions on MBR
In the previous section, you learned how to add primary partitions to an MBR disk. However, MBR supports only up to four primary partitions. To overcome this limitation and create more than four partitions, you can utilize extended and logical partitions.
Creating an Extended Partition
- Understanding Extended Partitions: If you’ve already allocated all four primary partitions on an MBR disk, you must create an extended partition to accommodate additional logical partitions.
- Advantages and Considerations:
- Flexibility: Allows creation of more than four partitions by housing logical partitions within it.
- Caution: All logical partitions reside within the extended partition. Any issue with the extended partition affects all logical partitions within it.
- Alternative Solutions:
- LVM: For scenarios requiring more than four distinct storage allocations, Logical Volume Management (LVM) offers a more flexible solution compared to logical partitions.
- GPT: If starting on a new disk, consider using the GPT partitioning scheme instead of MBR for modern features and enhanced capabilities.
Exercise: Creating Logical Partitions
To practice creating logical partitions within an extended partition, follow these steps:
- Open a root shell and type fdisk /dev/sdb to initiate the fdisk interface.
[root@server1 ~]# fdisk /dev/sdb
- Press n to create a new partition. Choose e when prompted for the partition type to create an extended partition.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e
- Accept default values for first and last sectors to allocate all remaining disk space to the extended partition.
Partition number (2-4, default 2):
First sector (2099200-41943039, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-41943039, default 41943039):
- Create logical partitions within the extended partition by pressing n again in the fdisk interface.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (2101248-41943039, default 2101248):
- Accept the default first sector and specify the size of the logical partition (e.g., +1G for 1 GiB).
First sector (2101248-41943039, default 2101248):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2101248-41943039, default 41943039): +1G
- Write the changes to disk and exit fdisk by typing w.
Command (m for help): w
Ensure to check and update the kernel partition table using partprobe if needed.
Creating GPT Partitions with gdisk
For disks larger than 2 TiB or when utilizing UEFI systems, GPT provides advanced features and compatibility. Use the gdisk utility to create GPT partitions.
Exercise: Creating GPT Partitions with gdisk
Follow these steps to create GPT partitions on a new disk (/dev/sdc):
- Open a root shell and type gdisk /dev/sdc to initialize the disk with GPT.
root@server1 ~]# gdisk /dev/sdc
- Press n to create a new partition, specify the partition number, and accept default values for first and last sectors to create a partition spanning the entire disk or specify a size (e.g., +1G).
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +1G
- Set the partition type if necessary and verify the partition details with p.
Command (? for help): p
- Write changes to disk by typing w and confirm with Y.
Command (? for help): w
- Update the kernel partition table using partprobe if needed to reflect the new partition.
These exercises provide hands-on experience with both MBR extended/logical partitions and GPT partitions using fdisk and gdisk utilities respectively.
Creating GPT Partitions with parted
In addition to fdisk and gdisk, the parted utility provides another option for creating partitions, albeit with fewer advanced features. Here’s a quick overview of how to work with parted:
To use parted, follow Exercise which guides you through the process of creating partitions. This exercise assumes you have an unused disk device, /dev/sdd, available.
Exercise: Creating Partitions with parted
- Open a root shell and type parted /dev/sdd to start the interactive parted shell.
- Type help to view available commands.
- Type print to view information about the unrecognized disk label.
- Enter mklabel and press Enter. When prompted for the disk label type, press Tab twice to see available options. Choose gpt from the list and press Enter.
- Type mkpart. Enter a partition name (e.g., part1).
- You’ll be prompted for a file system type. Although this doesn’t affect the partition’s file system, type xfs or another relevant type (e.g., ext4).
- Specify the start location. Use 1MiB or 1M to specify the start position (e.g., 1MiB).
- Specify the end of the partition (e.g., 1GiB). Type print to review the partition table.
- Type quit to exit parted and commit your changes.
- Use lsblk to verify the creation of /dev/sdd1.
- Format the partition with mkfs.ext4 /dev/sdd1.
Creating File Systems
Creating a partition is just the first step. To make it useful, you need to format it with a file system. Here are some commonly used file systems on RHEL 9:
- XFS: Default file system in RHEL 9.
- Ext4: Default file system in previous versions of RHEL, still supported in RHEL 9.
- Ext3: Previous version of Ext4, not needed on RHEL 9.
- Ext2: Basic file system from the early 1990s, not recommended for RHEL 9.
- BtrFS: Newer file system not supported in RHEL 9.
- NTFS: Windows-compatible file system not supported in RHEL 9.
- VFAT: Offers compatibility with Windows and macOS, functional equivalent of FAT32.
To format a partition with a specific file system, use mkfs command (e.g., mkfs.xfs for XFS). Example demonstrates formatting a file system with XFS.
Example: Formatting a File System with XFS
[root@server1 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Exercise: Creating a File System
In Exercise , you created /dev/sdb1. In this exercise, format it with an XFS file system using mkfs.xfs /dev/sdb1.
Changing File System Properties
When working with file systems, you may need to manage specific properties. For Ext4 file systems, use tune2fs:
- Use tune2fs -l to view file system properties. Example shows output with various properties.
- Use tune2fs -o to set default mount options. Use tune2fs -O to enable or disable file system features.
Example: Showing File System Properties with tune2fs -l
[root@server1 ~]# tune2fs -l /dev/sdd1
tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name: <none>
…
Managing XFS File System Properties
The XFS file system operates differently from others, necessitating a unique set of tools for property management. Unlike some file systems, XFS does not support setting attributes within its metadata directly. Instead, you can adjust specific properties using the xfs_admin command. For instance, to assign a label to the file system, use xfs_admin -L mylabel.
Adding Swap Partitions
On Linux servers, most partitions serve as regular file systems. Swap space, however, is crucial for managing memory efficiently. Typically allocated on a disk device—either as a partition or an LVM logical volume—swap space helps manage kernel memory by moving less frequently used memory pages to disk when physical RAM is scarce. Monitoring swap usage is essential to prevent performance issues.
Sometimes, increasing swap space becomes necessary. You can achieve this by creating a swap partition and formatting it accordingly. Exercise outlines this process:
Exercise: Creating a Swap Partition
- Open the disk in fdisk by typing fdisk /dev/sdb.
- Press n to create a new partition, specifying the start cylinder and size for a 1-GiB partition.
- Type t to change the partition type. Use swap (for fdisk) or 8200 (for gdisk).
- Save and exit by typing w.
- Format the partition as swap using mkswap, e.g., mkswap /dev/sdb6.
- Check current swap space with free -m. The newly created swap space won’t be active yet.
- Activate the new swap space using swapon, e.g., swapon /dev/sdb6.
- Verify the added swap space with free -m.
- Edit /etc/fstab to ensure the swap space is mounted automatically on reboot:
/dev/sdb6 none swap defaults 0 0
Adding Swap Files
When disk space is limited, creating a swap file provides an alternative solution. Although slightly slower than a dedicated swap partition, a swap file can meet immediate needs effectively. To create and use a swap file:
- Create a file with desired size using dd, e.g., dd if=/dev/zero of=/swapfile bs=1M count=100 for a 100-MiB file.
- Format the file as swap using mkswap, e.g., mkswap /swapfile.
- Activate the swap file with swapon, e.g., swapon /swapfile.
- Ensure automatic activation by adding the following line to /etc/fstab:
/swapfile none swap defaults 0 0
Mounting File Systems
Merely creating a partition and formatting it with a file system isn’t sufficient for use—it must also be mounted. Mounting a partition makes its contents accessible through a designated directory.
To manually mount a file system, use the mount command:
mount /dev/sdb5 /mnt
To unmount, use umount:
umount /dev/sdb5
Using Device Names, UUIDs, or Disk Labels
For stable mounting despite changing device names due to dynamic storage configurations, Linux uses universally unique IDs (UUIDs) or labels instead of device names. Every file system, including swap, has a UUID associated with it. Use blkid to find UUIDs:
blkid
To mount using a UUID or label:
mount UUID=”5d34b37c-5d32-4790-8364-d22a8b8f88db” /mnt
mount LABEL=”mylabel” /mnt
Using UUIDs ensures automated mounts remain reliable.
Automating File System Mounts Through /etc/fstab
Manually mounting file systems is not ideal for routine use. Once configurations are confirmed, automating mounts using /etc/fstab is recommended. This file defines how file systems should be mounted automatically. Example configurations are illustrated in the sample /etc/fstab file, shown below:
root@server1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Sep 1 12:06:40 2022
#
# Accessible filesystems are maintained under ‘/dev/disk/’.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run ‘systemctl daemon-reload’ to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=6c2b4028-1dcb-44cb-b5b7-c8e52352b06c /boot xfs defaults 0 0
/dev/mapper/rhel-swap none swap defaults 0 0
/dev/sr0 /repo iso9660 defaults 0 0
Each line in /etc/fstab consists of six fields, detailed in Table :
Table /etc/fstab Fields
- Device: The device to be mounted (device name, UUID, or label).
- Mount Point: Directory or kernel interface where the device mounts.
- File System: Type of file system.
- Mount Options: Specific options for mounting.
- Dump: Support for dump utility backups (usually set to 0).
- Automatic Check: Specifies if file system integrity should be checked during boot.
Understanding the mount command helps in comprehending the Device, Mount Point, and File System fields in /etc/fstab. Note that kernel interfaces are identifiable if their names lack a leading /.
The Mount Options field configures specific behaviors; if unspecified, it defaults to “defaults.” Numerous options, such as acl, user_xattr, ro, atime, and noexec, can be utilized for specific needs (refer to Table).
Table Common Mount Options
- auto / noauto: Automates or prevents automatic mounting.
- acl: Adds access control list support.
- user_xattr: Supports user-extended attributes.
- ro: Mounts read-only.
- atime / noatime: Modifies access time updates.
- noexec / exec: Allows or denies execution of files.
The fifth column of /etc/fstab pertains to the dump utility’s support, historically used for file system backups. In modern setups, this is typically set to 0.
The last column designates file system integrity checks during boot, with values 0 (no check), 1 (root file system check), or 2 (other file systems check).
After adding entries to /etc/fstab, it’s crucial to validate its syntax to avoid boot failures:
- findmnt –verify: Checks /etc/fstab syntax for errors.
- mount -a: Mounts all file systems specified in /etc/fstab that are not already mounted.
Exercise: demonstrates mounting partitions via /etc/fstab, like mounting the XFS-formatted partition /dev/sdb5:
Exercise: Mounting Partitions Through /etc/fstab
- Use blkid to obtain the UUID=”nnnn” for /dev/sdb5.
- Create a mount point with mkdir -p /mounts/data.
- Add the following line to /etc/fstab:
UUID=”nnnn” /mounts/data xfs defaults 0 0
- Test the configuration with mount -a before rebooting.
- Verify the mount with df -h.
Using Systemd Mounts
While /etc/fstab traditionally handles mounts, recent RHEL versions utilize it as input for systemd mounts. Generated files reside in /run/systemd/generator, exemplified by Example:
root@server1 ~]# cat /run/systemd/generator/repo.mount
# Automatically generated by systemd-fstab-generator
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
Before=local-fs.target
After=blockdev@dev-sr0.target
[Mount]
What=/dev/sr0
Where=/repo
Type=iso9660
Systemd mounts, created from /etc/fstab, can also be configured manually in /etc/systemd/system. Exercise outlines creating a systemd mount file for /dev/sdc1:
Exercise: Creating a Systemd Mount File
- Format /dev/sdc1 with Ext4 using mkfs.ext4 /dev/sdc1.
- Create a mount point with mkdir /exercise.
- Use vim /etc/systemd/system/exercise.mount with the following content:
[Unit]
Before=local-fs.target
[Mount]
What=/dev/sdc1
Where=/exercise
Type=ext4
[Install]
WantedBy=multi-user.target
- Enable and start the mount unit with systemctl enable –now exercise.mount.
- Verify the mount with mount | grep exercise.
- Check the unit file status with systemctl status exercise.mount.
This approach ensures efficient management of file system mounts using systemd, complementing the traditional /etc/fstab method.
This revised version aims to enhance clarity and conciseness while maintaining completeness and accuracy. Let me know if further adjustments are needed!
Exercise 1 Creating a Swap Partition
- Type fdisk /dev/sdb to open your disk in fdisk.
- Press n to add a new partition. Specify start cylinder and size to create a 1-GiB partition.
- Type t to change the partition type. If you are using fdisk, type swap to set the swap partition type to 82. If you are using gdisk, use partition type 8200. Press w to write and exit.
- Use mkswap to format the partition as swap space. Use, for instance, mkswap /dev/sdb6 if the partition you have just created is /dev/sdb6.
- Type free -m. You see the amount of swap space that is currently allocated. This does not include the swap space you have just created, as it still needs to be activated.
- Use swapon to switch on the newly allocated swap space. If, for instance, the swap device you have just created is /dev/sdb6, use swapon /dev/sdb6 to activate the swap space.
- Type free -m again. You see that the new swap space has been added to your server.
- Open the file /etc/fstab with an editor and, on the last line, add the following to ensure the swap space is also available after a reboot: /dev/sdb6 none swap defaults 0 0
Exercise 2 Mounting Partitions Through /etc/fstab
- From a root shell, type blkid. Use the mouse to copy the UUID=”nnnn” part for /dev/sdb5.
- Type mkdir -p /mounts/data to create a mount point for this partition.
- Open /etc/fstab in an editor and add the following line:
UUID=”nnnn” /mounts/data xfs defaults 0 0
- Before you attempt an automatic mount while rebooting, it is a good idea to test the configuration. Type mount -a. This command mounts everything that is specified in /etc/fstab and that has not been mounted already.
- Type df -h to verify that the partition has been mounted correctly.
Exercise 3 Creating a Systemd Mount File
- Use mkfs.ext4 /dev/sdc1 to format /dev/sdc1 with an Ext4 file system.
- Type mkdir /exercise to create the mount point.
- Use vim /etc/systemd/system/exercise.mount and give the file the following contents:
[Unit]
Before=local-fs.target
[Mount]
What=/dev/sdc1
Where=/exercise
Type=ext4
[Install]
WantedBy=multi-user.target
- Use systemctl enable –now exercise.mount to enable and start the mount unit.
- Type mount | grep exercise to verify the mount was created.
- Use systemctl status exercise.mount to verify the unit file.
These exercises are designed to provide practical hands-on experience with managing swap partitions, configuring /etc/fstab for automatic mounts, and setting up systemd mounts for file systems on a Linux server.