Chapter 14. Managing Storage

Image

The following topics are covered in this chapter:

The following RHCSA exam objectives are covered in this chapter:

  • List, create, delete partitions on MBR and GPT disks

  • Configure systems to mount file systems at boot by universally unique ID (UUID) or label

  • Add new partitions and logical volumes, and swap to a system nondestructively

  • Create, mount, unmount, and use vfat, ext4, and xfs file systems

Working with storage is an important task for a Linux administrator. In this chapter, you acquire the first set of essential storage skills. You learn how to create and manage partitions, format them with the file system you need to use, and mount these file systems.

“Do I Know This Already?” Quiz

The “Do I Know This Already?” quiz allows you to assess whether you should read this entire chapter thoroughly or jump to the “Exam Preparation Tasks” section. If you are in doubt about your answers to these questions or your own assessment of your knowledge of the topics, read the entire chapter. Table 14-1 lists the major headings in this chapter and their corresponding “Do I Know This Already?” quiz questions. You can find the answers in Appendix A, “Answers to the ‘Do I Know This Already?’ Quizzes and ‘Review Questions.’

Table 14-1 “Do I Know This Already?” Section-to-Question Mapping

Foundation Topics Section

Questions

Understanding MBR and GPT Partitions

1–2

Managing Partitions and File Systems

3–6

Mounting File Systems

7–10

1. Which of the following is not an advantage of using a GUID partition table over using an MBR partition table?

a. Access time to a directory is quicker.

b. A total amount of 8 ZiB can be addressed by a partition.

c. With GUID partitions, a backup copy of the partition table is created automatically.

d. There can be up to 128 partitions in total.

2. You want to create a partition with a size of 10245 bytes. What size should it be?

a. 1 PB

b. 1 PiB

c. 1 EB

d. 1 EiB

3. Which partition type is commonly used to create a Linux partition?

a. 81

b. 82

c. 83

d. 8e

4. What is the default disk device name you would expect to see in KVM virtual machines?

a. /dev/sda

b. /dev/hda

c. /dev/sda

d. /dev/xsda

5. Which of the following statements is not true?

a. Do not ever use gdisk on an MBR disk.

b. fdisk offers support to manage GPT partitions as well.

c. Depending on your needs, you can create MBR and GPT partitions on the same disk.

d. If your server boots from EFI, you must use GPT partitions.

6. Which of the following file systems is used as the default in RHEL8?

a. Ext4

b. XFS

c. btrfs

d. Ext3

7. Which command enables you to find current UUIDs set to the file systems on your server?

a. mount

b. df -h

c. lsblk

d. blkid

8. What would you put in the device column of /etc/fstab to mount a file system based on its unique ID 42f419c4-633f-4ed7-b161-519a4dadd3da?

a. 42f419c4-633f-4ed7-b161-519a4dadd3da

b. /dev/42f419c4-633f-4ed7-b161-519a4dadd3da

c. ID=42f419c4-633f-4ed7-b161-519a4dadd3da

d. UUID=42f419c4-633f-4ed7-b161-519a4dadd3da

9. Which of the following /etc/fstab lines would perform a file system check on the file system, but only after the root file system has been checked successfully?

a. /dev/sda1 /data xfs defaults 1 1

b. /dev/sda1 /data xfs defaults 1 2

c. /dev/sda1 /data xfs defaults 1 1

d. /dev/sda1 /data xfs defaults 0 2

10. Which mount option would you use in /etc/fstab to specify that the file system can be mounted only after the network is available?

a. network

b. _netdev

c. _network

d. netdev

Foundation Topics

Understanding MBR and GPT Partitions

To use a hard drive, it needs to have partitions. Some operating systems install everything to one partition, while other operating systems such as Linux normally have several partitions on one hard disk. Using more than one partition on a system makes sense for multiple reasons:

  • It’s easier to distinguish between different types of data.

  • Specific mount options can be used to enhance security or performance.

  • It’s easier to create a backup strategy where only relevant portions of the OS are backed up.

  • If one partition accidentally fills up completely, the other partitions still are usable and your system might not crash immediately.

Note

Instead of using multiple different partitions, you can also use different LVM logical volumes. Managing logical volumes is covered in Chapter 15, “Managing Advanced Storage.”

On RHEL 8, two different partitioning schemes are available. Before creating your first partition, you should understand these schemes and be able to determine which scheme works best in a specific environment.

Understanding the MBR Partitioning Scheme

When the personal computer was invented in the early 1980s, a system was needed to define hard disk layout. This system became known as the Master Boot Record (MBR) partitioning scheme. While booting a computer, the Basic Input Output System (BIOS) was loaded to access hardware devices. From the BIOS, the bootable disk device was read, and on this bootable device, the MBR was allocated. The MBR contains all that is needed to start a computer, including a boot loader and a partition table.

When hard disks first came out for PCs in the early 1980s, users could have different operating systems on them. Some of these included MS-DOS/PC-DOS, PC/IX (IBM’s UNIX for 8086 PCs), CPM86, and MPM86. The disk would be partitioned so each operating system installed got a part of the disk. One of the partitions would be made active, meaning the code in the boot sector in the MBR would read the first sector of that active partition and run the code. That code would then load the rest of the OS. This explains why four partitions were deemed “enough.” Some operating systems (such as SCO Xenix and SCO Unix) would have another layer of partitions in the dedicated UNIX partition (in SCO’s case, called divisions), with its own partition program (SCO: divvy)

The MBR was defined as the first 512 bytes on a computer hard drive, and in the MBR an operating system boot loader (such as GRUB 2; see Chapter 17, “Managing and Understanding the Boot Procedure”) was present, as well as a partition table. The size that was used for the partition table was relatively small, just 64 bytes, with the result that in the MBR no more than four partitions could be created. Since partition size data was stored in 32-bit values, and a default sector size of 512 bytes was used, the maximum size that could be used by a partition was limited to 2 TiB (hardly a problem in the early 1980s).

