----------------------- Lab #07 for NET2003 due March 12, 2007 (*NEW DATE*) ----------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Global weight: 3% of your total mark this term. Due date: before 10h00 Monday March 12, 2007 (*NEW DATE*) The deliverables for this lab exercise are to be submitted online on the Course Linux Server using the "netsubmit" method described in the lab exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty lab exercises that are submitted late but before 12h00 (noon) on Wednesday, March 14. (*NEW*) After that late-submission date, the lab exercise is worth zero marks. Lab exercises submitted by the *due date* will be marked online and your marks will be sent to you by email after the late-submission date. Lab Synopsis ------------ (The initials "ALN" refer to your required course textbook - Advanced Linux Networking.) Create a set-up script for your VMware Virtual Network Sandbox environment. The script will do these things (see below for details): START PDL for setup.sh S1) set up shell environment (#!, PATH, umask, etc.) S2) IF ntpd is NOT running update Debian packages fetch and install the NTP Internet Time daemon packages run the start-up script for the ntpdate package run the start-up script for the NTP package S2b) ELSE (this part is useful but optional) stop the NTP daemon (using its start-up script) run the start-up script for the ntpdate package run the start-up script for the NTP package ENDIF S3) PRINT the current date S4) SET shell variable for use in rest of script S5) IF disk is NOT mounted PRINT warning on stderr create mount point IF mounting the disk FAILS PRINT an error message on stderr EXIT the script with non-zero status ENDIF PRINT message saying disk was mounted ENDIF S6) IF disk/virtuals is NOT a directory PRINT message about setting up disk MOVE home/virtuals to disk/virtuals SYMLINK home/virtuals to disk/virtuals MOVE home/.uml to disk/.uml SYMLINK home/.uml to disk/.uml PRINT long listing of home/.uml and home/virtuals EXIT the script with zero status ENDIF S7) IF home/virtuals is already a symlink PRINT message that it already exists EXIT the script with zero status ENDIF S8) IF remove directory home/virtuals FAILS PRINT an error message on stderr EXIT the script with non-zero status ENDIF S9) IF recursively remove directory home/.uml FAILS PRINT an error message on stderr EXIT the script with non-zero status ENDIF S10) SYMLINK home/virtuals to disk/virtuals S11) SYMLINK home/.uml to disk/.uml S12) PRINT long listing of home/.uml and home/virtuals S13) EXIT the script with zero status END PDL for setup.sh Readings -------- Week #7 class notes and web Notes file exit_status.txt introduce how to run commands and test their exit statuses in shell scripts. Part I - Create a VNS set-up script ----------------------------------- There are things that we need to do after we reboot the VNS. Rather than have to type all those things in manually, we will create a script on the virtual disk that will do them for us. After a reboot, we need only execute the script to set up VNS for us. 1) Shut down your VNS and reboot it from scratch. First, shut down all your UML machines: # umlHalt Then shut down and reboot the main machine: # shutdown -r now Remember that only files saved on the virtual disk /mnt/hdb1/ will survive this reboot. Anything in $HOME will be lost. 2) After reboot, mount your virtual disk on /mnt/hdb1/. (See Lab #6 Steps 16-18 and/or Notes file virtual_network_sandbox.txt.) 3) Make sure your Internet is working (Lab #6 Step 10). See Notes file virtual_network_sandbox.txt if you need to restart DHCP to get a network address. /* START SCRIPT SECTION - PUT THESE COMMANDS IN /mnt/hdb1/setup.sh */ S1) Create a shell script in your VNS named /mnt/hdb1/setup.sh using the usual header lines, PATH, LC_COLLATE, LANG, umask, etc. (Of course you can copy an existing script template to do this; don't waste time typing it all in again.) S1b) Add the directory /usr/sbin to your PATH variable in your script. Your PATH variable should have four directories in it now. Have the script setup.sh implement the following actions prefixed by "S": Part II - Update the VNS time ----------------------------- On reboot, the VNS isn't running any time synchronization software. We will create script commands to fetch, install, and run the software if it isn't already running. To do this, we need to check to see if a process is running. The return status of "pgrep -x foo" will tell you if the "foo" process is executing, e.g. # if pgrep -x xload ; then echo "XLOAD is running" ; fi In a shell script, write the same thing; but, put multiple commands on separate lines: if pgrep -x xload ; then echo "$0: XLOAD is running" fi S2) If the script sees the "ntpd" program is *NOT* running, do these steps: a) update the Debian package list b) fetch and install the NTP Internet Time daemon packages c) run the start-up script for the ntpdate package d) run the start-up script for the NTP package Hints: You already did many of these commands; see Lab #6 Steps 19 and 28. The return status of "pgrep -x ntpd" will tell you if the "ntpd" process is running. To check if it is *not* running, prefix the command name with an exclamation mark in a shell IF statement. Throw away any output of pgrep; don't print it to the screen. Reference Notes file for shell IF statements: exit_status.txt S2b) Optional: If the "ntpd" program *IS* running, do these steps: a) stop the NTP daemon (using its start-up script) b) run the start-up script for the ntpdate package c) run the start-up script for the NTP package S3) Display the date. /* END SCRIPT SECTION */ Test that the script works: 4) Run your setup.sh script and ensure that the packages are downloaded and the time is set correctly. 5) Run your script a second time and make sure it only displays the date. Since the "ntpd" process is already started, your script must *NOT* download any packages or do any restarts the second time. 6) Stop the "ntpd" process using the "stop" argument to the NTP start-up script and re-run the startup.sh script. Make sure all the package update commands are run and the NTP server is started again. Part III - Move the UML virtual images to the virtual disk ---------------------------------------------------------- The disk images of UML machines started inside your VNS are saved in $HOME/virtuals, which is part of the Knoppix in-memory file system that is lost on reboot. You can back up these images using the umlBackup command (or equivalent Menu option), and save them onto your virtual disk mounted on /mnt/hdb1/ so that they persist over a reboot. This isn't very convenient. Instad of having to remember to back up and restore these UML images, we will add commands to our set-up script to move the directory that holds the UML images from $HOME/virtuals/ to /mnt/hdb1/virtuals/, so that the images are always saved on our virtual disk and not in Knoppix memory. The set-up script has to be smart enough not to try to move the directories if they are already moved; so, we can run the script twice safely. Remember that you may write the shell IF statement: if test something ; then using the syntactic sugar with square brackets: if [ something ]; then but you must remember to leave blanks around the brackets. See Notes file exit_status.txt /* START SCRIPT SECTION - PUT THESE COMMANDS IN /mnt/hdb1/setup.sh */ S4) Ensure that the name "/mnt/hdb1" is set as a shell variable near the start of the script so that it can be changed in one place if the location of the virtual disk changes. Do not repeat "/mnt/hdb1" all through your script. Use a variable to hold "/mnt/hdb1" and use the variable in the rest of the script. We should make sure that the virtual disk is really mounted on /mnt/hdb1 before the script continues. This sample pipeline below (not for inclusion in your script) will return a good exit status (from grep) and print MOUNTED only if /mnt/hdb1 is a mounted disk: # if df /mnt/hdb1 | grep /mnt/hdb1 ; then echo "MOUNTED" ; fi S5) Put a command in your startup.sh script that will check if the /mnt/hdb1 disk is NOT mounted and do these commands: a) issue a warning on standard error that /mnt/hdb1 is not mounted b) create the /mnt/hdb1 mount point (Lab #6 Step 16) c) mount /dev/hdb1 on /mnt/hdb1 (Lab #6 Step 17) and if this mount command returns a bad status: c1) print a good error message (four qualities from script_style.txt) c2) exit the script with a non-zero status d) print a message saying the disk was mounted Hints: To execute the body of an IF statement if a command *fails*, use the same trick as you used earlier to invert the return status of a command in a shell IF statement. Throw away the output of grep. The script should continue past this point only if /mnt/hdb1 is a mounted virtual disk. S6) If path /mnt/hdb1/virtuals is not yet a directory, do these things: a) display on the screen "XXX: Setting up /mnt/hdb1" where XXX is the name of the script being run b) move path $HOME/virtuals to be path /mnt/hdb1/virtuals c) create a symbolic link from $HOME/virtuals to /mnt/hdb1/virtuals d) move path $HOME/.uml to be path /mnt/hdb1/.uml e) create a symbolic link from $HOME/.uml to /mnt/hdb1/.uml f) show a long listing of $HOME/virtuals and $HOME/.uml (they should be two symbolic links pointing to directories under /mnt/hdb1/) g) exit the script with a zero status Hints: You can test for the existence of a directory foo using "test -d foo". You can test for the non-existence of a directory foo using "test ! -d foo". You can create a symlink foo containing bar using "ln -s bar foo" so that accessing "foo" leads to accessing "bar" instead. The variable containing the running script name is documented in Notes file shell_variables.txt S7) If the pathname $HOME/virtuals is already a symlink, the script should display "XXX: $HOME/virtuals is already a symlink" and exit the script with zero status. Replace XXX with the name of the running script. Do not output the letters XXX; output the name of the script. You can test for the existence of a symlink foo using an option to the "test" command. RTFM S8) Remove the empty directory "$HOME/virtuals". If the remove fails for any reason (perhaps because the directory is not empty), print an error message and exit the script with a non-zero exit status. Do not use a recursive removal. Shell script error messages must follow the four-part format documented in Notes file script_style.txt S9) Recursively remove "$HOME/.uml". If the remove fails, print an error message and exit the script with a non-zero status. Error messages must follow the four-part format documented in Notes file script_style.txt S10) Create a symbolic link from $HOME/virtuals to /mnt/hdb1/virtuals S11) Create a symbolic link from $HOME/.uml to /mnt/hdb1/.uml S12) Show a long listing of $HOME/virtuals and $HOME/.uml (they should be two symbolic links pointing to directories under /mnt/hdb1/) S13) Exit the script with a zero status. /* END SCRIPT SECTION */ Test that the script works: 7) Now run your script once. It should move the .uml/ and virtuals/ directories to /mnt/hdb1. 8) Run your script again. It should say that $HOME/virtuals is already a symlink and do nothing. 9) Remove the symlinks .uml and virtuals and run setup.sh again. You should see your error message and the script should exit with a non-zero exit status. 10) Create two empty directories named .uml and virtuals. Re-run your script. It should remove the empty directories and replace them with symbolic links. You will need to run this setup.sh script any time you reboot your VNS, if you want your UML images to be saved and restored on/from your virtual disk instead of in Knoppix memory (where they will be lost on reboot). Reboot your VNS and test your setup.sh script. 11) Add comments to your setup.sh script that explain what the purpose of the script is and what each section of the script does. See the comment style in script_style.txt for details on comment format. The comments tell why the script does things, not how. (You may add the Step numbers from this file to your script, as comments; but, they are not required.) 12) Put your assignment label in your setup.txt file and prepare it for submission. (Make sure the first line of any executable script always remains the path to the shell interpreter.) Part IV - Linux Boot Sequencing ------------------------------- Make sure your hdb1 virtual disk is mounted. Create a file using VI on the virtual disk named /mnt/hdb1/lab07answers.txt. Copy all the numbered questions labelled "ANSWER:" below into the lab07answers.txt file and, below each question, give your command line and answer to the question in the file. Warning: Do your work on your mounted hdb1 virtual disk; not in Knoppix memory! For full marks, whenever you are asked for the output of a command, include the command that generated the output in your answer as well. Always include the command lines along with the output of the commands. 13) Ensure that your UML images are being saved on your virtual disk by running your working setup.sh script. If you don't have a working setup.sh script, remember to use the Backup feature to save your UML images when you are done this lab. 14) Start up a fresh "White" UML machine and log in as root. (If you have already run a White machine, remove its virtual image from the virtuals/ directory before you start it up again. The command umlPurge will shut down and purge all UML machines.) 15) What file name contains the default run level for Linux? ANSWER: 16) What run level does this "White" UML machine default to at boot? Give a command line that selects and displays the line containing the default run level from the associated file, along with its output. ANSWER: 17) Note that the "White" UML machine delays for 10 seconds during the boot. Locate the associated start-up script symbolic link located in the start-up directory of the current Linux run level for the "White" UML machine. Show a long listing of the absolute path to the delay start-up script symlink (not to the script itself). (The symlink is the only entry in the whole run level directory; it is dated 2006-01-10 09:10) ANSWER: 18) Show a long listing of the absolute path to the delay start-up script itself (to the actual script, the target of the symlink, not to the symlink). (The real file is dated 2005-03-08 21:29.) ANSWER: 19) Remove the delay symlink from this run level of the "White" UML machine using an absolute pathname to the symlink. (Only remove the symlink; don't remove the actual script!) Give the command line, including the absolute pathname of the symbolic link you removed: ANSWER: 20) Reboot your "White" UML machine (not your host VNS!) using the console command "shutdown -r now". Make sure the "White" UML machine reboots without the 10 second delay. How long does a UML shutdown and reboot take? ANSWER: 21) VNS is Knoppix-based, which is a Debian-based Linux system. In which Debian directory does your text say we put "Local startup scripts" that will run at boot time? (ALN Table 4.1 p.81) ANSWER: 22) Which control script in /etc/init.d/ is responsible for running the executable scripts in the above directory? (Look for the above directory name in all the scripts. It appears on two lines in one script. The script name should match the name of the Debian "Startup control script" in ALN Table 4.1 p.81). In other words: In /etc/init.d/ there is one special script that is responsible for running all the boot-time start-up scripts and for running the "Local startup scripts". First, find the Debian name of the directory containing the "Local startup scripts". (You just did this in the previous question.) Look for that local startup script directory name in all the scripts under /etc/init.d/* in your White UML machine. You will find only one file that mentions this directory, on two comment lines. Give the command you used to find the name in all the init.d scripts, along with the two lines of output of the command: ANSWER: 23) You will note that the two lines of script comments that you find above in the White UML machine are commented out as being "for compatibility" - the UML machines do not use the old rc.boot method of running boot-time start-up scripts. The FOR loop above those two lines, in the same control script file, shows in which directory modern boot-time start-up scripts should be placed. (The comment at the top of the file also gives its name.) What is the name of the current boot-time start-up scripts directory, into which symbolic links are placed to each boot-time start-up script? Give a long listing of the contents of this directory (it contains a directory, a file, and three symlinks): ANSWER: 24) Put your assignment label on your lab07answers.txt file and prepare it for submission. Submission ---------- Submission Standards: See Lab #1 for details. A. Make sure all files contain an Exterior Assignment Submission label. For full marks, where possible, lines that you type must be shorter than 80 columns. The VIM editor will automatically wrap most lines as you type them if you enter "set wrapmargin=5" in your $HOME/.vimrc file. (Don't use autowrap for script files!) You only need to limit the length of the lines *you* type, not the length of the lines output by various commands. You don't have to shorten or alter command output. B. Submit your files for marking as Lab 07 using the following *single* netsubmit command line exactly as given here: $ netsubmit 07 lab07answers.txt setup.sh Always submit *all* files at the same time for every submission. Files submitted under the wrong names are worth zero marks. P.S. Did you spell all the assignment label fields and file names correctly? The VMplayer lets you try out various virtual machines, see: http://www.vmware.com/vmtn/appliances/index-r.html