OPS235 Lab 5

From CDOT Wiki
Revision as of 07:57, 21 June 2016 by Msaul (talk | contribs) (Created page with 'Category:OPS235 =LAB PREPARATION= {| width="40%" align="right" cellpadding="10" |- valign="top" | [[Image:software.png|thumb|right|200px|Monitory Disk Space can fix problems…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


LAB PREPARATION

Monitory Disk Space can fix problems before they become a crisis. 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 discuss how a Linux system administrator can manage partitions including adjusting the size of their Linux systems if space is required.


Main Objectives

  • Monitoring Disk Space (df -h).
  • Use crontab to automatically schedule running of shell script to "flag" low disk space.
  • Using LVM to resize partitions graphically and via command-line.
  • 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).


Minimum Required Materials
Linux Command Reference
Solid State Drive
USB key
(for backups)
Lab5 Log Book
LVM Information

vgs
pvs
lvs
vgdisplay
pvdisplay
lvdisplay

LVM Management

system-config-lvm
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 that occur.

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

Perform the following steps:

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


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

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.

Perform the following steps:

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


INVESTIGATION 2: MANAGING HARD DISK SPACE USING LVM

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,


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. Launch your centos2 VM and open a shell terminal.
  2. Open a shell terminal, and login 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.

  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: +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 issue the command 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)
  14. Re-issue the ssm command. Do you see a new /dev/sda3 partition under Physical Volumes?
  15. 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)
  16. Issue the following command to add your new-created and formatted partition called /dev/vda3 to your volume group:
    vgextend centos_centos2 /dev/vda3
  17. Create a new logical volume by issuing the following command:
    lvcreate -L 2G archive centos_centos2
  18. Issue the ssm list' command to confirm that the logical volume has been created.'


File:Ssm2.png
Issue the ssm list command to view available hard disk space available for the centos2 VM.
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

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 steps:

  1. Perform this Part in your centos3 VM.
  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 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
File:Ssm3.png
Issue the ssm list command to confirm the file system increase for your centos2 VM.
  1. Now rerun the ls /dev/vd* , ssm list 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.


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

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/vda3 /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 /arhive 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. 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.
  3. 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.

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

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.


LAB 5 SIGN-OFF (SHOW INSTRUCTOR)

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, centos2, and centos3 VMs are running.
  2. Switch to your centos2 VM, open a shell terminal, and issue the Linux command: wget http://matrix.senecac.on.ca/~murray.saul/ops235/lab5-check.bash
  3. Give the lab5-check.bash file execute permissions (for the file owner).
  4. Run the shell script and if any warnings, make fixes and re-run shell script until you receive "congratulations" message.
  5. Arrange proof of the following on the screen:
    centos2 VM:
    • Output from ssm list command.
    • Proof that /archive has been mounted
    • Output from running the lab5-check.bash script with all OK messages
    centos3 VM:
    • Output from ssm list command.
    c7host Machine:
    • Proof of creation of the shell script: monitor-disk-space.bash
    • Crontab entry for root account
    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.