[RHCSA] 04 – Configure local storage

    04.1 – List, create, delete partitions on MBR and GPT disks

    Overview

    Managing disk partitions is a fundamental skill for system administration. RHEL supports both MBR (Master Boot Record) and GPT (GUID Partition Table) partitioning schemes. The tools commonly used are fdisk for MBR disks and gdisk or parted for GPT disks.


    Identifying Disk Partitioning Scheme

    Before manipulating partitions, identify whether a disk uses MBR or GPT.

    sudo parted /dev/sda print

    Check the “Partition Table” field in the output:

    • msdos: MBR
    • gpt: GPT

    Listing Partitions

    Using fdisk (For MBR Disks)

    sudo fdisk -l /dev/sda

    Using gdisk (For GPT Disks)

    sudo gdisk -l /dev/sda

    Using parted (For Both MBR and GPT Disks)

    sudo parted /dev/sda print

    Creating Partitions

    Using fdisk (For MBR Disks)

    1. Start fdisk on the target disk:sudo fdisk /dev/sda
    2. Enter Command Mode:
      • Type n to create a new partition.
      • Choose p for a primary partition or e for an extended partition.
      • Enter the partition number (e.g., 1).
      • Set the first sector (press Enter for default).
      • Set the last sector or specify the size (e.g., +1G for a 1GB partition).
    3. Write Changes:
      • Type w to write the changes and exit.

    Example Session:

    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): p
    Partition number (2-4, default 2): 2
    First sector (2048-2097151, default 2048): [Press Enter]
    Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151): +1G
    
    Command (m for help): w

    Using gdisk (For GPT Disks)

    1. Start gdisk on the target disk:
    sudo gdisk /dev/sda
    1. Enter Command Mode:
      • Type n to create a new partition.
      • Accept the default partition number (or specify).
      • Set the first sector (press Enter for default).
      • Set the last sector or size (e.g., +2G).
      • Enter the hex code for the partition type (e.g., 8300 for Linux filesystem).
    2. Write Changes:
      • Type w to write changes and exit.

    Example Session:

    Command: n
    Partition number [1]: [Press Enter]
    First sector: [Press Enter]
    Last sector (+size{K,M,G,T,P}): +2G
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300): [Press Enter]
    
    Command: w

    Using parted (For Both MBR and GPT Disks)

    • Start parted on the target disk:
    sudo parted /dev/sda
    • Set the Unit to GiB (Optional):
    (parted) unit GiB
    • Create Partition:
    (parted) mkpart primary ext4 1 3
    • This creates a primary partition of type ext4 from 1GiB to 3GiB.
    • Quit parted:
    (parted) quit

    Deleting Partitions

    Using fdisk (For MBR Disks)

    1. Start fdisk:
    sudo fdisk /dev/sda
    1. Delete Partition:
      • Type d to delete a partition.
      • Enter the partition number.
    2. Write Changes:
      • Type w to save and exit.

    Example Session:

    Command (m for help): d
    Partition number (1-4): 2
    
    Command (m for help): w

    Using gdisk (For GPT Disks)

    1. Start gdisk:
    sudo gdisk /dev/sda
    1. Delete Partition:
      • Type d and specify the partition number.
    2. Write Changes:
      • Type w to save and exit.

    Example Session:

    Command: d
    Partition number (1-128): 2
    
    Command: w

    Using parted (For Both MBR and GPT Disks)

    • Start parted:
    sudo parted /dev/sda
    • Delete Partition:
    (parted) rm 2
    • Quit parted:
    (parted) quit

    Updating Kernel Partition Table

    After making changes, you may need to inform the kernel of the partition table changes.

    sudo partprobe /dev/sda

    Formatting New Partitions

    • Create a Filesystem:
    sudo mkfs.ext4 /dev/sda2
    • Create Mount Point:
    sudo mkdir /mnt/new_partition
    • Mount the Partition:
    sudo mount /dev/sda2 /mnt/new_partition

    Verifying Partitions

    • Using lsblk:
    lsblk
    • Using blkid:
    sudo blkid
    • View Partition Table:
    sudo fdisk -l /dev/sda

    Important Notes

    • Always back up data before modifying disk partitions.
    • Ensure you’re working on the correct disk (/dev/sda used as an example).
    • MBR Limitations:
      • Supports up to 4 primary partitions.
      • Maximum disk size of 2 TiB.
    • GPT Advantages:
      • Supports up to 128 partitions by default.
      • Supports disks larger than 2 TiB.
    • Use gdisk or parted for GPT disks, as fdisk may have limited support.

    Practice Tasks

    1. Create a New GPT Partition:
      • Use gdisk to create a 5GB partition on /dev/sdb.
      • Format it with xfs.
      • Mount it to /mnt/data.
    2. Delete an MBR Partition:
      • Use fdisk to delete partition /dev/sdc3.
    3. Convert an MBR Disk to GPT:
      • Use gdisk to convert /dev/sdd from MBR to GPT.

    Conclusion

    Being proficient in partition management is essential for the RHCSA exam. Practice creating, listing, and deleting partitions using different tools to become comfortable with both MBR and GPT disks.

    04.2 – Create and remove physical volumes

    Overview

    Physical volumes (PVs) are the building blocks of the Logical Volume Manager (LVM). They are the storage devices (hard drives, partitions, etc.) that provide space for volume groups and logical volumes. Managing PVs involves initializing disks or partitions for use with LVM and removing them when they are no longer needed.


    Creating Physical Volumes

    Step 1: Identify Available Storage Devices

    List all block devices to find the disk or partition you want to use:

    sudo lsblk

    Alternatively, view existing partitions:

    sudo fdisk -l

    Step 2: Prepare the Disk (If Necessary)

    If using an entire disk, you can skip this step. If using a partition, ensure it’s created and set to the correct type.

    • Using fdisk to create a partition:
    sudo fdisk /dev/sdb

    Create a new partition (n), select primary or extended, choose the partition number, accept the default first and last sectors or specify the size (e.g., +10G), change the partition type to Linux LVM (t and enter 8e), then write the changes (w).

    Step 3: Initialize the Physical Volume

    Use the pvcreate command to initialize the disk or partition for LVM.

    Syntax

    sudo pvcreate <device>

    Examples

    • Initialize an entire disk:
    sudo pvcreate /dev/sdb
    • Initialize a partition:
    sudo pvcreate /dev/sdb1

    Sample Output:

      Physical volume “/dev/sdb1” successfully created.

    Step 4: Verify Physical Volumes

    • List all physical volumes:
    sudo pvs
    • Display detailed information:
    sudo pvdisplay

    Sample pvs Output:

    PV         VG       Fmt  Attr PSize   PFree
    /dev/sdb1  vg_data  lvm2 a--  100.00g 100.00g

    Removing Physical Volumes

    Important Considerations

    • Data Safety: Ensure that the physical volume is not in use by any volume group or logical volume before removing it.
    • Backups: Always back up any important data before modifying storage configurations.

    Step 1: Move Data Off the Physical Volume (If Necessary)

    If the PV is part of a volume group and contains data, you need to move the data to another PV:

    sudo pvmove /dev/sdb1

    Step 2: Remove the Physical Volume from the Volume Group

    Use vgreduce to remove the PV from its volume group:

    sudo vgreduce <vg_name> <device>

    Example:

    sudo vgreduce vg_data /dev/sdb1

    Step 3: Delete the Physical Volume

    Use pvremove to remove the LVM label and delete the PV:

    sudo pvremove <device>

    Example:

    sudo pvremove /dev/sdb1

    Sample Output:

      Labels on physical volume “/dev/sdb1” successfully wiped.

    Step 4: Verify Removal

    • List physical volumes to ensure it’s gone:
    sudo pvs

    Practical Examples

    Example 1: Create a Physical Volume on a New Disk

    Assuming /dev/sdc is a new disk you want to use with LVM.

    • Initialize the Disk as a Physical Volume:
    sudo pvcreate /dev/sdc
    • Verify:
    sudo pvs

    Example 2: Remove a Physical Volume Safely

    Assuming /dev/sdd1 is a PV that’s part of vg_backup and you want to remove it.

    • Move Data Off the PV:
    sudo pvmove /dev/sdd1
    • Remove PV from Volume Group:
    sudo vgreduce vg_backup /dev/sdd1
    • Delete the Physical Volume:
    sudo pvremove /dev/sdd1
    • Verify:
    sudo pvs

    Additional Information

    Viewing Detailed PV Information

    • PV Attributes:
    sudo pvdisplay /dev/sdb1

    Partition Types for LVM

    • MBR Disks: Set partition type to 8e (Linux LVM) using fdisk.
    • GPT Disks: Use partition type GUID 8e00 with tools like gdisk.

    Wiping a Disk or Partition

    If you need to completely wipe a disk or partition:

    sudo dd if=/dev/zero of=/dev/sdb bs=1M count=100

    Tips and Best Practices

    • Consistent Naming: Label your physical volumes for easier identification.
    • Monitoring: Regularly check your PVs, VGs, and LVs using pvsvgs, and lvs.
    • Avoid Data Loss: Always double-check device names before running commands that modify disk data.
    • Backups: Maintain regular backups of your data.

    Conclusion

    Creating and removing physical volumes is essential for managing storage using LVM in RHEL. Mastery of these tasks ensures efficient utilization of storage resources and is crucial for the RHCSA exam.


    These notes should help you understand how to create and remove physical volumes for the RHCSA exam.

    04.3 – Assign physical volumes to volume groups

    Overview

    In the Logical Volume Manager (LVM), a Volume Group (VG) is a pool of storage that combines one or more Physical Volumes (PVs). Assigning PVs to a VG allows you to create Logical Volumes (LVs) that can use the combined storage space of the PVs. This is essential for flexible disk management in Linux.


    Creating a Volume Group

    Syntax

    To create a volume group, use the vgcreate command:

    sudo vgcreate <volume_group_name> <physical_volumes>
    • <volume_group_name>: The name you want to give to your volume group.
    • <physical_volumes>: The physical volumes you want to include in the volume group (e.g., /dev/sdb1).

    Example

    Assuming you have two physical volumes /dev/sdb1 and /dev/sdc1, and you want to create a volume group named vg_data:

    sudo vgcreate vg_data /dev/sdb1 /dev/sdc1

    Sample Output:

      Volume group “vg_data” successfully created


    Verifying Volume Groups and Physical Volumes

    List Volume Groups

    To list all volume groups:

    sudo vgs

    Sample Output:

    VG      #PV #LV #SN Attr   VSize   VFree  
    vg_data   2   0   0 wz--n- 199.99g 199.99g

    Display Detailed Information

    To display detailed information about a volume group:

    sudo vgdisplay vg_data

    List Physical Volumes

    To see which PVs are assigned to which VGs:

    sudo pvs

    Sample Output:

    PV         VG      Fmt  Attr PSize   PFree  
    /dev/sdb1  vg_data lvm2 a--  100.00g 100.00g
    /dev/sdc1  vg_data lvm2 a--   99.99g  99.99g

    Adding Physical Volumes to an Existing Volume Group

    You can extend an existing volume group by adding more physical volumes using the vgextend command.

    Syntax

    sudo vgextend <volume_group_name> <physical_volumes>

    Example

    Add /dev/sdd1 to vg_data:

    sudo vgextend vg_data /dev/sdd1

    Sample Output:

      Volume group “vg_data” successfully extended

    Verify the Extension

    Check the volume group size after extension:

    sudo vgdisplay vg_data

    Removing Physical Volumes from a Volume Group

    If you need to remove a physical volume from a volume group, use the vgreduce command.

    Syntax

    sudo vgreduce <volume_group_name> <physical_volume>

    Example

    Remove /dev/sdc1 from vg_data:

    • Ensure the PV is not in use by moving any data off it:
    sudo pvmove /dev/sdc1
    • Remove the PV from the VG:
    sudo vgreduce vg_data /dev/sdc1

    Sample Output:

      Removed “/dev/sdc1” from volume group “vg_data”


    Practical Examples

    Example 1: Create a Volume Group with Multiple PVs

    • Initialize Physical Volumes:
    sudo pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
    • Create Volume Group:
    sudo vgcreate vg_storage /dev/sdb1 /dev/sdc1 /dev/sdd1
    • Verify:
    sudo vgs

    Example 2: Extend a Volume Group

    • Initialize New Physical Volume:
    sudo pvcreate /dev/sde1
    • Extend Volume Group:
    sudo vgextend vg_storage /dev/sde1
    • Verify Updated Size:
    sudo vgdisplay vg_storage

    Example 3: Remove a Physical Volume Safely

    • Move Data Off the PV (if necessary):
    sudo pvmove /dev/sdb1
    • Remove PV from Volume Group:
    sudo vgreduce vg_storage /dev/sdb1
    • Verify:
    sudo vgs

    Handling Errors and Troubleshooting

    • Error: Physical Volume in UseIf you get an error stating the PV is in use, ensure that no logical volumes are using the space.
    • Check Which LVs are on a PV:
    sudo pvdisplay -m /dev/sdb1

    Additional Commands

    • Display Volume Group Attributes:
    sudo vgdisplay
    • Rename a Volume Group:
    sudo vgrename old_name new_name
    • Remove a Volume Group:
    sudo vgremove vg_name
    • Note: You must remove all logical volumes in the VG first.

    Tips and Best Practices

    • Consistent Naming Conventions: Use meaningful names for VGs to identify their purpose.
    • Monitoring Free Space:Check how much free space is available in a VG:sudo vgdisplay vg_data | grep “Free  PE”
    • Plan for Growth:When setting up storage, consider future expansion by leaving room to add more PVs to the VG.
    • Backup Important Data: Always back up data before making changes to storage configurations.

    Conclusion

    Assigning physical volumes to volume groups is a key step in managing storage with LVM. By understanding how to create, extend, and reduce volume groups, you can efficiently manage disk space and provide flexible storage solutions.


    These notes should help you understand how to assign physical volumes to volume groups for the RHCSA exam.

    04.4 – Create and delete logical volumes

    Overview

    Logical Volumes (LVs) are a key component of the Logical Volume Manager (LVM) in Linux. They provide a flexible way to manage disk storage by abstracting physical storage devices into logical ones. This allows for dynamic resizing, snapshots, and easier management of disk space.


    Prerequisites

    • Physical Volumes (PVs): Physical disks or partitions initialized for LVM use with pvcreate.
    • Volume Group (VG): A pool of storage created from one or more PVs using vgcreate.

    Creating Logical Volumes

    Step 1: Ensure a Volume Group Exists

    List existing volume groups:

    sudo vgs

    If no volume group exists, create one:

    sudo vgcreate <vg_name> <physical_volume(s)>

    Example:

    sudo vgcreate vg_data /dev/sdb1 /dev/sdc1

    Step 2: Create a Logical Volume

    Use the lvcreate command to create a logical volume.

    Syntax

    sudo lvcreate -L <size> -n <lv_name> <vg_name>
    • -L <size>: Specify the size (e.g., 10G for 10 GB).
    • -n <lv_name>: Name of the logical volume.
    • <vg_name>: Name of the volume group.

    Examples

    1. Create a 10 GB Logical Volumesudo lvcreate -L 10G -n lv_data vg_dataSample Output:  Logical volume “lv_data” created.
    2. Create a Logical Volume Using 100% of Free Spacesudo lvcreate -l 100%FREE -n lv_backup vg_backupNotes:
      • -l: Specifies the size in logical extents (using percentages like 100%FREE).

    Step 3: Verify Logical Volume Creation

    List logical volumes:

    sudo lvs

    Sample Output:

    LV       VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    
    lv_data  vg_data  -wi-a-----  10.00g

    Display detailed information:

    sudo lvdisplay /dev/vg_data/lv_data

    Step 4: Create a Filesystem on the Logical Volume

    Before you can use the logical volume, create a filesystem on it.

    Examples

    • Create an EXT4 Filesystem
    sudo mkfs.ext4 /dev/vg_data/lv_data
    • Create an XFS Filesystem
    sudo mkfs.xfs /dev/vg_data/lv_data

    Step 5: Mount the Logical Volume

    • Create a Mount Point
    sudo mkdir /mnt/data
    1. Mount the Filesystem
    sudo mount /dev/vg_data/lv_data /mnt/data
    • Verify Mount
    df -h /mnt/data

    Step 6: Make the Mount Permanent (Optional)

    Add an entry to /etc/fstab:

    sudo nano /etc/fstab

    Add the following line:

    /dev/vg_data/lv_data  /mnt/data  ext4  defaults  0 0

    Deleting Logical Volumes

    Warning: Deleting a logical volume destroys all data on it. Ensure you have backups if necessary.

    Step 1: Unmount the Logical Volume

    sudo umount /mnt/data

    Step 2: Remove the Logical Volume

    Use the lvremove command.

    Syntax

    sudo lvremove /dev/<vg_name>/<lv_name>

    Example

    sudo lvremove /dev/vg_data/lv_data

    Sample Output:

    Do you really want to remove active logical volume vg_data/lv_data? [y/n]: y
      Logical volume "lv_data" successfully removed

    Step 3: Verify Removal

    List logical volumes:

    sudo lvs

    The logical volume should no longer appear in the list.


    Practical Examples

    Example 1: Create and Mount a New Logical Volume

    • Create a 5 GB Logical Volume
    sudo lvcreate -L 5G -n lv_logs vg_data
    • Format with XFS Filesystem
    sudo mkfs.xfs /dev/vg_data/lv_logs
    • Create Mount Point
    sudo mkdir /var/logs
    • Mount the Logical Volume
    sudo mount /dev/vg_data/lv_logs /var/logs
    • Update /etc/fstab
    echo '/dev/vg_data/lv_logs /var/logs xfs defaults 0 0' | sudo tee -a /etc/fstab

    Example 2: Delete an Unused Logical Volume

    • Unmount the Logical Volume
    sudo umount /var/logs
    • Remove the Logical Volume
    sudo lvremove /dev/vg_data/lv_logs
    • Remove Entry from /etc/fstab
    sudo sed -i '/lv_logs/d' /etc/fstab
    • Remove the Mount Point
    sudo rmdir /var/logs

    Resizing Logical Volumes (Additional Topic)

    Extend a Logical Volume

    • Extend the Logical Volume
    sudo lvextend -L +5G /dev/vg_data/lv_data
    • Resize the Filesystem
      • For EXT4:
    sudo resize2fs /dev/vg_data/lv_data
    • For XFS:
    sudo xfs_growfs /mnt/data

    Reduce a Logical Volume

    Warning: Reducing a logical volume can cause data loss. Ensure you have a backup.

    • Shrink the Filesystem
      • For EXT4:
    sudo umount /mnt/datasudo e2fsck -f /dev/vg_data/lv_datasudo resize2fs /dev/vg_data/lv_data 5G
    1. XFS filesystems cannot be shrunk.
    2. Reduce the Logical Volume
    sudo lvreduce -L 5G /dev/vg_data/lv_data
    • Mount the Filesystem
    sudo mount /dev/vg_data/lv_data /mnt/data

    Tips and Best Practices

    • Naming Conventions: Use descriptive names for volume groups and logical volumes.
    • Monitoring: Regularly check available space using lvs and df -h.
    • Backups: Always have recent backups before resizing or deleting logical volumes.
    • Alignment: When creating LVs on SSDs or RAID arrays, ensure proper alignment for performance.

    Troubleshooting

    • Logical Volume Not Found:Ensure the VG is active:sudo vgchange -a y vg_data
    • Unable to Remove LV:
      • Make sure it’s not mounted.
      • Check for open files using lsof or fuser.

    Conclusion

    Creating and deleting logical volumes allows for flexible and dynamic disk management in Linux. Mastery of LVM commands enhances a system administrator’s ability to efficiently manage storage resources, which is essential for the RHCSA exam.


    These notes should help you understand how to create and delete logical volumes for the RHCSA exam.

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

    Overview

    Mounting file systems at boot ensures that they are available for use immediately after the system starts. Using UUIDs or labels to identify file systems in the /etc/fstab file provides a more reliable method than using device names, which can change between reboots.


    Identifying UUIDs and Labels

    List UUIDs and Labels

    Use the blkid command to list the UUIDs and labels of all file systems:

    sudo blkid

    Sample Output:

    /dev/sda1: UUID="123e4567-e89b-12d3-a456-426614174000" TYPE="ext4" PARTUUID="00000000-01"
    /dev/sda2: UUID="123e4567-e89b-12d3-a456-426614174001" TYPE="swap" PARTUUID="00000000-02"
    /dev/sdb1: UUID="123e4567-e89b-12d3-a456-426614174002" TYPE="xfs" PARTUUID="00000000-03" LABEL="data"

    View UUID of a Specific Device

    sudo blkid /dev/sda1

    View Label of a Specific Device

    sudo e2label /dev/sda1

    For XFS file systems:

    sudo xfs_admin -l /dev/sda1

    Configuring /etc/fstab

    The /etc/fstab file contains information about file systems and their mount points. Each line in /etc/fstab corresponds to a file system and specifies how and where it should be mounted.

    /etc/fstab Format

    <file system> <mount point> <type> <options> <dump> <pass>

    • <file system>: The UUID or label of the file system.
    • <mount point>: The directory where the file system will be mounted.
    • <type>: The file system type (e.g., ext4xfs).
    • <options>: Mount options (e.g., defaultsnoatime).
    • <dump>: Backup operation (0 or 1).
    • <pass>: File system check order (0, 1, or 2).

    Example: Mount by UUID

    • Identify the UUID
    sudo blkid /dev/sdb1

    Sample Output:

    /dev/sdb1: UUID="123e4567-e89b-12d3-a456-426614174002" TYPE="xfs"
    • Edit /etc/fstab
    sudo nano /etc/fstab
    • Add the Entry
    UUID=123e4567-e89b-12d3-a456-426614174002  /mnt/data  xfs  defaults  0  0
    1. Save and Exit Save the file and exit the editor.

    Example: Mount by Label

    • Identify the Label
    sudo blkid /dev/sdb1
    • Sample Output:
    /dev/sdb1: LABEL="data" UUID="123e4567-e89b-12d3-a456-426614174002" TYPE="xfs"
    • Edit /etc/fstab
    sudo nano /etc/fstab
    • Add the Entry
    LABEL=data  /mnt/data  xfs  defaults  0  0
    1. Save and Exit
    2. Save the file and exit the editor.

    Applying Changes

    Create the Mount Point

    Ensure the mount point directory exists:

    sudo mkdir -p /mnt/data

    Mount All File Systems

    Apply the changes without rebooting:

    sudo mount -a

    Verify the Mount

    Check if the file system is mounted:

    df -h /mnt/data

    Sample Output:

    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdb1       100G   1G   99G   1%  /mnt/data

    Practical Examples

    Example 1: Mount an EXT4 File System by UUID

    • Identify the UUID
    sudo blkid /dev/sda1

    Sample Output:

    /dev/sda1: UUID="123e4567-e89b-12d3-a456-426614174000" TYPE="ext4"
    • Edit /etc/fstab
    sudo nano /etc/fstab
    • Add the Entry
    UUID=123e4567-e89b-12d3-a456-426614174000  /mnt/backup  ext4  defaults  0  2
    • Create the Mount Point
    sudo mkdir -p /mnt/backup
    1. Mount All File Systems
    sudo mount -a
    • Verify the Mount
    df -h /mnt/backup

    Example 2: Mount an XFS File System by Label

    • Identify the Label
    sudo blkid /dev/sdb1

    Sample Output:

    /dev/sdb1: LABEL="data" UUID="123e4567-e89b-12d3-a456-426614174002" TYPE="xfs"
    • Edit /etc/fstab
    sudo nano /etc/fstab
    • Add the Entry
    LABEL=data  /mnt/data  xfs  defaults  0  2
    • Create the Mount Point
    sudo mkdir -p /mnt/data
    • Mount All File Systems
    sudo mount -a
    • Verify the Mount
    df -h /mnt/data

    Additional Tips

    • Use lsblk to Verify Mount Points:
    lsblk -f
    • Check /etc/fstab Syntax:Ensure there are no syntax errors in /etc/fstab to avoid boot issues.
    • Backup /etc/fstab:Before making changes, create a backup:
    sudo cp /etc/fstab /etc/fstab.bak
    • Unmount File Systems:If needed, unmount a file system:
    sudo umount /mnt/data

    Conclusion

    Configuring systems to mount file systems at boot by UUID or label ensures reliable and consistent mounting, even if device names change. Mastery of this task is essential for efficient system administration and is a key competency for the RHCSA exam.


    These notes should help you understand how to configure systems to mount file systems at boot by UUID or label for the RHCSA exam.

    04.6 – Add new partitions and logical volumes, and swap to a system non-destructively

    Overview

    Adding new partitions, logical volumes, and swap space to a system without destroying existing data is a common task in system administration. This process involves creating new partitions, initializing them for use with LVM, creating logical volumes, and configuring swap space.


    Adding New Partitions

    Step 1: Identify Available Storage

    List all block devices to find the disk where you want to create a new partition:

    sudo lsblk

    Step 2: Create a New Partition

    Use fdisk or parted to create a new partition.

    Using fdisk

    1. Start fdisk on the target disk:
    sudo fdisk /dev/sdb
    1. Create a New Partition:
      • Type n to create a new partition.
      • Choose p for a primary partition or e for an extended partition.
      • Enter the partition number (e.g., 1).
      • Set the first sector (press Enter for default).
      • Set the last sector or specify the size (e.g., +10G for a 10GB partition).
    2. Change the Partition Type to Linux LVM:
      • Type t to change the partition type.
      • Enter the partition number.
      • Type 8e for Linux LVM.
    3. Write Changes:
      • Type w to write the changes and exit.

    Example Session:

    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-20971519, default 2048): [Press Enter]
    Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +10G
    Command (m for help): t
    Partition number (1-4, default 1): 1
    Hex code (type L to list all codes): 8e
    
    Command (m for help): w

    Using parted

    • Start parted on the target disk:
    sudo parted /dev/sdb
    • Create a New Partition:
    (parted) mkpart primary ext4 1MiB 10GiB
    • Set the Partition Type to LVM:
    (parted) set 1 lvm on
    • Quit parted:
    (parted) quit

    Creating Logical Volumes

    Step 1: Initialize the New Partition as a Physical Volume

    sudo pvcreate /dev/sdb1

    Step 2: Create or Extend a Volume Group

    Create a New Volume Group

    sudo vgcreate vg_data /dev/sdb1

    Extend an Existing Volume Group

    sudo vgextend vg_data /dev/sdb1

    Step 3: Create a Logical Volume

    Syntax

    sudo lvcreate -L <size> -n <lv_name> <vg_name>

    Example

    Create a 5GB logical volume named lv_data in the vg_data volume group:

    sudo lvcreate -L 5G -n lv_data vg_data

    Step 4: Create a Filesystem on the Logical Volume

    Example

    Create an EXT4 filesystem:

    sudo mkfs.ext4 /dev/vg_data/lv_data

    Step 5: Mount the Logical Volume

    • Create a Mount Point:
    sudo mkdir /mnt/data
    • Mount the Filesystem:
    sudo mount /dev/vg_data/lv_data /mnt/data
    • Verify the Mount:
    df -h /mnt/data

    Step 6: Make the Mount Permanent

    Add an entry to /etc/fstab:

    sudo nano /etc/fstab

    Add the following line:

    /dev/vg_data/lv_data  /mnt/data  ext4  defaults  0  0

    Adding Swap Space

    Step 1: Create a Swap Partition or Logical Volume

    Using a Partition

    1. Create a New Partition (as shown in the “Adding New Partitions” section).
    2. Initialize the Partition for Swap:
    sudo mkswap /dev/sdb2

    Using a Logical Volume

    • Create a Logical Volume for Swap:
    sudo lvcreate -L 2G -n lv_swap vg_data
    • Initialize the Logical Volume for Swap:
    sudo mkswap /dev/vg_data/lv_swap

    Step 2: Enable the Swap Space

    sudo swapon /dev/vg_data/lv_swap

    Step 3: Make the Swap Permanent

    Add an entry to /etc/fstab:

    sudo nano /etc/fstab

    Add the following line:

    /dev/vg_data/lv_swap  swap  swap  defaults  0  0

    Step 4: Verify Swap Space

    sudo swapon --show

    Sample Output:

    NAME                TYPE      SIZE USED PRIO
    /dev/dm-1           partition 2G   0B   -2

    Practical Examples

    Example 1: Add a New Partition and Logical Volume

    • Create a New Partition:
    sudo fdisk /dev/sdb

    Follow the steps to create a new partition and set it to type 8e (Linux LVM).

    • Initialize the Partition as a Physical Volume:
    sudo pvcreate /dev/sdb1
    • Create a Volume Group:
    sudo vgcreate vg_data /dev/sdb1
    • Create a Logical Volume:
    sudo lvcreate -L 10G -n lv_data vg_data
    • Create a Filesystem:
    sudo mkfs.ext4 /dev/vg_data/lv_data
    • Mount the Logical Volume:
    sudo mkdir /mnt/data<br>sudo mount /dev/vg_data/lv_data /mnt/data
    • Update /etc/fstab:
    echo '/dev/vg_data/lv_data /mnt/data ext4 defaults 0 0' | sudo tee -a /etc/fstab

    Example 2: Add Swap Space Using a Logical Volume

    • Create a Logical Volume for Swap:
    sudo lvcreate -L 2G -n lv_swap vg_data
    • Initialize the Logical Volume for Swap:
    sudo mkswap /dev/vg_data/lv_swap
    • Enable the Swap Space:
    sudo swapon /dev/vg_data/lv_swap
    • Update /etc/fstab:
    echo '/dev/vg_data/lv_swap swap swap defaults 0 0' | sudo tee -a /etc/fstab
    • Verify Swap Space:
    sudo swapon --show

    Additional Tips

    • Backup Data: Always back up important data before modifying partitions or logical volumes.
    • Check Free Space: Ensure there is enough free space in the volume group before creating new logical volumes.
    • Monitor Disk Usage: Use df -h and lvs to monitor disk usage and logical volume status.
    • Use lsblk: To visualize the block devices and their mount points.

    Conclusion

    Adding new partitions, logical volumes, and swap space non-destructively is a crucial skill for system administration. Mastery of these tasks ensures efficient and flexible disk management, which is essential for the RHCSA exam.


    These notes should help you understand how to add new partitions, logical volumes, and swap to a system non-destructively for the RHCSA exam.


    Discover more from Altgr Blog

    Subscribe to get the latest posts sent to your email.

    Leave a Reply

    Your email address will not be published. Required fields are marked *