Difference between revisions of "OPS235 Lab 5 - CentOS7"

From CDOT Wiki
Jump to: navigation, search
 
(205 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Logical Volume Management and Software Package Management=
+
{{Admon/caution|THIS IS AN OLD VERSION OF THE LAB|'''This is an archived version. Do not use this in your OPS235 course.'''}}
 +
[[Category:OPS235]]
  
==Introduction==
+
=LAB PREPARATION=
  
In this lab you're going to:
+
==Purpose / Objectives of Lab 5==
 +
[[Image:disk-space.png|thumb|right|150px|Monitoring disk space and taking corrective action before problems arise is a critical operation for a system administrator.]]
  
:* Learn how to add and remove software packages on your different Linux systems.
 
:* Learn how to properly adjust your various Linux file systems by using and managing LVM both graphically (centos2) as well as command line (centos3).
 
  
== Required Materials (Bring to All Labs) ==
+
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.
  
* CentOS 6.5 x86_64 Live DVD
+
<u>Main Objectives</u>
* CentOS 6.5 x86_64 Installation DVD1
 
* SATA Hard Disk (in removable disk tray)
 
* USB Memory Stick
 
* Lab Logbook
 
  
==Prerequisites==
+
:* Using '''LVM''' to '''resize partitions graphically and via command-line'''.
 +
:* Create, partition and format '''virtual hard disks''' to increase the size of a file system.
 +
:* Manually connect and disconnect directories (mount points) to existing partitions ('''mount''', '''umount''').
 +
:* Monitoring Disk Space ('''df -h''').
 +
:* Create a Bash Shell script to '''monitor and report low disk size''' (run periodically in '''crontab''').
  
* Completion and Instructor "Sign-off" of Lab 2: [[OPS235 Lab 2 - CentOS6]]
 
  
==Linux Command Online Reference==
+
==Minimum Required Materials==
Each Link below displays online manpages for each command (via [http://linuxmanpages.com/ http://linuxmanpages.com]):
+
 
 +
{|cellpadding="15" width="40%"
  
{|width="100%" cellpadding="5"
 
|'''LVM Information Utilities:'''
 
|'''LVM Management Utilities:'''
 
|'''Additional Utilities:'''
 
 
|- valign="top"
 
|- valign="top"
|
 
*[http://linuxmanpages.com/man8/vgs.8.php vgs]
 
*[http://linuxmanpages.com/man8/lvs.8.php lvs]
 
*[http://linuxmanpages.com/man8/pvs.8.php pvs]
 
*[http://linuxmanpages.com/man8/vgdisplay.8.php vgdisplay]
 
*[http://linuxmanpages.com/man8/lvdisplay.8.php lvdisplay]
 
*[http://linuxmanpages.com/man8/pvdisplay.8.php pvdisplay]
 
|
 
*[http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-system-config-lvm.html system-config-lvm] (Tutorial)
 
*[http://linuxmanpages.com/man8/lvextend.8.php lvextend]
 
*[http://linuxmanpages.com/man8/resize2fs.8.php resize2fs]
 
*[http://linuxmanpages.com/man8/lvcreate.8.php lvcreate]
 
*[http://linuxmanpages.com/man8/lvreduce.8.php lvreduce]
 
*[http://linuxmanpages.com/man8/pvcreate.8.php pvcreate]
 
*[http://linuxmanpages.com/man8/vgextend.8.php vgextend]
 
|
 
*[http://linuxmanpages.com/man8/mount.8.php mount]
 
*[http://linuxmanpages.com/man8/umount.8.php umount]
 
*[http://linuxmanpages.com/man8/yum.8.php yum]
 
*[http://linuxmanpages.com/man8/rpm.8.php rpm]
 
|}
 
  
==Resources on the web==
+
|width="10%" | [[Image:harddrive.png|thumb|left|85px|<b>Removable Hard Disk Pack</b> (SATA)]]
Additional links to tutorials and HOWTOs:
 
'''Please read this page to get an overview of LVM:'''<br /><br />
 
:* [[Logical Volume Management]] ('''Note:''' It is recommended to return to this guide as a reference when performing the next several investigations)
 
:* [http://fedoranews.org/alex/tutorial/rpm/ Using the RPM Utility]
 
:* [http://www.centos.org/docs/5/html/yum/ Using the YUM Utility]
 
  
 +
|width="10%" |[[Image:ubs-key.png|thumb|left|85px|<b>USB key</b><br>(for backups)]]
  
 +
|width="10%" |[[Image:log-book.png|thumb|left|70px|<b>Lab5 Log Book</b>]]
  
=Logical Volume Management (Introduction)=
+
|}
  
== Check Current LVM Information Prior to Performing Investigations ==
+
==My Toolkit (CLI Reference)==
  
'''LVM''' ('''Logical Volume Management''') is used to manage hard disk drives / partitions for Unix/Linux systems. LVM provides more flexibility than just working with hard disks / hard disk partitions. '''Volume Groups''' are areas used to define '''Physical Volumes''' (i.e. hard disks, disk partitions, or other forms of storage devices). '''Logical Volumes''' are then used to relate directories (mount points) to a specific physical volume or for a "range" or "span" of physical volumes.
+
{|width="50%" cellpadding="15"
 +
|- valign="top"
 +
|width="10%" |<u>LVM Information:</u>
 +
*[http://man7.org/linux/man-pages/man8/vgs.8.html vgs]<br>
 +
*[http://man7.org/linux/man-pages/man8/pvs.8.html pvs]<br>
 +
*[http://man7.org/linux/man-pages/man8/lvs.8.html lvs]<br>
 +
*[http://man7.org/linux/man-pages/man8/vgdisplay.8.html vgdisplay]<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>
 +
|width="10%" |<u>LVM Management</u>
 +
*[http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-system-config-lvm.html system-config-lvm]<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/lvreduce.8.html lvreduce]<br>
 +
*[http://man7.org/linux/man-pages/man8/pvcreate.8.html pvcreate]<br>
 +
*[http://man7.org/linux/man-pages/man8/vgextend.8.html vgextend]<br>
 +
|width="10%" |<u>Miscellaneous</u>
 +
*[http://man7.org/linux/man-pages/man8/mount.8.html mount]<br>
 +
*[http://man7.org/linux/man-pages/man8/umount.8.html umount]<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://www.lehman.cuny.edu/cgi-bin/man-cgi?awk+1 awk]<br>
 +
*[http://tldp.org/HOWTO/Partition/fdisk_partitioning.html fdisk]<br>
 +
*[http://www.cyberciti.biz/faq/howto-format-create-linux-filesystem/ mkfs]
 +
*[http://man7.org/linux/man-pages/man5/fstab.5.html /etc/fstab]
 +
*[http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800 Using crontab]
  
Therefore, LVM allows more flexibility and growth potential for Linux systems (for example, having Logical volumes span multiple hard disks). CentOS uses LVM by default upon installation. Other Linux distributions may provide the capacity to install LVM, or later install and then use Logical Volume Management.
+
|}
  
'''Although the concept of LVM is simple, it takes practice and preparation (or "thinking ahead") in order to correctly use this valuable tool'''. It is important for students to gain an understanding of LVM, and how to manage their filesystems using LVM. It is recommended for students to read the following resource prior to this lab and/or return to the following resource while performing this lab: [[Logical Volume Management]]
+
=INVESTIGATION 1: MANAGING FILE-SYSTEMS USING LVM=
 +
{| width="40%" align="right" cellpadding="10"
 +
|- valign="top"
 +
|{{Admon/note|VM Backups and Recovery|Most of these investigations will take place in your virtual machines. If you make a significant mistake, your virtual machine may not boot. Remember that you created backups of your virtual machines in Lab 2, and you can restore them if something goes wrong.<br /><br />'''Remember: if you did not create backups for all of your VMs, then you don't have any restoration points to fall-back to!'''|}}
 +
|}
 +
Monitoring and ensuring adequate space for a Linux file-system is considered to be a critical task for any system administrator. An application called '''LVM''' is a very useful tool for Linux system administrators to easily manage file systems - even when the computer system is running!
  
At this point, having completed [[OPS235 Lab 2 - CentOS6|Lab 2]] you should have roughly the following disk storage setup:
+
'''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.
  
('''Note:''' These results are output from the '''vgs''', '''pvs''', and '''lvs''' commands that provide volume group, physical volume and logical volume information for each of the virtual machines that you created in lab2. Before proceeding, check these values with your own system to see if you are generally "on the right track".)
+
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,
  
* '''centos1'''
 
  
  VG        #PV #LV #SN Attr  VSize VFree
+
=== Part 1: Manage LVM Graphically===
  vg_centos1  1   2  0 wz--n- 9.50g    0
 
  
  PV        VG        Fmt  Attr PSize PFree
+
[[Image:lvm1.png|thumb|500px|right|The '''system-config-lvm''' application allows the Linux system administrator to manage LVMs Graphically. Your LVM information for '''centos2''' VM should be similarly displayed.]]
  /dev/vda3  vg_centos1 lvm2 a-- 9.50g    0
+
# Let's learn to administer (manage) our LVM graphically for our '''centos2''' Virtual Machine.
 
+
# Launch your '''centos2''' VM and open a shell terminal.
  LV      VG        Attr  LSize Origin Snap%  Move Log Copy%  Convert
+
# CentOS provides a tool called '''system-config-lvm''' to graphically administer LVM.<br><br>'''NOTE:''' This tool may be replaced by a future graphical LVM management tool and is NOT currently available in the default repositories. We will now perform a "work-around" in order to make the system-config-lvm utility work in our system.<br><br>
  lv_root vg_centos1 -wi-ao 8.03g                                   
+
# Issue the following command to download the system-config-lvm rpm:<br><b><code><span style="color:#3366CC;font-size:1.2em;">wget https://kojipkgs.fedoraproject.org//packages/system-config-lvm/1.1.18/1.fc19/noarch/system-config-lvm-1.1.18-1.fc19.noarch.rpm</span></code></b><br>(Although it is an rpm file for the Fedora Distribution, it will also work for Centos7).<br><br>
  lv_swap vg_centos1 -wi-ao 1.47g
+
# Make certain that you are logged in as '''root'''.
 
+
# Try issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm</span></code></b><br>You should notice an error.
* '''centos2'''
+
# Use the '''yum install''' command to resolve the following dependencies:<ul><li><b><code><span style="color:#3366CC;font-size:1.2em;">yum install gnome-python2-bonobo</span></code></b></li><li><b><code><span style="color:#3366CC;font-size:1.2em;">yum install gnome-python2-gnome</span></code></b></li><li><b><code><span style="color:#3366CC;font-size:1.2em;">yum install usermode-gtk</span></code></b></li></ul><br>
 
+
<ol>
  VG        #PV #LV #SN Attr  VSize  VFree
+
  <li value="8">Re-issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm</span></code></b><br>It should work this time.</li><li>Run the command: <b><code><span style="color:#3366CC;font-size:1.2em;">system-config-lvm</span></code></b></li><li>On the left-hand side, you can click on the '''Volume Group''', '''Physical Volume''' and '''Logical Volumes''' and view their properties on the on the right-hand side.</li><li>Determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers:
  vg_centos2  1   3  0 wz--n- 19.51g 7.77g
+
<ol type="a">
 
+
  <li>What are the names and sizes of the '''Volume Group'''?</li>
  PV        VG        Fmt  Attr PSize  PFree
+
  <li>What is the name and size of the '''Physical Volumes'''?</li>
  /dev/vda2  vg_centos2 lvm2 a-- 19.51g 7.77g
+
  <li>What are the names and sizes of the '''Logical Volumes'''?</li>
 
+
  <li>Is there any space in the VG which is not allocated to a LV?<br><br></li>
  LV      VG        Attr      LSize Pool Origin Data% Move Log Cpy%Sync Convert
+
</ol>
  lv_home vg_centos2 -wi-ao---- 1.95g                                           
+
  </li>
  lv_root vg_centos2 -wi-ao---- 7.81g                                           
+
<li>Click on '''Logical View''' (i.e. above home, root, swap) and then click '''Create New Logical Volume''' button. What happens? Why do you think this happened?<br><br>We will now create another partition ('''/dev/vda3''') using the '''fdisk''' utility, format this newly-created partition so we can create another logical volume called '''archive'''.<br><br> </li>
  lv_swap vg_centos2 -wi-ao---- 1.97g
+
  <li>Quit the '''system-config-lvm''' application.</li>
 
+
   <li>In the terminal 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>
* '''centos3 (Note:  This VM has no GUI installed)'''
+
   <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">fdisk /dev/vda</span></code></b><br>(or ''fdisk /dev/sda'' if the previous command didn't work)</li>
+
   <li>At the fdisk prompt issue the command: '''p'''. What does this do?</li>
   VG        #PV #LV #SN Attr  VSize VFree
+
   <li>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: +2G''' at ending block (create 2GB for partition) and '''press enter'''. </li>
   vg_centos3  3   0 wz--n- 14.51g 4.54g
+
  <li>At the fdisk prompt, issue the command '''p''' to review the partition information, then type '''w''' to save partition table and exit (ignore WARNING message).</li>
 
+
   <li>You <u>'''must'''</u> restart your centos2 VM to allow changes to take effect</li>
   PV        VG        Fmt  Attr PSize PFree
+
   <li>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'')</li>
   /dev/vda3 vg_centos3 lvm2 a--  14.51g 4.54g
+
</ol>
 
+
{| width="40%" align="right" cellpadding="10"
  LV      VG        Attr  LSize  Origin Snap% Move Log Copy%  Convert
+
|- valign="top"
  lv_home vg_centos3 -wi-ao----  1.00g                                           
+
|
   lv_root vg_centos3 -wi-ao----  8.00g                                           
+
[[Image:lvm3.png|thumb|right|500px|What '''system-config-lvm''' display should appear after using '''fdisk''' to create new partition (/dev/sda3), '''formatting''' partition, '''adding partition to LVM''', and '''allocating partition''' to our default volume group.]]
   lv_swap vg_centos3 -wi-ao---- 992.00m                                   
+
|
+
[[Image:lvm4.png|thumb|right|200px|Creating a new logical volume from our newly created physical volume]]
* '''c6host (i.e. "removable hard disk" or "disk pack")'''
+
|}
 
+
<ol>
Does not use LVM. Confirm this using the same commands used above.
+
   <li value="21">'''Restart the system-config-lvm utility'''. Do you see a new /dev/sda3 partition under Physical Volumes?</li>
 
+
   <li>To add the newly created partition, you need to add it into LVM to be used. '''Exit the system-config-lvm utility''' and '''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>
{{Admon/note|VM Backups and Recovery|Most of these investigations will take place in your virtual machines. If you make a significant mistake, your virtual machine may not boot. Remember that you created backups of your virtual machines in Lab 2, and you can restore them if something goes wrong.<br /><br />'''Remember: if you did not create backups for all of your VMs, then you don't have any restoration points to fall-back to!'''|}}
+
  <li>'''Restart system-config-lvm'''. You should notice a section at the bottom indicating <u>Unallocated</u> Physical Volumes. Click on the <u>unallocated</u> partition /dev/vda3 and then click '''Add to Existing Volume Group''' (click on other button in next screen to continue). The screen should now show the partition ''/dev/vda3'' contained in our default Volume Group (refer to diagram on right-side for reference).</li>
 +
  <li>Click on Logical View (i.e. above home, root, swap), and then click on '''Create New Logical Volume''' button and fill out the details for this new logical volume including: size: '''1GB''', LV Properties: '''linear''', file system: '''ext4''' file system name: '''archive''', select '''mount''' and mount it at: '''/archive'''. Have the system create the directory ''/archive'' if it does not exist (Refer to diagram on right for reference).</li>
 +
  <li>Confirm that this new Logical Volume now exists by changing to the directory /archive. Did you need to restart your centos2 VM to confirm that the '''/archive''' directory is now on your '''centos2''' VM?</li>
 +
  <li>Use system-config-lvm to resize (i.e. edit) the '''archive''' logical volume to '''1.5 GB''' and verify your results.</li>
 +
</ol>
  
== Investigation 4: Extend the size of lv_root using command-line tools ==
 
  
Perform this investigation in centos3.
+
'''Answer Part 1 observations / questions in your lab log book.'''
  
Let's say that you have run out of disk space on your computer (centos3), you need more space on the root filesystem, perhaps to host more webpages or a larger database or new software. What are your options? Getting a replacement harddrive would probably require reinstallation of the operating system and backup/restore of the data.
+
===Part 2: Managing LVM Via Command Line & Adding Virtual Hard Drives===
 +
[[Image:add_virtual_disk.png|thumb|400px|right|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'''.]]
 +
Let's say that you have run out of disk space on your '''centos3''' 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.
  
Because we're using LVM though - we can avoid that. We can add a new harddrive (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.
+
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 operations to increase the size of lv_root in centos3:'''
 
'''Perform the following operations to increase the size of lv_root in centos3:'''
  
<u>'''<font>Steps:</font>'''</u>
 
  
 +
# Perform this Part in your '''centos3''' VM.
 +
# Run the following commands and make note of the output:
 +
::<b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b>
 +
::<b><code><span style="color:#3366CC;font-size:1.2em;">pvs</span></code></b>
 +
::<b><code><span style="color:#3366CC;font-size:1.2em;">vgs</span></code></b>
 +
::<b><code><span style="color:#3366CC;font-size:1.2em;">lvs</span></code></b>
 +
::<b><code><span style="color:#3366CC;font-size:1.2em;">df -h</span></code></b>
 
<ol>
 
<ol>
  <li>Run the following commands and make note of the output:</li>
+
  <li value="3">Record the size of the volume group and the amount of free space</li>
  <code>ls /dev/vd*
+
<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>
  pvs
+
  <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'''.<br><br>'''NOTE: Make certain to click "select managed or other existing storage", and fill in the pathname circled in red as shown in the diagram on the right-hand-side.<br><br>'''</li>
  vgs
+
  <li>Go back to the '''console''' view</li>
  lvs
+
  <li>Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">ls /dev/vd*</span></code></b>, what has changed?</li>
  df -h</code>
+
<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>Open the centos3 virtual machine console</li>
+
  <li>Now we'll make the new device a '''physical volume''', add it to the '''volume group''', and extend '''lv_root''':</li>
<li>Go to the hardware details view</li>
+
</ol>
  <li>Click "Add Hardware" and add a new storage device of 2GBs, make sure it's a VirtIO disk.</li>
+
:: <b><code><span style="color:#3366CC;font-size:1.2em;">pvcreate /dev/vdb1</span></code></b> (enter '''y''' to proceed - ignore warning)
  <li>Go back to the console view</li>
 
  <li>Run the same ls command (performed in step 1), what's changed?</li>
 
  <li>Now we'll make the new device as a physical volume, add it to the volume group, and extend lv_root:</li>
 
  <code>pvcreate /dev/vdb
 
  
  vgextend vg_centos3 /dev/vdb
+
:: <b><code><span style="color:#3366CC;font-size:1.2em;">vgs</span></code></b> (to determine: ''name_of_volumegroup'')
  
  lvextend -L +2G -r vg_centos3/lv_root</code>
+
:: <b><code><span style="color:#3366CC;font-size:1.2em;">vgextend ''name_of_volumegroup''  /dev/vdb1</span></code></b>
<li>Now rerun the ls/pvs/vgs/lvs/df commands. What has changed and what caused those changes?</li>
 
<li>Among the changes, note that your root filesystem is now 2GB bigger - and you have not even rebooted your machine!</li>
 
</ol>
 
  
'''Answer the Investigation 4 observations / questions in your lab log book.'''
+
:: <b><code><span style="color:#3366CC;font-size:1.2em;">lvextend -L +2G -r ''name_of_volumegroup''/lv_root</span></code></b>
  
== Investigation 5: How is LVM managed using system-config-lvm? ==
+
{| width="40%" align="right" cellpadding="10"
 +
|- valign="top"
 +
|{{Admon/note|LVM Safeguards When Reducing File System Size|In older versions of LVM, the system administrator had to be careful when reducing the size of Linux file-systems using LVM. This usually required to separate operations:<ul><li>Reducing the file-system to make room (''resize2fs'')</li><li>Reduce the logical volume by using the '''lvreduce''' command</li></ul>This used to be performed to prevent a catastrophic loss of data if the logical volume was resize smaller than the available space on the file-system.<br>The newer LVM commands have "safeguards" (eg. option '''-r''' to prevent destroying the file-system (thus making the ''resize2fs'' command <u>obsolete</u>). '''|}}
 +
|}
  
'''Perform this investigation on the VM named ''<u>centos2</u>''.'''
 
[[Image:Ops235_lab4_1.png|thumb|right|Screenshot of system-config-lvm in CentOS. Click to enlarge.]]
 
# Let's learn to administer (manage) our LVM graphically for our '''centos2''' Virtual Machine.<br />CentOS provides a tool called <code>system-config-lvm</code> to graphically administer LVM. The package is not installed by default, so you will need to install it by issuing: (<code>yum install system-config-lvm</code>).
 
<ol>
 
<li value="2">Use this tool to determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers:
 
<ol type="a">
 
  <li>What are the names and sizes of the '''PV'''s?</li>
 
  <li>What is the name and size of the '''VG'''?</li>
 
  <li>What are the names and sizes of the '''LV'''s?</li>
 
  <li>Is there any space in the VG which is not allocated to a LV?</li>
 
</ol>
 
</li>
 
<li>Increase the size of the home file-system to '''4 GB''':
 
  <ol type="a">
 
  <li>Click on the LV containing the '''home''' filesystem.</li>
 
  <li>Click on ''Edit Properties''.</li>
 
  <li>Change the size to '''4 GB''' and click ''Ok''.</li>
 
  </ol>
 
<li>Create a '''new 3G LV''' (LV Properties: linear) containing an ext4 filesystem named lv_archive and mount it at '''/archive'''
 
  <li>Backup <code>'''/etc'''</code> into <code>'''/archive'''</code></li>
 
</ol>
 
::* Copy the files in /etc into the filesystem mounted at <code>/archive</code><br />(use the graphical tools or the command line. If using cp, remember to use the -R option).
 
 
<ol>
 
<ol>
  <li value="6">Shrink the size of <code>lv_archive</code> to '''1 GB'''.</li>
+
  <li value="13">Now rerun the '''ls /dev/vd*''' , '''pvs''' , '''vgs''' , '''lvs''' and '''df -h''' commands.</li>
<li>Try shrinking the home file-system. What happens? Why?</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>Reduce the size of the '''lv_root''' partition by '''1GB''' by issuing the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvreduce ''name_of_volumegroup''/lv_root  --size  -1G</span></code></b></li>Confirm that the file system has been reduced.</li><li>Increase the size of the '''lv_root''' partition by another '''1GB''' by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">lvextend ''name_of_volumegroup''/lv_root  --size  +1G</span></code></b></li>Confirm that the file system can been increased.</li><li>Record the LVM Management commands in your lab log-book.</li>
 
</ol>
 
</ol>
  
'''Answer the Investigation 5 observations / questions in your lab log book.'''
 
  
= Storage Setup (prior to starting Lab 4) =
+
'''Answer Part 2 observations / questions in your lab log book.'''
  
At this point, having completed [[OPS235 Lab 3 - CentOS6|Lab 3]] you should have <u>'''roughly'''</u> the following disk storage setup:<br />
 
('''Note:''' These results are output from the '''vgs''', '''lvs''', and '''pvs''' commands within each virtual machine. You will be using these commands in the following lab investigations)<br /><br />
 
  
* '''centos1'''
+
= INVESTIGATION 2: ADDITIONAL FILE-SYSTEM OPERATIONS =
  
  This VM has a GUI and no free space in the VG.
+
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. This was create automatically for the '''/archive''' mount point automatically when we used the system-config-lvm utility in the previous investigation. We need to learn now to do this manually by editing or adding an entry in the ''/etc/fstab'' file. The '''/etc/fstab''' (file system table) contains entries to mount various file systems automatically upon start-up of the Linux system.
 
  VG        #PV #LV #SN Attr  VSize VFree
 
  vg_centos1  1  2  0 wz--n- 9.80G    0
 
 
  LV      VG        Attr  LSize 
 
  lv_root vg_centos1 -wi-ao  8.83G                                   
 
  lv_swap vg_centos1 -wi-ao 992.00M                                   
 
 
  PV        VG        Fmt  Attr PSize PFree
 
  /dev/vda2  vg_centos1 lvm2 a-   9.80G    0
 
  
* '''centos2'''
+
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).
  
  This VM has a GUI and free space in the VG.
 
 
  VG        #PV #LV #SN Attr  VSize VFree
 
  vg_centos2  1  4  0 wz--n- 19.51g 4.73g
 
 
  LV      VG        Attr  LSize 
 
  lv_archive vg_centos2 -wi-ao---- 1.00g                                           
 
  lv_home    vg_centos2 -wi-ao---- 4.00g                                           
 
  lv_root    vg_centos2 -wi-ao---- 7.81g                                           
 
  lv_swap    vg_centos2 -wi-ao---- 1.97g                                 
 
 
  PV        VG        Fmt  Attr PSize PFree
 
  /dev/vda2  vg_centos2 lvm2 a--  19.51g 4.73g
 
  
* '''centos3'''
+
== Part 1: Mounting and Un-mounting Partitions ==
 +
[[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.]]
 +
# Perform this part in your '''centos2''' VM.
 +
# As you may recall in Part 1 of Investigation 1, we created another logical volume called '''archive''' using the graphical application '''system-config-lvm'''. This logical volume should be mounted (connected) to our existing file-system (2GB).
 +
# Issue the following command: <b><code><span style="color:#3366CC;font-size:1.2em;">mount</span></code></b>
 +
# What is the purposed of issuing this command without arguments? Can you see the '''/archive''' mount point?
 +
# Confirm that the directory named '''/archive''' is mounted.
 +
# You can use the '''umount''' command to unmount a file-system (eg. for maintenance purposes). Note the the name of the command is umount, <u>not</u> unmount.
 +
# Make certain you are not located in the /archive directory, and issue the following command to unmount the /archive directory:<br><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 unmounted.
 +
# 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>
 +
# 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.
 +
# 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>
 +
# 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>
 +
# '''A curious 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!).
  
  This VM has no GUI installed.
 
 
  VG        #PV #LV #SN Attr  VSize VFree
 
  vg_centos3  2  3  0 wz--n- 16.50g    0
 
 
  LV      VG        Attr  LSize 
 
  lv_home vg_centos3 -wi-ao----  1.00g                                           
 
  lv_root vg_centos3 -wi-ao----  10.00g                                           
 
  lv_swap vg_centos3 -wi-ao---- 992.00m                                   
 
 
  PV        VG        Fmt  Attr PSize  PFree
 
  /dev/vda3  vg_centos3 lvm2 a--  14.51g 2.54g
 
  /dev/vdb  vg_centos3 lvm2 a--  2.00g 2.00g
 
  
* '''c6host (i.e. "disk pack")'''
 
  
  /dev/sdc3        20G  3.7G  15G  20% /
+
'''Answer the Part 1 observations / questions in your lab log book.'''
  /dev/sdc2        29G  4.7G  23G  18% /home
 
  /dev/sdc1        97G  48G  45G  52% /var/lib/libvirt/images
 
  
= Logical Volume Management (Continued) =
 
  
{{Admon/note|Recovering VMs|'''Most of these investigations will take place in you virtual machines.''' If you make a significant mistake, your virtual machine may not boot. Remember that you created backups of your virtual machines in Lab 3, and you can '''restore them if something goes wrong'''.}}
+
== Part 2: Monitoring Disk Space ==
 +
[[Image:disk_usage.png|thumb|500px|right|The '''df''' and '''du''' commands are useful tools for Linux system administrators to flag disk space issues and investigate their causes.]]
 +
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.
  
== Resources ==
+
Monitoring disk space activity helps provide Linux system administrators information to help take corrective action before problems can occur.
  
Please read this page to get an overview of LVM:
+
# Remain in your '''centos2''' VM for this section.
[http://zenit.senecac.on.ca/wiki/index.php/Logical_Volume_Management Logical Volume Management]
+
# Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">df -h</span></code></b>
 +
# Note the disk space usage for '''/''', '''/home''', and '''/archive''' partitions.
 +
# If a partition is running out of available space, the Linux System Administrator can reallocate space among partitions or add another disk and grow the file system (like you did in a previous investigation). The administrator also can investigate the cause of low disk space. Two examples immediately come to mind: excessive use of space from users, and potential penetration from hackers.
 +
# To investigate excessive disk usage by regular users, you can obtain a total amount of disk usage for that user by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">du -hs /home/regularuserid</span></code></b>
 +
# To provided a more detailed list of usage (file-by-file), issue the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">du -h /home/regularuserid | 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.
 +
# The methods to monitor potential penetration to a Linux system are too numerous, and are taught in other coures (for example: SEC520). On method of monitoring potential penetration is to look for abnormally large files. Issue the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">find -P / -size +100000k</span></code></b>
 +
# Did you see any files that are that size?
 +
# The next section will apply some of these tools we have discussed into a shell script and crontab entry to periodically monitor and contact the system administrator of potential disk space issues (before they become a serious problem).
  
It is recommended to open another window(eg. '''right-click''', '''open in new tab or window''') to make reference to view LVM commands as you perform investigations 1 to 3.
 
  
== Investigation 1: How are LVMs are managed using Command-Line Tools  ==
+
'''Answer the Part 2 observations / questions in your lab log book.'''
# You are going to repeat the same LVM management operations (as your did with the ''centos2'' VM in '''lab3'''), but you will using command-line tools in the '''centos3''' VM. Since the centos3 VM only operates in command-line mode, you will need to refer to the '''"Logical Volume Management"''' link above.
 
# Write down the exact commands used at each step, and record appropriate command output:
 
:: a. Determine the current LVM configuration using the <code>pvs</code>, <code>vgs</code>, and <code>lvs</code> commands.
 
:: b.Grow the home filesystem to 2G using the command <code>lvextend</code> and <code>resize2fs</code>.
 
:: c.Create a new 2G LV containing an ext4 filesystem and mount it at /archive (use <code>mkdir</code>, <code>lvcreate</code>, <code>mkfs</code>, <code>mount</code>, edit the file <code>/etc/fstab</code> (read the resource: [https://wiki.archlinux.org/index.php/fstab https://wiki.archlinux.org/index.php/fstab] ), and then reboot to confirm automatic mount).
 
:: d.Copy the contents of <code>/etc</code> into <code>/archive</code>.
 
:: e.Shrink <code>lv_archive</code> to 1G (use <code>umount</code>, <code>resize2fs</code>, <code>lvreduce</code>,  and <code>mount</code>)
 
  
== Investigation 2: How can a PV be added to an existing VG using Command-Line Tools? ==
 
# Add an additional 2 GB virtual disk to your ''<u>centos1</u>'' system, we will use it as an additional physical volume.
 
# Do the rest of this investigation in the command line.
 
# You should now have both <code>/dev/vda</code> and <code>/dev/vdb</code>.
 
# Record the size of the volume group and the amount of free space (Hint: use a command that you learned in a previous lab).
 
# Read the resource [http://tldp.org/HOWTO/Partition/fdisk_partitioning.html Partitioning with fdisk] to learn how to properly create a partition with the fdisk command.
 
# Use fdisk to partition /dev/vdb with a Linux single partition that fills the whole disk.
 
# Check the messages printed when <code>fdisk</code> exits -- you may need to reboot the system in order for the new partition table to take effect.
 
# Mark ''vdb1'' as a physical volume usable by LVM.
 
# Add the new physical volume to the existing volume group.
 
# Verify that the volume group is larger and has more free space.
 
  
{{Admon/note|Think!|The next part of this investigation requires some research, thought, and creativity.}}
+
= INVESTIGATION 3: LOOKING AHEAD =
'''Using that additional space, create a separate filesystem for <code>/home</code>:'''
 
# Create the logical volume <code>lv_home</code> (1G ext4)
 
# Find a way to move the contents of <code>/home</code> onto it.
 
# Change your system configuration so that the new filesystem is mounted on <code>/home</code> from now on.
 
  
{{Admon/tip|SELinux relabelling may be required|Your system may report that the files are not present in the new /home filesystem when they are clearly there. This is due to the Security-Enhanced Linux system (SELinux) preventing access to the files, because the files were tampered with (moved) and are no longer recognized as home directory content. You can fix this problem by restoring the file context labels so that SELinux accepts the files as valid home directory content: <code>restorecon -r /home</code>}}
+
==Automating Routine Tasks (Scheduling The Running of Shell Script Via Crontab)==
 +
{|width="40%" align="right" cellpadding="10"
 +
|- valign="top"
 +
|{{Admon/tip|Bash Shell Scripting Tips:|<br><ul><li>'''Using awk to Manipulate Text:'''<br><br>Legend has it that the '''awk''' command was invented by three C programmers that wanted to create a utility in Unix that had programming syntax more like C programming. People started to use the command and found it very useful for report generation and file repair or manipulation.<br><br>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>'''<u>Examples</u>:'''<br><br>''awk '{print}' data-file.txt''<br><br>''awk -F";" '{print $5,$3}' data-file.txt''<br><br>''awk -F"," '$4 &gt;&#61; 10000 {print $1, $2}' salary.txt''</li></ul>}}
 +
|}
  
== Investigation 3: How can I use fdisk to manage storage on my disk pack? ==
+
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. 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.
'''On your <u>c6host</u> (i.e. "disk pack"), using your choice of the GUI and/or command-line tools:'''
 
# Create a new partition or logical drive ('''NOT a logical volume!''') - minimum 5G, leaving at least 10G free space in the extended partition.
 
# Format that partition (i.e. put a filesystem on it).
 
# Make sure it's mounted automatically on a mountpoint of your choosing (easiest is to create a new directory for a mountpoint).
 
  
= Updated backup instructions =
 
  
If you completed this lab correctly - please make sure you are still making full backups of your virtual machines.
 
  
Remember adding a new harddrive to centos3? You will need to back up that drive as well. If you don't - you will only have half of your machine backed up, which is of no use for a recovery.
+
#Perform this section in your '''c7host''' machine
 +
#Make certain you are logged in as '''root'''.
 +
#Download, study, and run the following shell script. Issue the command:<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>
 +
#Try to understand what this Bash Shell script does (refer to man pages for the '''awk''' command), and then run the script as root.
  
  
 +
In order to automatically run the above-mentioned script periodically, you use the scheduler in Linux called '''crontab'''. The term crontab stands for '''Chronograph Tables''', where a chronograph is the old term for a timepiece (the forerunner of the modern watch). You can run the crontab command to schedule commands and shell script to be run in a number of different ways.
 +
<br><br>
 +
<ol>
 +
  <li value="5">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/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 3 observations / questions in your lab log book.'''
  
==Resources on the web==
+
= LAB 5 SIGN-OFF (SHOW INSTRUCTOR) =
Additional links to tutorials and HOWTOs:
+
{{Admon/important|Time for a new backup, INCLUDING YOUR VIRTUAL HARD DRIVE!|If you have successfully completed this lab, make a new backup of your virtual machines. <br><br>'''Virtual hard-drives created in this lab are image files, and now need to be backed up as well!'''.}}
* [http://www.centos.org/docs/2/rhl-gsg-en-7.2/s1-zip-tar.html archiving with tar, gzip, gunzip]
 
* [[init vs systemd]]
 
* [https://wiki.debian.org/Debate/initsystem/upstart init vs systemd vs upstart]
 
* [https://www.centos.org/docs/5/html/5.2/Installation_Guide/s2-init-boot-shutdown-rl.html Runlevels]
 
 
 
 
 
 
 
  
==Completing the Lab==
+
'''Arrange proof of the following on the screen:'''
  
Arrange evidence for each of these items on your screen, then ask your instructor to check each item:
+
<ol><li><span style="color:green;font-size:1.5em;">&#x2713;</span> '''centos2''' VM:<blockquote><ul><li>'''Logical Volume''', '''Physical Volume''', and '''Volume Group''' information </li><li>Contents of '''/etc/fstab''' file</li><li>Proof that '''/archive''' has been mounted</li></ul></blockquote><li><span style="color:green;font-size:1.5em;">&#x2713;</span> '''centos3''' VM:<blockquote><ul><li>Proof of virtual hard disk creation</li><li>'''Logical Volume''', '''Physical Volume''', and '''Volume Group''' information </li></ul></blockquote></li><li><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></ul></blockquote><li><span style="color:green;font-size:1.5em;">&#x2713;</span> '''Lab5''' log-book filled out.</li></ol>
  
# Compressed files:  <code>/tmp/archive1.tar.gz</code> and <code>/tmp/archive2.tgz</code>
 
#<code>nled</code> application is installed
 
#Lab5 notes how to use service/chkconfig commands
 
#VMs backed-up
 
  
= Preparing for Quizzes =
+
== Practice For Quizzes, Tests, Midterm &amp; Final Exam ==
  
 
# What is a VG? PV? LV?  
 
# What is a VG? PV? LV?  
# What commands are used to determine VG / PV / LV information?
 
# What does yum do that rpm does not?
 
# List the steps to install a package via rpm command.
 
# List the steps to determine detailed information regarding an install package.
 
# List the steps to remove a package via rpm command.
 
# List the steps to install a package using the yum command.
 
# List the steps to remove a package using the yum command.
 
 
# What is the total size of the "main" VG on your system?
 
# What is the total size of the "main" VG on your system?
# How do you create a LV?
+
# How do you create an LV?
# How do you delete 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 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.
  
 
+
[[Category:OPS235]]
 
+
[[Category:OPS235 Labs]]
{{Admon/important | Backup your work |Backup your disk images for centos2 and centos3 Virtual Machines.}}
 

Latest revision as of 11:15, 27 November 2019

Stop (medium size).png
THIS IS AN OLD VERSION OF THE LAB
This is an archived version. Do not use this in your OPS235 course.

LAB PREPARATION

Purpose / Objectives of Lab 5

Monitoring disk space and taking corrective action before problems arise is a critical operation for a system administrator.


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

  • Using LVM to resize partitions graphically and via command-line.
  • Create, partition and format virtual hard disks to increase the size of a file system.
  • Manually connect and disconnect directories (mount points) to existing partitions (mount, umount).
  • Monitoring Disk Space (df -h).
  • Create a Bash Shell script to monitor and report low disk size (run periodically in crontab).


Minimum Required Materials

Removable Hard Disk Pack (SATA)
USB key
(for backups)
Lab5 Log Book

My Toolkit (CLI Reference)

LVM Information: LVM Management Miscellaneous

INVESTIGATION 1: MANAGING FILE-SYSTEMS USING LVM

Note.png
VM Backups and Recovery
Most of these investigations will take place in your virtual machines. If you make a significant mistake, your virtual machine may not boot. Remember that you created backups of your virtual machines in Lab 2, and you can restore them if something goes wrong.

Remember: if you did not create backups for all of your VMs, then you don't have any restoration points to fall-back to!

Monitoring and ensuring adequate space for a Linux file-system is considered to be a critical task for any system administrator. An application called LVM is a very useful tool for Linux system administrators to easily manage file systems - 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: Manage LVM Graphically

The system-config-lvm application allows the Linux system administrator to manage LVMs Graphically. Your LVM information for centos2 VM should be similarly displayed.
  1. Let's learn to administer (manage) our LVM graphically for our centos2 Virtual Machine.
  2. Launch your centos2 VM and open a shell terminal.
  3. CentOS provides a tool called system-config-lvm to graphically administer LVM.

    NOTE: This tool may be replaced by a future graphical LVM management tool and is NOT currently available in the default repositories. We will now perform a "work-around" in order to make the system-config-lvm utility work in our system.

  4. Issue the following command to download the system-config-lvm rpm:
    wget https://kojipkgs.fedoraproject.org//packages/system-config-lvm/1.1.18/1.fc19/noarch/system-config-lvm-1.1.18-1.fc19.noarch.rpm
    (Although it is an rpm file for the Fedora Distribution, it will also work for Centos7).

  5. Make certain that you are logged in as root.
  6. Try issuing the command: rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm
    You should notice an error.
  7. Use the yum install command to resolve the following dependencies:
    • yum install gnome-python2-bonobo
    • yum install gnome-python2-gnome
    • yum install usermode-gtk

  1. Re-issue the command: rpm -i ./system-config-lvm-1.1.18-1.fc19.noarch.rpm
    It should work this time.
  2. Run the command: system-config-lvm
  3. On the left-hand side, you can click on the Volume Group, Physical Volume and Logical Volumes and view their properties on the on the right-hand side.
  4. Determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers:
    1. What are the names and sizes of the Volume Group?
    2. What is the name and size of the Physical Volumes?
    3. What are the names and sizes of the Logical Volumes?
    4. Is there any space in the VG which is not allocated to a LV?

  5. Click on Logical View (i.e. above home, root, swap) and then click Create New Logical Volume button. What happens? Why do you think this happened?

    We will now create another partition (/dev/vda3) using the fdisk utility, format this newly-created partition so we can create another logical volume called archive.

  6. Quit the system-config-lvm application.
  7. In the terminal as root, issue the command: ls /dev/vd*

    NOTE: If nothing displays, issue the command: ls /dev/sd* and use that device pathname /dev/sda instead.

  8. Issue the command: fdisk /dev/vda
    (or fdisk /dev/sda if the previous command didn't work)
  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: +2G at ending block (create 2GB for partition) and press enter.
  11. At the fdisk prompt, issue the command p to review the partition information, then type w to save partition table and exit (ignore WARNING message).
  12. You must restart your centos2 VM to allow changes to take effect
  13. Open a terminal as root, and format your newly-created partition by issuing the command: mkfs -t ext4 /dev/vda3
    (or mkfs -t ext4 /dev/sda3)
What system-config-lvm display should appear after using fdisk to create new partition (/dev/sda3), formatting partition, adding partition to LVM, and allocating partition to our default volume group.
Creating a new logical volume from our newly created physical volume
  1. Restart the system-config-lvm utility. Do you see a new /dev/sda3 partition under Physical Volumes?
  2. To add the newly created partition, you need to add it into LVM to be used. Exit the system-config-lvm utility and issue the following command to add the partition into LVM:
    pvcreate /dev/vda3 (or pvcreate /dev/sda3 ) (enter y to proceed - ignore warning)
  3. Restart system-config-lvm. You should notice a section at the bottom indicating Unallocated Physical Volumes. Click on the unallocated partition /dev/vda3 and then click Add to Existing Volume Group (click on other button in next screen to continue). The screen should now show the partition /dev/vda3 contained in our default Volume Group (refer to diagram on right-side for reference).
  4. Click on Logical View (i.e. above home, root, swap), and then click on Create New Logical Volume button and fill out the details for this new logical volume including: size: 1GB, LV Properties: linear, file system: ext4 file system name: archive, select mount and mount it at: /archive. Have the system create the directory /archive if it does not exist (Refer to diagram on right for reference).
  5. Confirm that this new Logical Volume now exists by changing to the directory /archive. Did you need to restart your centos2 VM to confirm that the /archive directory is now on your centos2 VM?
  6. Use system-config-lvm to resize (i.e. edit) the archive logical volume to 1.5 GB and verify your results.


Answer Part 1 observations / questions in your lab log book.

Part 2: Managing LVM Via Command Line & Adding Virtual Hard Drives

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.

Let's say that you have run out of disk space on your centos3 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.

Because we're using LVM we can avoid this problem. We can add a new virtual 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 operations to increase the size of lv_root in centos3:


  1. Perform this Part in your centos3 VM.
  2. Run the following commands and make note of the output:
ls /dev/vd*
pvs
vgs
lvs
df -h
  1. Record the size of the volume group and the amount of free space
  2. At the top of your VM window (make certain not full screen mode) 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 as: VirtIO disk.

    NOTE: Make certain to click "select managed or other existing storage", and fill in the pathname circled in red as shown in the diagram on the right-hand-side.

  4. Go back to the console view
  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 defaults prompts would work), restart your centos3 VM and then format that partition for file type: ext4.
  7. Now we'll make the new device a physical volume, add it to the volume group, and extend lv_root:
pvcreate /dev/vdb1 (enter y to proceed - ignore warning)
vgs (to determine: name_of_volumegroup)
vgextend name_of_volumegroup /dev/vdb1
lvextend -L +2G -r name_of_volumegroup/lv_root
Note.png
LVM Safeguards When Reducing File System Size
In older versions of LVM, the system administrator had to be careful when reducing the size of Linux file-systems using LVM. This usually required to separate operations:
  • Reducing the file-system to make room (resize2fs)
  • Reduce the logical volume by using the lvreduce command
This used to be performed to prevent a catastrophic loss of data if the logical volume was resize smaller than the available space on the file-system.
The newer LVM commands have "safeguards" (eg. option -r to prevent destroying the file-system (thus making the resize2fs command obsolete).
  1. Now rerun the ls /dev/vd* , pvs , vgs , lvs and df -h commands.
  2. Record the size of the volume group and the amount of free space. What has changed and what caused those changes?
  3. 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!
  4. Reduce the size of the lv_root partition by 1GB by issuing the following command:
    lvreduce name_of_volumegroup/lv_root --size -1G
  5. Confirm that the file system has been reduced.
  6. Increase the size of the lv_root partition by another 1GB by issuing the command:
    lvextend name_of_volumegroup/lv_root --size +1G
  7. Confirm that the file system can been increased.
  8. Record the LVM Management commands in your lab log-book.


Answer Part 2 observations / questions in your lab log book.


INVESTIGATION 2: ADDITIONAL FILE-SYSTEM OPERATIONS

We take for granted that a file-system must be mounted (for example the root partition) in order for a Linux system to be usable upon system start-up. This was create automatically for the /archive mount point automatically when we used the system-config-lvm utility in the previous investigation. We need to learn now to do this manually by editing or adding an entry in the /etc/fstab file. The /etc/fstab (file system table) 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).


Part 1: Mounting and Un-mounting Partitions

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.
  1. Perform this part in your centos2 VM.
  2. As you may recall in Part 1 of Investigation 1, we created another logical volume called archive using the graphical application system-config-lvm. This logical volume should be mounted (connected) to our existing file-system (2GB).
  3. Issue the following command: mount
  4. What is the purposed of issuing this command without arguments? Can you see the /archive mount point?
  5. Confirm that the directory named /archive is mounted.
  6. You can use the umount command to unmount a file-system (eg. for maintenance purposes). Note the the name of the command is umount, not unmount.
  7. Make certain you are not located in the /archive directory, and issue the following command to unmount the /archive directory:
    umount /archive
  8. Issue the mount command (without arguments) to confirm it has been unmounted.
  9. View the contents of the file-system table /etc/fstab by issuing the following command:
    cat /etc/fstab
  10. 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.
  11. View and record the fields for the /archive mount in your lab logbook, and then issue the following command to reconnect or mount /archive:
    mount   -t ext4   /dev/centos_centos2/archive   /archive

    NOTE: If you are having problem mounting, use the /dev pathname listing in your /etc/fstab file.

  12. Confirm that this file-system has been properly mounted. Note: You could have also issued the command: mount -a to automatically mount the file-systems contained in the /etc/fstab file.

    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):
    echo "/dev/centos_centos2/archive /archive ext4 defaults 1 2" >> /etc/fstab
    (although we don't need to do this, since it was already done).

  13. A curious 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 Part 1 observations / questions in your lab log book.


Part 2: Monitoring Disk Space

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

Another essential duty of a Linux system administrator is to anticipate problems and take preventative measures to avoid computer system problems before that occur.

Monitoring disk space activity helps provide Linux system administrators information to help take corrective action before problems can occur.

  1. Remain in your centos2 VM for this section.
  2. Issue the command: df -h
  3. Note the disk space usage for /, /home, and /archive partitions.
  4. If a partition is running out of available space, the Linux System Administrator can reallocate space among partitions or add another disk and grow the file system (like you did in a previous investigation). The administrator also can investigate the cause of low disk space. Two examples immediately come to mind: excessive use of space from users, and potential penetration from hackers.
  5. To investigate excessive disk usage by regular users, you can obtain a total amount of disk usage for that user by issuing the command:
    du -hs /home/regularuserid
  6. To provided a more detailed list of usage (file-by-file), issue the command:
    du -h /home/regularuserid | more
  7. 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.
  8. The methods to monitor potential penetration to a Linux system are too numerous, and are taught in other coures (for example: SEC520). On method of monitoring potential penetration is to look for abnormally large files. Issue the command:
    find -P / -size +100000k
  9. Did you see any files that are that size?
  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).


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


INVESTIGATION 3: LOOKING AHEAD

Automating Routine Tasks (Scheduling The Running of Shell Script Via Crontab)

Idea.png
Bash Shell Scripting Tips:

  • Using awk to Manipulate Text:

    Legend has it that the awk command was invented by three C programmers that wanted to create a utility in Unix that had programming syntax more like C programming. People started to use the command and found it very useful for report generation and file repair or 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

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. 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.


  1. Perform this section in your c7host machine
  2. Make certain you are logged in as root.
  3. Download, study, and run the following shell script. Issue the command:
    wget https://scs.senecac.on.ca/~murray.saul/monitor-disk-space.bash
  4. Try to understand what this Bash Shell script does (refer to man pages for the awk command), and then run the script as root.


In order to automatically run the above-mentioned script periodically, you use the scheduler in Linux called crontab. The term crontab stands for Chronograph Tables, where a chronograph is the old term for a timepiece (the forerunner of the modern watch). You can run the crontab command to schedule commands and shell script to be run in a number of different ways.

  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/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 3 observations / questions in your lab log book.

LAB 5 SIGN-OFF (SHOW INSTRUCTOR)

Important.png
Time for a new backup, INCLUDING YOUR VIRTUAL HARD DRIVE!
If you have successfully completed this lab, make a new backup of your virtual machines.

Virtual hard-drives created in this lab are image files, and now need to be backed up as well!.

Arrange proof of the following on the screen:

  1. centos2 VM:
    • Logical Volume, Physical Volume, and Volume Group information
    • Contents of /etc/fstab file
    • Proof that /archive has been mounted
  2. centos3 VM:
    • Proof of virtual hard disk creation
    • Logical Volume, Physical Volume, and Volume Group information
  3. c7host Machine:
    • Proof of creation of the shell script: monitor-disk-space.bash
    • Crontab entry for root account
  4. 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.