In the MBR, just four partitions could be created. Because many PC operating systems needed more than four partitions, a solution was found to go beyond the number of four. In the MBR, one partition could be created as an extended partition, as opposed to the other partitions that were created as primary partitions. Within the extended partition, multiple logical partitions could be created to reach a total number of 15 partitions that could be addressed by the Linux kernel.

Understanding the Need for GPT Partitioning

Current computer hard drives have become too big to be addressed by MBR partitions. That is one of the main reasons why a new partitioning scheme was needed. This partitioning scheme is the GUID Partition Table (GPT). On computers that are using the new Unified Extensible Firmware Interface (UEFI) as a replacement for the old BIOS system, GPT partitions are the only way to address disks. Also, older computer systems that are using BIOS instead of UEFI can be configured with GUID partitions, which is necessary if a disk with a size bigger than 2 TiB needs to be addressed.

Using GUID offers many benefits:

  • The maximum partition size is 8 zebibyte (ZiB), which is 1024 × 1024 × 1024 × 1024 gibibytes.

  • In GPT, up to a maximum number of 128 partitions can be created.

  • The 2 TiB limit no longer exists.

  • Because space that is available to store partitions is much bigger than 64 bytes, which was used in MBR, there is no longer a need to distinguish between primary, extended, and logical partitions.

  • GPT uses a 128-bit global unique ID (GUID) to identify partitions.

  • A backup copy of the GUID partition table is created by default at the end of the disk, which eliminates the single point of failure that exists on MBR partition tables.

Understanding Storage Measurement Units

When talking about storage, different measurement units are used. In some cases, units like megabyte (MB) are used. In other cases, units like mebibyte (MiB) are used. The difference between these two is that a megabyte is a multiple of 1,000, and a mebibyte is a multiple of 1,024. In computers, it makes sense to talk about multiples of 1,024 because that is how computers address items. However, confusion was created when hardware vendors a long time ago started referring to megabytes instead of mebibytes.

In the early days of computing, the difference was not that important. The difference between a kilobyte (KB) and a kibibyte (KiB) is just 24 bytes. The bigger the numbers grow, the bigger the difference becomes. A gigabyte, for instance, is 1,000 × 1,000 × 1,000 bytes, so 1,000,000,000 bytes, whereas a gibibyte is 1,024 × 1,024 × 1,024 bytes, which makes a total of 1,073,741,824 bytes, which is over 70 MB larger than 1 GB.

On current Linux distributions, the binary numbers (MiB, not MB) have become the standard—but do realize that some utilities still measure in MB and not MiB. In Table 14-2, you can see an overview of the values that are used.

In the past, KB, MB, and so on, were used both in decimal and binary situations; sometimes they were even mixed. For example, 1-Mbps line speed is one million bits per second. The once famous “1.44 MB” floppy disk was really 1,440,000 bytes in size (80 tracks × 2 heads × 9 sectors × 512-byte sectors), creating a mixed meaning of MB: 1.44 × (decimal K) × (binary K).

Key topic

Table 14-2 Disk Size Specifications

Symbol

Name

Value

Symbol

Name

Value

KB

Kilobyte

10001

KiB

Kibibyte

10241

MB

Megabyte

10002

MiB

Mebibyte

10242

GB

Gigabyte

10003

GiB

Gibibyte

10243

TB

Terabyte

10004

TiB

Tebibyte

10244

PB

Petabyte

10005

PiB

Pebibyte

10245

EB

Exabyte

10006

EiB

Exbibyte

10246

ZB

Zettabyte

10007

ZiB

Zebibyte

10247

YB

Yottabyte

10008

YiB

Yobibyte

10248

Managing Partitions and File Systems

As discussed in the previous section, there are two different types of partitions that can be used on RHEL 8. To match the different partition types, there are also two different partitioning utilities. The fdisk utility has been around for a long time and is used to create MBR partitions. The gdisk utility is used to create GPT partitions. In this section, you learn how to use both.

Apart from fdisk and gdisk, there are other partitioning utilities as well, of which parted is probably the most important. Some people like it, as it is relatively easy to use, but at the same time it hides some of the more advanced features. For that reason, this chapter focuses on working with fdisk and gdisk and introduces parted only briefly.

For both MBR and GPT partitions, you need to specify the name of the disk device as an argument. Table 14-3 shows the most common disk device names that you work with on RHEL 8.

Key topic

Table 14-3 Common Disk Device Types

Device Name

Description

/dev/sda

A hard disk that uses the SCSI driver. Used for SCSI and SATA disk devices. Common on physical servers but also in VMware virtual machines.

/dev/nvme0n1

The first hard disk on an NVM Express (NVMe) interface. NVMe is a server-grade method to address advanced SSD devices. Note at the end of the device name that the first disk in this case is referred to as n1 instead of a (as in common with the other types).

/dev/hda

The (legacy) IDE disk device type. You will seldom see this device type on modern computers.

/dev/sda

A disk in a KVM virtual machine that uses the virtio disk driver. This is the common disk device type for KVM virtual machines.

/dev/xvda

A disk in a Xen virtual machine that uses the Xen virtual disk driver. You see this when installing RHEL as a virtual machine in Xen virtualization. RHEL 8 cannot be used as a Xen hypervisor, but you might see RHEL 8 virtual machines on top of the Xen hypervisor using these disk types.

As you can see in Table 14-3, almost all disk device names end with the letter a. That is because it is the first disk that was found in your server. The second SCSI disk, for instance, would have the name /dev/sdb. If many disks are installed in a server, you can have up to /dev/sdz and even beyond. After /dev/sdz, the kernel continues creating devices with names like /dev/sdaa and /dev/sdab.

Creating MBR Partitions with fdisk

