65、UNIX 内核配置全解析

UNIX内核配置详解

UNIX 内核配置全解析

1. 设备驱动与 SCSI 设备分配

1.1 设备驱动列表

以下是一系列设备驱动的列表:

device−driver   sbus         # 'driver' for sbus interface
device−driver   sbwtwo       # monochrome frame buffer
device−driver   gthree       # 8−bit color frame buffer
device−driver   cgsix        # 8−bit accelerated color frame buffer
device−driver   cgtwelve     # 24−bit accelerated color frame buffer
device−driver   dma          # 'driver' for dma engine on sbus interface
device−driver  esp           # Emulex SCSI interface
device−driver  fd            # Floppy disk
device−driver  audioamd      # AMD79C30A sound chip
device−driver  le            # LANCE ethernet
device−driver   zs           # UARTs

1.2 SCSI 设备分配

SCSI 设备的分配情况如下:

scsibus0 at esp                     # declare first scsi bus
disk sd0 at scsibus0 target 3 lun 0 # first hard SCSI disk
disk sd1 at scsibus0 target 1 lun 0 # second hard SCSI disk
disk sd2 at scsibus0 target 2 lun 0 # third hard SCSI disk
disk sd3 at scsibus0 target 0 lun 0 # fourth hard SCSI disk
tape st0 at scsibus0 target 4 lun 0 # first SCSI tape
tape st1 at scsibus0 target 5 lun 0 # second SCSI tape
disk sr0 at scsibus0 target 6 lun 0 # CD−ROM device
scsibus1 at esp                     # declare second scsi bus
scsibus2 at esp                     # declare third scsi bus
scsibus3 at esp                     # declare fourth scsi bus

2. BSD 风格的内核配置过程

2.1 配置步骤

BSD 风格的内核配置过程包含以下几个主要步骤:
1. 选择配置名称 :为系统配置选择一个名称,例如 PICKLE。
2. 创建配置文件 :通过复制 GENERIC 文件并使其可写来创建配置文件。

cp GENERIC PICKLE; chmod +w PICKLE
  1. 编辑配置文件 :编辑 PICKLE 文件以反映系统情况,例如删除系统中不存在的设备。
  2. 运行配置命令
/etc/config PICKLE

如果指定了 -n 标志,则不会执行 make depend
5. 构建新系统

cd ../PICKLE
make
  1. 保存原内核并安装新内核
mv /vmunix /vmunix.old
cp vmunix /vmunix
/etc/halt
b vmunix
  1. 回退过程 :如果新内核无法正常工作,可按以下步骤回退:
/etc/halt
b vmunix.old -s
mv /vmunix.old /vmunix
^D [Ctrl−D]

2.2 对象唯一分发配置

对于对象唯一分发的配置文件,必须包含以下内容:

machine           "sunN"     # where 'N' is 2 or 3 or 4
options           INET
pseudo−device     loop

2.3 添加设备驱动

新的设备驱动需要在 ../../sun/conf.c 文件中添加条目,可能还需要在 ../../sun/swapgeneric.c devices 中添加。在配置文件中提及设备名称即可包含该驱动。

2.4 关键要点

从管理角度来看,回退过程非常重要。同时,内核镜像名称也很关键,一些重要的 UNIX 命令期望 /vmunix 是运行的内核,因此要保留该名称。

3. config 命令

3.1 命令语法

config 命令用于配置系统内核,语法如下:

/usr/etc/config [−fgnp ] [−o obj_dir ] config_file

3.2 命令功能

config 命令用于构建系统配置文件,为构建新的系统内核做准备。它需要当前目录中的多个输入文件,若 ../config_file 目录不存在,会自动创建。其输出文件之一是 makefile ,用于构建内核。

3.3 可用选项

