Difference between revisions of "OPS235 Lab 5 - CentOS7"

From CDOT Wiki
Jump to: navigation, search
Line 58: Line 58:
 
=INVESTIGATION 1: Adjusting File System Sizes with LVM=
 
=INVESTIGATION 1: Adjusting File System Sizes with LVM=
  
Monitoring and ensuring adequate space for a Linux file-system is considered to be an important task for a sys admin.
+
Monitoring and ensuring adequate space for a Linux file-system is considered to be an important task for a sys admin. An application called LVM is a very useful tool for Linux system adminstrators.
  
 +
'''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.
  
 +
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.
  
=== Part 1: Obtaining System Information with LVM ===
 
  
We have seen that maintaining unneeded '''packages can be a security risk''' due to the unnecessary increase in the complexity of your system. Similarly, it is also unnecessarily hazardous, and even more so, to leave unneeded services running. In this investigation, we will learn how to '''control services, and turn off those services that we think are not necessary to help reduce security risks'''.}}
+
=== Part 1: Obtaining System Information with LVM Graphically===
 +
 
  
 
# x
 
# x
Line 71: Line 73:
  
  
===Part 2: Adjusting Partition Sizes with LVM===
+
===Part 2: Obtaining System Information with LVM Via Command Line===
  
 
x
 
x
Line 81: Line 83:
 
'''Answer Part 2 observations / questions in your lab log book.'''
 
'''Answer Part 2 observations / questions in your lab log book.'''
  
===Part 3: Adding Virtual Hard Disks and Managing with LVM===
+
 
 +
===Part 3: Adjusting Partition Sizes with LVM Graphically===
  
 
x
 
x
Line 90: Line 93:
  
 
'''Answer Part 3 observations / questions in your lab log book.'''
 
'''Answer Part 3 observations / questions in your lab log book.'''
 +
 +
===Part 4: Adjusting Partition Sizes with LVM Via Command Line===
 +
 +
x
 +
 +
 +
# x
 +
 +
 +
'''Answer Part 4 observations / questions in your lab log book.'''
 +
 +
 +
 +
= INVESTIGATION 2: CREATING VIRTUAL HARD DRIVES =
 +
 +
===Part 1: Adding Virtual Hard Disks and Managing with LVM===
 +
 +
x
 +
 +
 +
# x
 +
 +
 +
'''Answer Part 1 observations / questions in your lab log book.'''
  
  
= INVESTIGATION 2: Mounting / Un-Mounting File-systems & Monitoring Disk Space  =
+
= INVESTIGATION 3: Mounting / Un-Mounting File-systems & Monitoring Disk Space  =
  
 
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. The '''/etc/fstab''' (file system table) 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. The '''/etc/fstab''' (file system table) contains entries to mount various file systems automatically upon start-up of the Linux system.
Line 113: Line 140:
  
  
= INVESTIGATION 3: LOOKING AHEAD =
+
= INVESTIGATION 4: LOOKING AHEAD =
  
 
==Automating Routine Tasks (Shell Scripting and Using Crontab)==
 
==Automating Routine Tasks (Shell Scripting and Using Crontab)==

Revision as of 06:25, 2 May 2015


LAB PREPARATION

Purpose / Objectives of Lab 5

The purpose of this lab is to discuss how a Linux sys admin can manage partitions including adjusting the size of their Linux systems if space is required. Various topics will be discussed including:


  • Connecting and Disconnecting Directories to existing partitions (mount, umount).
  • Monitoring Disk Space (df -h).
  • Using LVM to resize partitions graphically and via commands.
  • Create, partition and format virtual hard disks to increase the size of a file system.
  • 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: Adjusting File System Sizes with LVM

Monitoring and ensuring adequate space for a Linux file-system is considered to be an important task for a sys admin. An application called LVM is a very useful tool for Linux system adminstrators.

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.

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.


Part 1: Obtaining System Information with LVM Graphically

  1. x

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


Part 2: Obtaining System Information with LVM Via Command Line

x


  1. x


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


Part 3: Adjusting Partition Sizes with LVM Graphically

x


  1. x


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

Part 4: Adjusting Partition Sizes with LVM Via Command Line

x


  1. x


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


INVESTIGATION 2: CREATING VIRTUAL HARD DRIVES

Part 1: Adding Virtual Hard Disks and Managing with LVM

x


  1. x


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


INVESTIGATION 3: Mounting / Un-Mounting File-systems & Monitoring Disk Space

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. The /etc/fstab (file system table) contains entries to mount various file systems automatically upon start-up of the Linux system.

The Linux sys admin 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).

We will now learn how to perform these operations (including monitoring of disk space usage) in Part 1.

Part 1: Mounting and Un-mounting Partitions

  1. x

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

Part 2: Monitoring Disk Space

  1. x

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


INVESTIGATION 4: LOOKING AHEAD

Automating Routine Tasks (Shell Scripting and Using Crontab)

Idea.png
Bash Shell Scripting Tips:

  • The case statement:

    The case statement is a control-flow statement that works in a similar way as the if-elif-else statement (but is more concise). This statement presents scenerios or "cases" based on values or regular expressions (not ranges of values like if-elif-else statements). After action(s) are taken for a particular scenerio (or "case"), a break statement (;;) is used to "break-out" of the statement (and not perform other actions). A default case (*) is also used to catch exceptions.

    Examples (try in shell script):

    read -p "pick a door (1 or 2): " pick
    case $pick in
      1) echo "You win a car!" ;;
      2) echo "You win a bag of dirt!" ;;
      *) echo "Not a valid entry"
         exit 1 ;;
    esac


    read -p "enter a single digit: " digit
    case $digit in
      [0-9]) echo "Your single digit is: $digit" ;;
             *) echo "not a valid single digit"
                 exit 1 ;;
    esac


  • The getopts function:

The getopts function allows the shell scripter to create scripts that accept options (like options for Linux commands). This provides the Linux administrator with scripts that provide more flexibility and versatility. A built-in function called getopts (i.e. get command options) is used in conjunction with a while loop and a case statement to carry out actions based on if certain options are present when the shell script is run. The variable $OPTARG can be used if an option accepts text (denoted in the getopts function with an option letter followed by a colon. Case statement exceptions use the :) and \?) cases for error handling.

Example of getopts (try in script and run with options)

while getopts abc: name
do
  case $name in
    a) echo "Action for option \"a\"" ;;
    b) echo "Action for option \"b\"" ;;
    c) echo "Action for option \"c\""
        echo Value is: $OPTARG" ;;
    :) echo "Error: You need text after -c option"
        exit 1 ;;
    \?) echo "Error: Incorrect option"
        exit 1 ;;
esac


We will now use shell scripting to help automate the task for a Linux adminstrator to create regular user accounts.


  1. Download, study, and run the following shell script. Issue the command:
    wget https://scs.senecac.on.ca/~murray.saul/user-create.bash
  2. Try to understand what these Bash Shell scripts do, and then run the script as root. After running the shell script, view the contents of the /home directory to confirm.


Although the zenity command is a "user-friendly" way to run shell scripts, Linux administrators usually create shell scripts that resemble common Linux commands. In this lab, you will learn to create a shell script using the getopts function to make your shell script behave more like actual Linux commands (including the use of options). Refer to the notes section on the right-hand-side for reference about the case statement and the getopts function.


  1. Open a Bash shell terminal and login as root.
  2. Use the wget command to download the input file called user-data.txt by issuing the command:
    wget https://scs.senecac.on.ca/~murray.saul/user-data.txt
  3. View the contents on the user-data.txt file to confirm there are 3 fields (username, fullname, and e-mail address)which are separated by the colon (:) symbol.
  4. Use a text editor (such as vi or nano) to create a Bash Shell script called: createUsers.bash in /root's home directory.
  5. Enter the following text content into your text-editing session:


#!/bin/bash

# createUsers.bash
# Purpose: Generates a batch of user accounts (user data stored in a text file)
#
# USAGE:
#
# /root/createUsers.bash [-i {input-path}]
#
# Author: *** INSERT YOUR NAME ***
# Date: *** CURRENT DATE ***

if [ $PWD != "/root" ] # only runs if in root's home directory
then
 echo "You must be in root's home directory." >&2
 exit 1
fi
if [ "$#" -eq 0 ] # if no arguments after command
then
 echo "You must enter an argument" >&2
 echo "USAGE: $0 [-i {input-path}]" >&2
 exit 2
fi

  1. Save your editing session, but remain in the text editor.
  2. The code displayed below uses the getopt function set the input file pathname or check for invalid options or missing option text. Add the following code



outputFlag="n"
while getopts i: name
do
 case $name in
   i) inputFile=$OPTARG ;;
   :) echo "Error: You need text after options requiring text"
       exit 1 ;;
   \?) echo "Error: Incorrect option"
        exit 1 ;;
 esac
done

  1. Save your editing session, but remain in the text editor.
  2. The code displayed below uses logic to exit the script if the input file does not exist. Command substitution is used to store each line of the input file as a positional parameter. There is one subtle problem here: The full names of the users contain spaces which can create havoc when trying to set each line as a separate positional parameter. In this case the sed command is used to convert spaces to plus signs (+), which will be converted back later. Finally, a for loop is used to create each account (useradd) and mail the user their account information (mail). Add the following code:



if [ ! -f $inputFile ]
then
  echo "The file pathname \"$inputFile\" is empty or does not exist" >&2
  exit 2
fi

set $(sed 's/ /+/g' $inputFile) # temporarily convert spaces to + for storing lines as positional parameters

for x
do
    useradd -m -c "$(echo $x | cut -d":" -f2 | sed 's/+/ /g')" -p $(date | md5sum | cut -d" " -f1) $(echo $x | cut -d":" -f1)
    mail -s "Server Account Information" $(echo $x | cut -d":" -f3) <<+
    Here is your server account information:
    servername: myserver.senecac.on.ca
    username: $(echo $x | cut -d":" -f1)
    password: $(date | md5sum | cut -d" " -f1)
    Regards,
    IT Department
+
done

echo -e "\n\nAccounts have been created\n\n"
exit 0

  1. Save, set permissions, and then run that shell script for the input text file user-data.txt. Did it work? Try running the script without an argument - What did it do?
  2. You have completed lab4. Proceed to Completing The Lab, and follow the instructions for "lab sign-off".

Answer Investigation 3 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. Remember to also make a backup of the new second virtual disk drive on centos1 -- you now have two virtual disks on centos1, and therefore two image files, and therefore will need two backup files.

Arrange proof of the following on the screen:

x
x
x
x
x

Preparing for the Quizzes

  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 a LV?
  4. How do you delete 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. What is the purpose of /etc/fstab?