Overview of Hardware and the Kernel
Kernel's role
-
hardware mediator
-
provides access to CPU, memory, PCI
bus, serial and parallel ports
Kernel device drivers
-
provide support for peripheral devices
-
coordinates device resources such as
IRQs and ioports
-
IRQ: hardware line by which device
sends a request for service to the CPU
-
I/O Port: channel through which information
flows between the device and the CPU
Two way to implement device driver
-
dynamically loaded kernel module
-
statically compiled into the
kernel itself
Benefits of Kernel Dynamic Kernel
Modules
-
these drivers are not compiled into
the kernel itself
-
exist as a separate file (not in /boot/vmlinuz,the
kernel)
-
loaded (and unloaded) into running
kernel without rebooting
-
keeps the kernel small
-
configured at load time by modprobe
or insmod
-
easier to manage, more flexibility
-
reconfigured without rebooting
-
default options are set in /etc/modules.conf
-
Typically used by sounds cards, network
interface cards, SCSI adapters
Modules Statically Compiled into
Kernel
-
configuration is built into kernel
and loaded at boot time
-
sometime configuration options may
be changed without a reboot
Loadable Kernel modules are stored in /lib/modules/`uname
-r`
To view the currently loaded modules
# lsmod
To manually load a module
# insmod /some/path/mymodule.o
You can pass parameters to loadable kernel modules
# insmod /some/path/mymodule.o
io=0x300 irq=10
If a module is not being used you can remove it
# rmmod
A better way to load modules and check for dependencies
# modprobe mymodule
-
modprobe understands dependencies
-
refers to the file /etc/modules.conf
to figure out how to load each module
-
to dynamically create a modules.conf
file for your system run modprobe
-c
Can also be used to remove modules
# modprobe -r mymodule
This happens as part of the boot process from inside
/etc/rc.d/rc.sysinit
To list all the modules in /lib/modules
# modprobe -l
To rebuild the dependency file in /lib/modules/`uname
-r`/modules.dep
# depmod -a
-
This command is run at boot time by the script /etc/rc.d/rc.sysinit
How do system utilities such as
fdisk, dumpe2fs, swapon, etc access devices?
In Unix everything is a file. Take
a look inside the /dev directory
$ ls -l /dev/hda?
brw-rw---- 1 root
disk 3, 1 Apr 11 07:25
/dev/hda1
brw-rw---- 1 root
disk 3, 2 Apr 11 07:25
/dev/hda2
brw-rw---- 1 root
disk 3, 3 Apr 11 07:25
/dev/hda3
...
$ ls -l /dev/tty[0-9]
crw--w---- 1 pattyo
sysadmin 4, 0 Aug 22 23:34 /dev/tty0
crw--w---- 1 pattyo
tty 4, 1 Sep 11 10:49
/dev/tty1
crw------- 1 root
root 4, 2 Aug 22 23:35
/dev/tty2
...
Utilities can access devices without
knowing the driver's implementation details
-
access is done via the filesystem
device nodes in /dev
-
the devices are accessed just like
any file is accessed
-
can be located anywhere in the filesystem,
typically in /dev
This command is used to create RedHat
bootable floppy
# cat boot.img > /dev/fd0
# ls -l /dev/fd0
brw-rw----
1 pattyo floppy 2, 0 Apr 11 07:25 /dev/fd0
-
What does the leading character, b,
denote?
Characteristics of device nodes
-
Block and Character Devices
-
b - random access devices
-
c - character or stream devices
-
Major and minor numbers
-
major: which driver indexed
into kernel
-
minor: address of the device,
which is the device driver parameter
The mknod command
-
used to create device nodes
mknod Example
-
Create a block device file in /tmp (major number 3 is /dev/hda)
mknod /tmp/mydisk b 3 0
ls -l /dev/hda
-
The kernel doesn't care what you name the device as long
as the major and minor numbers can be identified
-
Test your device
-
Compare those results to /dev/hda
-
Remove the device file you created
-
The device files can go anywhere, even /tmp
-
It is more secure to have them in /dev than /tmp
Virtual Block Devices
-
loopback (/sbin/losetup)
-
Software RAID
Filesystem Example
-
Create a 1 MB file
dd if=/dev/zero of=/tmp/foo bs=1024 count=1000
-
Build a filesystem on it
mkfs /tmp/foo
-
look at your file, then mount the file with the loop option
file /tmp/foo
mkdir /mnt/foobar
mount -o loop /tmp/foo /mnt/foobar
-
Add some files to it
cd /mnt/foobar
touch a b c
ls -l
Umount the filesystem
cd ..
umount /mnt/foobar
System Bus Support
-
The PCI Bus can be probed with /sbin/lspci
-
note controllers, other devices, and
controllers that bridge other buses
-
This information is also available
in /proc/bus/pci
-
Plug and Play ISA devices configured
by the kernel can be found in /proc/isapnp
Kernel Random Number Source Device
-
Hardware events on your computer are
used to seed
-
/dev/random, /dev/urandom
-
/dev/random is used by the kernel to
generate random numbers
-
Many applications depend on that device
being present
man 4 random
The random number
generator gathers environmental noise
from device drivers and
other sources into an entropy pool.
The generator also keeps
an estimate of the number of bit
of the noise in the entropy
pool. From this entropy pool
random numbers are created.
Example /dev/random
# tty
/dev/pts/1
cat /dev/random, you will see lots of binary junk
on your screen
# cat /dev/random
Don't move your mouse or touch your
keyboard
Now move your mouse
Wait...
Now type a key
Notice that more data in /dev/random
is generated by this activity
Configuring and building a custom kernel
-
The following process is for building a RedHat kernel from
the kernel-source package.
-
Before you begin check to make sure you have the necessary
packages on your system
-
run rpm -q package for the following
packages which include libraries, compiler utilities, and kernel sources.
ncurses-devel
glibc-devel
dev86
cpp
binutils
gcc
make
glibc-kernheaders or kernel-headers
kernel-source
# rpm -q kernel-source
-
Become familiar with your system's hardware
-
/proc is very useful for examining hardware
Downloading the RedHat kernel source
Download the kernel source rpm from the Linux ftp server,
gandalf.
# cd /tmp
# ftp somehost.example.edu
login: anonymous
password is your login account and fully qualified hostname:
root@stationX.example.edu
ftp> cd pub/rh7.3
ftp> cd Redhat/RPMS
ftp> ls
ftp> bin
ftp> mget kernel-source
use mget so you don't have to type the version number
ftp> quit
Install the rpm
# rpm -ivh kernel-source*
1. Go to kernel directory and modify Makefile so your
custom kernel will have a different name than the generic kernel
cd /usr/src/linux-2.4
vi Makefile
modify the line that reads
EXTRAVERSION=
by appending the work custom (or whatever makes sense)
-
This will ensure that the modules you create will go into
a new directory and not overwrite the current kernel modules
2. Clean up any previous builds by removing old .o files
and dependencies that might be lying around. Note, this will also remove
any .config files in the directory
make mrproper
3. Use a config file that matches your processor type.
This gives you a good baseline from which to begin tayloring to your kernel
cp configs/kernel-*-i686.config .config
4. To save time, configure based on the existing configuration
by reading the ./.config file. Do this only if you have previously configured
the kernel and have a .config file on hand.
make oldconfig
5. Use one of the available tools to refine the configuration
further
make xconfig -- X windows based
config tool
make config -- the most criptic
of the tools
make menuconfig --
text based color menus
-
You must be running X to use "make xconfig". This is probably
the easiest tool to use.
-
You have the choice of
-
statically compiling an the component into the kernel,
-
including it as a dynamically loadable modules, or
-
excluding it completely
6. Once the config file is created, propagate defined
configuration to other kernel subdirectories
make dep
7. Compile the kernel
make bzImage
8. Compile the kernel modules (and take a coffee break :)
make modules
9. Install the kernel modules
make modules_install
-
Take a look at the modules installed in /lib/modules/<new-kernel-version>
10. Install the kernel files to appropriate directories under /boot
make install
-
If you have a SCSI adapter and the SCSI driver is a module
-
This step is done for you during "make install".
/sbin/mkinitrd /boot/newinitrd-image 2.4.x
-
Where 'x' is the replaced with the kernel patch level.
Linux Boot Loader
-
In order to boot your newly created kernel, you will need
to modify lilo or grub depending on which boot loader you are using.
Grub
-
By default, the grub config file will be modified for you
during the install process
-
There is no need to run a command (similar to /sbin/lilo)
to install the grub MBR
cat /boot/grub/grub.conf
default=1
timeout=10
splashimage=(hd0,2)/grub/splash.xpm.gz
title Red Hat Linux (2.4.20custom)
root (hd0,2)
kernel /vmlinuz-2.4.20custom ro root=/dev/hda5
initrd /initrd-2.4.20custom.img
title Red Hat Linux (2.4.18-18.8.0)
root (hd0,2)
kernel /vmlinuz-2.4.18-18.8.0 ro root=LABEL=/
initrd /initrd-2.4.18-18.8.0.img
title DOS
rootnoverify (hd0,0)
chainloader +1
-
The default kernel will be vmlinuz-2.4.18-18.8.0
because of the default setting
-
(hd0,2)
-
first disk, third partition
Lilo
-
The configuration file for lilo is /etc/lilo.conf
-
After modifying the config file, run the command /sbin/lilo
to install the lilo boot loader
-
RedHat comes with a lilo config template
-
Rename this file to lilo.conf and modify
cat /etc/lilo.conf.anaconda
prompt
timeout=50
default=linux
boot=/dev/hda
map=/boot/map
install=/boot/boot.b
message=/boot/message
linear
image=/boot/vmlinuz-2.4.18-14
label=linux
initrd=/boot/initrd-2.4.18-14.img
read-only
append="root=LABEL=/"
image=/boot/vmlinuz-2.4.17
label=linux-old
initrd=/boot/initrd-2.4.17.img
read-only
append="root=LABEL=/"
other=/dev/hda1
optional
label=DOS
Add the lines for the old kernel just in case the new one
doesn't boot!
** Don't forget to run /sbin/lilo to write the new loading
map