Practice Test 02
Updated Apr 22, 2021 ·
These labs are based on Sander Van Vugt's O'Reailly course, "Red Hat Certified System Administrator (RHCSA), 3/e"
Lab 01 - Writing-Shell-Scripts
Tasks:
Write a shell script that:
- Check an argument is provided and if there's none, exits.
- Evaluate the provided argument:
- if yes, print "that's nice!"
- if no, print 'Sorry to hear that!"
- if neither of the two, print "unknown argument provided"
Solution
Create the script.
[root@tst-rhcsa ~]# vim run-script.sh
#!/bin/bash
# Checks if argument is provided, exits if there's none
if [ -z $1 ]
then
echo "Please provide an argument."
exit 2
fi
# Evaluates the provided argument
if [ $1 = yes ]
then
echo "that's nice!"
elif [ $1 = no ]
then
echo "sorry to hear that"
else
echo "Unknown argument provided"
fi
Grant an execute permission for the script.
[root@tst-rhcsa ~]# ll
total 4
-rw-r--r--. 1 root root 333 Mar 13 10:34 run-script.sh
[root@tst-rhcsa ~]# chmod +x run-script.sh
[root@tst-rhcsa ~]# ll
total 4
-rwxr-xr-x. 1 root root 333 Mar 13 10:34 run-script.sh
Test the script.
[root@tst-rhcsa ~]# ./run-script.sh
Please provide an argument.
[root@tst-rhcsa ~]# ./run-script.sh yes
that's nice!
[root@tst-rhcsa ~]# ./run-script.sh no
sorry to hear that
[root@tst-rhcsa ~]# ./run-script.sh ohyeah
Unknown argument provided
Another way to write the script is to use case
[root@tst-rhcsa ~]# cp run-script.sh run-script2.sh
[root@tst-rhcsa ~]# ll
total 8
-rwxr-xr-x. 1 root root 302 Mar 13 10:44 run-script2.sh
-rwxr-xr-x. 1 root root 333 Mar 13 10:40 run-script.sh
[root@tst-rhcsa ~]# vim run-script2.sh
#!/bin/bash
# Checks if argument is provided, exits if there's none
if [ -z $1 ]
then
echo "Please provide an argument."
exit 2
fi
# Evaluates the provided argument
case $1 in
yes)
echo "that's nice!"
;;
no)
echo "sorry to hear that"
;;
*)
echo "Unknown argument provided"
esac
Test the second script.
[root@tst-rhcsa ~]# ./run-script2.sh
Please provide an argument.
[root@tst-rhcsa ~]# ./run-script2.sh yes
that's nice!
[root@tst-rhcsa ~]# ./run-script2.sh no
sorry to hear that
[root@tst-rhcsa ~]# ./run-script2.sh ohyeah
Unknown argument provided
To ensure that all provided arguments are in lowercase, we can add this to the script.
[root@tst-rhcsa ~]# cp run-script2.sh run-script3.sh
[root@tst-rhcsa ~]# ll
total 12
-rwxr-xr-x. 1 root root 302 Mar 13 10:44 run-script2.sh
-rwxr-xr-x. 1 root root 302 Mar 13 10:50 run-script3.sh
-rwxr-xr-x. 1 root root 333 Mar 13 10:40 run-script.sh
[root@tst-rhcsa ~]# vim run-script3.sh
#!/bin/bash
# Checks if argument is provided, exits if there's none
if [ -z $1 ]
then
echo "Please provide an argument."
exit 2
fi
# Change all provided arguments to lowercase
ANS=$(echo $1 | tr [:upper:] [:lower:])
# Evaluates the provided argument
case $ANS in
yes)
echo "that's nice!"
;;
no)
echo "sorry to hear that"
;;
*)
echo "Unknown argument provided"
esac
Test the third script.
[root@tst-rhcsa ~]# ./run-script3.sh YES
that's nice!
[root@tst-rhcsa ~]# ./run-script3.sh NO
sorry to hear that
[root@tst-rhcsa ~]# ./run-script3.sh YEY
Unknown argument provided
Lab 02 - Resizing LVM
Tasks:
Change the logical volume capacity named vo from 190M to 300M. and the size of the floating range should set between 280 and 320. (This logical volume has been mounted in advance.)
Solution
sudo su -
lsblk
lsblk -f
vgs
umount vo
lvextend -r -L 110M /dev/mapper/vo
mount -a
Lab 03 - Managing Partitions
Tasks:
- In the remaining disk of your server, add a 1G partition. Do this in such a way that it is possible to add more partitions later.
- Format this partition with EXT4 filesystem and set the label "dbfiles" on the partition. Configure your system to mount this partition persistently on the directory /dbfiles, using the partition label
Solution
I'll use /dev/xvdd for this lab.
[root@tst-rhcsa ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 20G 0 part /
xvdb 202:16 0 10G 0 disk
├─xvdb1 202:17 0 1G 0 part
├─xvdb2 202:18 0 1K 0 part
├─xvdb5 202:21 0 500M 0 part
└─xvdb6 202:22 0 500M 0 part
xvdc 202:32 0 10G 0 disk
├─xvdc1 202:33 0 1G 0 part
│ └─vgdc-lvdc 253:0 0 1020M 0 lvm
├─xvdc2 202:34 0 1G 0 part
└─xvdc3 202:35 0 8G 0 part
└─vdo1 253:1 0 10T 0 vdo /mount/vdo1
xvdd 202:48 0 10G 0 disk
[root@tst-rhcsa ~]#
[root@tst-rhcsa ~]#
[root@tst-rhcsa ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1
└─xvda2 xfs root 209b92d1-3b0e-4ae9-b097-6f1a28febc31 /
xvdb
├─xvdb1 ext4 7871a560-57cd-4cb9-b17f-59025eed3710
├─xvdb2
├─xvdb5 xfs 3fd91150-c499-46e9-8ab9-850263aec60c
└─xvdb6 swap 5c43e377-cffc-4ed8-afab-e0efb1713820
xvdc
├─xvdc1 LVM2_member VCzrg0-uUEw-xnhB-afHf-9u5D-udgX-WsjacI
│ └─vgdc-lvdc xfs e2136976-c1d0-4066-a4b3-8f4ec778ab4d
├─xvdc2 crypto_LUKS 5ff7ebc1-9011-462c-8606-d491c3525d99
└─xvdc3 vdo ab76e682-7a3d-4fb7-9781-274f8562bfdd
└─vdo1 xfs eed66dc5-5a34-4f25-93d1-a4dfb23f0514 /mount/vdo1
xvdd
Create the partition and load the filesystem.
[root@tst-rhcsa ~]# sudo fdisk /dev/xvdd
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.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd036f212.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@tst-rhcsa ~]# mkfs.ext4 -L dbfiles /dev/xvdd1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: d33fe124-58e5-46be-974e-2fe5d36a7f84
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@tst-rhcsa ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1
└─xvda2 xfs root 209b92d1-3b0e-4ae9-b097-6f1a28febc31 /
xvdb
├─xvdb1 ext4 7871a560-57cd-4cb9-b17f-59025eed3710
├─xvdb2
├─xvdb5 xfs 3fd91150-c499-46e9-8ab9-850263aec60c
└─xvdb6 swap 5c43e377-cffc-4ed8-afab-e0efb1713820
xvdc
├─xvdc1 LVM2_member VCzrg0-uUEw-xnhB-afHf-9u5D-udgX-WsjacI
│ └─vgdc-lvdc xfs e2136976-c1d0-4066-a4b3-8f4ec778ab4d
├─xvdc2 crypto_LUKS 5ff7ebc1-9011-462c-8606-d491c3525d99
└─xvdc3 vdo ab76e682-7a3d-4fb7-9781-274f8562bfdd
└─vdo1 xfs eed66dc5-5a34-4f25-93d1-a4dfb23f0514 /mount/vdo1
xvdd
└─xvdd1 ext4 dbf43d1c-87f9-4c23-8f17-f4f7b9108d43
Create mountpoint and create entry in /etc/fstab.
[root@tst-rhcsa ~]# mkdir /dbfiles
[root@tst-rhcsa ~]# vim /etc/fstab
UUID=209b92d1-3b0e-4ae9-b097-6f1a28febc31 / xfs defaults 0 0
# EDEN: VDO
/dev/mapper/vdo1 /mount/vdo1 xfs x-systemd.requires=vdo.service 0 0
# EDEN: Lab13-Managing Paritions
LABEL=dbfiles /dbfiles ext4 defaults 0 0
Mount the partition and verify.
[root@tst-rhcsa ~]# mount -a
[root@tst-rhcsa ~]#
[root@tst-rhcsa ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 20G 0 part /
xvdb 202:16 0 10G 0 disk
├─xvdb1 202:17 0 1G 0 part
├─xvdb2 202:18 0 1K 0 part