Difference between revisions of "OPS235 Lab 5"

From CDOT Wiki
Jump to: navigation, search
(Part 2: Adding Additional Virtual Hard Drives)
 
(84 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:OPS235]]
 
 
 
=LAB PREPARATION=
 
=LAB PREPARATION=
 
{| width="40%" align="right" cellpadding="10"
 
{| width="40%" align="right" cellpadding="10"
 
|- valign="top"
 
|- valign="top"
 
|
 
|
[[Image:software.png|thumb|right|200px|Monitory 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:software.png|thumb|right|200px|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.]]
 
[[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===
 
===Purpose / Objectives of Lab 5===
  
The purpose of this lab is to discuss how a Linux system administrator can manage partitions including adjusting the size of their Linux systems if space is required.
+
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.
  
  
Line 20: Line 15:
  
 
:* Monitoring Disk Space with utilities such as '''ssm list''', '''df -h''', and '''du -ah'''.
 
:* Monitoring Disk Space with utilities such as '''ssm list''', '''df -h''', and '''du -ah'''.
:* Use the '''crontab''' utility to automatically schedule running of shell script to "flag" low disk space.
+
:* 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'''.
 
:* Use '''LVM''' to '''resize partitions via command-line utilities'''.
 
:* Create, partition and format '''virtual hard disks''' to increase the size of file systems.
 
:* Create, partition and format '''virtual hard disks''' to increase the size of file systems.
Line 51: Line 45:
 
[http://man7.org/linux/man-pages/man8/pvdisplay.8.html pvdisplay]<br>
 
[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://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
 
|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/lvextend.8.html lvextend]<br>
 
[http://man7.org/linux/man-pages/man8/lvcreate.8.html lvcreate]<br>
 
[http://man7.org/linux/man-pages/man8/lvcreate.8.html lvcreate]<br>
Line 63: Line 57:
 
[http://man7.org/linux/man-pages/man1/df.1.html df]<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://man7.org/linux/man-pages/man1/du.1.html du]<br>
[http://www.lehman.cuny.edu/cgi-bin/man-cgi?awk+1 awk]<br>
+
[http://man7.org/linux/man-pages/man1/awk.1p.html awk]<br>
 
[http://tldp.org/HOWTO/Partition/fdisk_partitioning.html fdisk]<br>
 
[http://tldp.org/HOWTO/Partition/fdisk_partitioning.html fdisk]<br>
 
[http://www.cyberciti.biz/faq/howto-format-create-linux-filesystem/ mkfs]<br>
 
[http://www.cyberciti.biz/faq/howto-format-create-linux-filesystem/ mkfs]<br>
Line 69: Line 63:
 
[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]
 
[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]
 
|}
 
|}
 
  
 
= INVESTIGATION 1: MONITORING HARD DISK SPACE =
 
= INVESTIGATION 1: MONITORING HARD DISK SPACE =
Line 77: Line 70:
 
=== Part 1: Hard Disk Space Utilities===
 
=== 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> that occur. An example could be to periodically monitor hard disk space in order to make adjustments before it impacts on system performance.
+
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 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 before problems can occur.
+
Therefore, we are going to learn in this section how to monitor disk space activity to help take corrective action.
  
'''Perform the following steps:'''
+
:'''Perform the following steps:'''
  
 
# Launch your '''c7host''' and '''centos2''' VMs.
 
# Launch your '''c7host''' and '''centos2''' VMs.
Line 88: Line 81:
 
# Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">df -h</span></code></b>
 
# Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">df -h</span></code></b>
 
# Note the disk space usage for the '''/''' and '''/home''' partitions.
 
# 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 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.
+
# 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:<br><b><code><span style="color:#3366CC;font-size:1.2em;">du -ha . | more</span></code></b>
 
# To investigate excessive disk usage by regular users, you can obtain a total amount of disk usage for that user by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">du -ha . | more</span></code></b>
 
# 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.
 
# 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 command:<br><b>find -P / -size +100000k</b>
+
# 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 command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">find -P / -size +100000k</span></code></b>
 
# 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).
 
# 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).
  
Line 99: Line 92:
 
|{{Admon/tip|Bash Shell Scripting Tips:|<br>'''<u>Using awk to Manipulate Text</u>'''<br><br><ul><li>Very useful command for report generation, text file repair, or text and floating-point decimal manipulation. The command mimics a C program, with braces '''{  }''' that surround the action to perform based on records from a database file matching either test conditions, regular expressions, etc. Fields appear as numbers with $.<br><br></li><li>Examples:<br><br>''awk '{print}' data-file.txt''<br>''awk -F";" '{print $5,$3}' data-file.txt''<br>''awk -F"," '$4 &gt;&#61; 10000 {print $1, $2}' salary.txt''<br><br></li></ul>'''<u>Crontab (Chronograph Tables)</u>'''<br><br><ul><li>Used to automatically run (as opposed to manually run) scripts, programs, or commands. There are many tables (files), but the main one is: '''/etc/cron'''. The '''crontab''' command can be used to ''list'', ''create'', ''modify'' or ''remove'' scheduled jobs in the file.<br><br></li><li>Examples:<br><br>''crontab -e -u user'' # create/modify<br>''crontab -r -u user-name'' # remove specific user's crontab<br>''crontab -l -u username'' #List current schedules</li></ul>}}
 
|{{Admon/tip|Bash Shell Scripting Tips:|<br>'''<u>Using awk to Manipulate Text</u>'''<br><br><ul><li>Very useful command for report generation, text file repair, or text and floating-point decimal manipulation. The command mimics a C program, with braces '''{  }''' that surround the action to perform based on records from a database file matching either test conditions, regular expressions, etc. Fields appear as numbers with $.<br><br></li><li>Examples:<br><br>''awk '{print}' data-file.txt''<br>''awk -F";" '{print $5,$3}' data-file.txt''<br>''awk -F"," '$4 &gt;&#61; 10000 {print $1, $2}' salary.txt''<br><br></li></ul>'''<u>Crontab (Chronograph Tables)</u>'''<br><br><ul><li>Used to automatically run (as opposed to manually run) scripts, programs, or commands. There are many tables (files), but the main one is: '''/etc/cron'''. The '''crontab''' command can be used to ''list'', ''create'', ''modify'' or ''remove'' scheduled jobs in the file.<br><br></li><li>Examples:<br><br>''crontab -e -u user'' # create/modify<br>''crontab -r -u user-name'' # remove specific user's crontab<br>''crontab -l -u username'' #List current schedules</li></ul>}}
 
|}
 
|}
===Part 2: Using crontab to Alert System Administrator of Low Hard Disk Space===
+
===Part 2: Using crontab to Automatically Alert System Administrator of Low Hard Disk Space===
  
This emphasis on this section focuses on how to run useful shell scripts or shell script that we have created at specific dates/times. It would be silly 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.  
+
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 files 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.
+
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 the following steps:'''
  
 
#Perform this section in your '''c7host''' machine
 
#Perform this section in your '''c7host''' machine
 
#Make certain you are logged in as '''root'''.
 
#Make certain you are logged in as '''root'''.
 
#Change to the '''/root/bin''' directory.
 
#Change to the '''/root/bin''' directory.
#Download, study, and run the following shell script. Issue the command:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">wget https://scs.senecac.on.ca/~murray.saul/monitor-disk-space.bash</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://ict.senecacollege.ca/~ops235/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.
 
#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.
 
#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>. 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 5 messages, type '''d1-5''' and then press '''ENTER''' and enter '''q''' to exit the mail application).
+
#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 '''ALERT''' value from 90 to '''30''', save your editing session, and re-run this shell script.
+
#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 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 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?
 
#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.
  
  
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.
+
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>
 
<br><br>
 
<ol>
 
<ol>
   <li value="10">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 * * /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>
 
   <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>
Line 128: Line 121:
  
 
'''Answer INVESTIGATION 1 observations / questions in your lab log book.'''
 
'''Answer INVESTIGATION 1 observations / questions in your lab log book.'''
 
  
 
=INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM=
 
=INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM=
Line 144: Line 136:
 
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.
 
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:'''
+
:'''Perform the following steps:'''
  
 
# Remain in your '''centos2''' VM for this section.
 
# Remain in your '''centos2''' VM for this section.
 
# Make certain that you are logged in as '''root'''.
 
# Make certain that you are logged in as '''root'''.
#Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b><br><br>'''NOTE:''' If nothing displays, issue the command: '''ls /dev/sd*''' and use that device pathname '''/dev/sda''' instead.<br><br>
+
#Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b><br><br>'''NOTE:''' If nothing displays, issue the command: '''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;">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;">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;">ssm list</span></code></b>. Take a few moments to note the volume group, physical volume and logical volume sections of the command output.
Line 154: Line 146:
 
# 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;">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;">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 size, and type: <b><code><span style="color:#3366CC;font-size:1.2em;">+2G</span></code></b> at ending block (create 2GB for partition) and press '''enter'''.  
+
#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;">+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 (ignore WARNING message).
 
#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).
#You <u>'''must'''</u> '''restart''' your centos2 VM to allow changes to take effect.
+
 
#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>
+
{{Admon/important|You MUST reboot your centos2 VM|You MUST now reboot your centos2 VM before proceeding!}}
#Open a terminal as root, and format your newly-created partition by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">mkfs -t ext4 /dev/vda3</span></code></b><br>(or ''mkfs -t ext4 /dev/sda3'')
+
 
#Re-issue the '''ssm''' command. Do you see a new /dev/sda3 partition under Physical Volumes?
+
<ol><li value="13">You <u>'''must'''</u> '''restart''' your centos2 VM to allow changes to take effect.</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>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>
# 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>Re-issue the '''ssm''' command. Do you see a new /dev/vda3 partition under Physical Volumes?</li>
# Create a new logical volume by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvcreate -L 1.5G -n archive centos_centos2</span></code></b>
+
<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>
#Issue the '''ssm list''' command to view the new physical volume and logical volume information.
+
</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 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>
 +
<li>Increase the file-size by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">lvextend -r -L +1G centos_centos2/archive</span></code></b></li>
 +
<li>Issue the '''ssm list''' command to verify.</li>
 +
</ol>
  
  
Line 173: Line 178:
 
[[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.]]
 
|}
 
|}
 +
 
===Part 2: Adding Additional Virtual Hard Drives===
 
===Part 2: Adding Additional Virtual Hard Drives===
  
Let's say that you have run out of disk space on your '''centos2''' VM, and you need more space on the root file-system in order to host more web-pages, or to support a larger database. What are your options? Getting a replacement hard-drive would probably require re-installation of the operating system and/or backing up and restoring data on the old hard disk.
+
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!
Because we're using LVM we can avoid this problem. We can '''add a new <u>virtual</u> 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. 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:'''
+
:'''Perform the following steps:'''
  
 
# Remain in your '''centos2''' VM for this section.
 
# Remain in your '''centos2''' VM for this section.
Line 185: Line 190:
 
<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>
  <li>At the top of your VM window (make certain <u>not</u> full screen mode) click the '''view''' menu and change view from '''Console''' to  <u>Details</u>''' .</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: '''VirtIO''' disk.</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, 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;">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 prompts would work), restart your '''centos3''' VM and then '''format''' that partition for file type: '''ext4'''.</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>Now we'll make the new device a '''physical volume''', add it to the '''volume group''', and extend '''lv_archive''':</li>
+
<li> Format your new ''vdb1'' partition with file type: '''ext4'''</li>
</ol>
+
  <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>
:: <b><code><span style="color:#3366CC;font-size:1.2em;">pvcreate /dev/vdb1</span></code></b> (enter '''y''' to proceed - ignore warning)
+
<li>Re-issue the '''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>
:: <b><code><span style="color:#3366CC;font-size:1.2em;">vgextend cent0s_centos2 /dev/vdb1</span></code></b>
 
 
 
:: <b><code><span style="color:#3366CC;font-size:1.2em;">lvextend -L +2G -r centos_centos2/lv_archive</span></code></b>
 
 
 
<ol>
 
<li value="10">Now rerun the '''ls /dev/vd*''' , '''ssm list''' and '''df -h''' commands.</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>Among the changes, note that your root 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 '''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>Reduce the size of the '''lv_archive''' partition by '''1GB''' by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvreduce centos_centos_2/lv_archive  --size  1G</span></code></b></li>Confirm that the file system has been reduced.</li><li>Record the LVM Management commands in your lab log-book.</li>
+
  <li>'''Reboot''' your centos2 VM</li>
 +
<li>Record the LVM Management commands in your lab log-book.</li>
 
</ol>
 
</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.]]
 
