Changes

Jump to: navigation, search

OPS245 Lab 1

4,019 bytes removed, 16:55, 7 January 2023
LAB PREPARATION
| |[[Image:ssd.png|thumb|left|175px|<b>Solid State Drive</b><br>Minimum Size: 250GB]]
| [[Image:log-book.png|thumb|left|100px|<b>[[:Media:OPS235_logbook.pdf|Lab Logbook]]</b><br>[[:Media:OPS245-Logbook-Online.doc|(lab 1Click Here to Download)]]]]
| style="padding-left:15px;" |Package Management
*'''Name:''' c7host
*'''Boot media / Installation:''' CentOS 7 Full Install DVD (image file)
:::Use the links on the [https://mirror.senecacollege.ca/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso Download at Seneca Lab]:::[OPS245 | Course https://mirror.netflash.net/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso Download from Home Page]] to download the required ISO file.
*'''Disk space:''' 238GB
*'''CPUs:''' 1 CPU, 4 cores ('''Do not mix and match! Always use 1 CPU, and multiples of 2 for cores.''')
[[Image:Format_ExFAT.png|thumb|right|250px|'''Format drive window''' in order to format your new SSD as '''exFAT''' ('''Windows''')]]
{{Admon/caution|If you are using an external drive: FORMAT YOUR External SSD AS exFAT|'''You ''must'' format your external drive as exFAT.'''<br>By default, most external drives will be formatted for NTFS. <u>'''NTFS-formatted drives will cause issues in this course!'''</u> When you plug your drive in, open My Computer, right-click on the new drive, and select ''Format...''.<br />If you are storing the vmdk file on your machine's internal storage, this is not necessary.}}
{{Admon/important|Confirm External SSD Device is Recognized as a Drive in Windows Explorer|It is essential that your Windows machine recognizes your SSD device with a drive letter on your Windows machine. You may experience problems if you are using the Kingston SSD. If so, then run '''diskmgmt.msc''' in order to create a volume and format your disk1.}}
<li>You will return to your c7host VM. Click '''Power on this virtual machine''' icon near the top to boot your c7host VM to start your c7host VM, and proceed to Part 2.</li>
</ol>
 
===Part 2: Install CentOS on your Newly-Created VM ===
<li>In the '''Add a New Mount Point''', click the drop-down button and select '''/boot/efi''' for the mount point and type '''2954''' as the desired capacity, and then click '''Add mount point'''.</li>
<li>When you return to the main screen, make certain that this mount point is a '''Standard Partition''' and <u>not</u> ''LVM''.</li>
<li>Add the <b>/boot</b> mount point with a size of <b>500</b>. Leave the default settings and file system as '''xfs'''.</li>
<li>Add another mount point, but in the '''Add a New Mount Point''' screen, select '''/''' as the mount-point (either by typing or selecting from drop-down menu), and enter '''30720''' in for partition size and click '''Add Mount Point''' button.</li>
<li> You will return to the previous dialog box.<br><span style="background-color:yellow">For the '''/''' partition, change the file-system type from '''xfs''' to '''ext4''' and make certain that the Device Type is set to LVM</span>.</li>
<li>Repeat the same steps above for the '''/home''' partition (calculate the equivalent size for '''40GiB''' ('''ext4''' file-system type and LVM as device type).</li>
<li>Add a mount point '''/var/lib/libvirt/images''' (type yourself, check spelling!) for size '''100GiB''' (file-system type '''ext 4 ''' and device type '''LVM''').</li><li>Recheck each of the created partitions, and make certain that the file-system type is set to '''ext4''' and the Device Type should be '''LVM''', '''unless the instructions above tell you otherwise'''.</li>
<li>Finally, add a swap partition (Mount Point: swap) for '''16 GiB'''.</li>
<li>Check that your partition settings are correct (you can ask your instructor or lab monitor to confirm), and then click '''Done''' (possibly '''<u>twice</u>''') in order to proceed<br><br></li>
'''Answer Investigation 2 observations (all parts and questions) in your lab log book.'''
= INVESTIGATION 3: Using the Shell Scripting to Generate System Information Reports =
{|width="40%" align="right" cellpadding="10"
|- valign="top"
|
{{Admon/note|Bash Shell Scripting Reference Guide:|<br>'''<u>She-bang Line</u>'''<ul><li>Forces shell script to run in a specific Shell</li><li>Must be at beginning of first line (eg. '''#!/bin/bash''')<br><br></li></ul>'''<u>Variables</u>'''<blockquote>'''Environment'''<ul><li>System-wide or "global" variable</li><li>Usually appear in UPPERCASE letters</li><li>Can view with command: '''set &#124; more'''</li><li>'''$''' in front to expand variable to value<li>Examples: '''USER''', '''PATH''', '''HOME''', '''SHELL'''</li></ul></blockquote><blockquote>'''User-defined''' <ul><li>Variable created by user (command line, scripting)</li><li>Examples:<br>''myVar&#61;"my value"; readonly myVar; export myVar''<br>''read -p "enter value: " myVar''</li></ul></blockquote><blockquote>'''Positional parameters'''<ul><li>Assign values with set command or shell script arguments</li><li>These variables are numbered (eg. $1, $2 ... $10}</li><li>Special parameters: $*, $@, $#, $$, $?<br></li></ul></blockquote>'''<u>Command Substitution</u>'''<ul><li>Useful method to expand output from a command to be used as an argument for another command.</li><li>Examples:<br>''file $(ls)''<br>''set $(ls);echo $#;echo $*''<br>''echo "hostname: $(hostname)"''<br><br></li></ul>'''<u>if / elif / else statements</u>'''<ul><li>If a command runs (even pipeline command like to grep to match) will be true (0); otherwise, false (non-zero), thus can use with logic statements.</li>Example:<br>''if echo $myVar &#124; grep "match"''<br>''then''<br>''echo "Match"''<br>''fi''<br></li><li>The '''test''' command is used to test conditions. Square brackets '''[ ]''' is short-cut for test command (args contained inside with spaces). The '''exit''' command can be used to terminate the shell script with a false value.<br>Example:<br>''if [ $USER &#61; "root" ]''<br>''then''<br>&nbsp;''echo "You must be root" ''<br>&nbsp;''exit1''<br>''fi''<br></li><li>For numberic comparison, use the '''test options''': '''-gt''','''-ge''', '''-lt''', '''-le''', '''-eq''', '''-ne'''<br>Example:<br>''if [ $grade -gt 79 ]''<br>''then''<br>&nbsp;''echo "You get Good Mark"''<br>''elif [ $grade -gt 49 ]''<br>''then''<br>&nbsp;''echo "You pass"''<br>''else''<br>&nbsp;''echo "You fail"''<br>''fi''<br></li><li>For testing for file information, you can use '''-d''' to test if directory pathname exists, and '''-f''' if the file pathname exists. You can use '''!''' for negation.<br>Examples:<br>''if [ -d directory-pathname ]''<br>''then''<br> ''echo "directory exists"''<br>''fi''<br><br>''if [ ! - f file-pathname ]''<br>''then''<br> ''echo "File does not exist"''<br>''fi''</li></ul>}}
|}
:You may have learned about creating and running Bash Shell Scripts in your ULI101 course. Shell scripts help Linux users and system administrators to automate repetitive tasks to become more efficient and to help them save time. In order to avoid confusion with syntax, you will not be writing bash scripts in this course. You will be reviewing and building a basic Bash Shell script however still run bash commands to perform various tasks, e.g. to generate information reports for your newly-installed Linux host machine.
<ol><li value="4">Refer to the Bash Shell Scripting Reference Guide prior to proceeding with this section. As you continue, you are required to make Bash Shell scripting notes in your lab1 logbook.</li>
<li>Create a directory called bin in your home directory to store your shell scripts by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">mkdir ~/bin</span></code></b></li>
<li>Change to that newly-created '''bin''' directory</li>
</ol>
 
 
:'''NOTE:''' Although it is possible to copy and paste, is it highly recommended to manually enter the following Bash Shell scripting content to become familiar with writing Bash Shell scripting code. Remember: you will be required to create a Bash Shell script on your final exam, so you need the practice!
 
<ol>
<li value="7">Launch a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vim</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) Using output redirection, send the output from each of the following commands to create a Bash Shell script file called: <b><code><span style="color:#3366CC;font-size:1.2em;">myreport.bash</span></code></b> in your current directory.</li><li>Copy and paste the text below into your vi editing session for your file report.bash<br> (how do you copy and paste efficiently in Linux?)<br></li></ol> <code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;"><br>&#35;!/bin/bash<br><br>&#35; Author: *** INSERT YOUR NAME ***<br>&#35; Date: *** CURRENT DATE ***<br>&#35;<br>&#35; Purpose: Creates system info report<br>&#35;<br>&#35; USAGE: ./myreport.bash<br><br>if [ $(whoami) != "root" ] # if not logged in as root<br>then<br>&nbsp;echo "You must be logged in as root." >&2<br>&nbsp;exit 1<br>fi<br></code><br><ol><li value="9">Save your editing session, assign the '''myreportreport.bashtxt''' . Note that when you are done, you should have one file read and execute permissions (at least for the owner) and run by typing:<br><b><code><span style="color:#3366CC;font-size:1.2em;">./myreport.bash</span></code></b></li><li> Did it work?</li><li>Run it again with <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ./myreport.bash</span></code></b></li><li>Reopen your text-editing session for '''~/bin/myreport.bash''' and add the following lines that has output from all of code to the bottom of the shell script file:</ol><br><code style="color:#3366CC;font-family:courier;font-size:commands.9em;font-weight:bold;">&#35; Create report title<brul><br>echo "SYSTEM REPORT" li> /root/report.txt<br>echo "Date: $(date +'%A %B %d, %Y (%I:%M %p)')" >> /root/report.txt<br>echo >> /root/report.txt<br></code><brli><ol><li value="13">Save and run the bash shell script (remember to use sudo to run it as root). View the contents of the file called '''report.txt''' that was generated (I hope you are using the up arrow key to issue previously issued commands in order to save time!). Notice how the redirection symbol &gt; is used at the beginning of the report, and then the other redirection symbol &gt;&gt; is used to help "grow" the report with the other content.hostname</li><li>The only remaining content of the report would be the system information. We can use a shell scripting trick called "command substitution" $( .. ) in order place results from an command to be used by another command (like echo). Reuname -edit the shell script and add the following code at the bottom of the shell script file:rv</li></ol><brli>ps aux<code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">echo "Hostname: $(hostname)" >> /root/report.txt<brli>echo >> /root/report.txt<brli>echo "Kernel Version: $(uname -rv)" >> /root/report.txtip address show<br>echo >> /root/report.txt<brli></codeul><br/li><ol><li value="15">Save, run the script, and view View the ''report.txt'' contents (are you using tip that was given to save time?).</li><li>Edit the shell script and include output from the <b><code><span style="color:#3366CC;font-size:1.2em;">ps aux</span></code></b> and <b><code><span style="color:#3366CC;font-size:1.2em;">ip address show</span></code></b> commands (with appropriate titles). Remember You should be able to redirect understand them because you just put that output to add to the bottom of the file!</li><li>Savecontent there, run and confirm that the shell script is working correctly.</li><li>What but what would be the use of keeping this shell script as a Linux system administrator?</li></ol><ol><li value="19">Here are some more "complex" Bash Shell scripts, that perform the same task. Although look like if you are not required to understand some of these other tricks, it is recommended that you view look at the contents of the scripts and save them for future consideration or examples.</li><li>The <b><code>wget</code></b> command can be used to quickly download files file several months from the Internet. Issue the following command:<br><b><code><span now? style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">wget https://ict.senecacollege.ca/~peter.callaghan/ops245/labs/text-report.bash</span></code></b></li><li>Verify that the file '''text-report.bash''' was downloaded In order to your current directory.</li><li>Assign read and execute permissions for make this file by issuing more readable, use the command: <b><code><span style="color:#3366CC;font-size:1.2em;">chmod u+rx text-report.bash</span></code></b></li><li>Run this Bash Shell script by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">./text-report.bash</span></code></b></li><li>Check line to see if it created add a report in your current directory. What is blank line between the purpose of the report?</li><li>Use the <b>vi</b> text editor to view the contents of the file <b>text-report.bash</b>. Can you understand how this script works?<br><br></li><li>Use the <b><code>wget</code></b> output from each command to download, study, and run a header before each command briefly describing what the following shell scripts onoutput is (note that this will likely require re-line:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">https://ict.senecacollege.ca/~ops235/labs/report.bash<br>&nbsp;https://ict.senecacollege.ca/~ops235/labs/report3.bash</span></code></b><br><br></li><li>Try to understand what running all of these Bash Shell scripts docommands).</li></ol>
'''Answer Investigation 3 observations (all parts and questions) in your lab log book.'''
= INVESTIGATION 4: Using Python Scripting to Generate System Information Reports =
Before we can successfully automate parts of configuration using python, we need to learn how to use it. Initially we will duplicate perform simple tasks we have already learned how to do in bash. This way can learn how the language works as we go.
In this investigation you will write a python script that duplicates (as closely as possible with the parts of python we have covered so far) the bash script from file we created in the previous investigation.
:'''Perform the Following Steps:'''
<ol>
<li>Install Check if python3 is installed on your Centos Host machine. Open a terminal and type:</li><code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">which python3</code><li>The output should of the previous command should show python3 is already installed. If it is not, install python3 on your Centos Host machine.</li><code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">sudo yum install python3</code><li><ul><li>Create a new file in your '''~/bin ''' directory called '''myreport.py'''</li> <li>Since we haven't covered if statements in python yet, we won't be able to check if the user running the script is root. For now, we will have to trust ourselves to remember to use elevated permisisons permissions to run this script. We will correct this in lab 2.</li>
</ul></li>
<li>Populate the beginning of the file with sh-bang line and block comment equivlent to describing what was in '''myreport.bash'''<code style="colorthis script does:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
<br>
<pre style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;>&#35;!/usr/bin/env python3<br><br>&#35; Author: *** INSERT YOUR NAME ***<br>&#35; Date: *** CURRENT DATE ***<br>&#35;<br>&#35; Purpose: Creates system info report<br>&#35;<br>&#35; USAGE: ./myreport.py<br></codepre></li>
<li>Add a line that will print out the heading '''System Report'''<br />
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
print('System Report\n')
</code>
</li>
<li>Save your script and run it. Does it work?</li>
<li>You'll notice that the python script is currently sending its output to your terminal. Since we haven't covered how to write to a file yet, this is ok for now. Just use output redirection on the command line when you run the script to send the output to '''~/bin/pythonreport.txt'''.</li><li>Add Open your script in a text editor (like Vi) again, and add the following lines below the print statement:</li><pre style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;>&#35; Import the Operating System moduleimport os &#35; Print a heading for the date command outputprint('Current Date:') &#35; Call the date command using the os moduleos.system("date +'%A %B %d, %Y (%I:%M %p)'")</pre><li>Save your script and run it again. Observe the output. What do you think the os.system command did?</li><li>Based on the above example and output, add the extra commands for your python script to also output (with appropriate headings):
<ul>
<li>The current date.</li>
<li>The hostname of the machine.</li>
<li>The kernel version.</li>
</ul>
</li>
<li>Run your script to make sure it works. Note that the output does not need to match the bash script investigation 3 exactly, but it should be very close.</li>
</ol>
 
= LAB 1 SIGN-OFF (SHOW INSTRUCTOR) =
Follow the submission instructions for lab 1 on Blackboard.
# Make certain that your '''c7host''' VM is running, open the Bash Shell terminal.<br><br>
# Change to the '''~/bin''' directory.<br><br>
#Download the checking script by issuing the following Linux command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">wget https://ictraw.senecacollegegithubusercontent.cacom/~peter.callaghanOPS245/ops245labs/labsmain/lab1-check.bash</span></code></b><br><!--For Andrew's sections use this script instead:<br><b><code><span style="color:#3366CC;font-size:1.2em;">wget http://littlesvr.ca/ops245/lab1-check-andrew.bash</span></code></b>--><br>
# Give that downloaded shell script file execute permissions (for the file owner).<br><br>
# Run the shell script using '''sudo ''' and if there are any warnings, make fixes and re-run shell script until you receive a "congratulations" message.<br><br>
#Arrange evidence (command output) for each of these items on your screen:
::<span style="color:green;font-size:1.5em;">&#x2713;</span> Proof that c7host VM is set for '''virtualization''' (Refer to: '''INVESTIGATION 1''' - '''Part 1''' - '''Step #29''')
#: 7. Take a screenshot of the evidence and upload it, the file generated by the lab1-check.bash script, your logbook, and your myreport.py to blackboard.
= Practice For Quizzes, Tests, Midterm &amp; Final Exam =
# Define the term Virtual Machine.
# List the major screens (steps) in the installation of Centos7 CentOS 7 full install DVD.
# What key-combination is used to toggle the view of your running VM from "window-mode" to "full-screen-mode"?
# List the steps for setting SELinux to permissive mode.
# What command is used to get a list of running processes on your newly-installed system?
# Write the Linux command to download the on-line file: http://linux.server.org/package.tar.gz
# Write a Bash Python Shell Script to prompt the user for a directory, and then display the file types for all files in that specified directory (hint: use the '''read''' command and then use the '''file''' command and '''command substitution''' with the '''ls''' command). Test the Bash Shell script by adding execute permissions and run the Bash Shell Script.# Modify the previously created shell script to perform error checking after prompting for a directory to test if the specified directory does not exist. If it does NOT exist (i.e. true), display an error message indicating that the directory does NOT exist, and issue the command exit 1 to terminate the Bash Shell Script. Test the Bash Shell script by adding execute permissions and run the Bash Shell Script.
[[Category:peter.callaghan]]

Navigation menu