选项 功能
-f 设置 makefile 以实现快速构建,通过构建包含所有无源码 .o 文件的 vmunix.o 文件来减少系统构建时需要处理的文件数量。
-g 若可能,从 SCCS 历史记录中获取缺失源文件的当前版本。
-n 不执行 make depend ,使用该选项时, config 完成前会提示“别忘了执行 ‘make depend’”。
-p 为系统配置进行性能分析,此选项仅适用于具有完整源码发布的系统。
-o obj_dir 使用 ../obj_dir 代替 ../OBJ 作为查找对象文件的目录,以便生成编译和链接内核所需的文件。

3.4 注意事项

运行 config 时要注意错误,不要使用 config 报错的内核。若 config 成功完成,可切换到 ../config_file 目录,使用 make 构建内核。

以下是 BSD 风格内核配置过程的 mermaid 流程图:

graph LR
    A[选择配置名称] --> B[创建配置文件]
    B --> C[编辑配置文件]
    C --> D[运行配置命令]
    D --> E[构建新系统]
    E --> F[保存原内核并安装新内核]
    F --> G{新内核是否正常工作}
    G -- 是 --> H[正常使用]
    G -- 否 --> I[回退过程]
    I --> H

4. 其他风格的内核重新配置

4.1 HP - UX 10.x 内核配置

4.1.1 配置过程

在 HP - UX 10.x 中,内核二进制文件是离线创建的,新创建的内核二进制文件将在下一次系统重启时执行。 config 命令用于配置设备驱动、交换和转储设备以及可调系统参数。它读取 /stand/system 文件中的系统描述和主内核配置表,生成以下内容:
- 定义系统配置的 C 程序源文件。
- 用于编译 C 程序和重新链接新配置系统的 makefile

4.1.2 主文件

主文件位于 /usr/conf/master.d 目录,例如 core - hpux 文件定义了所有可调内核参数。

4.1.3 内核描述文件示例
* Drivers and Subsystems
asp
btlan1
ccio
cdfs
clone
core
diag0
       ...
       ...
pts
sad
sc
scsi1
sdisk
sio
stape
tpiso
uipc
vxbase
wsio
*Kernel Device info
vxadv
diag2
dmem
dev_config
dump lvol
* Tunable parameters
maxfiles             75
maxfiles_lim       1275
maxswapchunks      1031
maxuprc             175
maxusers            350
msgmax            32768
msgmnb            32768
msgmni              100
msgseg             7168
msgtql              256
nfile             15364
npty                250
nstrpty             250
semmnu             1170
msgssz                8
4.1.4 回退过程

如果新内核出现问题,可按以下步骤回退:
1. 停止或重启系统(需以 root 用户登录)。
2. 遵循控制台消息,中断系统启动。
3. 继续启动,在菜单提示处输入 boot
4. 输入 y 以与 IPL 交互。
5. 使用旧内核将系统带入单用户模式:

hpux -is /stand/vmunix.fbk
  1. 在单用户模式下,用旧内核替换新内核:
mv /stand/vmunix.fbk /stand/vmunix
  1. 重启系统进入多用户模式:
shutdown -r 0

4.2 Solaris 2.x 内核配置

4.2.1 内核特点

Solaris 内核是一个软件模块集合,包括核心镜像文件 /kernel/unix 和所有加载到系统内存的可加载模块。它在系统启动时加载基本操作系统服务,可加载模块可在启动时或之后加载。

4.2.2 内核配置文件

通过 /etc/system 文件控制内核配置,该文件包含关键字/值对命令,用于自定义系统对可加载模块的处理。支持的命名空间包括 drv (设备驱动)、 exec (执行格式模块)、 fs (文件系统)等。

4.2.3 /etc/system 文件示例
*ident  "@(#)system  1.15  92/11/14 SMI" /* SVR4 1.5 */
*
* SYSTEM SPECIFICATION FILE
*
* moddir:
*  Set the search path for modules. This has a format similar to the
*  csh path variable. If the module isn't found in the first directory
*  it tries the second and so on. The default is /kernel /usr/kernel
*  Example:
   *    moddir: /kernel /usr/kernel /other/modules