To create an MBR disk partition, you have to apply a multiple-step procedure, as shown in Exercise 14-1.

Exercise 14-1 Creating MBR Partitions with fdisk

This exercise has been written to use an installation of RHEL/CentOS that contains nonpartitioned disk space. If you do not have such an installation, you can use a second disk device on your demo environment. This can be a virtual disk that is added through your virtualization program, or a USB flash drive if you’re working on a physical installation. In that case, make sure the device names in this exercise are replaced with the device names that match your hardware.

Tip

In the end-of-chapter lab, you have to create partitions again. It will be a lot easier if at that point you can start from a clean installation. The following two steps help you revert easily to a system on which you have not created any partitions yet.

  1. Type dd if=/dev/sda of=/root/diskfile bs=1M count=1. (If your disk is /dev/vda and not /dev/sda, change the disk name accordingly.) Using this command allows you to create a backup of the first megabyte of raw blocks and write that to the file /root/diskfile. This file allows you to easily revert to the situation that existed at the start of this exercise, as the command makes a backup of the MBR and all relevant metadata on disk.

  2. Type cp /etc/fstab /root/fstab to make a backup of the /etc/fstab file as well.

  3. Open a root shell and run the fdisk command. This command needs as its argument the name of the disk device where you want to create the partition. This exercise uses /dev/sda. Change that, if needed, according to your hardware.

    [root@localhost ~]# 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):
  4. Before doing anything, it is a good idea to check how much disk space you have available. Press p to see an overview of current disk allocation:

    Command (m for help): p
    Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: 0x7ad1a34b

    Device     Boot   Start      End  Sectors  Size Id Type
    /dev/sda1  *       2048  1026047  1024000  500M 83 Linux
    /dev/sda2       1026048 24111103 23085056   11G 8e Linux LVM

    In the output of this command, in particular look for the total number of sectors and the last sector that is currently used. If the last partition does not end on the last sector, you have available space to create a new partition.

  5. Type n to add a new partition:

    Command (m for help): n
    Partition type
       p   primary (2 primary, 0 extended, 2 free)
       e   extended (container for logical partitions)
  6. Assuming you have a /dev/sda1 and a /dev/sda2 partition and nothing else, press p to create a primary partition. Accept the partition number that is now suggested, which should be /dev/sda3.

  7. Specify the first sector on disk that the new partition will start on. The first available sector is suggested by default, so press Enter to accept.

  8. Specify the last sector that the partition will end on. By default, the last sector available on disk is suggested. If you use that, after this exercise you will not have any disk space left to create additional partitions or logical volumes, so you should use another last sector. To use another last sector, you can do one of the following:

    • Enter the number of the last sector you want to use.

    • Enter +number to create a partition that sizes a specific number of sectors.

    • Enter +number(K,M,G) to specify the size you want to assign to the partition in KiB, MiB, or GiB.

    • Type +1G to make this a 1-GiB partition.

      Command (m for help): n
      Partition type
         p   primary (2 primary, 0 extended, 2 free)
         e   extended (container for logical partitions)
      Select (default p): p
      Partition number (3,4, default 3):
      First sector (24111104-41943039, default 24111104):
      Last sector, +sectors or +size{K,M,G,T,P} (24111104-41943039,
        default 41943039): +1G
      
      Created a new partition 3 of type 'Linux' and of size 1 GiB

    After you enter the partition’s ending boundary, fdisk will show a confirmation.

  9. At this point, you can define the partition type. By default, a Linux partition type is used. If you want the partition to be of any other partition type, use t to change it. For this exercise there is no need to change the partition type. Common partition types include the following:

    • 82: Linux swap

    • 83: Linux

    • 8e: Linux LVM

  10. If you are happy with the modifications, press w to write them to disk and exit fdisk. If you have created a partition on a disk that is already in use, you may now see the following message:

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

    Calling ioctl() to re-read partition table.

    WARNING: Re-reading the partition table failed with error 16:
      Device or resource busy.
    The kernel still uses the old table. The new table will be
      used at the next reboot or after you run partprobe(8) or
      kpartx(8) Syncing disks.
    [root@localhost ~]#
  11. This message indicates that the partition has successfully been added to the partition table, but the in-memory kernel partition table could not be updated. You can see that by comparing the output of fdisk -l /dev/sda with the output of cat/proc/partitions, which shows the kernel partition table.

  12. Type partprobe /dev/sda to write the changes to the kernel partition table. The partition has now been added, and you can create a file system on it as described in the section “Creating File Systems.”

Note

You see the “re-reading the partition table failed with error 16” message only if you are adding partitions to a disk that already has some mounted partitions. If you are working on a new disk that does not have mounted partitions, you will not see this error, and you will not have to use the partprobe command.

Using Extended and Logical Partitions on MBR

In the previous procedure, you learned how to add a primary partition. If three partitions have been created already, there is room for one more primary partition, after which the partition table is completely filled up. If you want to go beyond four partitions on an MBR disk, you have to create an extended partition. Following that, you can create logical partitions within the extended partition.

Using logical partitions does allow you to go beyond the limitation of four partitions in the MBR; there is a disadvantage as well, though. All logical partitions exist within the extended partition. If something goes wrong with the extended partition, you have a problem with all logical partitions existing within it as well. If you need more than four separate storage allocation units, you might be better off using LVM instead of logical partitions. In Exercise 14-2 you learn how to work with extended and logical partitions.

Note

An extended partition is only used for the purpose of creating logical partitions. You cannot create file systems directly on an extended partition!

