Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Absolute BSD - The Ultimate Guide To FreeBSD (2002).pdf
Скачиваний:
25
Добавлен:
17.08.2013
Размер:
8.15 Mб
Скачать

Loading and Unloading Modules

Load and unload software modules with kldload(8) and kldunload(8). For example, to load the warp console−mode screen saver, enter this command:

................................................................

# kldload /modules/warp_saver.ko

#

................................................................

Once you've finished, you can unload the module with this command:

................................................................

# kldunload warp_saver.ko

#

................................................................

If all possible functions were compiled into the kernel, the kernel would be much larger than it is. This way, you can have a smaller, more efficient kernel and only load modules as you need them.

Customizing the Kernel

You'll eventually find that you cannot tweak your kernel as much as you desire using only modules and sysctl, and your only solution will be to build your own custom kernel. But don't worry, the process is perfectly straightforward if you take it step by step.

The kernel shipped in a default install is called GENERIC. GENERIC is designed to run on a wide variety of hardware, though not necessarily to run well or optimally. GENERIC boots nicely on a 486 and later systems, but newer x86 systems have advanced features and optimizations that help them perform better, and GENERIC doesn't take advantage of these features because it's aiming for the lowest common denominator.

When you customize your kernel, you'll get better performance, and you can also include new functionality in it, or support for new hardware.

Preparation

You must have the kernel source code before you can consider building a kernel. If you followed the advice in Chapter 1, you're all set. If not, you can either go back into the installer and load the kernel sources or jump ahead to Chapter 6 and use CVSup instead.

If you don't know whether you have the kernel source code installed, look for a /sys directory. If it exists, and there are a bunch of files and directories in it, you have the kernel sources.

Before building a new kernel, you must know what hardware your system has. This can be difficult to determine, because the brand name on a component doesn't necessarily have any relationship to the device's identity or abilities. After all, many different companies made an NE2000−compatible network card. Even if the box said "3com," the circuits inside the chip said "ne2000."[5] Similarly, companies such as Linksys rebrand inexpensive network cards that all have very different internals. The boxes all say "Linksys," but the chip says something else depending on the month of

79

manufacture.

Fortunately, PCI−based systems have sophisticated hardware−recognition systems, and FreeBSD will almost certainly find these devices at boot. If yours is an older ISA system, on the other hand, you might have to dig through the component manual to learn what sort of device you have and how to set IRQs and I/O ports.