* root device and root filesystem configuration:
*   The following may be used to override the defaults provided by
*   the boot program:
*   rootfs:    Set the filesystem type of the root.
*   rootdev:   Set the root device. This should be a fully
*              expanded physical pathname. The default is the
*              physical pathname of the device where the boot
*              program resides. The physical pathname is
*              highly platform and configuration dependent.
*   Example:
*     rootfs:ufs
*     rootdev:/sbus@1,f80 00000/esp@0,800000/sd@3,0:a
   *  (Swap device configuration should be specified in /etc/vfstab.)
* exclude:
*   Modules appearing in the moddir path which are NOT to be loaded,
*   even if referenced. Note that 'exclude' accepts either a module name,
*   or a filename which includes the directory.
*   Examples:
*    exclude: win
   *    exclude: sys/shmsys
* forceload:
*   Cause these modules to be loaded at boot time, (just before mounting
*   the root filesystem) rather than at first reference. Note that
*   forceload expects a filename which includes the directory. Also
*   note that loading a module does not necessarily imply that it will
*   be installed.
*   Example:
   *     forceload: drv/foo
* set:
*   Set an integer variable in the kernel or a module to a new value.
*   This facility should be used with caution. See system(4).
*   Examples:
   *   To set variables in 'unix':
*     set nautopush = 32
*     set maxusers = 40
*   To set a variable named 'debug' in the module named 'test_module'
*     set test_module:debug = 0×13
4.2.4 硬件配置检查

默认情况下,Solaris 重启时不检查新的硬件配置。可通过以下方式强制检查:

ok boot -r
# reboot-- -r
4.2.5 显示系统配置的工具
命令 功能
prtconf 显示机器的一般配置,包括机器类型、型号、内存量和已配置设备的硬件信息。
sysinfo 显示伪设备驱动、可调内核参数和已加载模块的文件名。
modinfo 显示动态加载模块的信息。
4.2.6 检查内核参数

可使用 Solaris 通用调试器 adb 检查内核内存中的实际参数,示例如下:

# uname –a
SunOS atlas 5.7 Generic sun4u sparc SUNW,Ultra−80
# cd/
# modload /kernel/sys/shmsys
# modload /kernel/sys/semsys
# modload /kernel/sys/msgsys
# adb -k /dev/ksyms /dev/mem
physmem 7cf0e
shminfo/2E2D
shminfo:
shminfo:         1048576       1    100      6
seminfo/10D
seminfo:
seminfo:              10      10     60     30
25      10     10    104
32767   16384
msginfo/6D1u
msginfo:
msginfo:             100   65535  65535    50
16      40   8192
$q

以下是 Solaris 内核配置检查硬件配置的 mermaid 流程图:

graph LR
    A[系统重启] --> B{是否强制检查硬件配置}
    B -- 否 --> C[正常启动]
    B -- 是 --> D[搜索硬件配置和设备]
    D --> C

4.2 Solaris 2.x 内核配置(续)

4.2.7 内核参数理解与指定

要理解显示的内核参数值以及如何指定适当数量的参数和显示格式,可以参考 /usr/include/sys 目录下的相应头文件,如 shm.h sem.h msg.h

共享内存参数
# cat /usr/include/sys/shm.h
.....
/*
* IPC Shared Memory Facility.
*/
.....
.....
/*
* Shared memory information structure
*/
struct     shminfo {
size_t    shmmax,    /* max shared memory segment size */
shmmin;    /* min shared memory segment size */
int       shmmni,    /* # of shared memory identifiers */
shmseg;    /* max attached shared memory */
/* segments per process */
};
.....
.....

每个显示的值对应一个可调的“共享内存”参数:
| 显示值 | 对应参数 |
| ---- | ---- |
| 1048576 | shmmax |
| 1 | shmmin |
| 100 | shmmni |
| 6 | shmseg |

