Bookmark this page

Add Partitions, File Systems, and Persistent Mounts

Objectives

Create storage partitions, format them with file systems, and mount them for use.

Partition Disks

Disk partitioning divides a hard drive into multiple logical storage partitions. You can use partitions to divide storage based on different requirements, and this division provides many benefits:

  • Limit available space to applications or users.

  • Separate operating system and program files from user files.

  • Create a separate area for memory swapping.

  • Limit disk space use to improve the performance of diagnostic tools and backup imaging.

MBR Partition Scheme

The Master Boot Record (MBR) partitioning scheme is the standard on systems that run BIOS firmware. This scheme supports a maximum of four primary partitions. On Linux systems, with extended and logical partitions, you can create up to 15 partitions. With a 32-bit partition size, disks that are partitioned with MBR can have a size of up to 2 TiB.

Figure 9.1: MBR partitioning of the /dev/vdb storage device

The 2 TiB disk and partition size limit is now a common and restrictive limitation. Consequently, the legacy MBR scheme is superseded by the GUID Partition Table (GPT) partitioning scheme.

GPT Partition Scheme

For systems that run Unified Extensible Firmware Interface (UEFI) firmware, GPT is the standard for disk partitioning, and addresses the limitations of the MBR scheme. A GPT provides a maximum of 128 partitions. The GPT scheme allocates 64 bits for logical block addresses, to support partitions and disks of up to eight zebibytes (ZiB) or eight billion tebibytes (TiB).

Figure 9.2: GPT partitioning of the /dev/vdb storage device

GPT partitioning offers additional features and benefits over MBR. A GPT uses a globally unique identifier (GUID) to identify each disk and partition. A GPT makes the partition table redundant, with the primary GPT at the head of the disk, and a backup secondary GPT at the end of the disk. A GPT uses a checksum to detect errors in the GPT header and partition table.

Manage Partitions

An administrator can use a partition editor program to change a disk's partitions, such as creating and deleting partitions, and changing partition types.

The standard partition editor on the command line in Red Hat Enterprise Linux is parted. You can use the parted partition editor with storage that uses either the MBR partitioning scheme or the GPT partitioning scheme.

The parted command takes as its first argument the device name that represents the entire storage device or disk to modify, followed by subcommands. The following example uses the print subcommand to display the partition table on the disk that is the /dev/vda block device (the first "virtualized I/O" disk detected by the system).

[root@host ~]# parted /dev/vda print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  xfs          boot
 2      10.7GB  53.7GB  42.9GB  primary  xfs

Use the parted command without a subcommand to open an interactive partitioning session.

[root@host ~]# parted /dev/vda
GNU Parted 3.4
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  xfs          boot
 2      10.7GB  53.7GB  42.9GB  primary  xfs

(parted) quit
[root@host ~]#

By default, the parted command displays sizes in powers of 10 (KB, MB, GB). You can change the unit size with the unit parameter, which accepts the following values:

  • s for sector

  • B for byte

  • MiB , GiB , or TiB (powers of 2)

  • MB , GB , or TB (powers of 10)

[root@host ~]# parted /dev/vda unit s print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 104857600s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start      End         Size       Type     File system  Flags
 1      2048s      20971486s   20969439s  primary  xfs          boot
 2      20971520s  104857535s  83886016s  primary  xfs

As shown in the previous example, you can also specify multiple subcommands (here, unit and print) on the same line.

Write the Partition Table on a New Disk

To partition a new drive, first write a disk label. The disk label indicates which partitioning scheme to use. Use parted to write an MBR disk label or a GPT disk label.

[root@host ~]# parted /dev/vdb mklabel msdos

[root@host ~]# parted /dev/vdb mklabel gpt

Warning

The mklabel subcommand wipes the existing partition table. Use the mklabel subcommand when the intent is to reuse the disk without regard to the existing data. If a new label moves the partition boundaries, then all data in existing file systems becomes inaccessible.

Create MBR Partitions

The following instructions create an MBR disk partition. Specify the disk device to create the partition on.

Run the parted command and specify the disk device name as an argument, to start in interactive mode. The session displays (parted) as a subcommand prompt.

[root@host ~]# parted /dev/vdb
GNU Parted 3.4
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Use the mkpart subcommand to create a primary or extended partition.

