OPS235 Lab 5 - CentOS7
Contents
LAB PREPARATION
Purpose / Objectives of Lab 5
The purpose of this lab is to discuss how a Linux sys admin can manage partitions including adjusting the size of their Linux systems if space is required.
Main Objectives
- Using LVM to resize partitions graphically and via commands.
- Create, partition and format virtual hard disks to increase the size of a file system.
- Manually Connecting and Disconnecting Directories to existing partitions (mount, umount).
- Monitoring Disk Space (df -h).
- Create a Bash Shell Script to monitor and report low disk size (run periodically in crontab).
Minimum Required Materials
My Toolkit (CLI Reference)
LVM Information: | LVM Management | Miscellaneous |
INVESTIGATION 1: MANAGING FILE-SYSTEMS USING LVM
Monitoring and ensuring adequate space for a Linux file-system is considered to be a critical task for a sys admin. An application called LVM is a very useful tool for Linux system administrators to easily manage file systems (even when system is running!).
LVM (Logical Volume Management) is used to manage hard disk drives / partitions for Unix/Linux systems. LVM provides more flexibility than just working with hard disks / hard disk partitions. Volume Groups are areas used to define Physical Volumes (i.e. hard disks, disk partitions, or other forms of storage devices). Logical Volumes are then used to relate directories (mount points) to a specific physical volume or for a "range" or "span" of physical volumes.
Therefore, LVM allows more flexibility and growth potential for Linux systems (for example, having Logical volumes span multiple hard disks). CentOS uses LVM by default upon installation. Other Linux distributions may provide the capacity to install LVM, or later install and then use Logical Volume Management.
Part 1: Manage LVM Graphically
- Let's learn to administer (manage) our LVM graphically for our centos2 Virtual Machine.
- Launch your centos2 VM and open a shell terminal.
- CentOS provides a tool called system-config-lvm to graphically administer LVM.
NOTE: This tool may be replaced by a future graphical LVM management tool and is NOT currently available in the default repositories. We will now perform a "work-around" in order to make the system-config-lvm utility work in our system. - Issue the following command to download the system-config-lvm rpm:
wget https://kojipkgs.fedoraproject.org//packages/system-config-lvm/1.1.18/1.fc19/noarch/system-config-lvm-1.1.18-1.fc19.noarch.rpm
(Although it is an rpm file for the Fedora Distribution, it will also work for Centos7). - Make certain that you are logged in as root.
- Try issuing the command:
rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm
You should notice an error. - Use the yum install command to resolve the following dependencies:
yum install gnome-python2-bonobo
yum install gnome-python2-gnome
yum install usermode-gtk
- Re-issue the command:
rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm
It should work this time. - Run the command:
system-config-lvm
- On the left-hand side, you can click on the Volume Group, Physical Volume and Logical Volumes and view their properties on the on the right-hand side.
- Determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers:
- What are the names and sizes of the Volume Group?
- What is the name and size of the Physical Volumes?
- What are the names and sizes of the Logical Volumes?
- Is there any space in the VG which is not allocated to a LV?
- Click on Logical View (i.e. above home, root, swap) and then click Create New Logical Volume button. What happens? Why do you this happened?
- Let's Create a new partition for /dev/sda by using a graphical utility called: gparted (stands for G Partition Editor).
- While system-config-lvm utility is running, open another terminal and login as root.
- Issue the command:
yum install gparted
- Verify that the root logical volume is now 6GB.
- Now, click on Create New Logical Volume button and fill out the details for this new logical volume including: size: 1GB, LV Properties: linear, file system: ext4 file system name: lv_archive and mount it at: /archive
- Confirm that this new Logical Volume now exists.
- Issue the following command:
cp -R /etc/* /archive
- Shrink the size of lv_archive to 1 GB. What happens?
- If you could not shrink the size of lv_archive, what do you think is the cause for the problem?
</ol>
Answer Part 1 observations / questions in your lab log book.
Part 2: Managing LVM Via Command Line & Adding Virtual Hard Drives
Let's say that you have run out of disk space on your centos3 VM, you need more space on the root file-system, perhaps to host more webpages or a larger database or new software. What are your options? Getting a replacement hard-drive would probably require re-installation of the operating system and backup/restore of the data.
Because we're using LVM we can avoid this problem. We can add a new hard-drive (which will serve as a physical volume) to the volume group, and extend the root logical volume to make use of the new available space.
Perform the following operations to increase the size of lv_root in centos3:
- Perform this Part in your centos3 VM.
- Run the following commands and make note of the output:
ls /dev/vd*
pvs
vgs
lvs
df -h
- Record the size of the volume group and the amount of free space
- Open the centos3 virtual machine Details view.
- Go to the hardware details view
- Click Add Hardware and add a new storage device of 2GBs, make sure it's a VirtIO disk.
- Go back to the console view
- Issue the command:
ls /dev/vd*
, what has changed? - Read the resource [ Partitioning with fdisk ] to learn how to properly create a partition with the fdisk command.
- Use fdisk to partition /dev/vdb with a Linux single partition that fills the whole disk.
- Check the messages printed when
fdisk
exits -- you may need to reboot the system in order for the new partition table to take effect. - Now we'll make the new device a physical volume, add it to the volume group, and extend lv_root:
-
pvcreate /dev/vdb
-
-
vgextend vg_centos3 /dev/vdb
-
-
lvextend -L +2G -r vg_centos3/lv_root
-
- Now rerun the ls /dev/vd* , pvs , vgs , lvs and df -h commands.
- Record the size of the volume group and the amount of free space. What has changed and what caused those changes?
- Among the changes, note that your root file-system is now 2GB bigger, and you have not even rebooted your machine!
- Reduce the size of the lv_root partition by 1GB by issuing the following command:
lvreduce vg_centos/lv_root --size -1G
Confirm that the file system has been reduced. - Increase the size of the lv_root partition by another 1GB by issuing the command:
lvextend vg_centos/lv_root --size +1G
<Confirm that the file system can been increased. - Record the LVM Management commands in your lab log-book.
Answer Part 2 observations / questions in your lab log book.
INVESTIGATION 2: ADDITIONAL FILE-SYSTEM OPERATIONS
We take for granted that a file-system must be mounted (for example the root partition) in order for a Linux system to be usable upon system start-up. The /etc/fstab (file system table) contains entries to mount various file systems automatically upon start-up of the Linux system.
The Linux sys admin also has the ability to manually mount (connect) and un-mount (disconnect) partitions in order to perform maintenance on the file system (for example un-mounting the /home partition to install software and prevent users from logging in during that process).
Part 1: Mounting and Un-mounting Partitions
- Perform this Part in your centos2 VM.
- As you may recall in Part 1 of Investigation 1, we created another logical volume called lv_archive using the graphical application system-config-lvm. This logical volume should be mounted (connected) to our existing file-system (2GB).
- Issue the following command:
mount
- What is the purposed of issuing this command without arguments? Can you see the lv_archive partition?
- Create a new directory called /archive.
- Issue the following command:
mount /dev/vdb -t ext4 /archive
- Issue the mount command to verify that the lv_archive logical volume is mounted.
- Without changing to the /archive, list the contents of that directory.
- Issue the following command:
umount /archive
- Issue the mount command to verify that lv_archive logical volume has been unmounted.
- Although you might have been successful un-mounting the lv_archive logical volume, can you think of any reasons why you might not be able to un-mount that logical volume?
- Quickly view the tutorial about the /etc/fstab file to understand the purpose of this file and the major elements contain in this file.
- Issue the following command:
echo "dev/vdb /archive ext4 defaults 1 1" >> /etc/fstab
- Confirm that those contents have been added to the bottom of /etc/fstab.
- Issue the command:
mount -a
- Check to see if /archive is now mounted. What do you think the above operation did? Confirm by rebooting your VM to see if /archive is mounted.
Answer the Part 1 observations / questions in your lab log book.
Part 2: Monitoring Disk Space
Another essential duty of a Linux system administrator is to anticipate problems and take preventative measures to avoid computer system problems before that occur.
Monitoring disk space activity helps provide Linux system administrators information to help take corrective action.
- Remain in your centos2 VM for this section.
- Issue the command:
df -h
- Note the disk space usage for /, /home, and /archive partitions.
- If a partition is running out of available space, the Linux System Administrator can reallocate space among partitions or add another disk and grow the file system (like you did in a previous investigation). The administrator also can investigate the cause of low disk space. Two examples immediately come to mind: excessive use of space from users, and potential penetration from hackers.
- To investigate excessive disk usage by regular users, you can obtain a total amount of disk usage for that user by issuing the command:
du -hs /home/regularuserid
- To provided a more detailed list of usage (file-by-file), issue the command:
du -h /home/regularuserid | more
- If there is a recurring space usage problem with regular users, the Linux system administrator can impose quotas (caps on disk usage). This method is not taught in this course.
- The methods to monitor potential penetration to a Linux system are too numerous, and are taught in other coures (for example: SEC520). On method of monitoring potential penetration is to look for abnormally large files. Issue the command:
find / -size +10000k –print
- Did you see any files that are that size?
- The next section will apply some of these tools we have discussed into a shell script and crontab entry to periodically monitor and contact the system administrator of potential disk space issues (before they become a serious problem).
Answer the Part 2 observations / questions in your lab log book.
INVESTIGATION 3: LOOKING AHEAD
Automating Routine Tasks (Scheduling The Running of Shell Script Via Crontab)
We will now use shell scripting to help automate the task for a Linux administrator to be notified of potential disk space issues.
- Perform this section in your c7host machine
- Make certain you are logged in as root.
- Download, study, and run the following shell script. Issue the command:
wget https://scs.senecac.on.ca/~murray.saul/monitor-disk-space.bash
- Try to understand what this Bash Shell script does (refer to man pages for the awk command), and then run the script as root.
In order to automatically run the above-mentioned script periodically, you use the scheduler in Linux called crontab. The term crontab stands for Chronograph Tables, where a chronograph is the old term for a timepiece (the forerunner of the modern watch). You can run the crontab command to schedule commands and shell script to be run in a number of different ways.
- Quickly view the tutorial about the Using crontab file to understand the purpose of this file and how to basically set up a schedule to run a shell script.
- Issue the following command to setup a crontab entry for root:
crontab -e
- Enter the following line in order to run at 6:00 on the first day of every month:
0 6 1 * * /root/monitor-disk-space.bash #Runs first day of each month (6:00 am)
- Save the crontab entry.
- Confirm that the entry was properly saved by issuing the following command:
crontab -l
Answer Investigation 3 observations / questions in your lab log book.
LAB 5 SIGN-OFF (SHOW INSTRUCTOR)
Arrange proof of the following on the screen:
- ✓ centos2 VM:
- Logical Volume, Physical Volume, and Volume Group information
- Contents of /etc/fstab file
- Proof that /archive has been mounted
- ✓ centos3 VM:
- Proof of virtual hard disk creation
- Logical Volume, Physical Volume, and Volume Group information
- ✓ c7host Machine:
- Proof of creation of the shell script: monitor-disk-space.bash
- Crontab entry for root account
- ✓ Lab5 log-book filled out.
Preparing for the Quizzes
- What is a VG? PV? LV?
- What is the total size of the "main" VG on your system?
- How do you create a LV?
- How do you delete an LV?
- How would you add the disk partition /dev/sdb7 to your volume group "main"?
- How would you increase the size of the root filesystem by 50 MB?
- How can you determine if a partition has been mounted onto a system?
- How can you unmount an existing partition from the file-system?
- How can you temporarily mount a partition on a file-system?
- How can you permanently mount a partition on a file-system upon boot-up?
- What are the separate elements (fields) of the /etc/fstab file?
- Describe the tools that a Linux system administrator have to monitor disk space usage.