FreeBSD's bootstrapping (startup) process is the means by which the system's kernel is first located within its BSD partition and slice on your hard disk, and then launched in the manner necessary to activate all your hardware devices and make the system available to users. Accomplishing all this is more difficult than it soundsit's far from a straightforward procedure. Because of the way that PC hardware works, with bootable hard drive partitions governed by the BIOS (Basic Input/Output System) software located on a chip on the motherboard, FreeBSD must launch itself in several stageseach stage having a very limited function and scope, and each executing one crucial step before passing off control to the next stage. As you'll learn in Chapter 20, "Adding Hard Disk Storage," the hard disk is divided into partitions (which FreeBSD refers to as "slices"); the BIOS can only look for a Master Boot Record (MBR) in one of these partitions, rather than in the secondary partitioning scheme known as "BSD partitions" (where the FreeBSD kernel is located). The BIOS can't address the kernel directly and has no knowledge of how to launch it even if it couldthere are several levels of increasingly complex addressing schemes in between. This means that at each level of the architecture dictated by legacy PC hardware standards, there must exist a FreeBSD boot blocka small chunk of executable code that has the ability to look one level deeper into the boot process, understanding the architecture of the next stage and knowing how to launch the next boot block. This process continues stage by stage until the kernel is finally located and launched. Chances are that you won't ever need to know the details of how these boot stages work, but knowing them can't hurtespecially if you're trying to figure out why something went wrong! When you first power on the machine, the first thing FreeBSD does is run the hardware checks and probes that are specified by the BIOS and the CMOS configuration. This part of the startup process depends entirely on your hardware configuration. In it, your RAM is checked, any SCSI controllers you might have installed are activated and tested, and you're given the opportunity to reconfigure your BIOS. This stage doesn't have anything to do with FreeBSDit occurs the same way whether you've got FreeBSD installed on your hard disk, Windows, or even no operating system at all. Moving Through the Boot ProcessAfter the BIOS prints a table showing the hardware data it has collected, it reads the Master Boot Record (MBR) of the primary disk for the first and most rudimentary boot block. It is the job of this and the next two boot blocks to find and run the loader, which configures and loads the kernel. Each boot block is sequentially a little more complex than the previous one. The first two are limited to 512 bytes in size (by the standardized sizes of the MBR and of the boot sector of a given slice), so they are both very simple. Here are each of the boot blocks and their functions:
This concludes the boot block phase of the bootstrapping process. FreeBSD has successfully run each piece of code that leads along the path to the bootable kernel, and the final phase of the boot process is where the complete FreeBSD system starts to come into play. That's where the kernel loads itself into memory, probes its available devices, and runs the resource configuration scripts that construct a working environment and start up the various system services. The remaining steps are as follows:
Finally, if all goes according to plan, you get a login prompt, and the system is up and ready for use. This whole process usually takes no more than a minute. Securing the Boot ProcessOf concern to the security-conscious administrator is the fact that by default, if you choose to boot into single-user mode (option 4 in the loader menu, or boot -s at the loader command prompt), you are not prompted for a password, and the system comes up automatically with full root access. If you're sharing the machine with anyone else, such that this person can sit down in front of the machine and physically reboot it if he wants to, this constitutes a grave and gaping security hole. Fortunately, it's easy to plug this hole. In /etc/ttys, which contains terminal settings for the various access methods (the console, virtual terminals, serial terminals, and network [pseudo] terminals), you can change the setting on the console line from secure to insecure: console none unknown off insecure This tells the system to treat the console as an insecure access point and to present a challenge (login prompt) to anyone who tries to boot into single-user mode. The root password is required in order to pass this challenge point. Note The console terminal method is only used in single-user mode. When you're booting into full multiuser mode, multiple virtual terminals are available at the physical terminal (you can switch between them with Alt+F1, Alt+F2, and so on). These terminals always present a login prompt. The secure and insecure settings in this case control whether you can log in as root or whether you're required to log in as a regular user and then use su to gain root access. If others have access to the physical terminal (even if you've secured the single-user console), you have a second security hole to plug. This security weakness results from the fact that Ctrl+Alt+Delete causes the system to reboot, whether or not you're logged in as root, or even logged in at all. The three-finger-salute works even at the login prompt. Allowing this kind of access is appropriate in some situations (a rack-mounted server in a trusted machine room, for example), but not in others. To disable it, you'll need to add an option to your kernel configuration and rebuild the kernel: options SC_DISABLE_REBOOT Other important considerations in securing your FreeBSD machine's operating environment are to make sure that it can't be booted from any device other than the primary hard drive (to protect against, for example, someone booting it from a CD-ROM or an external hard drive), and to protect the BIOS with a password. Of course, if unauthorized persons have physical access to the machine, you can't ever completely protect it; the best policy is to ensure that the operating environment is secured against all unauthorized access. For a detailed discussion of kernel options and configuring your kernel, see Chapter 18, "Kernel Configuration." What Can Slow Down the Boot Process?You should allow about a minute after leaving the loader menu before you see the login prompt signaling the end of the boot process. There's usually no reason for it to take any longer than thisbut there's always the possibility that something is going wrong and needs to be corrected. Because of the scripted, serial nature of the boot process and the verbose console messages emitted during startup, it's fairly simple to see where it's hanging. Two common culprits are Sendmail and httpd (Apache); in both cases, the associated slowness results from a network connectivity problem or misconfiguration. Both Sendmail and Apache have to figure out your machine's hostname. To do this, they need to perform a reverse lookup against the domain name server (DNS) configured in /etc/resolv.conf. (TCP/IP setup will be covered thoroughly in Chapter 23, "Configuring Basic Networking Services.") This lookup will have to time out for every configured name server before it fails and allows the startup process to proceed. Network timeouts are often fairly long, which is why networking is the most common cause of boot hang-ups. One solution to this problem is to make sure the name server listed first in /etc/resolv.conf is reachable from your FreeBSD machine. This ensures that Sendmail and Apache will be able to determine the machine's hostname and start up without delay. If this configuration is not possible, list 127.0.0.1 (localhost) as the first name server and disable the rest. Incidentally, this potential slowdown is why init uses the nonfs mount type (mount -a -t nonfs) when doing its initial "preen" to see whether it can safely go into multiuser mode. NFS, the Network Filesystem, has an astonishingly long timeout period, and a process waiting for that timeout to elapse can be almost impossible to kill. The nonfs option on the mount command, which you learned about in Chapter 12, avoids mounting NFS resources until later, in the resource configuration phase, so that it can come up properly in multiuser mode without user intervention. However, init does mount all filesystems later in /etc/rc, including NFS ones. Any NFS resources that you configured for automatic mounting in /etc/fstab (for example, without the noauto option) have the potential to freeze the system for a long time when the /etc/rc script reaches that point. Therefore, don't auto-mount NFS resources at boot unless you're sure they'll always be available to your machine on the network! |