信号量参数
# cat /usr/include/sys/sem.h
.....
/*
* IPC Semaphore Facility.
*/
.....
.....
/*
* Semaphore information structure
*/
struct  seminfo {
int  semmap;    /* # of entries in semaphore map */
int  semmni;    /* # of semaphore identifiers */
int  semmns;    /* # of semaphores in system */
int  semmnu;    /* # of undo structures in system */
int  semmsl;    /* max # of semaphores per id */
int  semopm;    /* max # of operations per semop call */
int  semume;    /* max # of undo entries per process */
int  semusz;    /* size in bytes of undo structure */
int  semvmx;    /* semaphore maximum value */
int  semaem;    /* adjust on exit max value */
};
.....
.....

每个显示的值对应一个可调的“信号量”参数:
| 显示值 | 对应参数 |
| ---- | ---- |
| 100 | semmap |
| 10 | semmni |
| 60 | semmns |
| 30 | semmnu |
| 25 | semmsl |
| 10 | semopm |
| 10 | semume |
| 104 | semusz |
| 32767 | semvmx |
| 16384 | semaem |

在这个示例中,显示的参数值是共享内存和信号量的默认内核参数,意味着该内核在这方面未进行调整。

消息参数
# cat /usr/include/sys/msg.h
.....
/*
* IPC Message Facility.
*/
.....
.....
/*
* Message information structure.
*/
struct  msginfo {
    ...
};

显示的消息参数也对应着 msginfo 结构体中的各个字段。

5. 总结与对比

5.1 不同风格内核配置对比

内核风格 配置方式 关键文件 回退机制 硬件配置检查
BSD 风格 通过复制、编辑配置文件,运行 config 命令构建新内核 GENERIC PICKLE 保存旧内核 /vmunix.old ,出现问题时替换新内核 无默认检查机制
HP - UX 10.x 使用 config 命令读取 /stand/system 文件生成新内核 /stand/system /usr/conf/master.d 下的主文件 保存旧内核 /stand/vmunix.fbk ,出现问题时替换新内核 无默认检查机制
Solaris 2.x 通过 /etc/system 文件控制内核配置,加载可加载模块 /etc/system 无明确回退文件,可通过修改配置文件恢复 默认不检查,可通过 -r 标志强制检查

5.2 关键要点总结

  • 回退机制 :在进行内核重新配置时,回退机制是必不可少的,它能确保系统在新内核出现问题时可以恢复到可用状态。
  • 内核镜像名称 :一些重要的 UNIX 命令依赖于特定的内核镜像名称,如 /vmunix ,因此要保留该名称以确保命令正常工作。
  • 硬件配置检查 :不同系统对于硬件配置检查的默认策略不同,在需要时可通过特定标志或命令强制检查。

以下是不同风格内核配置流程的 mermaid 流程图:

graph LR
    classDef startend fill:#F5EBFF,stroke:#BE8FED,stroke-width:2px
    classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px
    classDef decision fill:#FFF6CC,stroke:#FFBC52,stroke-width:2px

    A([开始]):::startend --> B{选择内核风格}:::decision
    B -->|BSD 风格| C(复制、编辑配置文件):::process
    B -->|HP - UX 10.x| D(修改 /stand/system 文件):::process
    B -->|Solaris 2.x| E(修改 /etc/system 文件):::process
    C --> F(运行 config 命令):::process
    D --> G(运行 config 命令):::process
    E --> H(等待下次启动加载配置):::process
    F --> I(构建新内核):::process
    G --> J(生成新内核二进制文件):::process
    I --> K{新内核是否正常工作}:::decision
    J --> K
    H --> K
    K -- 是 --> L([正常使用]):::startend
    K -- 否 --> M(执行回退机制):::process
    M --> N([恢复到可用状态]):::startend

通过以上对不同 UNIX 内核配置方式的详细介绍,我们可以根据具体的系统需求和硬件环境选择合适的配置方法,并在配置过程中注意关键要点,确保系统的稳定性和可靠性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值