Certified Series: Red Hat #3

RHCSA Exam Preparedness: Disk Management

This is a huge subject including partitions, physical and logical volumes, as well as file systems. Please follow the video series ( 1, 2, 3, 4) by Ralph Nyberg for a comprehensive “lecture” on these Red Hat Certified Systems Administrator certification exam objectives:

  • List, create, and delete partitions on MBR and GPT disks
  • Add new partitions and logical volumes and swap to a system non-destructively
  • Create, mount, unmount, and use vfat, ext4, and xfs file systems
  • Create and remove physical and logical volumes assigned to volume groups
  • Extend existing logical volumes

(Virtual Machine Manager and KVM were used for lab purposes. Additional drive space was added to the virtual machine by adding storage hardware within Virtual Machine Manager.)

MBR

  • 2 TB maximum partition size
  • Allows up to 4 primary partitions, one of which is an extended partition
  • Can create additional logical partitions/volumes within extended partitions
  • fdisk can be used to edit MBR partitions

List MBR partitions:

fdisk -l

Enter a device such as sdb:

fdisk /dev/sdb

List options with help:

m

Add a new partition:

n

Select primary (p) or extended (e):

p

Enter the Partition number:

1

Enter the first sector:

2048 (default)

Enter the last sector, +sectors or +size {K,H,G}:

+512MB

View and verify the created partition with print:

p

Rinse and repeat the process to create two more primary partitions:

n
p
2
1001472 (default)
+512MB
p
n
p
3
2000896 (default)
+512MB
p

At this point, no more primary partitions are available. An extended partition must now be utilized to accommodate additional logical partitions/volumes:

n
e
4
3000320 (default)
+512MB
p
n
3002368 (default)
+256MB
p

The last partition needs to be converted to Swap space by changing the Fifth Partition’s Id or type:

m
t
5
L
82
p

Finally, write the partition table to disk, and exit the fdisk utility:

w

It is recommended to run partprobe after exiting fdisk or gdisk:

partprobe

View the partition blocks to verify the changes:

cat /proc/partitions

GPT

  • No constraints on partition size or partition types
  • gdisk can be used to edit GPT partitions

Enter the designated device sdc:

gdisk /dev/sdc

Enter ? for help:

?

Create a new partition:

n

Enter the partition number:

enter (default is 1)

Enter the first sector:

enter (default is 2048)

Enter the last sector, or enter the size in {KMGTP}:

+1G

Enter the hex code for the Linux Filesystem. Use L to show the code options:

L
enter (default is 8300 for the Linux filesystem)

Use p to verify the creation of the GPT partition:

p

Create two other Linux filesystem partitions:

n
2
enter (default is 2099200)
+1G
enter (default is 8300 for the Linux filesystem)
p
n
3
enter (default is 4196352)
+1G
enter (default is 8300 for the Linux filesystem)
p

Create a smaller Linux filesystem partition to be changed to a swap partition:

n
4
enter (default is 6293504)
+256M
enter (default is 8300 for the Linux filesystem)
p

Change the fourth partition to type Linux swap:

?
t
L
8200
p

After the changes have been verified, write the table to disk and exit:

?
w

Run partprobe and verify the partition table changes:

partprobe
cat /proc/partitions
lsblk

Create filesystems on partitions

List available filesystems that can be created:

mkfs -t 'tab'

create an xfs filesystem on sdb1:

mkfs.xfs /dev/sdb1

Mount the sdb1 filesystem by first creating a mount point:

mkdir /entfs1
mount /dev/sdb1 /entfs1/

Verify that the partition successfully mounted using the df with the type and human readable options:

df -Th

Dismount the filesystem prior to making persistent changes to the fstab file:

umount /dev/sdb1

These changes need to be made to the /etc/fstab for persistence after restarting. Add the following to the /etc/fstab file:

# UUID/Label - Mount Point - Filesystem - Options   
/dev/sdb1    /entfs1       xfs          defaults  0 0

Vim users can mount filesystems in the fstab file by importing the UUID using blkid from inside vim:

:!blkid

The UUID can be copied and pasted into the fstab file, or the output of blkid can be copied into the vim session using :r with blkid:

:r!blkid

Delete the unnecessary lines and information so that the fstab file line looks as follows to utilize the UUID instead of the Label:

UUID="62285499-234d-342-32223141" /entfs1 xfs defaults 0 0

