Changes

Jump to: navigation, search

OPS235 Lab 5

14 bytes removed, 20:20, 28 December 2019
URL change for lab files.
[[Category:OPS235]]
 
=LAB PREPARATION=
{| width="40%" align="right" cellpadding="10"
|- valign="top"
|
[[Image:software.png|thumb|right|200px|Monitory Monitoring Disk Space can fix problems <u>'''before'''</u> they become a crisis (like running low on hard disk space). We will use LVM to easily resize Linux file-systems.]]
|
[[Image:crontab.png|thumb|right|175px|Linux system administrators need to schedule Linux shell scripts and commands (via '''crontab''') to automatically run in order to be more productive.]]
|}
 
 
 
===Purpose / Objectives of Lab 5===
:* Monitoring Disk Space with utilities such as '''ssm list''', '''df -h''', and '''du -ah'''.
:* Use the '''crontab''' utility to automatically schedule running 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.
[http://man7.org/linux/man-pages/man8/pvdisplay.8.html pvdisplay]<br>
[http://man7.org/linux/man-pages/man8/lvdisplay.8.html lvdisplay]<br>
[http://manpages.ubuntu.com/manpages/trusty/man8/ssm.8.html ssm]<br>
|style="padding-left:20px;" |LVM Management
[http://manpages.ubuntu.com/manpages/trusty/man8/ssm.8.html ssm]<br>
[http://man7.org/linux/man-pages/man8/lvextend.8.html lvextend]<br>
[http://man7.org/linux/man-pages/man8/lvcreate.8.html lvcreate]<br>
[http://man7.org/linux/man-pages/man1/df.1.html df]<br>
[http://man7.org/linux/man-pages/man1/du.1.html du]<br>
[http://wwwman7.lehman.cuny.eduorg/cgi-binlinux/man-cgi?pages/man1/awk+1 .1p.html awk]<br>
[http://tldp.org/HOWTO/Partition/fdisk_partitioning.html fdisk]<br>
[http://www.cyberciti.biz/faq/howto-format-create-linux-filesystem/ mkfs]<br>
[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]
|}
 
= 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 <u>before</u> they occur. An example could 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.
#Make certain you are logged in as '''root'''.
#Change to the '''/root/bin''' directory.
#Download the following shell script by issuing the following command: <br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">wget https://scs.senecacict.onsenecacollege.ca/~murray.saulops235/labs/monitor-disk-space.bash</span></code></b>
#Try to understand what this Bash Shell script does (refer to man pages for the '''awk''' command), and then run the script as root.
#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 shell 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.bash ''' shell script, and set the '''<b><code><span style="color:#3366CC;font-size:1.2em;">ALERT''' =90</span></code></b> value from 90 to <b><code><span style="color:#3366CC;font-size:1.2em;">30ALERT=10</span></code></b>, . Then save your editing session, and re-run this shell 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.
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.
<br><br>
<ol>
<li value="1011">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>
<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 * * /root/bin/monitor-disk-space.bash #Runs first day of each month (6:00 am)</span></code></b></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>
'''Answer INVESTIGATION 1 observations / questions in your lab log book.'''
 
=INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM=
# 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;">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?
#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 size, and type: <b><code><span style="color:#3366CC;font-size:1.2em;">+2G3G</span></code></b> at ending block (create 2GB for 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.
#Enter the command <b><code><span style="color:#3366CC;font-size:1.2em;">w</span></code></b> to save partition table and exit (ignore WARNING message).
#{{Admon/important|You MUST reboot your centos2 VM|You MUST now reboot your centos2 VM before proceeding!}} <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;">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>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;">pvcreate /dev/vda3</span></code></b> (or ''pvcreate /dev/sda3'' ) (enter '''y''' to proceed - ignore warning)</li># </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.}}<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;">vgextend centos_centos2 /dev/vda3</span></code></b></li># <li>Create a new logical volume by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvcreate -L 1G 2G -n archive centos_centos2</span></code></b></li># <li>Format your newly-created partition by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">mkfs -t ext4 /dev/centos_centos2/archive</span></code></b></li># <li>Issue the '''ssm list''' command to view the new physical volume and logical volume information.</li></ol>{{Admon/important|Pay attention to syntax|Note that the prefixed "+" or "-" in lvextend and lvreduce will add or subtract from the current size. Omitting these prefixes will <b>set</b> the LV size to what you specified.}}# <ol><li value="21">Reduce the file-size by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">lvreduce -r -L -0.5G centos_centos2/archive</span></code></b></li># <li>Issue the '''ssm list''' command to verify.</li># Increate <li>Increase the file-size by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">lvextend -r -L +0.25G 1G centos_centos2/archive</span></code></b></li># <li>Issue the '''ssm list''' command to verify.</li></ol>
[[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.]]
|}
 
===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 <u>virtual</u> 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:'''
<li value="3">Record the size of the volume group and the amount of free space</li>
<li>At the top of your KVM window for '''centos2''', click the '''view''' menu and change view from '''Console''' to <u>'''Details'''</u>''' .</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 as: <u>'''VirtIOusing the same type as your first drive that's already there''' disk</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>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">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 defaults default prompts would workand '''type w to write your changes'''!), restart your '''centos3centos2''' VM and then '''format''' that partition for 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>Re-issue the '''ssm list''' command to see if there is any change.</li>
</ol><br>:'''NOTE:''' There is a problem extending the home logical volume because we logged in as a regular user (prior to logging in as root), thus the /home directory cannot be unmounted in order to extend the logical volume for centos_centos2/home. We can get around this problem by changing our '''run level to 1 (single-user mode)''' and unmount the '''/home''' partition.<br><ol> <li value="10">Issue the following command as root to change the run-level to 1 (single-user mode): <b><code><span style="color:#3366CC;font-size:1.2em;">init 1</span></code></b></li>  <li>Issue the following command to unmount the /home partition: <b><code><span style="color:#3366CC;font-size:1.2em;">umount /home</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;">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>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>'''Reboot''' your centos2 VM</li>
<li>Record the LVM Management commands in your lab log-book.</li>
</ol>
[[Image:mount.png|thumb|700px|right|Using the '''mount''' command with no arguments displays file-systems that are already mounted. The Linux system administrator can use the '''mount''' and '''umount''' commands to connect and disconnect different partitions from the file-system to perform maintenance.]]
 
=== Part 3: Manually &amp; Automatically Mount Partitions ===
# 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 mount the partition:<br><b><code><span style="color:#3366CC;font-size:1.2em;">mount -t ext4 /dev/vda3 centos_centos2/archive /archive</span></code></b>
# 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: <b><code><span style="color:#3366CC;font-size:1.2em;">umount /archive</span></code></b>
# Issue the '''mount''' command (without arguments) to confirm it has been mounted.
<br> 
: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
<br> 
<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>Note the line that automatically mounts a file-system (''/dev/sda3'', type ''ext4'') to '''/archive'''. This was automatically performed for you via the '''system-config-lvm''' utility.</li><li>View and record the fields for the '''/archive''' mount in your lab logbook, and then issue the following command to reconnect or mount '''/archive''':<br><b><code><span style="color:#3366CC;font-size:1.2em;">mount &nbsp; -t ext4 &nbsp; /dev/centos_centos2/archive &nbsp; /archive</span></code></b></li><li>Use the ls command to view the contents of the /archive directory. What do you see?</li><li>Unmount the /archive directory by issuing the following command: <br><b><code><span style="color:#3366CC;font-size:1.2em;">umount /archive</span></code></b></li><li>Issue the following command to add an entry to automatically mount the /archive directory upon bootup:<br><b><code><span style="color:#3366CC;font-size:1.2em;">echo "/dev/centos_centos2/archive /archive ext4 defaults 1 2" >> /etc/fstab</span></code></b><br>(although we don't need to do this, since it was already done).</li><li>'''Reboot ''' your centos2 VM so see if the /archive directory is automatically mounted.<br><br>'''Note:''' You could have also issued the command: '''mount -a''' to <u>automatically</u> mount the file-systems contained in the '''/etc/fstab''' file without booting the VM (good for testing before trying a reboot).<br><br></li>
</ol>
'''Answer the INVESTIGATION 2 observations / questions in your lab log book.'''
 
= LAB 5 SIGN-OFF (SHOW INSTRUCTOR) =
# Make certain that your '''c7host''' and '''centos2''' VMs are running.
# Switch to your '''centos2c7host''' VMmachine.
# Open a shell terminal, '''su -''' into root, and change to the '''/root/bin''' directory., open a shell terminal
# Issue the Linux command: <b><code><span style="color:#3366CC;font-size:1.2em;">wget httphttps://matrixict.senecac.onsenecacollege.ca/~murray.saulops235/ops235labs/lab5-check.bash</span></code></b>
# 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.
#Arrange proof of the following on the screen:<br><blockquote><span style="color:green;font-size:1.5em;">&#x2713;</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;">&#x2713;</span> '''c7host''' Machine:<blockquote><ul><li>Proof of creation of the shell script: '''monitor-disk-space.bash'''</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;">&nbsp;OK&nbsp;</span></code></b> messages)</li></ul></blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''Lab5''' log-book filled out.
 
= Practice For Quizzes, Tests, Midterm &amp; Final Exam =
[[Category:OPS235]]
[[Category:OPS235 Labs]]
[[Category:CentOS 7]]
[[Category:SSD2]]

Navigation menu