(parted) mkpart
Partition type?  primary/extended? primary

Note

If you need more than four partitions on an MBR-partitioned disk, then create three primary partitions and one extended partition. The extended partition serves as a container within which you can create multiple logical partitions.

Indicate the file-system type to create on the partition, such as xfs or ext4. This value is only a useful partition type label, and does not create the file system.

File system type?  [ext2]? xfs

To list the supported file-system types, use the following command:

[root@host ~]# parted /dev/vdb help mkpart
  ...output omitted...
  mkpart PART-TYPE [FS-TYPE] START END     make a partition

    PART-TYPE is one of: primary, logical, extended
    FS-TYPE is one of: udf, btrfs, nilfs2, ext4, ext3, ext2, f2fs, fat32, fat16,
    hfsx, hfs+, hfs, jfs, swsusp, linux-swap(v1), linux-swap(v0), ntfs,
    reiserfs, hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4, amufs3,
    amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2,
    affs1, affs0, linux-swap, linux-swap(new), linux-swap(old)

   'mkpart' makes a partition without creating a new file system on the
    partition.  FS-TYPE may be specified to set an appropriate partition
    ID.

Specify the disk sector to start the new partition on.

Start? 2048s

The s suffix provides the value in sectors, or uses the MiB, GiB, TiB, MB, GB, or TB suffixes. The parted command defaults to the MB suffix. The parted command rounds provided values to satisfy disk constraints.

When the parted command starts, it retrieves the disk topology from the device, such as the disk physical block size. The parted command ensures that the start position that you provide correctly aligns the partition with the disk structure, to optimize performance. If the start position results in a misaligned partition, then the parted command displays a warning. With most disks, a start sector that is a multiple of 2048 is safe.

Specify the disk sector where the new partition should end, and exit parted. You can specify the end as a size or as an ending location.

End? 1000MB
(parted) quit
Information: You may need to update /etc/fstab.

[root@host ~]#

When you provide the end position, the parted command updates the partition table on the disk with the new partition details.

Run the udevadm settle command. This command waits for the system to detect the new partition and to create the associated device file in the /dev directory. The prompt returns when the task is done.

[root@host ~]# udevadm settle

As an alternative to interactive mode, you can create a partition in a single command:

[root@host ~]# parted /dev/vdb mkpart primary xfs 2048s 1000MB

Create GPT Partitions

The GPT scheme also uses the parted command to create partitions. Specify the disk device to create the partition on.

As the root user, execute the parted command and specify the disk device name as an argument.

[root@host ~]# parted /dev/vdb
GNU Parted 3.4
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Use the mkpart subcommand to begin creating the partition. With the GPT scheme, each partition is given a name.

(parted) mkpart
Partition name?  []? userdata

Indicate the file-system type to create on the partition, such as xfs or ext4. This value does not create the file system, but is a useful partition type label.

File system type?  [ext2]? xfs

Specify the disk sector that the new partition starts on.

Start? 2048s

Specify the disk sector for the new partition to end, and exit parted. When you provide the end position, the parted command updates the GPT on the disk with the new partition details.

End? 1000MB
(parted) quit
Information: You may need to update /etc/fstab.

[root@host ~]#

Run the udevadm settle command. This command waits for the system to detect the new partition and to create the associated device file in the /dev directory. The prompt returns when the task is done.

[root@host ~]# udevadm settle

As an alternative to interactive mode, you can create a partition in a single command:

[root@host ~]# parted /dev/vdb mkpart userdata xfs 2048s 1000MB

Delete Partitions

The following instructions apply for both the MBR and GPT partitioning schemes. Specify the disk that contains the partition to remove.

Run the parted command with the disk device as the only argument.

[root@host ~]# parted /dev/vdb
GNU Parted 3.4
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Identify the partition number of the partition to delete.

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size   File system  Name       Flags
 1      1049kB  1000MB  999MB  xfs          usersdata

Delete the partition, and exit parted. The rm subcommand immediately deletes the partition from the partition table on the disk.

(parted) rm 1
(parted) quit
Information: You may need to update /etc/fstab.

[root@host ~]#

As an alternative to interactive mode, you can delete a partition in a single command:

[root@host ~]# parted /dev/vdb rm 1

Create File Systems

After a block device is created, the next step is to add a file system to it. Red Hat Enterprise Linux supports multiple file-system types, and XFS is the recommended default.

As the root user, use the mkfs.xfs command to apply an XFS file system to a block device. For an ext4 file system, use the mkfs.ext4 command.

[root@host ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=60992 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=243968, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1566, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Mount File Systems

After you add the file system, the last step is to mount the file system to a directory in the directory structure. When you mount a file system to the directory hierarchy, user-space utilities can access or write files on the device.

Manually Mount File Systems

Use the mount command to manually attach a device to a mount point directory location. The mount command requires a device and a mount point, and can include file-system mount options. File-system options customize the behavior of the file system.

[root@host ~]# mount /dev/vdb1 /mnt

You also use the mount command to view currently mounted file systems, the mount points, and their options.

[root@host ~]# mount | grep vdb1
/dev/vdb1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

Persistently Mount File Systems

Manually mounting a file system is a good way to verify that a formatted device is accessible and is working as expected. However, when the server reboots, the system does not automatically mount the file system again.

To configure the system to automatically mount the file system during system boot, add an entry to the /etc/fstab file. This configuration file lists the file systems to mount at system boot.

The /etc/fstab file is a white-space-delimited file with six fields per line.

[root@host ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Apr 5 12:05:19 2022
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=a8063676-44dd-409a-b584-68be2c9f5570   /        xfs   defaults   0 0
UUID=7a20315d-ed8b-4e75-a5b6-24ff9e1f9838   /dbdata  xfs   defaults   0 0

The first field specifies the device. This example uses a UUID to specify the device. File systems create and store the UUID in the partition super block at creation time. Alternatively, you could use the device file, such as /dev/vdb1.

The second field is the directory mount point, from which the block device is accessible in the directory structure. The mount point must exist; if not, create it with the mkdir command.

The third field contains the file-system type, such as xfs or ext4.

The fourth field is the comma-separated list of options to apply to the device. defaults is a set of commonly used options. The mount(8) man page documents the other available options.

The fifth field is used by the dump command to back up the device. Other backup applications do not usually use this field.

The last field, the fsck order field, determines whether to run the fsck command at system boot to verify that the file systems are clean. The value in this field indicates the order in which fsck should run. For XFS file systems, set this field to 0, because XFS does not use fsck to verify its file-system status. For ext4 file systems, set it to 1 for the root file system, and to 2 for the other ext4 file systems. By using this notation, the fsck utility processes the root file system first, and then verifies file systems on separate disks concurrently, and file systems on the same disk in sequence.

Note

An incorrect entry in /etc/fstab might render the machine non-bootable. Verify that an entry is valid by manually unmounting the new file system and then by using mount /mountpoint to read the /etc/fstab file, and remount the file system with that entry's mount options. If the mount command returns an error, then correct it before rebooting the machine.

Alternatively, use the findmnt --verify command to parse the /etc/fstab file for partition usability.

When you add or remove an entry in the /etc/fstab file, run the systemctl daemon-reload command, or reboot the server, to ensure that the systemd daemon loads and uses the new configuration.

[root@host ~]# systemctl daemon-reload

Red Hat recommends the use of UUIDs to persistently mount file systems, because block device names can change in certain scenarios, such as if a cloud provider changes the underlying storage layer of a virtual machine, or if disks are detected in a different order on a system boot. The block device file name might change, but the UUID remains constant in the file-system's super block.

Use the lsblk --fs command to scan the block devices that are connected to a machine and retrieve the file-system UUIDs.

[root@host ~]# lsblk --fs
NAME   FSTYPE  FSVER  LABEL    UUID            FSAVAIL FSUSE% MOUNTPOINTS
vda
├─vda1
├─vda2 xfs            boot     49dd...75fdf    312M    37%    /boot
└─vda3 xfs            root     8a90...ce0da    4.8G    48%    /

 

References

info parted (GNU Parted User Manual)

parted(8), mkfs(8), mount(8), lsblk(8), and fstab(5) man pages

For more information, refer to the Configuring and Managing File Systems guide at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/managing_file_systems/index

Revision: rh199-9.0-4fecb06