System control exercise

This activity exercises 3 mechanisms for controlling your system. You will pass boot parameters to the kernel that it adopts when it loads. You will control the behavior of various bootup scripts by supplying them values (sysconfig) that they are written to heed. And you will influence kernel behavior dynamically (while the kernel is running) by modifying its in-memory contol variable and flags.

Passing kernel boot parameters

Compare and contrast the states reached when booting the kernel with different sets of kernel parameters.

First, boot your system normally and log in as user root. Observe the environment variables by executing:

env

Now observe the approximate number of them:

env | wc -l

Remember the number.

Observe the running processes by executing:

ps -ef

Now observe the approximate number of them:

ps -ef | wc -l

Remember the number.

Exercise the virtual terminal feature, going to another vt and back by successive keypresses:

ctrl-alt-F2
ctrl-alt-F1

Exit from the current terminal:

exit

You are back at the login prompt. Now log back in again and shutdown the system:

shutdown -h now

Second, turn the machine back on. In order to supply parameters to the kernel, you need the bootloader's help (since he's the guy who makes the kernel execute, so is in position to give it parameters). GRUB is the bootloader used here. It probably shows it's menu, briefly, during bootup. If not, hold down the shift key during P.O.S.T (power on self test, the initial booting stage from ROM code on the motherboard) to bring GRUB's screen up. Then you will probably need to press the ESC key quickly to freeze the screen, before it times out and goes ahead with default booting (which you don't want). To supply custom parameters to the kernel, press the "e" (edit) key. At the end of the line starting with "linux" that appears in the resulting editor, append the following additional parameter:

init=/bin/bash 

then press F10 or ctl-X to start the boot.

When you get a shell prompt, try the same things as you did above to contrast your current environment with that previous one. Observe the environment variables by executing:

env

Now observe the approximate number of them:

env | wc -l

How does the number compare with the one you got when you did the same thing before? Is there anything different here?

Observe the running processes by executing:

ps -ef

Now observe the approximate number of them:

ps -ef | wc -l

How does this number compare?

Exercise the virtual terminal feature, going to another vt and back by successive keypresses:

ctrl-alt-F2
ctrl-alt-F1

It doesn't work.

Shutdown the system:

shutdown -h now

It doesn't work. Is there anything different here?

Exit from the current terminal:

exit

This time you didn't go back to the login prompt. Where are you? What do you do now??

Third, start again from square one. Turn the machine off with the power button (hold it in if necessary till the machine switches dead off). Turn it back on and at the same GRUB menu option you used to boot before press the "e" key to supply custom parameters to the kernel. From the "linux" line that appears in the resulting editor append the following two additional parameters:

panic=5     init=/bin/bash 

then press F10 or ctl-X to start the boot.

When you get a shell prompt, provoke the same "panic" situation in which you dead-ended above by typing "exit" by executing it again:

exit

What happens differently this time? What is responsible for it?

Boot the machine normally again (i.e., without adding any kernel parameters).

Documentation for kernel parameters, identifying what parameter there are and what each does:
 https://www.kernel.org/doc/Documentation/kernel-parameters.txt
 man bootparam 

Exercising control over bootup scripts by setting values in sysconfig's files

A variety of scripts run during bootup. Many of them use files in /etc/sysconfig as the source of values for variables that condition their behavior and operation. For example, in the course of bootup events the kernel calls the init program, which calls agetty, which calls login, which calls the shell, which calls profile. The point is, profile lies in this path and will get called. profile (fullname: /etc/profile) in turn ends up calling lang.sh (fullname: /etc/profile.d/lang.sh) which calls/runs /etc/sysconfig/i18n (traditionally) or /etc/locale.conf (with advent of systemd in recent Fedora around version 15). The only code in i18n or locale.conf is a few lines to set some variables, but these critically determine the localization behavior (language in which error messages appear, format of dates and monetary expressions, etc) for the duration of the shell session.

 

Fedora 18  

 

 

 

 

 

------>

 

/etc/profile.d/lang.sh:

# /etc/profile.d/lang.sh - set i18n stuff

sourced=0

if [ -n "$LANG" ]; then
  saved_lang="$LANG"
  [ -f "$HOME/.i18n" ] && . "$HOME/.i18n" && sourced=1
  LANG="$saved_lang"
  unset saved_lang
else
  for langfile in /etc/sysconfig/i18n /etc/locale.conf "$HOME/.i18n" ; do
    [ -f $langfile ] && . $langfile && sourced=1
  done
fi

/etc/locale.conf:

LANG="en_US.UTF-8"

 

Run this command:

locale

Note the values. Other values are available to be set, to view them run:

locale -a

Now change a couple of them, then re-run locale to see the changes you made. Run these commands:

export LC_TIME=french
export LANG=spanish
locale

See the changes? To see their effect, run for example these commands:

rm <file that does not exist>
ls -l   [note the reported date stamps]
cal
date

Avez-vous remarqué les changements de langue qui ont eu lieu comme resultat... OH, Sorry, so did you notice the language changes resulting from the new values of these variables? What we have done here manually can be arranged to take place on the next boot, as we will do below.

Modifying tunable kernel parameters in the /proc virtual directory

View all the kernel parameters that sysctl can configure by running

sysctl -a  |  less

(scroll down and up with ctrl-B/PgDn and ctrl-F/PgUp, exit with q).

The hostname is controlled by one of them, /proc/sys/kernel/hostname You can view the current hostname with any of the following commands, which illustrate the 3 methods of access to the information in /proc. Perform all three:

filesystem method:               cat  /proc/sys/kernel/hostname
sysctl command method:      sysctl kernel.hostname
dedicated utility method:      hostname

And you can change it to, say, starfish with any of the following.

filesystem method:                echo starfish > /proc/sys/kernel/hostname
sysctl command method:       sysctl  -w  kernel.hostname=starfish
dedicated utility method:       hostname starfish

Run these to vary the hostname, using one of the viewer commands each time to confirm. If you log out and back in or log in on another virtual terminal, the default shell prompt (which includes the hostname) will reflect the change. Finally, change the hostname back to what it was originally.

Arranging to do it at boot time

Without rebooting, we have seen dynamic and immediate effects of changing values of certain environment variables and kernel parameters. As well, there are easy related mechanisms to set both whenever the system boots. For environment variable values, you enter their desired values in files under /etc/sysconfig. For kernel parameter values, you enter them in /etc/sysctl.conf. We will set up certain changes and then reboot to see the effect. Before going ahead, we will make backup copies in our (root's) home directory of the files we intend to modify. Please run these commands:

cp  /etc/sysconfig/network-scripts/ifcfg-em1  ~/ifcfg-em1.org       #on your filesystem might be eth0, p1p1, or otherwise, instead of em1; find out
cp  /etc/locale.conf  ~/locale.conf.org
cp  /etc/sysctl.conf  ~/sysctl.conf.org
cp  /etc/hostname   ~/hostname.org

Suppose we want the system to boot up French, and we want to specify our IP address. We also want to change the hostname to "garcon." The file we'll use to make our system French is /etc/locale.conf; the one for setting the IP address is /etc/sysconfig/network-scripts/ifcfg-em1 And hostname will be controlled via the /etc/sysctl.conf file that tells sysctl, which runs during bootup, what to set.

Edit /etc/locale.conf, changing it as follows:

from:     LANG="en_US.UTF-8"     to:              LANG="fr_FR.UTF-8"

This has no effect yet, but it will when you reboot.

Edit /etc/sysconfig/network-scripts/ifcfg-em1 by adding an "IPADDR= " line.Your file will look something like this:

             
from:     whatever it is                     to:
DEVICE=em1
BOOTPROTO=none
IPADDR=1.2.3.4
NM_CONTROLLED=no
ONBOOT=yes

No effect yet, but you'll see it following a reboot.

The new IPADDR directive will cause the IP software to utilize 1.2.3.4 as the address of its packets. To see the aftereffect, you need to note the status quo ante now, so run the command

ifconfig em1

The second line of its output might look like:

inet 10.20.30.40 netmask 255.0.0.0 broadcast 10.255.255.255

or there may be no "inet" second line there at all. If there is one, note the IP address shown for your computer's interface card. It's going to change.

To control the hostname we'll set sysctl to apply our chosen value, garcon, upon reboot. To do that, edit /etc/sysctl.conf, adding the following final line to it:

kernel.hostname=garcon

(There are a couple of other hostname-setting mechanisms that could supersede this one. In order to prevent that let's deactivate those mechanisms.First, if there is a "HOSTNAME=      " line found in /etc/sysconfig/network, comment it out by putting a # in front of it. Second, find /etc/hostname and delete the hostname it contains. Either of these would operate to overwrite the sysctl setting.)

With the three changes you've made, you're ready to execute the following command:

reboot

When the system is back up, make sure the changes took effect. To do so, run the following commands and examine their output:

cal
ifconfig em1
hostname

When satisfied, restore the original versions of the controlling files.

cp  ~/ifcfg-em1.org  /etc/sysconfig/network-scripts/ifcfg-em1
cp  ~/locale.conf.org  /etc/locale.conf
cp  ~/sysctl.conf.org  /etc/sysctl.conf
cp  ~/hostname.org  /etc/hostname

Then reboot and go on your way.