OPS235 Lab 5 - CentOS7
Contents
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
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: Manage LVM Graphically
- Let's learn to administer (manage) our LVM graphically for our centos2 Virtual Machine.
- CentOS provides a tool called system-config-lvm to graphically administer LVM. Install the system-config-lvm application by issuing the command:
yum install system-config-lvm
- Open a shell as root and run the command:
system-config-lvm
- 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.
- Determine the current LVM configuration by clicking on the appropriate element and reading the properties in the right-hand panel -- write down the answers:
- What are the names and sizes of the Volume Group?
- What is the name and size of the Physical Volumes?s
- What are the names and sizes of the Logical Volumess?
- Is there any space in the VG which is not allocated to a LV?
- Increase the size of the home file-system to 4 GB:
- Click on the LV containing the home filesystem.
- Click on Edit Properties.
- Change the size to 4 GB and click Ok.
- Create a new 3G LV (LV Properties: linear) containing an ext4 filesystem named lv_archive and mount it at /archive
- Backup
/etc
into/archive
- Copy the files in /etc into the filesystem mounted at
/archive
(use the graphical tools or the command line. If using cp, remember to use the -R option).
- Copy the files in /etc into the filesystem mounted at
- Shrink the size of
lv_archive
to 1 GB. - Try shrinking the home file-system. What happens? Why?
Answer Part 1 observations / questions in your lab log book.
Part 2: Obtaining System Information with LVM Via Command Line
x
- x
Answer Part 2 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 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
- x
Answer the Part 1 observations / questions in your lab log book.
Part 2: Monitoring Disk Space
- x
Answer the Part 2 observations / questions in your lab log book.
INVESTIGATION 4: LOOKING AHEAD
Automating Routine Tasks (Shell Scripting and Using Crontab)
We will now use shell scripting to help automate the task for a Linux adminstrator to create regular user accounts.
- Download, study, and run the following shell script. Issue the command:
wget https://scs.senecac.on.ca/~murray.saul/user-create.bash
- 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.
- Open a Bash shell terminal and login as root.
- 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
- 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.
- Use a text editor (such as
vi
ornano
) to create a Bash Shell script called:createUsers.bash
in /root's home directory. - 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
- Save your editing session, but remain in the text editor.
- 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
- Save your editing session, but remain in the text editor.
- 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
- 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?
- 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)
Arrange proof of the following on the screen:
- ✓ x
- ✓ x
- ✓ x
- ✓ x
- ✓ x
Preparing for the Quizzes
- What is a VG? PV? LV?
- What is the total size of the "main" VG on your system?
- How do you create a LV?
- How do you delete 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?
- What is the purpose of /etc/fstab?