Exercise 14-2 Creating Logical Partitions

  1. In a root shell, type fdisk /dev/sda to open the fdisk interface.

  2. To create a logical partition, when fdisk prompts which partition type you want to create, enter e.

    Command (m for help): n
    Partition type
       p   primary (3 primary, 0 extended, 1 free)
       e   extended (container for logical partitions)
    Select (default e):
  3. If the extended partition is the fourth partition that you are writing to the MBR, it will also be the last partition that can be added to the MBR. For that reason, it should fill the rest of your computer’s hard disk. Press Enter to accept the default first sector and press Enter again when fdisk prompts for the last sector. Note that this extended partition should use the rest of the remaining disk space, because all disk space not included in this partition cannot be allocated at a later stage.

    Using default response e.
    Selected partition 4
    First sector (26208256-41943039, default 26208256):
    Last sector, +sectors or +size{K,M,G,T,P} (26208256-41943039,
      default 41943039):
    
    Created a new partition 4 of type 'Extended' and of size 7.5 GiB
  4. Now that the extended partition has been created, you can create a logical partition within it. Still from the fdisk interface, press n again. The utility will prompt that all primary partitions are in use now and by default suggests adding a logical partition with partition number 5.

    Command (m for help): n
    All primary partitions are in use.
    Adding logical partition 5
    First sector (26210304-41943039, default 26210304):
  5. Press Enter to accept the default first sector. When asked for the last sector, enter +100M (or any other size you want to use).

    First sector (26210304-41943039, default 26210304):
    Last sector, +sectors or +size{K,M,G,T,P} (26210304-41943039,
      default 41943039): +100M
    
    Created a new partition 5 of type 'Linux' and of size 100 MiB
  6. Now that the logical partition has been created, enter w to write the changes to disk and quit fdisk. To complete the procedure, enter partprobe to update the kernel partition table. The new partition is now ready for use.

Tip

The fdisk utility writes changes to disk only when you enter w, which is the fdisk write command. If you have made a mistake and want to get out, press q to quit.

In Exercise 14-2, you used the partprobe command to push changes in the partition table to the kernel partition table. This normally works out well, but in some cases does not. If at any time you are getting an error using partprobe, just reboot your computer, using the reboot command. If partitions have not been written to your system correctly, you really do not want to continue modifying and managing partitions, because you risk creating severe problems on your server.

Creating GPT Partitions with gdisk

If a disk is configured with a GUID Partition Table (GPT), or if it is a new disk that does not contain anything yet and has a size that goes beyond 2 TiB, you need to use the gdisk utility to create GUID partitions. This utility has a lot of similarities with fdisk but some differences as well. The following procedure shows how to create partitions in gdisk.

Warning!

Do not ever use gdisk on a disk that has been formatted with fdisk and already contains fdisk partitions. gdisk will detect that an MBR is present, and it will convert this to a GPT (see the following code listing). Your computer will most likely not be able to boot after doing this!

