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 printCheck the “Partition Table” field in the output:
msdos: MBRgpt: GPT
Listing Partitions
Using fdisk (For MBR Disks)
sudo fdisk -l /dev/sdaUsing gdisk (For GPT Disks)
sudo gdisk -l /dev/sdaUsing parted (For Both MBR and GPT Disks)
sudo parted /dev/sda printCreating Partitions
Using fdisk (For MBR Disks)
- Start
fdiskon the target disk:sudo fdisk /dev/sda - Enter Command Mode:
- Type
nto create a new partition. - Choose
pfor a primary partition orefor an extended partition. - Enter the partition number (e.g.,
1). - Set the first sector (press
Enterfor default). - Set the last sector or specify the size (e.g.,
+1Gfor a 1GB partition).
- Type
- Write Changes:
- Type
wto write the changes and exit.
- Type
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): wUsing gdisk (For GPT Disks)
- Start
gdiskon the target disk:
sudo gdisk /dev/sda- Enter Command Mode:
- Type
nto create a new partition. - Accept the default partition number (or specify).
- Set the first sector (press
Enterfor default). - Set the last sector or size (e.g.,
+2G). - Enter the hex code for the partition type (e.g.,
8300for Linux filesystem).
- Type
- Write Changes:
- Type
wto write changes and exit.
- Type
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: wUsing parted (For Both MBR and GPT Disks)
- Start
partedon 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
ext4from 1GiB to 3GiB. - Quit
parted:
(parted) quitDeleting Partitions
Using fdisk (For MBR Disks)
- Start
fdisk:
sudo fdisk /dev/sda- Delete Partition:
- Type
dto delete a partition. - Enter the partition number.
- Type
- Write Changes:
- Type
wto save and exit.
- Type
Example Session:
Command (m for help): d
Partition number (1-4): 2
Command (m for help): wUsing gdisk (For GPT Disks)
- Start
gdisk:
sudo gdisk /dev/sda- Delete Partition:
- Type
dand specify the partition number.
- Type
- Write Changes:
- Type
wto save and exit.
- Type
Example Session:
Command: d
Partition number (1-128): 2
Command: wUsing parted (For Both MBR and GPT Disks)
- Start
parted:
sudo parted /dev/sda- Delete Partition:
(parted) rm 2- Quit
parted:
(parted) quitUpdating Kernel Partition Table
After making changes, you may need to inform the kernel of the partition table changes.
sudo partprobe /dev/sdaFormatting 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_partitionVerifying Partitions
- Using
lsblk:
lsblk- Using
blkid:
sudo blkid- View Partition Table:
sudo fdisk -l /dev/sdaImportant Notes
- Always back up data before modifying disk partitions.
- Ensure you’re working on the correct disk (
/dev/sdaused 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
gdiskorpartedfor GPT disks, asfdiskmay have limited support.
Practice Tasks
- Create a New GPT Partition:
- Use
gdiskto create a 5GB partition on/dev/sdb. - Format it with
xfs. - Mount it to
/mnt/data.
- Use
- Delete an MBR Partition:
- Use
fdiskto delete partition/dev/sdc3.
- Use
- Convert an MBR Disk to GPT:
- Use
gdiskto convert/dev/sddfrom MBR to GPT.
- Use
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 lsblkAlternatively, view existing partitions:
sudo fdisk -lStep 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
fdiskto create a partition:
sudo fdisk /dev/sdbCreate 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/sdb1Sample Output:
Physical volume “/dev/sdb1” successfully created.
Step 4: Verify Physical Volumes
- List all physical volumes:
sudo pvs- Display detailed information:
sudo pvdisplaySample pvs Output:
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg_data lvm2 a-- 100.00g 100.00gRemoving 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/sdb1Step 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/sdb1Step 3: Delete the Physical Volume
Use pvremove to remove the LVM label and delete the PV:
sudo pvremove <device>Example:
sudo pvremove /dev/sdb1Sample Output:
Labels on physical volume “/dev/sdb1” successfully wiped.
Step 4: Verify Removal
- List physical volumes to ensure it’s gone:
sudo pvsPractical 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 pvsExample 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 pvsAdditional Information
Viewing Detailed PV Information
- PV Attributes:
sudo pvdisplay /dev/sdb1Partition Types for LVM
- MBR Disks: Set partition type to
8e(Linux LVM) usingfdisk. - GPT Disks: Use partition type GUID
8e00with tools likegdisk.
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=100Tips and Best Practices
- Consistent Naming: Label your physical volumes for easier identification.
- Monitoring: Regularly check your PVs, VGs, and LVs using
pvs,vgs, andlvs. - 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/sdc1Sample Output:
Volume group “vg_data” successfully created
Verifying Volume Groups and Physical Volumes
List Volume Groups
To list all volume groups:
sudo vgsSample Output:
VG #PV #LV #SN Attr VSize VFree
vg_data 2 0 0 wz--n- 199.99g 199.99gDisplay Detailed Information
To display detailed information about a volume group:
sudo vgdisplay vg_dataList Physical Volumes
To see which PVs are assigned to which VGs:
sudo pvsSample 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.99gAdding 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/sdd1Sample Output:
Volume group “vg_data” successfully extended
Verify the Extension
Check the volume group size after extension:
sudo vgdisplay vg_dataRemoving 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/sdc1Sample 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 vgsExample 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 vgsHandling 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/sdb1Additional 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 vgsIf no volume group exists, create one:
sudo vgcreate <vg_name> <physical_volume(s)>Example:
sudo vgcreate vg_data /dev/sdb1 /dev/sdc1Step 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.,10Gfor 10 GB).-n <lv_name>: Name of the logical volume.<vg_name>: Name of the volume group.
Examples
- Create a 10 GB Logical Volumesudo lvcreate -L 10G -n lv_data vg_dataSample Output: Logical volume “lv_data” created.
- 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 like100%FREE).
Step 3: Verify Logical Volume Creation
List logical volumes:
sudo lvsSample Output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_data vg_data -wi-a----- 10.00gDisplay detailed information:
sudo lvdisplay /dev/vg_data/lv_dataStep 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_dataStep 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 Mount
df -h /mnt/dataStep 6: Make the Mount Permanent (Optional)
Add an entry to /etc/fstab:
sudo nano /etc/fstabAdd the following line:
/dev/vg_data/lv_data /mnt/data ext4 defaults 0 0Deleting 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/dataStep 2: Remove the Logical Volume
Use the lvremove command.
Syntax
sudo lvremove /dev/<vg_name>/<lv_name>Example
sudo lvremove /dev/vg_data/lv_dataSample Output:
Do you really want to remove active logical volume vg_data/lv_data? [y/n]: y
Logical volume "lv_data" successfully removedStep 3: Verify Removal
List logical volumes:
sudo lvsThe 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/fstabExample 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/logsResizing 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/dataReduce 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- XFS filesystems cannot be shrunk.
- Reduce the Logical Volume
sudo lvreduce -L 5G /dev/vg_data/lv_data- Mount the Filesystem
sudo mount /dev/vg_data/lv_data /mnt/dataTips and Best Practices
- Naming Conventions: Use descriptive names for volume groups and logical volumes.
- Monitoring: Regularly check available space using
lvsanddf -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
lsoforfuser.
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 blkidSample 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/sda1View Label of a Specific Device
sudo e2label /dev/sda1For XFS file systems:
sudo xfs_admin -l /dev/sda1Configuring /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.,ext4,xfs).<options>: Mount options (e.g.,defaults,noatime).<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/sdb1Sample 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- 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- Save and Exit
- Save the file and exit the editor.
Applying Changes
Create the Mount Point
Ensure the mount point directory exists:
sudo mkdir -p /mnt/dataMount All File Systems
Apply the changes without rebooting:
sudo mount -aVerify the Mount
Check if the file system is mounted:
df -h /mnt/dataSample Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 100G 1G 99G 1% /mnt/dataPractical Examples
Example 1: Mount an EXT4 File System by UUID
- Identify the UUID
sudo blkid /dev/sda1Sample 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- Mount All File Systems
sudo mount -a- Verify the Mount
df -h /mnt/backupExample 2: Mount an XFS File System by Label
- Identify the Label
sudo blkid /dev/sdb1Sample 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/dataAdditional Tips
- Use
lsblkto Verify Mount Points:
lsblk -f- Check
/etc/fstabSyntax:Ensure there are no syntax errors in/etc/fstabto 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/dataConclusion
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 lsblkStep 2: Create a New Partition
Use fdisk or parted to create a new partition.
Using fdisk
- Start
fdiskon the target disk:
sudo fdisk /dev/sdb- Create a New Partition:
- Type
nto create a new partition. - Choose
pfor a primary partition orefor an extended partition. - Enter the partition number (e.g.,
1). - Set the first sector (press
Enterfor default). - Set the last sector or specify the size (e.g.,
+10Gfor a 10GB partition).
- Type
- Change the Partition Type to Linux LVM:
- Type
tto change the partition type. - Enter the partition number.
- Type
8efor Linux LVM.
- Type
- Write Changes:
- Type
wto write the changes and exit.
- Type
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): wUsing parted
- Start
partedon 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) quitCreating Logical Volumes
Step 1: Initialize the New Partition as a Physical Volume
sudo pvcreate /dev/sdb1Step 2: Create or Extend a Volume Group
Create a New Volume Group
sudo vgcreate vg_data /dev/sdb1Extend an Existing Volume Group
sudo vgextend vg_data /dev/sdb1Step 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_dataStep 4: Create a Filesystem on the Logical Volume
Example
Create an EXT4 filesystem:
sudo mkfs.ext4 /dev/vg_data/lv_dataStep 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/dataStep 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 0Adding Swap Space
Step 1: Create a Swap Partition or Logical Volume
Using a Partition
- Create a New Partition (as shown in the “Adding New Partitions” section).
- Initialize the Partition for Swap:
sudo mkswap /dev/sdb2Using 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_swapStep 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/fstabAdd the following line:
/dev/vg_data/lv_swap swap swap defaults 0 0Step 4: Verify Swap Space
sudo swapon --showSample Output:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2Practical Examples
Example 1: Add a New Partition and Logical Volume
- Create a New Partition:
sudo fdisk /dev/sdbFollow 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/fstabExample 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 --showAdditional 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 -handlvsto 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.
