Difference between revisions of "OPS245 Lab 5"
m (→Part 1: Hard Disk Space Utilities: - clarifying instructions) |
|||
(28 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Admon/caution|DO NOT USE THIS VERSION OF THE COURSE. This page will no longer be updated.|'''Debian version here:''' https://seneca-ictoer.github.io/OPS245 | ||
+ | <br>'''CentOS version here:''' https://seneca-ictoer.github.io/OPS245-C7<br>'''Andrew's version here:''' http://wiki.littlesvr.ca/wiki/OPS245_Lab_5}} | ||
=LAB PREPARATION= | =LAB PREPARATION= | ||
{| width="40%" align="right" cellpadding="10" | {| width="40%" align="right" cellpadding="10" | ||
Line 102: | Line 104: | ||
#Perform this section in your '''c7host''' machine | #Perform this section in your '''c7host''' machine | ||
− | + | #Change to your '''bin''' directory. | |
− | #Change to | + | #Download the following shell script by issuing the following command: <br><b><code><span style="cursor:default;color:#3366CC;font-size:1.2em;">wget https://raw.githubusercontent.com/OPS245/labs/main/monitor-disk-space.py</span></code></b> |
− | #Download the following shell script by issuing the following command: <br><b><code><span style="cursor:default;color:#3366CC;font-size:1.2em;">wget https:// | + | #Try to understand what this script does (refer to man pages for the '''awk''' command), and then run the script with elevated permissions. |
− | #Try to understand what this | ||
#Give execute permissions and run this shell script. This script is supposed to notify the root user by email if there are any potential partition size issues. | #Give execute permissions and run this shell script. This script is supposed to notify the root user by email if there are any potential partition size issues. | ||
− | #Issue the follow command: <b><code><span style="color:#3366CC;font-size:1.2em;">mail</span></code></b> (if you get an error, install email by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">yum install mailx</span></code></b><br>Check to see if there are any mail messages. If there are mail messages, they do not relate to this | + | #Issue the follow command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo mail -u root</span></code></b> (if you get an error, install email by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">yum install mailx</span></code></b><br>Check to see if there are any mail messages. If there are mail messages, they do not relate to this script execution. Remove all mail messages by typing d immediately followed by a mail message number range (eg. to remove all messages. For example, if there are 5 messages, type '''d1-5''' and then press '''ENTER''' and enter '''q''' to exit the mail application). |
− | #Edit the '''monitor-disk-space. | + | #Edit the '''monitor-disk-space.py''' script, and set the <b><code><span style="color:#3366CC;font-size:1.2em;">ALERT=90</span></code></b> value to <b><code><span style="color:#3366CC;font-size:1.2em;">ALERT=10</span></code></b>. Then save your editing session, and re-run this script. |
#Run the '''mail''' command. Do you have a mail message? Enter the mail message number to view the message. If there is a message, what is the purpose of this message? | #Run the '''mail''' command. Do you have a mail message? Enter the mail message number to view the message. If there is a message, what is the purpose of this message? | ||
# Exit from the mail command. | # Exit from the mail command. | ||
+ | # The script as it is currently written will send the email to root, but we won't be logged in as root most of the time. Change the ADMIN variable in the script to your own username. | ||
+ | # Run the script again and make sure the email message gets delivered to your normal user. | ||
Line 118: | Line 121: | ||
<li value="11">Quickly view the tutorial about the <b>[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]</b> file to understand the purpose of this | <li value="11">Quickly view the tutorial about the <b>[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]</b> file to understand the purpose of this | ||
file and how to basically set up a schedule to run a shell script.</li> | file and how to basically set up a schedule to run a shell script.</li> | ||
− | <li>Issue the following command to setup a crontab entry for root:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">crontab -e</span></code></b></li><li>Enter the following line in order to run at 6:00 on the first day of every month:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">0 6 1 * * / | + | <li>Issue the following command to setup a crontab entry for root:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">crontab -e</span></code></b></li><li>Enter the following line in order to run at 6:00 on the first day of every month:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">0 6 1 * * /home/<YOURUSERNAME>/bin/monitor-disk-space.py #Runs first day of each month (6:00 am)</span></code></b><br />Note: Make sure you put your own username in that entry.</li><li>'''Save''' the crontab entry.</li><li>Confirm that the entry was properly saved by issuing the following command:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">crontab -l</span></code></b></li></ol> |
Line 124: | Line 127: | ||
=INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM= | =INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM= | ||
+ | |||
+ | {{Admon/caution|DO NOT USE THIS VERSION OF THE COURSE. This page will no longer be updated.|'''Debian version here:''' https://seneca-ictoer.github.io/OPS245 | ||
+ | <br>'''CentOS version here:''' https://seneca-ictoer.github.io/OPS245-C7}} | ||
An application called '''LVM''' is a very useful tool for Linux system administrators to easily manage file systems, in some cases, even when the computer system is running! | An application called '''LVM''' is a very useful tool for Linux system administrators to easily manage file systems, in some cases, even when the computer system is running! | ||
Line 142: | Line 148: | ||
#Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ls /dev/vd*</span></code></b><br><br>'''NOTE:''' If nothing displays, issue the command: '''sudo ls /dev/sd*''' and use that device pathname '''/dev/sda''' instead, and notify your instructor when about to run your lab5-check.bash shell script at the end of this lab.<br><br> | #Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ls /dev/vd*</span></code></b><br><br>'''NOTE:''' If nothing displays, issue the command: '''sudo ls /dev/sd*''' and use that device pathname '''/dev/sda''' instead, and notify your instructor when about to run your lab5-check.bash shell script at the end of this lab.<br><br> | ||
# Issue the following command to install the '''ssm''' command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum install system-storage-manager</span></code></b> | # Issue the following command to install the '''ssm''' command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum install system-storage-manager</span></code></b> | ||
− | # Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ssm list</span></code></b>. Take a few moments to note the volume group, physical volume and logical volume sections of the command output. | + | # Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ssm list</span></code></b>. Take a few moments to note the volume group, physical volume and logical volume sections of the command output. |
# Compare this output from the ssm command with these other lvm commands: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo lvs</span></code></b>, <b><code><span style="color:#3366CC;font-size:1.2em;">sudo pvs</span></code></b>, and <b><code><span style="color:#3366CC;font-size:1.2em;">sudo vgs</span></code></b>. Which method do you prefer to use? | # Compare this output from the ssm command with these other lvm commands: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo lvs</span></code></b>, <b><code><span style="color:#3366CC;font-size:1.2em;">sudo pvs</span></code></b>, and <b><code><span style="color:#3366CC;font-size:1.2em;">sudo vgs</span></code></b>. Which method do you prefer to use? | ||
# Check to see if there is any remaining space on your existing hard disk. Can you see any? | # Check to see if there is any remaining space on your existing hard disk. Can you see any? | ||
# You can create a partition by using the fdisk command. Issue the following command: <br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo fdisk /dev/vda</span></code></b> (or fdisk /dev/sda if there is no /dev/vda). | # You can create a partition by using the fdisk command. Issue the following command: <br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo fdisk /dev/vda</span></code></b> (or fdisk /dev/sda if there is no /dev/vda). | ||
#At the '''fdisk''' prompt, issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b>. What does this do? | #At the '''fdisk''' prompt, issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b>. What does this do? | ||
− | #Now issue the commands <b><code><span style="color:#3366CC;font-size:1.2em;">n</span></code></b> (new partition), <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b> (primary partition), <b><code><span style="color:#3366CC;font-size:1.2em;">3</span></code></b> (i.e. next available partition number). When prompted for initial block, press '''enter''' to accept the default beginning block | + | #Now issue the commands <b><code><span style="color:#3366CC;font-size:1.2em;">n</span></code></b> (new partition), <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b> (primary partition), <b><code><span style="color:#3366CC;font-size:1.2em;">3</span></code></b> (i.e. next available partition number). When prompted for initial block, press '''enter''' to accept the default beginning block, and type: <b><code><span style="color:#3366CC;font-size:1.2em;">+3G</span></code></b> at ending block (create a 3GB partition) and press '''enter'''. |
#At the fdisk prompt, issue the command <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b> to review the partition information. | #At the fdisk prompt, issue the command <b><code><span style="color:#3366CC;font-size:1.2em;">p</span></code></b> to review the partition information. | ||
− | #Enter the command <b><code><span style="color:#3366CC;font-size:1.2em;">w</span></code></b> to save partition table and exit ( | + | #Enter the command <b><code><span style="color:#3366CC;font-size:1.2em;">w</span></code></b> to save partition table and exit (read the WARNING message). |
{{Admon/important|You MUST reboot your centos2 VM|You MUST now reboot your centos2 VM before proceeding!}} | {{Admon/important|You MUST reboot your centos2 VM|You MUST now reboot your centos2 VM before proceeding!}} | ||
Line 155: | Line 161: | ||
<ol><li value="13">You <u>'''must'''</u> '''restart''' your centos2 VM to allow changes to take effect.</li> | <ol><li value="13">You <u>'''must'''</u> '''restart''' your centos2 VM to allow changes to take effect.</li> | ||
<li>Verify that you created this partition by issuing the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo fdisk -l /dev/vda</span></code></b></li> | <li>Verify that you created this partition by issuing the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo fdisk -l /dev/vda</span></code></b></li> | ||
− | <li>Re-issue the '''ssm''' command. Do you see a new /dev/vda3 partition under Physical Volumes?</li> | + | <li>Re-issue the '''ssm list''' command. Do you see a new /dev/vda3 partition under Physical Volumes?</li> |
<li>To add the newly created partition, you need to add it into LVM to be used. Issue the following command to add the partition into LVM: <br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo pvcreate /dev/vda3</span></code></b> (or ''pvcreate /dev/sda3'' ) (enter '''y''' to proceed - ignore warning)</li> | <li>To add the newly created partition, you need to add it into LVM to be used. Issue the following command to add the partition into LVM: <br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo pvcreate /dev/vda3</span></code></b> (or ''pvcreate /dev/sda3'' ) (enter '''y''' to proceed - ignore warning)</li> | ||
</ol> | </ol> | ||
− | {{Admon/important|Check your VG name|Run <b>vgs</b> to determine your Volume Group name. If it is just <b>centos</b> or <b>cl</b>, replace <b>centos_centos2</b> with <b>centos</b> or <b>cl</b> for the rest of the following commands in this lab.}} | + | {{Admon/important|Check your VG name|Run <b>vgs</b> to determine your Volume Group name. If it is just <b>centos</b> or <b>cl</b>, replace <b>centos_centos2</b> with <b>centos</b> or <b>cl</b> for the rest of the following commands in this lab.<br /> Do '''NOT''' try to rename your volume group.}} |
<ol> | <ol> | ||
<li value="17">Issue the following command to add your new-created and formatted partition called /dev/vda3 to your volume group:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo vgextend centos_centos2 /dev/vda3</span></code></b></li> | <li value="17">Issue the following command to add your new-created and formatted partition called /dev/vda3 to your volume group:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo vgextend centos_centos2 /dev/vda3</span></code></b></li> | ||
Line 176: | Line 182: | ||
|- valign="top" | |- valign="top" | ||
| | | | ||
− | [[Image:Add_virtual_disk.png|thumb|right|400px|You can add virtual hard disks for a VM by changing to the Details section for the VM (as opposed to console), click Add Hardware, fill information in the Add New Virtual Hardware dialog box and clicking Finish.]] | + | [[Image:Add_virtual_disk.png|thumb|right|400px|You can add virtual hard disks for a VM by changing to the Details section for the VM (as opposed to console), click Add Hardware, fill information in the Add New Virtual Hardware dialog box and clicking Finish. '''Notice that I have my original storage in the background so I know what type of disk to select for this second device. They should match.''']] |
|} | |} | ||
Line 187: | Line 193: | ||
# Remain in your '''centos2''' VM for this section. | # Remain in your '''centos2''' VM for this section. | ||
− | # Run the following commands and make note of the output of the commands:<br><b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b>,<br><b><code><span style="color:#3366CC;font-size:1.2em;">ssm list</span></code></b>, and <b><code><span style="color:#3366CC;font-size:1.2em;">df -h</span></code></b> | + | # Run the following commands and make note of the output of the commands:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo ls /dev/vd*</span></code></b>,<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo ssm list</span></code></b>, and <b><code><span style="color:#3366CC;font-size:1.2em;">sudo df -h</span></code></b> |
<ol> | <ol> | ||
<li value="3">Record the size of the volume group and the amount of free space</li> | <li value="3">Record the size of the volume group and the amount of free space</li> | ||
Line 193: | Line 199: | ||
<li>At the bottom left-hand corner, click '''Add Hardware''' and add a new storage device of '''2GBs''', make sure the '''Bus type''' is selected <u>'''using the same type as your first drive that's already there'''</u>. If your first drive is SATA, IDE, or VirtIO, select that.</li> | <li>At the bottom left-hand corner, click '''Add Hardware''' and add a new storage device of '''2GBs''', make sure the '''Bus type''' is selected <u>'''using the same type as your first drive that's already there'''</u>. If your first drive is SATA, IDE, or VirtIO, select that.</li> | ||
<li>Click the '''VM''' menu again, and return to the '''console''' view to access your centos2 VM display.</li> | <li>Click the '''VM''' menu again, and return to the '''console''' view to access your centos2 VM display.</li> | ||
− | <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b>, what has changed?</li> | + | <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ls /dev/vd*</span></code></b>, what has changed?</li> |
<li>Use '''fdisk''' (''refer to how to use in Part 1'') to create a new single '''primary''' partition for '''/dev/vdb''' that fills the ''entire'' disk, save partition table (accepting default prompts would work and '''type w to write your changes'''!), restart your '''centos2''' VM. | <li>Use '''fdisk''' (''refer to how to use in Part 1'') to create a new single '''primary''' partition for '''/dev/vdb''' that fills the ''entire'' disk, save partition table (accepting default prompts would work and '''type w to write your changes'''!), restart your '''centos2''' VM. | ||
<li> Format your new ''vdb1'' partition with file type: '''ext4'''</li> | <li> Format your new ''vdb1'' partition with file type: '''ext4'''</li> | ||
− | <li>Now we'll make the new device a '''physical volume''', add it to the '''volume group''' by issuing the following commands:<br><b><code><span style="color:#3366CC;font-size:1.2em;">pvcreate /dev/vdb1</span></code></b> (enter '''y''' to proceed - ignore warning)<br><b><code><span style="color:#3366CC;font-size:1.2em;">vgextend centos_centos2 /dev/vdb1</span></code></b><br><br>'''NOTE:''' If you experience an error message, issue the '''ssm list''' command, and check the '''volume group name''' under the '''"pool"''' section.<br>If the volume group name is different than '''centos_centos2''', then use that volume group name for all remaining commands that use "centos_centos2"<br><br></li> | + | <li>Now we'll make the new device a '''physical volume''', add it to the '''volume group''' by issuing the following commands:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo pvcreate /dev/vdb1</span></code></b> (enter '''y''' to proceed - ignore warning)<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo vgextend centos_centos2 /dev/vdb1</span></code></b><br><br>'''NOTE:''' If you experience an error message, issue the '''sudo ssm list''' command, and check the '''volume group name''' under the '''"pool"''' section.<br>If the volume group name is different than '''centos_centos2''', then use that volume group name for all remaining commands that use "centos_centos2"<br><br></li> |
− | <li>Re-issue the '''ssm list''' command to see if there is any change.</li> | + | <li>Re-issue the '''sudo ssm list''' command to see if there is any change.</li> |
− | <li>Issue the following command to extend the logical volume for the home file-system: <b><code><span style="color:#3366CC;font-size:1.2em;">lvextend -r centos_centos2/home --size +2G</span></code></b></li> | + | <li>Issue the following command to extend the logical volume for the home file-system: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo lvextend -r centos_centos2/home --size +2G</span></code></b></li> |
<li>Record the size of the volume group and the amount of free space. What has changed and what caused those changes?</li> | <li>Record the size of the volume group and the amount of free space. What has changed and what caused those changes?</li> | ||
− | <li>Issue the '''ssm list''' command. Note that your home file-system is now 2GB bigger, and you have not even rebooted your machine since you used fdisk to create a partition!</li> | + | <li>Issue the '''sudo ssm list''' command. Note that your home file-system is now 2GB bigger, and you have not even rebooted your machine since you used fdisk to create a partition!</li> |
<li>'''Reboot''' your centos2 VM</li> | <li>'''Reboot''' your centos2 VM</li> | ||
<li>Record the LVM Management commands in your lab log-book.</li> | <li>Record the LVM Management commands in your lab log-book.</li> | ||
Line 217: | Line 223: | ||
# Perform this part in your '''centos2''' VM. | # Perform this part in your '''centos2''' VM. | ||
− | # Issue the following command to create a mount-point (directory to connect /dev/dva3 partition to):<br><b><code><span style="color:#3366CC;font-size:1.2em;">mkdir /archive</span></code></b> | + | # Issue the following command to create a mount-point (directory to connect /dev/dva3 partition to):<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo mkdir /archive</span></code></b> |
− | # Issue the following command to mount the partition:<br><b><code><span style="color:#3366CC;font-size:1.2em;">mount -t ext4 /dev/centos_centos2/archive /archive</span></code></b> | + | # Issue the following command to mount the partition:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo mount -t ext4 /dev/centos_centos2/archive /archive</span></code></b> |
# Use the '''ls''' command to view the contents of the /archive directory. What do you see? | # Use the '''ls''' command to view the contents of the /archive directory. What do you see? | ||
# Issue the '''mount''' command (without arguments) to confirm it has been mounted. | # Issue the '''mount''' command (without arguments) to confirm it has been mounted. | ||
− | # Unmount the '''/archive''' directory by issuing the following commmand: <b><code><span style="color:#3366CC;font-size:1.2em;">umount /archive</span></code></b> | + | # Unmount the '''/archive''' directory by issuing the following commmand: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo umount /archive</span></code></b> |
− | # Issue the '''mount''' command (without arguments) to confirm it has been | + | # Issue the '''mount''' command (without arguments) to confirm it has been unmounted. |
Line 229: | Line 235: | ||
<ol><li value="8">View the contents of the file-system table '''/etc/fstab''' by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">cat /etc/fstab</span></code></b></li> | <ol><li value="8">View the contents of the file-system table '''/etc/fstab''' by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">cat /etc/fstab</span></code></b></li> | ||
− | <li> | + | <li>use <b><code><span style="color:#3366CC;font-size:1.2em;">sudo vi /etc/fstab</span></code></b> to add an entry to automatically mount the /archive directory upon bootup:<br><b><code><span style="color:#3366CC;font-size:1.2em;">/dev/centos_centos2/archive /archive ext4 defaults 1 2</span></code></b> |
− | <li> | + | ::Note: do not alter any of the lines that are already in that file.</li> |
+ | <li>Issue the command <b><code><span style="color:#3366CC;font-size:1.2em;">sudo mount -a</span></code></b> to see if the entry in '''/etc/fstab''' works correctly. If there are any errors you must correct them before rebooting your machine.</li> | ||
+ | <li>Reboot the machine and make sure the '''/archive''' directory is automatically mounted when the machine boots.</li> | ||
</ol> | </ol> | ||
Line 237: | Line 245: | ||
=INVESTIGATION 3: Scripting= | =INVESTIGATION 3: Scripting= | ||
− | + | The script used to monitor disk space earlier in the lab is very useful but has a noticeable flaw: You have to manually change the script to modify the percentage of disk space usage that will trigger a warning. It also will only ever send the email to root (unless you manually change the script). | |
+ | <ol> | ||
+ | <li>Make a copy of of "monitor-disk-space.py" called '''disk-monitor.py''' and place it in your user's bin directory.</li> | ||
+ | <li>Keep the original preamble comments in the script, but add a line indicating that you modified it (and when you did so). Make sure you indicate the change in script name too.</li> | ||
+ | <li>Modify the script so that it will use '''argparse''' to accept command line input from the user for their preferred values for who to send the email to, and what percentage of use is worth sending an email over. | ||
+ | ::You may choose the letters for each of these options, just document your choice in the comments in the script. | ||
+ | ::Make sure your script keeps the original values as defaults, in case the user doesn't specify one of them (i.e. if they don't use the option to set who to send the email to, just continue to use root).</li> | ||
+ | <li>Test your script with both good and bad data to make sure it works.</li> | ||
+ | <li>When you are confident your script works, you are ready to submit the lab.</li> | ||
+ | </ol> | ||
= LAB 5 SIGN-OFF (SHOW INSTRUCTOR) = | = LAB 5 SIGN-OFF (SHOW INSTRUCTOR) = | ||
Line 248: | Line 265: | ||
# Make certain that your '''c7host''' and '''centos2''' VMs are running. | # Make certain that your '''c7host''' and '''centos2''' VMs are running. | ||
# Switch to your '''c7host''' machine. | # Switch to your '''c7host''' machine. | ||
− | # Open a shell terminal, | + | # Open a shell terminal, change to your user's '''bin''' directory. |
− | # Issue the Linux command: <b><code><span style="color:#3366CC;font-size:1.2em;">wget https:// | + | # Issue the Linux command: <b><code><span style="color:#3366CC;font-size:1.2em;">wget https://raw.githubusercontent.com/OPS245/labs/main//lab5-check.bash</span></code></b><!--<br />For Andrew's sections use this script instead:<b><code><span style="color:#3366CC;font-size:1.2em;">wget http://littlesvr.ca/ops245/lab5-check-andrew.bash</span></code></b>--> |
# Give the '''lab5-check.bash''' file execute permissions (for the file owner). | # Give the '''lab5-check.bash''' file execute permissions (for the file owner). | ||
− | # Run the shell script and if any warnings, make fixes and re-run shell script until you receive "congratulations" message. | + | # Run the shell script (with elevated permissions) and if there are any warnings, make fixes and re-run shell script until you receive "congratulations" message. |
− | #Arrange proof of the following on the screen:<br><blockquote><span style="color:green;font-size:1.5em;">✓</span> '''centos2''' VM:<blockquote><ul><li>Output from '''ssm list''' command.</li><li>Proof that '''/archive''' has been mounted</li></ul></blockquote><span style="color:green;font-size:1.5em;">✓</span> '''c7host''' Machine:<blockquote><ul><li>Proof of creation of the shell script: '''monitor-disk-space. | + | #Arrange proof of the following on the screen:<br><blockquote><span style="color:green;font-size:1.5em;">✓</span> '''centos2''' VM:<blockquote><ul><li>Output from '''sudo ssm list''' command.</li><li>Proof that '''/archive''' has been mounted</li></ul></blockquote><span style="color:green;font-size:1.5em;">✓</span> '''c7host''' Machine:<blockquote><ul><li>Proof of creation of the shell script: '''monitor-disk-space.py'''</li><li>Crontab entry for '''root''' account</li><li>Run the '''lab5-check.bash''' script in front of your instructor (must have all <b><code><span style="color:#66cc00;border:thin solid black;font-size:1.2em;"> OK </span></code></b> messages)</li></ul></blockquote><span style="color:green;font-size:1.5em;">✓</span> '''Lab5''' log-book filled out. |
+ | #Upload a screen of the proof from the previous step, along with the file generated by '''lab5-check.bash''', your log book, and your disk-monitior.py script to blackboard. | ||
= Practice For Quizzes, Tests, Midterm & Final Exam = | = Practice For Quizzes, Tests, Midterm & Final Exam = |
Latest revision as of 19:54, 9 March 2024
Contents
LAB PREPARATION
Purpose / Objectives of Lab 5
The purpose of this lab is to demonstrate how a Linux system administrator can monitor hard disk space availability, and to manage file system size via the Logical Volume Manager (LVM) application. This lab will also demonstrate how to manually mount (i.e. connect) and unmount (disconnect) partitions to file system directories, and demonstrate how to have partitions automatically mounted to directories upon Linux system startup.
Main Objectives
- Monitoring Disk Space with utilities such as ssm list, df -h, and du -ah.
- Use the crontab utility to automatically schedule the execution of a shell script to "flag" low disk space.
- Use LVM to resize partitions via command-line utilities.
- Create, partition and format virtual hard disks to increase the size of file systems.
- Manually connect and disconnect directories (mount points) to existing partitions (mount, umount).
- Edit the /etc/fstab file to automatically mount partitions upon Linux server boot-up, and test the configuration prior to Linux server boot-up.
Minimum Required Materials |
Linux Command Reference | ||||
LVM Information | LVM Management | Miscellaneous |
INVESTIGATION 1: MONITORING HARD DISK SPACE
Part 1: Hard Disk Space Utilities
Another essential duty of a Linux system administrator is to anticipate problems and take preventative measures to avoid computer system problems before they occur. An example would be to periodically monitor hard disk space in order to make adjustments before it impacts on system performance.
Therefore, we are going to learn in this section how to monitor disk space activity to help take corrective action.
- Perform the following steps:
- Launch your c7host and centos2 VMs.
- Switch to your centos2 machine.
- Open a terminal.
- Issue the command:
df -h
- Note the disk space usage for the / and /home 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. The administrator can also 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 -ha ~<username> | 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 courses (for example: SEC520). One method of monitoring potential penetration is use the find command (Note that find relies on the permissions of the user currently running it. Compare the results of running this command with and without sudo):
find -P / -size +100000k
- 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).
Part 2: Using crontab to Automatically Alert System Administrator of Low Hard Disk Space
This section focuses on how to automatically run shell scripts without the Linux system administrator being there to issue that shell script. It would be highly unlikely to expect a system administrator to stay up late (eg. 2 a.m.) to manually run a shell script to terminate processes or to re-boot Linux servers. Database files (tables) are used to provide instructions on how frequent shell scripts or commands can be run.
The cron daemon is used to refer to these shell scripts (or other commands or programs) and to run them on a pre-determined basis. The term cron comes from the old word chronograph meaning a special type of watch (actually a stop-watch) to help monitor and schedule routine tasks.
- Perform the following steps:
- Perform this section in your c7host machine
- Change to your bin directory.
- Download the following shell script by issuing the following command:
wget https://raw.githubusercontent.com/OPS245/labs/main/monitor-disk-space.py
- Try to understand what this script does (refer to man pages for the awk command), and then run the script with elevated permissions.
- Give execute permissions and run this shell script. This script is supposed to notify the root user by email if there are any potential partition size issues.
- Issue the follow command:
sudo mail -u root
(if you get an error, install email by issuing the command:yum install mailx
Check to see if there are any mail messages. If there are mail messages, they do not relate to this script execution. Remove all mail messages by typing d immediately followed by a mail message number range (eg. to remove all messages. For example, if there are 5 messages, type d1-5 and then press ENTER and enter q to exit the mail application). - Edit the monitor-disk-space.py script, and set the
ALERT=90
value toALERT=10
. Then save your editing session, and re-run this script. - Run the mail command. Do you have a mail message? Enter the mail message number to view the message. If there is a message, what is the purpose of this message?
- Exit from the mail command.
- The script as it is currently written will send the email to root, but we won't be logged in as root most of the time. Change the ADMIN variable in the script to your own username.
- Run the script again and make sure the email message gets delivered to your normal user.
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 stop-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 * * /home/<YOURUSERNAME>/bin/monitor-disk-space.py #Runs first day of each month (6:00 am)
Note: Make sure you put your own username in that entry. - Save the crontab entry.
- Confirm that the entry was properly saved by issuing the following command:
crontab -l
Answer INVESTIGATION 1 observations / questions in your lab log book.
INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM
An application called LVM is a very useful tool for Linux system administrators to easily manage file systems, in some cases, even when the computer system is running!
LVM (Logical Volume Management) is used to manage hard disk drives / partitions for Linux and Unix systems. LVM provides more flexibility than just partitioning hard disks. 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.
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,
Part 1: Managing File System Size with Existing Hard Drive
We will now use LVM in order to grow and reduce our file system, using extra unallocated space on our existing (default) virtual hard disk for our centos2 VM.
- Perform the following steps:
- Remain in your centos2 VM for this section.
- Issue the command:
sudo ls /dev/vd*
NOTE: If nothing displays, issue the command: sudo ls /dev/sd* and use that device pathname /dev/sda instead, and notify your instructor when about to run your lab5-check.bash shell script at the end of this lab. - Issue the following command to install the ssm command:
sudo yum install system-storage-manager
- Issue the command:
sudo ssm list
. Take a few moments to note the volume group, physical volume and logical volume sections of the command output. - Compare this output from the ssm command with these other lvm commands:
sudo lvs
,sudo pvs
, andsudo vgs
. Which method do you prefer to use? - Check to see if there is any remaining space on your existing hard disk. Can you see any?
- You can create a partition by using the fdisk command. Issue the following command:
sudo fdisk /dev/vda
(or fdisk /dev/sda if there is no /dev/vda). - At the fdisk prompt, issue the command:
p
. What does this do? - Now issue the commands
n
(new partition),p
(primary partition),3
(i.e. next available partition number). When prompted for initial block, press enter to accept the default beginning block, and type:+3G
at ending block (create a 3GB partition) and press enter. - At the fdisk prompt, issue the command
p
to review the partition information. - Enter the command
w
to save partition table and exit (read the WARNING message).
- You must restart your centos2 VM to allow changes to take effect.
- Verify that you created this partition by issuing the following command:
sudo fdisk -l /dev/vda
- Re-issue the ssm list command. Do you see a new /dev/vda3 partition under Physical Volumes?
- To add the newly created partition, you need to add it into LVM to be used. Issue the following command to add the partition into LVM:
sudo pvcreate /dev/vda3
(or pvcreate /dev/sda3 ) (enter y to proceed - ignore warning)
- Issue the following command to add your new-created and formatted partition called /dev/vda3 to your volume group:
sudo vgextend centos_centos2 /dev/vda3
- Create a new logical volume by issuing the following command:
sudo lvcreate -L 2G -n archive centos_centos2
- Format your newly-created partition by issuing the command:
sudo mkfs -t ext4 /dev/centos_centos2/archive
- Issue the ssm list command to view the new physical volume and logical volume information.
- Reduce the file-size by issuing the command:
sudo lvreduce -r -L -0.5G centos_centos2/archive
- Issue the ssm list command to verify.
- Increase the file-size by issuing the command:
sudo lvextend -r -L +1G centos_centos2/archive
- Issue the ssm list command to verify.
Part 2: Adding Additional Virtual Hard Drives
What if your have noted while monitoring disk space, that you starting to run-out of space on your home file-system, although you do NOT have any available space on your current hard disk? You could obtain an additional hard-drive. We can add a new virtual hard-drive (which will serve as a physical volume to the volume group), and extend the home logical volume to make use of the new available space. Creating virtual hard drives is not only inexpensive, but a great way for students to learn now to simulate growing the size of the file system!
- Perform the following steps:
- Remain in your centos2 VM for this section.
- Run the following commands and make note of the output of the commands:
sudo ls /dev/vd*
,sudo ssm list
, andsudo df -h
- Record the size of the volume group and the amount of free space
- At the top of your KVM window for centos2, click the view menu and change view from Console to Details .
- At the bottom left-hand corner, click Add Hardware and add a new storage device of 2GBs, make sure the Bus type is selected using the same type as your first drive that's already there. If your first drive is SATA, IDE, or VirtIO, select that.
- Click the VM menu again, and return to the console view to access your centos2 VM display.
- Issue the command:
sudo ls /dev/vd*
, what has changed? - Use fdisk (refer to how to use in Part 1) to create a new single primary partition for /dev/vdb that fills the entire disk, save partition table (accepting default prompts would work and type w to write your changes!), restart your centos2 VM.
- Format your new vdb1 partition with file type: ext4
- Now we'll make the new device a physical volume, add it to the volume group by issuing the following commands:
sudo pvcreate /dev/vdb1
(enter y to proceed - ignore warning)sudo vgextend centos_centos2 /dev/vdb1
NOTE: If you experience an error message, issue the sudo ssm list command, and check the volume group name under the "pool" section.
If the volume group name is different than centos_centos2, then use that volume group name for all remaining commands that use "centos_centos2" - Re-issue the sudo ssm list command to see if there is any change.
- Issue the following command to extend the logical volume for the home file-system:
sudo lvextend -r centos_centos2/home --size +2G
- Record the size of the volume group and the amount of free space. What has changed and what caused those changes?
- Issue the sudo ssm list command. Note that your home file-system is now 2GB bigger, and you have not even rebooted your machine since you used fdisk to create a partition!
- Reboot your centos2 VM
- Record the LVM Management commands in your lab log-book.
Part 3: Manually & Automatically Mount Partitions
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. We need to learn how to do this manually by editing or adding an entry in the file system table (/etc/fstab). This file contains entries to mount various file systems automatically upon start-up of the Linux system.
The Linux system administrator 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).
- Perform the following steps:
- Perform this part in your centos2 VM.
- Issue the following command to create a mount-point (directory to connect /dev/dva3 partition to):
sudo mkdir /archive
- Issue the following command to mount the partition:
sudo mount -t ext4 /dev/centos_centos2/archive /archive
- Use the ls command to view the contents of the /archive directory. What do you see?
- Issue the mount command (without arguments) to confirm it has been mounted.
- Unmount the /archive directory by issuing the following commmand:
sudo umount /archive
- Issue the mount command (without arguments) to confirm it has been unmounted.
- We will now edit the /etc/fstab file in order to have the /dev/vda3 partition automatically mounted to the /archive directory upon system boot-up
- View the contents of the file-system table /etc/fstab by issuing the following command:
cat /etc/fstab
- use
sudo vi /etc/fstab
to add an entry to automatically mount the /archive directory upon bootup:/dev/centos_centos2/archive /archive ext4 defaults 1 2
- Note: do not alter any of the lines that are already in that file.
- Issue the command
sudo mount -a
to see if the entry in /etc/fstab works correctly. If there are any errors you must correct them before rebooting your machine. - Reboot the machine and make sure the /archive directory is automatically mounted when the machine boots.
Answer the INVESTIGATION 2 observations / questions in your lab log book.
INVESTIGATION 3: Scripting
The script used to monitor disk space earlier in the lab is very useful but has a noticeable flaw: You have to manually change the script to modify the percentage of disk space usage that will trigger a warning. It also will only ever send the email to root (unless you manually change the script).
- Make a copy of of "monitor-disk-space.py" called disk-monitor.py and place it in your user's bin directory.
- Keep the original preamble comments in the script, but add a line indicating that you modified it (and when you did so). Make sure you indicate the change in script name too.
- Modify the script so that it will use argparse to accept command line input from the user for their preferred values for who to send the email to, and what percentage of use is worth sending an email over.
- You may choose the letters for each of these options, just document your choice in the comments in the script.
- Make sure your script keeps the original values as defaults, in case the user doesn't specify one of them (i.e. if they don't use the option to set who to send the email to, just continue to use root).
- Test your script with both good and bad data to make sure it works.
- When you are confident your script works, you are ready to submit the lab.
LAB 5 SIGN-OFF (SHOW INSTRUCTOR)
- Perform the Following Steps:
- Make certain that your c7host and centos2 VMs are running.
- Switch to your c7host machine.
- Open a shell terminal, change to your user's bin directory.
- Issue the Linux command:
wget https://raw.githubusercontent.com/OPS245/labs/main//lab5-check.bash
- Give the lab5-check.bash file execute permissions (for the file owner).
- Run the shell script (with elevated permissions) and if there are any warnings, make fixes and re-run shell script until you receive "congratulations" message.
- Arrange proof of the following on the screen:
✓ centos2 VM:
✓ c7host Machine:- Output from sudo ssm list command.
- Proof that /archive has been mounted
✓ Lab5 log-book filled out.- Proof of creation of the shell script: monitor-disk-space.py
- Crontab entry for root account
- Run the lab5-check.bash script in front of your instructor (must have all
OK
messages)
- Upload a screen of the proof from the previous step, along with the file generated by lab5-check.bash, your log book, and your disk-monitior.py script to blackboard.
Practice For Quizzes, Tests, Midterm & Final Exam
- What is a VG? PV? LV?
- What is the total size of the "main" VG on your system?
- How do you create an LV?
- How do resize 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.