[root@localhost ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***********************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format in
memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q'
if you don't want to convert your MBR partitions to GPT format!
***********************************************************************


Command (? for help):

To save you the hassle of going through this, I verified it does what it says. After converting an MBR to a GPT, your machine will not start anymore.

Exercise 14-3 demonstrates how to create partitions using gdisk.

Exercise 14-3 Creating GPT Partitions with gdisk

To apply the procedure in this exercise, you need a new disk device. Do not use a disk that contains data that you want to keep, because this exercise will delete all data on it. If you are using this exercise on a virtual machine, you may add the new disk through the virtualization software. If you are working on a physical machine, you can use a USB thumb drive as a disk device for this exercise. Note that this exercise works perfectly on a computer that starts from BIOS and not EFI; all you need is a dedicated disk device.

  1. To create a partition with gdisk, type gdisk /dev/sdb. (Replace /dev/sdb with the exact device name used on your computer.) gdisk will try to detect the current layout of the disk, and if it detects nothing, it will create the GPT partition table and associated disk layout.

    [root@localhost ~]# gdisk /dev/sdb
    GPT fdisk (gdisk) version 1.0.3
    
    Partition table scan:
      MBR: not present
      BSD: not present
      APM: not present
      GPT: not present
    
    Creating new GPT entries.
    
    Command (? for help):
  2. Type n to enter a new partition. You can choose any partition number between 1 and 128, but it is wise to accept the default partition number that is suggested.

    Command (? for help): n
    Partition number (1-128, default 1):
  3. You now are asked to enter the first sector. By default, the first sector that is available on disk will be used, but you can specify an offset as well. This does not make sense, so just press Enter to accept the default first sector that is proposed.

    First sector (34-2097118, default = 2048) or {+-}size{KMGTP}:
  4. When asked for the last sector, by default the last sector that is available on disk is proposed (which would create a partition that fills the entire hard disk). You can specify a different last sector, or specify the disk size using +, the size, and KMGTP. So to create a 100-MiB disk partition, use +100M.

    Last sector (2048-2097118, default = 2097118) or
      {+-}size{KMGTP}: +100M
  5. You now are asked to set the partition type. If you do not do anything, the partition type is set to 8300, which is the Linux file system partition type. Other options are available as well. You can press l to show a list of available partition types.

    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300): l
    0700 Microsoft basic data  0c01 Microsoft reserved  2700 Windows RE
    3000 ONIE boot              3001 ONIE config         3900 Plan 9
    4100 PowerPC PReP boot     4200 Windows LDM data     4201 Windows LDM
                                                               metadata
    4202 Windows Storage Spac  7501 IBM GPFS              7f00 ChromeOS
                                                                 kernel
    7f01 ChromeOS root         7f02 ChromeOS reserved     8200 Linux swap
    8300 Linux filesystem      8301 Linux reserved        8302 Linux /home
    8303 Linux x86 root (/)    8304 Linux x86-64 root (/  8305 Linux ARM64
                                                                 root (/)
    8306 Linux /srv            8307 Linux ARM32 root (/)  8400 Intel Rapid
                                                                   Start
    8e00 Linux LVM             a000 Android bootloader    a001 Android
                                                                bootloader
                                                          2
    a002 Android boot          a003 Android recovery      a004 Android
                                                                 misc
    a005 Android metadata      a006 Android system        a007 Android
                                                                 cache
    a008 Android data          a009 Android persistent    a00a Android
                                                                 factory
    a00b Android fastboot/ter  a00c Android OEM           a500 FreeBSD
                                                                 disklabel
    a501 FreeBSD boot           a502 FreeBSD swap          a503 FreeBSD UFS
    a504 FreeBSD ZFS            a505 FreeBSD Vinum/RAID   a580 Midnight
                                                                 BSD data
    a581 Midnight BSD boot     a582 Midnight BSD swap     a583 Midnight
                                                                 BSD UFS
    a584 Midnight BSD ZFS      a585 Midnight BSD Vinum    a600 OpenBSD
                                                                 disklabel
    a800 Apple UFS              a901 NetBSD swap           a902 NetBSD FFS
    a903 NetBSD LFS             a904 NetBSD concatenated  a905 NetBSD
                                                                 encrypted
    a906 NetBSD RAID            ab00 Recovery HD           af00 Apple
                                                                  HFS/HFS+
    af01 Apple RAID             af02 Apple RAID offline   af03 Apple label

    The relevant partition types are as follows:

    • 8200: Linux swap

    • 8300: Linux file system

    • 8e00: Linux LVM

    Notice that these are the same partition types as the ones that are used in MBR, with two 0s added to the IDs. You can also just press Enter to accept the default partition type 8300.

  6. The partition is now created (but not yet written to disk). Press p to show an overview, which allows you to verify that this is really what you want to use.

    Command (? for help): p
    Disk /dev/sdb: 20971520 sectors, 10.0 GiB
    Model: VMware Virtual S
    Sector size (logical/physical): 512/512 bytes
    Disk identifier (GUID): D9D1F660-E1DC-4737-8117-DC6990E665C9
    Partition table holds up to 128 entries
    Main partition table begins at sector 2 and ends at sector 33
    First usable sector is 34, last usable sector is 20971486
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 20766653 sectors (9.9 GiB)
    
    Number  Start (sector)  End (sector) Size      Code  Name
       1             2048        206847   100.0 MiB  8300  Linux
                                                           filesystem
  7. If you are satisfied with the current partitioning, press w to write changes to disk and commit. This gives a warning, after which the new partition table is written to the GUID partition table.

    Command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL
      OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    The operation has completed successfully.
  8. If at this point you get an error message indicating that the partition table is in use, type partprobe to update the kernel partition table.

Creating GPT Partitions with parted

As previously mentioned, apart from fdisk and gdisk, the parted utility can be used to create partitions. Because it lacks support for advanced features, I have focused on fdisk and gdisk, but I’d like to give you a quick overview of working with parted.

To use parted, you need to know that it has an interactive shell in which you can work with its different options. Exercise 14-4 guides you through the procedure of creating partitions using parted.

Exercise 14-4 Creating Partitions with parted

You need a new disk to work with this procedure. This exercise assumes that the new disk name is /dev/sdc.

  1. From a root shell, type parted /dev/sdc. This opens the interactive parted shell.

  2. Type help to get an overview of available commands.

  3. Type print. You will see a message about an unrecognized disk label.

  4. Type mklabel and press Enter. parted will now prompt for a disk label type. Press the Tab key twice to see a list of available disk label types. From the list, select gpt and press Enter.

  5. Type mkpart. The utility prompts for a partition name. Type part1 (the partition name doesn’t really matter).

  6. Now the utility prompts for a file system type. This is a very confusing option, because it suggests that you are setting a file system type here, but that is not the case. Also, when using Tab completion, you’ll see a list of file systems that you’ve probably never used before. In fact, you could just press Enter to accept the default suggestion of ext2, as the setting isn’t used anyway, but I suggest using a file system type that comes close to what you’re going to use on the partition. So type xfs and press Enter to continue.

  7. Now you are prompted for a start location. You can specify the start location as a number of blocks, or an offset from the start of the device. Notice that you can type 1M to specify the start of the partition at 1 megabyte, or type 1 MiB to have it start at 1 MiB. This is confusing, so make sure you specify the appropriate value here. At this point, type 1MiB and press Enter.

  8. Type 1GiB to specify the end of the partition. After doing so, type print to print the current partition table, and type quit to quit the utility and commit your changes.

Creating File Systems

At this point, you know how to create partitions. A partition all by itself is not very useful. It only becomes useful if you decide to do something with it. That often means that you have to put a file system on top of it. In this section, you learn how to do that.

Different file systems can be used on RHEL 8. Table 14-4 provides an overview of the most common file systems.

Table 14-4 File System Overview

Key topic

File System

Description

XFS

The default file system in RHEL 8.

Ext4

The default file system in previous versions of RHEL; still available and supported in RHEL 8.

Ext3

The previous version of Ext4. On RHEL 8, there is no need to use Ext3 anymore.

Ext2

A very basic file system that was developed in the early 1990s. There is no need to use this file system on RHEL 8 anymore.

BtrFS

A relatively new file system that is not supported in RHEL 8.

NTFS

A Windows-compatible file system that is not supported on RHEL 8.

VFAT

A file system that offers compatibility with Windows and Mac and is the functional equivalent of the FAT32 file system. Useful on USB thumb drives that exchange data with other computers but not on a server’s hard disks.

To format a partition with one of the supported file systems, you can use the mkfs command, using the option -t to specify which specific file system to use. Alternatively, one of the file system–specific tools can be used, such as mkfs.ext4 to format an Ext4 file system.

Note

If you use mkfs without any further specification of which file system you want to format, an Ext2 file system will be formatted. This is probably not what you want to use, so do not forget to specify which file system you want to use.

To format a partition with the default XFS file system, use the command mkfs -t xfs. Example 14-1 shows the output of this command.

Example 14-1 Formatting a File System with XFS

[root@localhost ~]# mkfs -t xfs /dev/sda5
meta-data=/dev/sda5         isize=512    agcount=4, agsize=6400 blks
         =                  sectsz=512   attr=2, projid32bit=1
         =                  crc=1        finobt=1, sparse=1, rmapbt=0
         =                  reflink=1
data     =                  bsize=4096   blocks=25600, imaxpct=25
         =                  sunit=0      swidth=0 blks
naming   =version 2         bsize=4096   ascii-ci=0, ftype=1
log      =internal log      bsize=4096   blocks=1368, version=2
         =                  sectsz=512   sunit=0 blks, lazy-count=1
realtime =none              extsz=4096   blocks=0, rtextents=0

In Exercise 14-5, you learn how to create a file system.

Exercise 14-5 Creating a File System

In Exercise 14-1, you have created a partition /dev/sda3. In this exercise, you format it with an XFS file system. This exercise has one step only.

  1. From a root shell, type mkfs.xfs /dev/sda3.

Changing File System Properties

When working with file systems, some properties can be managed as well. File system properties are specific for the file system you are using, so you work with different properties and different tools for the different file systems.

Managing Ext4 File System Properties

The generic tool for managing Ext4 file system properties is tune2fs. This tool was developed a long time ago for the Ext2 file system and is compatible with Ext3 and Ext4 also. When managing Ext4 file system properties, tune2fs -l is a nice command to start with. Example 14-2 presents the output of this command where different file system properties are shown.

Example 14-2 Showing File System Properties with tune2fs –l

[root@localhost ~]# tune2fs -l /dev/sda3
tune2fs 1.44.3 (10-July-2018)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          ad8c3cf0-675b-4540-bc9a-4cfe63a90ffa
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index
                            filetype extent 64bit flex_bg sparse_super
                            large_file huge_file dir_nlink extra_isize
                            metadata_csum
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              25688
Block count:              102400
Reserved block count:     5120
Free blocks:              93504
Free inodes:              25677
First block:              1
Block size:               1024
Fragment size:            1024
Group descriptor size:    64
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         1976
Inode blocks per group:   247
Flex block group size:    16
Filesystem created:       Wed Jun 19 03:29:06 2019
Last mount time:           n/a
Last write time:           Wed Jun 19 03:29:06 2019
Mount count:               0
Maximum mount count:       -1
Last checked:              Wed Jun 19 03:29:06 2019
Check interval:            0 (<none>)
Lifetime writes:           4441 kB
Reserved blocks uid:       0 (user root)
Reserved blocks gid:       0 (group root)
First inode:               11
Inode size:                128
Journal inode:             8
Default directory hash:    half_md4
Directory Hash Seed:       54b529bb-2ef1-4983-ae42-dfdef7ba8797
Journal backup:            inode blocks
Checksum type:             crc32c
Checksum:                  0xf36c81e5

As you can see, the tune2fs -l command shows many file system properties. One interesting property is the file system label, which shows as the Filesystem volume name. Labels are used to set a unique name for a file system, which allows the file system to be mounted in a consistent way, even if the underlying device name changes. Also interesting are the file system features and default mount options.

To change any of the default file system options, the tune2fs command enables you to do so with other parameters. Some common usage examples are listed here:

  • Use tune2fs -o to set default file system mount options. When set to the file system, the option does not have to be specified while mounting through /etc/fstab anymore. Use, for instance, tune2fs -o acl,user_xattr to switch on access control lists and user-extended attributes. Use a ^ in front of the option to switch it off again, as in tune2fs -o ^acl,user_xattr.

  • Ext file systems also come with file system features that may be enabled as a default. To switch on a file system feature, use tune2fs -O followed by the feature. To turn a feature off, use a ^ in front of the feature name.

  • Use tune2fs -L to set a label on the file system. As described in the section “Mounting File Systems” later in this chapter, you can use a file system label to mount a file system based on its name instead of the device name. Instead of tune2fs -L, the e2label command enables you to do so.

Managing XFS File System Properties

The XFS file system is a completely different file system, and for that reason also has a completely different set of tools to manage its properties. It does not allow you to set file system attributes within the file system metadata. You can, however, change some XFS properties, using the xfs_admin command. For instance, use xfs_admin -L mylabel to set the file system label to mylabel.

Adding Swap Partitions

You use most of the partitions on a Linux server for regular file systems. On Linux, swap space is normally allocated on a disk device. That can be a partition or an LVM logical volume (discussed in Chapter 15). In case of an emergency, you can even use a file to extend the available swap space.

Using swap on Linux is a convenient way to improve Linux kernel memory usage. If a shortage of physical RAM occurs, non-recently used memory pages can be moved to swap, which makes more RAM available for programs that need access to memory pages. Most Linux servers for that reason are configured with a certain amount of swap. If swap starts being used intensively, you could be in trouble, though, and that is why swap usage should be closely monitored.

Sometimes, allocating more swap space makes sense. If a shortage of memory occurs, this shortage can be alleviated by allocating more swap space in some situations. (See Chapter 25, “Configuring Time Services,” for more information about system performance optimization.) This is done through a procedure where first a partition is created with the swap partition type, and then this partition is formatted as swap. Exercise 14-6 describes how to do this.

Exercise 14-6 Creating a Swap Partition

  1. Type fdisk /dev/sda to open your disk in fdisk. (Use gdisk if you are using a disk with a GUID partition table.)

  2. Press n to add a new partition. Specify start and stop cylinders and size.

  3. Type t to change the partition type. If you are using fdisk, use partition type 82. If you are using gdisk, use partition type 8200.

  4. Use mkswap to format the partition as swap space. Use, for instance, mkswap /dev/sda6 if the partition you have just created is /dev/sda6.

  5. 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.

  6. Use swapon to switch on the newly allocated swap space. If, for instance, the swap device you have just created is /dev/sda6, use swapon /dev/sda6 to activate the swap space.

  7. Type free -m again. You see that the new swap space has been added to your server.

Adding Swap Files

If you do not have free disk space to create a swap partition and you do need to add swap space urgently, you can use a swap file as well. From a performance perspective, it does not even make that much difference if a swap file is used instead of a swap device such as a partition or a logical volume, and it may help you fulfill an urgent need in a timely manner.

To add a swap file, you need to create the file first. The dd if=/dev/zero of=/ swapfile bs=1M count=100 command would add 100 blocks with a size of 1 MiB from the /dev/zero device (which generates 0s) to the /swapfile file. The result is a 100-MiB file that can be configured as swap. To do so, you can follow the same procedure as for swap partitions. First use mkswap /swapfile to mark the file as a swap file, and then use swapon /swapfile to activate it.

Mounting File Systems

Just creating a partition and putting a file system on it is not enough to start using it. To use a partition, you have to mount it as well. By mounting a partition (or better, the file system on it), you make its contents accessible through a specific directory.

To mount a file system, some information is needed:

  • What to mount: This information is mandatory and specifies the name of the device that needs to be mounted.

  • Where to mount it: This is also mandatory information that specifies the directory on which the device should be mounted.

  • What file system to mount: Optionally, you can specify the file system type. In most cases, this is not necessary. The mount command will detect which file system is used on the device and make sure the correct driver is used.

  • Mount options: Many mount options can be used when mounting a device. Using options is optional and depends on the needs you may have for the file system.

Manually Mounting File Systems

To manually mount a file system, the mount command is used. To disconnect a mounted file system, the umount command is used. Using these commands is relatively easy. To mount the file system that is on /dev/sda5 on the directory /mnt, for example, use the following command:

mount /dev/sda5 /mnt

To disconnect the mount, you can use umount with either the name of the device or the name of the mount point you want to disconnect. So, both of the following commands will work:

umount /dev/sda5
umount /mnt

Using Device Names, UUIDs, or Disk Labels

To mount a device, the name of the device can be used, as in the command mount /dev/sda5 /mnt. If your server is used in an environment where a dynamic storage topology is used, this is not always the best approach. You may today have a storage device /dev/sda5, which after changes in the storage topology can be /dev/sdb5 after the next reboot of your server. This is why on a default RHEL 8 installation, UUIDs are used instead of device names.

Every file system by default has a UUID associated to it—not just file systems that are used to store files but also special file systems such as the swap file system. You can use the blkid command to get an overview of the current file systems on your system and the UUID that is used by that file system.

Before the use of UUIDs was common, file systems were often configured to work with labels, which can be set using the e2label command or the xfs_admin -L command. This has become more uncommon in recent Linux versions. If a file system has a label, the blkid command will also show it, as can be seen in Example 14-3.

Example 14-3 Using blkid to Find Current File System UUIDs

[root@server3 ~]# blkid
/dev/sda1: UUID="02305166-840d-4f74-a868-c549b3229e65" TYPE="xfs"
/dev/sda2: UUID="Hasz5q-nZF3-XN94-L2fm-xUwz-3VEq-qVCAYi"
             TYPE="LVM2_member"
/dev/sda5: UUID="42f419c4-633f-4ed7-b161-519a4dadd3da" TYPE="xfs"
/dev/mapper/centos-swap: UUID="5867ba02-fd89-475c-be56-7922febde43b"
                           TYPE="swap"
/dev/mapper/centos-root: UUID="b2022ac4-73c6-4e6b-a52f-703e3e2476b7"
                           TYPE="xfs"

To mount a file system based on a UUID, you use UUID=nnnnn instead of the device name. So if you want to mount /dev/sda5 from Example 14-3 based on its UUID, the command becomes as follows:

mount UUID="42f419c4-633f-4ed7-b161-519a4dadd3da" /mnt

Manually mounting devices using the UUID is not exactly easier. If mounts are automated as discussed in the next section, however, it does make sense using UUIDs instead of device names.

To mount a file system using a label, you use the mount LABEL=labelname command. For example, use mount LABEL=mylabel /mnt to temporarily mount the file system with the name mylabel on the /mnt directory.

Automating File System Mounts Through /etc/fstab

Normally, you do not want to be mounting file systems manually. Once you are happy with them, it is a good idea to have them mounted automatically. The classical way to do this is through the /etc/fstab file. Example 14-4 shows sample contents of this file.

Example 14-4 Sample /etc/fstab File Contents

[root@server3 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jan 16 10:28:41 2015
#
# Accessible filesystems, by reference, are maintained under
'/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8)
for more info
#
/dev/mapper/centos-root /      xfs   defaults  1 1
UUID=02305166-840d-4f74 /      xfs   defaults  1 2
/dev/mapper/centos-swap swap   swap   defaults  0 0

In the /etc/fstab file, everything is specified to mount the file system automatically. For this purpose, every line has six fields, as summarized in Table 14-5.

Key topic

Table 14-5 /etc/fstab Fields

Field

Description

Device

The device that must be mounted. A device name, UUID, or label can be used.

Mount Point

The directory or kernel interface where the device needs to be mounted.

File System

The file system type.

Mount Options

Mount options.

Dump Support

Use 1 to enable support to back up using the dump utility. This may be necessary for some backup solutions.

Automatic Check

Specifies whether the file system should be checked automatically when booting. Use 0 to disable automated check, 1 if this is the root file system and it has to be checked automatically, and 2 for all other file systems that need automatic checking while booting. Network file systems should have this option set to 0.

Based on what has previously been discussed about the mount command, you should have no problem understanding the Device, Mount Point, and File System fields in /etc/fstab. Notice that in the mount point not all file systems use a directory name. Some system devices such as swap are not mounted on a directory, but on a kernel interface. It is easy to recognize when a kernel interface is used; its name does not start with a / (and does not exist in the file system on your server).

The Mount Options field defines specific mount options that can be used. If no specific options are required, this line will just read “defaults.” To offer specific functionality, a large number of mount options can be specified here. Table 14-6 gives an overview of some of the more common mount options.

Key topic

Table 14-6 Common Mount Options

Option

Use

auto/ noauto

The file system will [not] be mounted automatically.

acl

Adds support for file system access control lists (see Chapter 7, “Permissions Management”).

user_xattr

Adds support for user-extended attributes (see Chapter 7).

ro

Mounts the file system in read-only mode.

atime / noatime

Disables or enables access time modifications.

noexec / exec

Denies or allows execution of program files from the file system.

_netdev

Use this to mount a network file system. This tells fstab to wait until the network is available before mounting this file system.

The fifth column of /etc/fstab specifies support for the dump utility. This is a utility that was developed to create file system backups. It is good practice to switch this feature on by specifying a 1 for all real file systems, and switch it off by specifying 0 for all system mounts.

The last column indicates if the file system integrity needs to be checked while booting. Put a 0 if you do not want to check the file system at all, a 1 if this is the root file system that needs to be checked before anything else, and a 2 if this is a nonroot file system that needs to be checked while booting.

In Exercise 14-7, you learn how to mount partitions through /etc/fstab by mounting the XFS-formatted partition /dev/sda5 that you created in previous exercises.

Warning

If a file system through /etc/fstab is flagged for automatic file system check and something prevents the file system from being checked correctly, your system stops booting and prompts “enter root password to enter maintenance mode.” To prevent this from ever happening, you could choose to disable automated checks while booting. See Chapter 18, “Essential Troubleshooting Skills,” for more information on how to fix this specific case.

Exercise 14-7 Mounting Partitions Through /etc/fstab

  1. From a root shell, type blkid. Use the mouse to copy the UUID="nnnn" part for /dev/sda5.

  2. Type mkdir -p /mounts/data to create a mount point for this partition.

  3. Open /etc/fstab in an editor and add the following line:

    UUID="nnnn"    /mounts/data    xfs    defaults 1 2
  4. Before attempting an automatic mount while rebooting, it is a good idea to test the configuration. Type mount -a. This mounts everything that is specified in /etc/fstab and that has not been mounted already.

  5. Type df -h to verify that the partition has been mounted correctly.

Summary

In this important chapter, you learned how to work with partitions and file systems on RHEL 8. You learned how to create partitions for MBR and GPT disks and how to put a file system on top of the partition. You also learned how to mount these partitions manually and automatically through /etc/fstab.

Exam Preparation Tasks

As mentioned in the section “How to Use This Book” in the Introduction, you have several choices for exam preparation: the end-of-chapter labs; the memory tables in Appendix B; Chapter 26, “Final Preparation”; and the practice exams.

Review All Key Topics

Review the most important topics in the chapter, noted with the Key Topic icon in the outer margin of the page. Table 14-7 lists a reference of these key topics and the page numbers on which each is found.

Key topic

Table 14-7 Key Topics for Chapter 14

Key Topic Element

Description

Page

Table 14-2

Disk size specifications

314

Table 14-3

Common disk device types

315

Table 14-4

File system types

326

Table 14-5

/etc/fstab fields

333

Table 14-6

Common mount options

334

Complete Tables and Lists from Memory

Print a copy of Appendix B, “Memory Tables” (found on the companion website), or at least the section for this chapter, and complete the tables and lists from memory. Appendix C, “Memory Tables Answer Key,” includes completed tables and lists to check your work.

Define Key Terms

Define the following key terms from this chapter and check your answers in the glossary:

BIOS

MBR

partition

primary partition

extended partition

logical partition

GPT

mount

umount

UUID

label

Ext2

Ext3

Ext4

XFS

BtrFS

VFAT

fstab

Review Questions

The questions that follow use an open-ended format that is meant to help you test your knowledge of concepts and terminology and the breadth of your knowledge. You can find the answers to these questions in Appendix A.

1. Which tool do you use to create GUID partitions?

2. Which tool do you use to create MBR partitions?

3. What is the default file system on RHEL 8?

4. What is the name of the file that is used to automatically mount partitions while booting?

5. Which mount option do you use if you want a file system not to be mounted automatically while booting?

6. Which command enables you to format a partition that has type 82 with the appropriate file system?

7. You have just added a couple of partitions for automatic mounting while booting. How can you safely test if this is going to work without actually rebooting?

8. Which file system is created if you use the mkfs command without any file system specification?

9. How do you format an Ext4 partition?

10. How do you find UUIDs for all devices on your computer?

End-of-Chapter Lab

In the exercises you have worked through in this chapter, you have already created partitions. Before working on this end-of-chapter lab, it is a good idea to start from a clean installation. In Exercise 14-1, you created some backup files. Before starting to work on this lab, restore the original setup using the following two steps:

  1. Type dd if=/dev/diskfile of=/dev/sda. (Use of=/dev/vda if your disk device is /dev/vda instead of /dev/sda.)

  2. Copy the backup of the /etc/fstab file, using cp /root/fstab /etc.

This restores the original configuration. You are now ready to start the end-of-chapter lab. After successfully completing this lab, repeat this procedure. This allows you to work on clean disks when creating LVM logical volumes, as described in the next chapter.

Lab 14.1

1. Add two partitions to your server. If possible, put them on the primary disk that is in use on your server. If that is not possible, use a second (virtual or USB) disk to add these partitions. Create both partitions with a size of 100 MiB. One of these partitions must be configured as swap space; the other partition must be formatted with an Ext4 file system.

2. Configure your server to automatically mount these partitions. Mount the Ext4 partition on /mounts/data and mount the swap partition as swap space.

3. Reboot your server and verify that all is mounted correctly. In case of problems, read Chapter 18, “Essential Troubleshooting Skills,” for tips on how to troubleshoot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127