Changes

Jump to: navigation, search

OPS245 Lab 2

2,131 bytes added, 23:11, 9 January 2021
Finishing python investigation
# Use elevated privileges ist the size and names of files in<b><code><span style="color:#3366CC;font-size:1.2em;">/var/lib/libvirt/images/</span></code></b><ul><li>What do these files contain?</li></ul>
# Use the command <b><code><span style="color:#3366CC;font-size:1.2em;">sudo -i</span></code></b> and enter your password if prompted. You are now root until you use the command <b><code><span style="color:#3366CC;font-size:1.2em;">exit</span></code></b> to return to your normal user account.
# Change to the images directory by issuing the following command: <b><code><span style="color:#3366CC;font-size:1.2em;"> cd/var/lib/libvirt/images/</span></code></b>. Note that you did not need to use sudo, as you are already using elevated permissions.# Make a compressed backup of your '''centos1.qcow2''', '''centos2.qcow2''', and '''centos3.qcow2''' files to your regular user's home directory by issuing each command (one at a time):<br><b><code><span style="color:#3366CC;font-size:1.2em;">gzip < centos1.qcow2 > ~YourRegularUsername/backups/centos1.qcow2.backup.gz</span></code></b><br><b><code><span style="color:#3366CC;font-size:1.2em;">gzip < centos2.qcow2 > ~YourRegularUsername/backups/centos2.qcow2.backup.gz</span></code></b><br><b><code><span style="color:#3366CC;font-size:1.2em;">gzip < centos3.qcow2 > ~YourRegularUsername/backups/centos3.qcow2.backup.gz</span></code></b><ul>'''NOTE:''' Make certain to use the redirection signs "<" and ">" properly in the command!</ul>
{{Admon/important |Please be patient|It may look like the command prompt is stuck but it could take a while for gzip to compress an entire operating system. '''NOTE:''' Do NOT press '''&lt;ctrl&gt;c''' to cancel this process. If you do, your archive will become incomplete and your recovery will be corrupt.}}
<ol><li value="5"> Compare the size of the compressed and original files (hint: use '''ls -lh'''). If file is very large (like 15GB), you didn't compress it and you need to remove that file and perform the previous step until you get it right!</li>
<li> Wreck <u>only</u> your centos3 system! Try this command inside the centos3 virtual machine:<b><code><span style="color:#3366CC;font-size:1.2em;">sudo rm -rf /*</span></code></b> (ignore error messages).</li>
<li> Shut down and restart the centos3 VM. It will not boot since all system files have been removed!</li>
<li> Restore the original image from the backup from your home directory to your '''images''' directory by typing this command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo gunzip < ~YourUserId/backups/centos3.qcow2.backup.gz > /var/lib/libvirt/images/centos3.qcow2'''</span></code></b></li>
<li> Restart the VM. Is it working normally?</li>
<li> You should also make a copy of the XML configuration file for each VM in case you "wipe" and re-install the host machine, and want to add a restored VM backups to the virtual machine manager list. We will demonstrate using the centos3 XML configuration file, and prove that a "clone" can be added to your list.Please perform the following step:</li>
&nbsp;do<br>
&nbsp;&nbsp;echo "Backing up VM #$num"<br>
&nbsp;&nbsp;gzip < /var/lib/libvirt/images/centos$num.qcow2 > ~yourusername/rootbackups/centos$num.qcow2.backup.gz<br>
<br>&nbsp;&nbsp;echo "VM #$num BACKUP DONE"<br>
&nbsp;done<br><br>
&nbsp;done<br>
&nbsp;echo "Backing up VM #$numanswer"<br>
&nbsp;gzip < /var/lib/libvirt/images/centos$numanswer.qcow2 > /root~yourusername/centos$numanswer.qcow2.backup.gz<br><br>
&nbsp;echo "VM #$numanswer BACKUP DONE"<br>
else<br>
=INVESTIGATION 4: USING PYTHON TO AUTOMATE MANAGING VIRTUAL MACHINES=
===UNDER CONSTRUCTION===
This week you have added some significant capabilities to your python scripting. The ability to run loops and make decisions makes your scripts much more powerful. In this investigation you will write a python script that backs up every VM you have, regardless of its name. It will also allow the user to specify which VMs they want backed up, just in case they only want one or two.
 
 
{|width="40%" align="right" cellpadding="10"
|- valign="top"
|
{{Admon/tip|Python Scripting Tips:|<br>'''<u>String Methods</u>'''<br><ul><li>UNDER CONSTUCTIONThe string data type has a number of methods that we can use on it to get a modified version of the data a string variable holds.</li><li>Note that these don't change the existing string, just return a copy of it with some change. You can store this updated copy in a variable, or use it in a command.</li><li>Some examples include:<ul><li>find() - locates a value (e.g. another string) if it is in this string.</li><li>lower() - convert the entire string to lower-case</li><li>replace() - finds a value in the string, and replaces it with a different value.</li><li>split() - breaks the string up on a value, and returns a list.</li><li>strip() - removes leading and trailing whitespace</li><li>upper() - convert the entire string to upper-case</li></ul></li><li>There are [https://www.w3schools.com/python/python_ref_string.asp many more options] than this, but you will get a lot of utility out of these.</li></ul><br />'''<u>Methods in General</u>'''<br><ul><li>Python is an object oriented programming language. In this course, that won't actually affect us much, but it does change how you run some commands.</li><li>In bash, every command ran on its own (while you might provide the command some arguments, or pipe different data into it). In python, the modules we import, and the types of variables we use can (will) have commands built into them. For example: converting all letters in a string to UPPER CASE).</li><li>While you could write a command in bash that would do that and pass it a variable holding a series of letters as an argument, in python that behaviour (and more) is built into the string type of variable.</li><li>The actions that are built into types are called '''methods'''. To use a method on a variable just add .methodname() to the end of the variable. Instead of getting back the value, you'll get something else (based on what that method does). Suppose we had a variable called course that currently had the value 'ops245'. Course codes are usually written in ALL-CAPS, so instead of just saying print(course), we could say print(course.upper())</li></ul>}}
|}
 
This week you have added some significant capabilities to your python scripting. The ability to run loops and make decisions makes your scripts much more powerful. In this investigation you will write a python script that backs up the centos1, centos2, and centos3 VMs, or lets the user to specify which VMs they want backed up.
<ol>
<li>Try to run that script. You'll notice it does not work. No matter what you do, it always says you are not root.</li>
<li>Modify the print statement that tells the user they must be root to also include the current username, then run the program again.</li>
<li>It should print out root, but with an axtra extra new-line. You may have noticed this in your other python scripts so far: the data we get from os.popen() has an extra new-line on the end. We will need to modify the string (s) it gives us a bit. See the side-bar for hints on how to do so.</li>
<li>Modify the if statement so it is just getting the current username, not the username and a newline. You can do this using several steps and several variables, but it can also be done in a single line.</li>
<li>Now that the script recognizes you as being root (or at least running the script with root permissions), it should work. Notice how we've used the + to combine several strings together to pass to the os.system command. We did this because this script needs the python variable to be evaluated before the whole line gets handed over to os.system. If you left the variable names inside the quotes, python will ignore them as just being part of a string. By putting them outside of a string, and concatenating their value to that string, we can evaluate them and feed them into that command.</li>
<li>Convert the rest of the bash script above into python, then test your python version to make sure it works.</li>
</ol>
932
edits

Navigation menu