The best place to see what hardware your FreeBSD system found is the file /var/run/dmesg.boot, which contains the boottime kernel messages buffer, also known as all that garbage you saw on boot. (There's an example of dmesg.boot in Chapter 3.) If you've never looked at your dmesg.boot file, take a few moments to do so now. You probably never knew that your computer had so much stuff in it!

When looking at the dmesg.boot file, you'll find the device names at the beginning of the dmesg lines. Each piece of hardware has a separate device name, typically a few letters followed by a unit number, such as npx0. The letters are the name of the driver (npx), and each device is numbered, starting with 0. One device might span several lines, and if you have multiple devices, they'll show up with sequential unit numbers.

Your Backup Kernel

A bad kernel can render your system unbootable, so you absolutely must keep a good kernel around at all times. While the kernel install process retains one old kernel, it's easy to overwrite it.

If you don't keep a good, reliable kernel around, here's what can happen: You forget to put a network driver in your current kernel, so you decide to rebuild it. Your rebuilt kernel becomes the current kernel, your previous (imperfect) kernel becomes the old kernel, and your old working kernel goes off to the Land of Oz. When you discover that your new kernel won't keep running for more than a few hours, you'll really regret the loss of that reliable kernel.

A common place to keep a known good kernel is /kernel.good. Back up your working, reliable kernel to /kernel.good before tweaking your kernel, like this:

................................................................

#mkdir modules.good

#cp kernel kernel.good

#cp −R modules/;* modules.good/

................................................................

Note Don't be afraid to keep a variety of kernels on hand. Some people even put kernels in directories named by date, so that they can have a long−running history of kernels. You can have too many kernels on hand, but only if they fill up your root partition.

Editing Kernel Files

You've now backed up your working kernel and are ready to build a new one. To begin, check out /sys/i386/conf, where you should find several files. The important ones for your purposes are GENERIC and LINT. GENERIC is the kernel configuration file for the standard kernel used on first install. LINT contains all kernel options and the documentation for them, including a variety of really obscure ones.

80

Do not edit any of the files you find in /sys/i386/conf directly. Instead, copy GENERIC to a new file and edit the copy, not the original, and name the file after your machine (the most common convention). For example, if you have a server called "webserver," you would do this:

................................................................

# cp GENERIC WEBSERVER

................................................................

Now open the new configuration file in your favorite text editor. Here's a snippet from the part of the GENERIC configuration that covers IDE (aka ATAPI) drives.

................................................................

# ATA and ATAPI devices

 

 

device

ata0

at isa? port IO_WD1 irq 14

device

ata1

at isa? port IO_WD2 irq 15

device

ata

 

 

device

atadisk

 

# ATA disk drives

device

atapicd

 

# ATAPI CDROM drives

device

atapifd

 

# ATAPI floppy drives

device

atapist

 

# ATAPI tape drives

options

ATA_STATIC_ID

#Static device numbering

................................................................

Each line in the kernel configuration file is either a comment or description of a kernel feature. The pound sign (#) marks comments and the computer ignores them; they're there for your benefit.

Some lines have comments that start in the middle of the line, describing what appears earlier on the line. Lines beginning with "device" are device drivers; in this example, you'll see entries for IDE disks, IDE CD−ROM drives, IDE floppy drives, and IDE tape drives. There are also entries for the actual IDE bus on the motherboard, and for both of its connectors.

Other lines are for software features, or "options." In this example, the option ATA_STATIC_ID enables "static device numbering"; you'll learn what that means in Chapter 16. You'll also see a few special−purpose keywords, such as "pseudo−device" and "cpu," which are either software options or descriptions of hardware.

Because the GENERIC kernel is designed to run on the greatest variety of equipment, it includes a huge array of network drivers, disk drivers, controllers, and features. As a general rule, begin customizing a kernel by commenting out unnecessary entries to shrink and simplify your kernel. Of course, when you streamline the kernel in this way, you'll have to rebuild it when you change your hardware, and if you're one of those folks who constantly swaps hardware in and out, you probably don't want to gut your kernel. On the other hand, if you have a specific server hardware setup and you mass−produce kernels, strip out anything unnecessary.

Your copied kernel configuration file (WEBSERVER, in our example) starts off with comments describing the purpose of the configuration file and containing pointers to the official FreeBSD documentation. Once you skip these comments, the new config file starts with the following:

................................................................

machine

i386

cpu

I486_CPU

cpu

I586_CPU

cpu

I686_CPU

ident

GENERIC

81

maxusers 0

................................................................

machine

The machine keyword in the preceding listing describes the system architecture. You really don't want to change this, unless you're building a kernel for your Alpha on an x86.

cpu

The cpu statements describe the on−chip features the kernel can expect to use and support. This is important because newer CPUs provide instructions that others don't. (For example, consider the Pentium versus the Pentium with MMX.)

You only need to include the CPU you have. If you're not sure of the CPU in your hardware, check dmesg.boot. My laptop's dmesg.boot includes the following lines:

................................................................

CPU: Pentium III/Pentium III Xeon/Celeron (497.56−MHz 686−class CPU) Origin = "GenuineIntel" Id = 0x681 Stepping = 1

Features=0x383f9ff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE3

6,MMX,FXSR,SSE>

................................................................

The important part of this description is the 686−class CPU at the end of the first line. This tells me that I can remove the cpu statements I486_CPU and I586_CPU to make my kernel smaller and faster. As a result, the kernel will use 686−class CPU−specific optimizations instead of slower generic code.

ident

The ident statement is the kernel's name, which is usually the same as the server name. If you build one kernel and install it on many machines, you might want to give the kernel a name that reflects its purpose, such as WEBSERVER.

maxusers

The maxusers value is a rough value used to compute the size of various in−kernel tables (not the maximum number of users). These in−kernel tables control things such as the number of available network connections and the number of files that can be open at one time.

Beginning with FreeBSD 4.5, the kernel will look at a system's resources and assign a maxusers value that it believes is appropriate for most users. The maxusers 0 entry means the kernel will take the defaults, which will be entirely appropriate for most systems. (You can still hard−code a MAXUSERS value if you wish, however, as I describe below.)

On FreeBSD versions 4.4 and earlier, you needed to hard−code your maxusers value. I typically ran an X−based laptop with a maxusers value of 16, which is fine for my laptop because I'm the only user of the system; no matter how many fancy desktop widgets I fire up, or how many Web pages

82