Objectives
Upon completion of this unit, you should be able to:
- Understand the purpose and organization of the kernel
- Understand how to load and configure kernel modules
- Know how to configure the kernel using /proc and sysctl
- Explore hardware devices available on the system
The Linux Kernel
1) The kernel constitutes the core part of the Linux operating system.
2) Kernel duties:
- System initialization: detects hardware resources and boots up the system
- Process scheduling: determines when processes should run and for how long
- Memory management: allocates memory on behalf of running processes
- Security: constantly verifies filesystem permissions, SELinux contexts and firewall rules
- Provides buffers and caches to speed up hardware access
- Implement standard network protocols and filesystem formats
3) Documentation available in the kernel-doc RPM package
Kernel Images and Variants
1) Architectures supported: x86, X86_64, IA64/Itanium, PowerPC64, s390x.
2) Three kernel versions available for x86:
- Regular: one or more processors but 4GB of RAM or less
- PAE: multiple processors and up to 64G of RAM
- Xen: needed for virtualization
3) Kernels always installed under /boot/vmlinuz-*
Kernel Modules
1) Modules are small kernel extension that may be loadede and unloaded at will
2) Can implement drivers, filesystem, firewall, and more
3) Are located under /lib/modules/$(uname –r)/
4) Compiled for a specific kernel version and are provided with the kernel RPM
5) Third party modules may ba added
Kernel Module Utilities
1) lsmod: provides a list of loaded modules
2) modprobe: can load and unload modules
3) modinfo: display information about any available modules
4) /etc/modprobe.conf used for module configuration:
- Parameters to pass to a module whenever it is loaded
- Aliases to represent a module name
- Commands to execute when a module is loaded or unloaded
Managing the initrd Image
1) The initial RAM disk provides modules loaded early in the boot process
2) This file is located under /boot/initrd-$ (uname –r).img
3) Extra modules sometimes need to be added due to:
- New hardware added to the system. i.e. SCSI controller
- New features needed such as USB devices.
- Module needs to load automatically at boot time.
4) Use mkinitrd and the –with option to rebuild with an extra module:
mkinitrd –-with=module_name /boot/initrd-$(uname –r).img \$(uname –r)
Accessing Drivers Through /dev
1) Files under /dev used to access drivers
2) Reading from and writing to those files are valid operations:
- Read from serial port: cat /dev/ttyS0
- Write to serial port: echo “Message” > /dev/ttyS0
3) Three file attributes determine which driver to access:
- Device type (character or block)
- Major number
- Minor number
Device Node Examples
1) Block Devices
- /dev/hda, /dev/hdc – IDE hard disk, CDROM
- /dev/sda, /dev/sdb – SCSI, SATA, or USB Storage
- /dev/md0, /dev/md1 – Software RAID
2) Character Devices
- /dev/tty[0-6] – virtual consoles
- /dev/null, /dev/zero – software devices
- /dev/random, /dev/urandom – random numbers
Managing /dev with udev
1) udev manages files stored under /dev/
2) Files only created if corresponding device is plugged in
3) Files are automatically removed when devices is disconnected
4) udev statements under /etc/udev/rules.d/ determine:
- Filenames
- Permissions
- Owners and groups
- Commands to execute when a new device shows up
Adding Files Under /dev
1) The right way to add a /dev entry involves udev:
- Create a new file under /etc/udev/rules.d/
- Insert a statement such as: KERNEL==”sda”, NAME=”usbkey”, SYMLINK=”usbstorage”
- This creates a device file named usbkey and a symlink named usbstorage next time /dev/sda get plugged
2) Files can be added manually with mknod:
mknod /dev/usbdevice b 8 0
3) mknod not persistent!
Kernel Configuration With /proc
1) /proc used to get or set kernel configuration
2) Virtual filesystem: files not stored on hard disk
3) Entries not persistent: modifications get reinitialized after a reboot
4) Used to display process information, memory resources, hardware devices, kernel memory, etc.
5) Can be used to modify network and memory subsystems or modify kernel features
6) Modifications apply immediately
/proc Examples
1) Read-only files
- /proc/cpuinfo
- /proc/1/*
- /proc/partitions
- /proc/meminfo
2) Read-Write entries under /proc/sys/:
- /proc/sys/kernel/hostname
- /proc/sys/net/ipv4/ip_forward
- /proc/sys/vm/drop_caches
- /proc/sys/vm/swappiness
sysctl: Persistent Kernel Configuration
1) sysctl adds persistence to /proc/sys settings
2) Statements added to /etc/sysctl.conf automatically reflected under /proc after a reboot
3) Configuration maintained or monitored using the sysctl command:
- List all current settings: sysctl –a
- Reload setting from sysctl.conf: sysctl –p
- Set a /proc value dynamically: sysctl –w net.ipv4.ip_forward=1
Exploring Hardware Devices
1) A snapshot of all connected device is maintained by HAL: Hardware Abstraction Layer
2) hal-device lists all devices in text mode
3) hal-device-manager displays all devices on a graphica window
4) lspci and lsusb list devices connected to the PCI and USB buses, repectively
5) The /proc and /sys filesystems also contain bus and device specific information
Monitoring Processes and Resources
1) Information available under /proc/ can be hard to understand
2) Interfaces are available to format the date and make it more accessible:
- Memory: free, vmstat, swapon –s, pmap
- Processes: ps, top, gnome-system-monitor
- Kernel state: uname, uptime, tload
转载于:https://blog.51cto.com/jackiechen/157374