[[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 ===
 
=== Part 3: Manually &amp; 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 now 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.
+
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).
+
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 the following steps:'''
  
 
# 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;">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 /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>
# 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 /arhive 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;">umount /archive</span></code></b>
 
# Issue the '''mount''' command (without arguments) to confirm it has been mounted.
 
# Issue the '''mount''' command (without arguments) to confirm it has been mounted.
  
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
+
 
 +
: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
 +
 
  
 
<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>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>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></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><br><br>'''NOTE:''' If you are having problem mounting, use the /dev pathname listing in your /etc/fstab file.<br><br></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>
<li>Confirm that this file-system has been properly mounted. Note: You could have also issued the command: '''mount -a''' to <u>automatically</u> mount the file-systems contained in the '''/etc/fstab''' file.<br><br>Tip: If you had to do manually configure /etc/fstab (eg. only in text-based mode - centos3), you could have issued the command (as root):<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).<br><br></li></ol>
+
</ol>
'''An Interesting Question''': If root needs to unmount the '''/home''' directory for maintenance but we had to first login in graphically as a regular user, then su to "root", isn't our regular user still logged in so we can't un-mount the /home directory?!? How can we as a Linux System Administrator get around the problem (hint: you already learned it somewhere in lab3!!! Wow, using what we already taught can pay-off in the future ... lol!).
 
  
  
 
'''Answer the INVESTIGATION 2 observations / questions in your lab log book.'''
 
'''Answer the INVESTIGATION 2 observations / questions in your lab log book.'''
 
  
 
= LAB 5 SIGN-OFF (SHOW INSTRUCTOR) =
 
= LAB 5 SIGN-OFF (SHOW INSTRUCTOR) =
 +
===Exclusively for Summer 2020 term, submissions are accepted only online!===
 +
Follow the submission instructions for lab 5 on Blackboard.
 
{{Admon/important|Time for a new backup!|If you have successfully completed this lab, make a new backup of your virtual machines as well as your host machine.}}
 
{{Admon/important|Time for a new backup!|If you have successfully completed this lab, make a new backup of your virtual machines as well as your host machine.}}
  
'''Perform the Following Steps:'''
+
:'''Perform the Following Steps:'''
  
 
# Make certain that your '''c7host''' and '''centos2''' VMs are running.
 
# Make certain that your '''c7host''' and '''centos2''' VMs are running.
# Switch to your '''centos2''' VM.
+
# Switch to your '''c7host''' machine.
 
# Open a shell terminal, '''su -''' into root, and change to the '''/root/bin''' directory., open a shell terminal
 
# 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 http://matrix.senecac.on.ca/~murray.saul/ops235/lab5-check.bash</span></code></b>
+
# Issue the Linux command: <b><code><span style="color:#3366CC;font-size:1.2em;">wget https://ict.senecacollege.ca/~ops235/labs/lab5-check.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 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.
 
#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 =
 
= Practice For Quizzes, Tests, Midterm &amp; Final Exam =
Line 266: Line 268:
 
[[Category:OPS235]]
 
[[Category:OPS235]]
 
[[Category:OPS235 Labs]]
 
[[Category:OPS235 Labs]]
 +
[[Category:CentOS 7]]
 +
[[Category:SSD2]]
 +
[[Category:Digital Classroom]]

Latest revision as of 12:15, 5 August 2020

LAB PREPARATION

Monitoring Disk Space can fix problems before they become a crisis (like running low on hard disk space). We will use LVM to easily resize Linux file-systems.
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

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
Solid State Drive
USB key
(for backups)
Lab5 Log Book
LVM Information

vgs
pvs
lvs
vgdisplay
pvdisplay
lvdisplay
ssm

LVM Management

lvextend
lvcreate
lvreduce
pvcreate
vgextend

Miscellaneous

mount
umount
df
du
awk
fdisk
mkfs
/etc/fstab
Using crontab

INVESTIGATION 1: MONITORING HARD DISK SPACE

The df and du commands are useful tools for Linux system administrators to flag disk space issues and investigate their causes.

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:
  1. Launch your c7host and centos2 VMs.
  2. Switch to your centos2 machine.
  3. Open a terminal, and login as root.
  4. Issue the command: df -h
  5. Note the disk space usage for the / and /home partitions.
  6. 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.
  7. 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 . | more
  8. 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.
  9. 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 command:
    find -P / -size +100000k
  10. 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).


Idea.png
Bash Shell Scripting Tips:

Using awk to Manipulate Text

  • Very useful command for report generation, text file repair, or text and floating-point decimal manipulation. The command mimics a C program, with braces { } that surround the action to perform based on records from a database file matching either test conditions, regular expressions, etc. Fields appear as numbers with $.

  • Examples:

    awk '{print}' data-file.txt
    awk -F";" '{print $5,$3}' data-file.txt
    awk -F"," '$4 >= 10000 {print $1, $2}' salary.txt

Crontab (Chronograph Tables)

  • Used to automatically run (as opposed to manually run) scripts, programs, or commands. There are many tables (files), but the main one is: /etc/cron. The crontab command can be used to list, create, modify or remove scheduled jobs in the file.

  • Examples:

    crontab -e -u user # create/modify
    crontab -r -u user-name # remove specific user's crontab
    crontab -l -u username #List current schedules

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:
  1. Perform this section in your c7host machine
  2. Make certain you are logged in as root.
  3. Change to the /root/bin directory.
  4. Download the following shell script by issuing the following command:
    wget https://ict.senecacollege.ca/~ops235/labs/monitor-disk-space.bash
  5. Try to understand what this Bash Shell script does (refer to man pages for the awk command), and then run the script as root.
  6. 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.
  7. Issue the follow command: mail (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 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).
  8. Edit the monitor-disk-space.bash shell script, and set the ALERT=90 value to ALERT=10. Then save your editing session, and re-run this shell script.
  9. 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?
  10. 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.

  1. 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.
  2. Issue the following command to setup a crontab entry for root:
    crontab -e
  3. Enter the following line in order to run at 6:00 on the first day of every month:
    0 6 1 * * /root/bin/monitor-disk-space.bash #Runs first day of each month (6:00 am)
  4. Save the crontab entry.
  5. 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,


Graphical programs like system-config-lvm are deprecated, and no longer come bundled with Centos. There are other graphical LVM programs, but are for the KDE desktop environment as opposed to Gnome. Command-line tools such as ssm (System Storage Manager), fdisk, mkfs, pvcreate, lvextend, and lvreduce are sufficient to resize file systems when using 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:
  1. Remain in your centos2 VM for this section.
  2. Make certain that you are logged in as root.
  3. Issue the command: ls /dev/vd*

    NOTE: If nothing displays, issue the command: 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.

  4. Issue the following command to install the ssm command:
    yum install system-storage-manager
  5. Issue the command: ssm list. Take a few moments to note the volume group, physical volume and logical volume sections of the command output.
  6. Compare this output from the ssm command with these other lvm commands: lvs, pvs, and vgs. Which method do you prefer to use?
  7. Check to see if there is any remaining space on your existing hard disk. Can you see any?
  8. You can create a partition by using the fdisk command. Issue the following command:
    fdisk /dev/vda (or fdisk /dev/sda if there is no /dev/vda).
  9. At the fdisk prompt, issue the command: p. What does this do?
  10. 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 size, and type: +3G at ending block (create a 3GB partition) and press enter.
  11. At the fdisk prompt, issue the command p to review the partition information.
  12. Enter the command w to save partition table and exit (ignore WARNING message).
Important.png
You MUST reboot your centos2 VM
You MUST now reboot your centos2 VM before proceeding!
  1. You must restart your centos2 VM to allow changes to take effect.
  2. Verify that you created this partition by issuing the following command: fdisk -l /dev/vda
  3. Re-issue the ssm command. Do you see a new /dev/vda3 partition under Physical Volumes?
  4. 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:
    pvcreate /dev/vda3 (or pvcreate /dev/sda3 ) (enter y to proceed - ignore warning)
Important.png
Check your VG name
Run vgs to determine your Volume Group name. If it is just centos or cl, replace centos_centos2 with centos or cl for the rest of the following commands in this lab.
  1. Issue the following command to add your new-created and formatted partition called /dev/vda3 to your volume group:
    vgextend centos_centos2 /dev/vda3
  2. Create a new logical volume by issuing the following command:
    lvcreate -L 2G -n archive centos_centos2
  3. Format your newly-created partition by issuing the command: mkfs -t ext4 /dev/centos_centos2/archive
  4. Issue the ssm list command to view the new physical volume and logical volume information.
Important.png
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 set the LV size to what you specified.
  1. Reduce the file-size by issuing the command: lvreduce -r -L -0.5G centos_centos2/archive
  2. Issue the ssm list command to verify.
  3. Increase the file-size by issuing the command: lvextend -r -L +1G centos_centos2/archive
  4. Issue the ssm list command to verify.


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 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:
  1. Remain in your centos2 VM for this section.
  2. Run the following commands and make note of the output of the commands:
    ls /dev/vd*,
    ssm list, and df -h
  1. Record the size of the volume group and the amount of free space
  2. At the top of your KVM window for centos2, click the view menu and change view from Console to Details .
  3. 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.
  4. Click the VM menu again, and return to the console view to access your centos2 VM display.
  5. Issue the command: ls /dev/vd*, what has changed?
  6. 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.
  7. Format your new vdb1 partition with file type: ext4
  8. Now we'll make the new device a physical volume, add it to the volume group by issuing the following commands:
    pvcreate /dev/vdb1 (enter y to proceed - ignore warning)
    vgextend centos_centos2 /dev/vdb1

    NOTE: If you experience an error message, issue the 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"

  9. Re-issue the ssm list command to see if there is any change.
  10. Issue the following command to extend the logical volume for the home file-system: lvextend -r centos_centos2/home --size +2G
  11. Record the size of the volume group and the amount of free space. What has changed and what caused those changes?
  12. 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!
  13. Reboot your centos2 VM
  14. Record the LVM Management commands in your lab log-book.


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 & 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:
  1. Perform this part in your centos2 VM.
  2. Issue the following command to create a mount-point (directory to connect /dev/dva3 partition to):
    mkdir /archive
  3. Issue the following command to mount the partition:
    mount -t ext4 /dev/centos_centos2/archive /archive
  4. Use the ls command to view the contents of the /archive directory. What do you see?
  5. Issue the mount command (without arguments) to confirm it has been mounted.
  6. Unmount the /archive directory by issuing the following commmand: umount /archive
  7. Issue the mount command (without arguments) to confirm it has been mounted.


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


  1. View the contents of the file-system table /etc/fstab by issuing the following command:
    cat /etc/fstab
  2. Issue the following command to add an entry to automatically mount the /archive directory upon bootup:
    echo "/dev/centos_centos2/archive /archive ext4 defaults 1 2" >> /etc/fstab
  3. Reboot your centos2 VM so see if the /archive directory is automatically mounted.

    Note: You could have also issued the command: mount -a to automatically mount the file-systems contained in the /etc/fstab file without booting the VM (good for testing before trying a reboot).


Answer the INVESTIGATION 2 observations / questions in your lab log book.

LAB 5 SIGN-OFF (SHOW INSTRUCTOR)

Exclusively for Summer 2020 term, submissions are accepted only online!

Follow the submission instructions for lab 5 on Blackboard.

Important.png
Time for a new backup!
If you have successfully completed this lab, make a new backup of your virtual machines as well as your host machine.
Perform the Following Steps:
  1. Make certain that your c7host and centos2 VMs are running.
  2. Switch to your c7host machine.
  3. Open a shell terminal, su - into root, and change to the /root/bin directory., open a shell terminal
  4. Issue the Linux command: wget https://ict.senecacollege.ca/~ops235/labs/lab5-check.bash
  5. Give the lab5-check.bash file execute permissions (for the file owner).
  6. Run the shell script and if any warnings, make fixes and re-run shell script until you receive "congratulations" message.
  7. Arrange proof of the following on the screen:
    centos2 VM:
    • Output from ssm list command.
    • Proof that /archive has been mounted
    c7host Machine:
    • Proof of creation of the shell script: monitor-disk-space.bash
    • Crontab entry for root account
    • Run the lab5-check.bash script in front of your instructor (must have all  OK  messages)
    Lab5 log-book filled out.

Practice For Quizzes, Tests, Midterm & Final Exam

  1. What is a VG? PV? LV?
  2. What is the total size of the "main" VG on your system?
  3. How do you create an LV?
  4. How do resize an LV?
  5. How would you add the disk partition /dev/sdb7 to your volume group "main"?
  6. How would you increase the size of the root filesystem by 50 MB?
  7. How can you determine if a partition has been mounted onto a system?
  8. How can you unmount an existing partition from the file-system?
  9. How can you temporarily mount a partition on a file-system?
  10. How can you permanently mount a partition on a file-system upon boot-up?
  11. What are the separate elements (fields) of the /etc/fstab file?
  12. Describe the tools that a Linux system administrator have to monitor disk space usage.