Test the changes made to the fstab file using the -a option with the mount command and look for “successfully mounted”:

mount -av

Let’s make a few more directories to mount more partitions on:

mkdir /entfs{2..6}

Create an ext4 filesystem on sdb2, mount the partition under /entfs2, and verify the changes:

mkfs.ext4 /dev/sdb2
mount /dev/sdb2 /entfs2/
df -Th

Dismount the filesystem and partition prior to making the change persistent by editing the fstab file:

umount /dev/sdb2

Edit /etc/fstab by adding a line to mount sdb2 persistently by UUID:

vim /etc/fstab
:r!blkid (dd and delete until the UUID remains)

The line should appear as:

UUID=000ea07e-672d-44e7-948b-f15356d0cebe /entfs2 ext4 defaults 0 0

Verify the changes:

mount -av
df -Th
fdisk -l /dev/sdb

Create a swap partition with the space on sdb5 and verify its creation:

mkswap /dev/sdb5
swapon -s
swapon /dev/sdb5
swapon -s

Define the swap partition in fstab for persistence:

swapoff /dev/sdb5
vi /etc/fstab

Try using the label method to define the swap partition:

/dev/sdb5  swap  swap  defaults  0 0

After saving the file, test the swap entry in the fstab file:

swapon -a
swapon -s

Verify the changes were a success with the -a option added to mount:

mount -av

Run the verification again after a reboot for peace of mind.

Logical Volume Manager (LVM)

LVM is able to group physical and logical partitions into a single logical volume.

Dismount any mountpoints on sdb or sdc:

umount /entfs{1..2}

Create LVM partitions on sdb1 and sdb2 with fdisk, and create LVM partitions on sdc1 and sdc2 with gdisk:

fdisk /dev/sdb
t
1
8e
p
t
2
8e
p
w
partprobe
gdisk /dev/sdc
t
1
8e00
p
t
2
8e00
p
w
partprobe

Make these LVM partitions available to the Logical Volume Manager with the pv utility:

pvcreate /dev/sdb1
pvcreate /dev/sdb2
pvdisplay /dev/sdb1
pvdisplay /dev/sdb2
pvcreate /dev/sdc1
pvcreate /dev/sdc2
pvdisplay /dev/sdc1
pvdisplay /dev/sdc2

Create the volume group using the vg tool:

vgcreate vg00 /dev/sdb1

View the volume group with vgdisplay, and compare the two partitions with pvdisplay:

vgdisplay vg00
pvdisplay /dev/sdb1
pvdisplay /dev/sdb2

Extend the volume group to include sdb2, sdc1, and sdc2:

vgextend vg00 /dev/sdb2
vgextend vg00 /dev/sdc1
vgextend vg00 /dev/sdc2
vgdisplay vg00
pvdisplay /dev/sdb2
pvdisplay /dev/sdc1
pvdisplay /dev/sdc2

Create logical volumes with the volume group vg00 named lv1 and lv2:

lvcreate -L 256M -n lv1 vg00
lvcreate -L 256M -n lv2 vg00

Place filesystems on the logical volumes lv1 and lv2:

mkfs.ext4 /dev/mapper/vg00-lv1
mkfs.xfs /dev/mapper/vg00-lv2

Mount the logical volumes on a pair of directories created earlier:

mount /dev/mapper/vg00-lv1 /entfs3
mount /dev/mapper/vg00-lv2 /entfs4

Verify that the volumes mounted as anticipated:

df -Th

Dismount the volumes and make these changes persistent in the fstab file. Also update the fstab file for out most recent tasks:

umount /entfs{3..4}
vi /etc/fstab

The lines should look similar to the following:

/dev/mapper/vg00-lv1  /entfs3  ext4  defaults  0 0
/dev/mapper/vg00-lv2  /entfs4  ext4  defaults  0 0

Verify the entries mount successfully by restarting or by running the following:

mount -av
df -Th

We are almost done. Another task to complete is expanding the logical volumes. Let’s start by expanding lv1:

lvextend -L 1G /dev/vg00/lv1
!d

The ext4 filesystem also needs to be expanded to reflect this change:

resize2fs -f /dev/vg00/lv1
!d

Lastly, let us expand logical volume lv2, and expand the xfs filesystem:

lvextend -L 1G /dev/vg00/lv2
xfs_growfs -d /dev/vg00/lv2
!d