GNU/Linux - 系统启动流程及rcS脚本介绍

Linux系统启动流程

在 Linux 系统启动过程中,会按特定顺序执行多个脚本和初始化例程,以使系统进入可用状态。虽然具体顺序可能因 Linux 发行版和版本而异,但以下是典型执行顺序的概括性概述:

1. BIOS/UEFI: 系统开机后,BIOS(或 UEFI 固件)会执行开机自检(POST),然后加载引导加载程序。

对于嵌入式 Linux,它们可能是芯片中的特殊引导 ROM。

2. 引导加载程序: 引导加载器,如 GRUB(Grand Unified Bootloader,统一引导加载器)或 LILO(Linux Loader,Linux 加载器),将 Linux 内核加载到内存中并开始执行,同时按需要初始化重要的硬件组件。

嵌入式系统常用的引导加载器包括 U-Boot、Das U-Boot 和 Barebox。

3. Linux 内核初始化: 一旦引导加载器将内核映像加载到内存中,Linux 内核就会开始控制。Linux 内核会初始化基本硬件组件或设备、设置内存管理、挂载初始 ramdisk(initramfs)、挂载根文件系统、配置其他基本系统参数,并开始初始化根文件系统的过程。

4. 初始化进程: 内核初始化完成后,会启动初始进程。传统上,这是 System V init(SysVinit),但现在许多现代发行版都使用 systemd 或 Upstart 作为 init 系统。

在嵌入式系统中,内核初始化完成后,init 进程(通常为 /sbin/init)就会启动。init 进程负责初始化用户空间环境和启动系统服务。对于嵌入式系统,init 进程可以是一个简单的 shell 脚本,也可以是一个最小的 init 系统(如 BusyBox)。

5. 用户空间初始化和初始脚本:  

init 进程运行后,会执行启动脚本或其他初始化任务来设置用户空间环境。

init 进程会读取配置文件并执行启动脚本,这些脚本负责初始化各种系统服务和配置。这些脚本通常位于 /etc/init.d 等目录中,或定义为 /etc/systemd/system 中的 systemd 单元文件。

这可能包括加载额外的文件系统、配置网络接口、设置系统日志以及初始化其他系统服务。

6.  运行级别或目标单元: 根据系统的运行级别(在 SysVinit 中)或目标单元(在 systemd 中),会执行不同的启动脚本或单元文件集。运行级别/目标单元定义了不同的系统状态,如单用户模式、多用户模式或图形模式。

7. 应用程序启动:

系统完全初始化后,将启动配置为自动启动的任何用户应用程序或服务。

这些应用程序可能包括针对嵌入式系统功能的定制软件,以及系统监控或管理工具。

8. 服务管理: 系统服务根据其依赖关系和配置启动。这包括联网、文件系统挂载和系统日志等基本系统服务。

9. 用户会话: 如果系统支持图形用户界面,则启动显示管理器(如 GDM、LightDM),并初始化用户会话。

10. 用户交互或登录提示:

最后,系统显示登录提示(文本或图形),允许用户登录并与系统交互。

根据嵌入式系统的设计,它可能会提供一个用户交互界面,如命令行界面或图形用户界面。

用户可以与系统交互,执行任务或监控系统运行。

本序列提供了 Linux 系统启动过程的高级概览。实际细节和具体步骤可能因发行版、init 系统和配置而异。

英文版:

During the startup process on a Linux system, several scripts and initialization routines are executed in a specific sequence to bring the system to a usable state. While the exact sequence may vary depending on the distribution and version of Linux, here's a generalized overview of the typical execution sequence:

1. BIOS/UEFI: When the system is powered on, the BIOS (or UEFI firmware) performs Power-On Self Test (POST) and then loads the bootloader.

For embedded linux, they may be special boot ROM in the chip.

2. Bootloader: The bootloader, such as GRUB (Grand Unified Bootloader) or LILO (Linux Loader), loads the Linux kernel into memory and starts its execution, and initialize required essential hardware components.

Common bootloaders for embedded systems include U-Boot, Das U-Boot, and Barebox.

3. Linux Kernel Initialization: Once the bootloader has loaded the kernel image into memory, the Linux kernel takes control. The Linux kernel initializes essential hardware components or devices, sets up memory management, mounts the initial ramdisk (initramfs), mounts the root filesystem, configures other essential system parameters, and begins the process of initializing the root filesystem.

4. Init Process: Once the kernel has initialized, it starts the init process. Traditionally, this was System V init (SysVinit), but many modern distributions now use systemd or Upstart as the init system.

In embedded system, after the kernel initialization, the init process (typically /sbin/init) is started. The init process is responsible for initializing the user space environment and launching system services. For embedded systems, the init process may be a simple shell script or a minimal init system like BusyBox.

5. User Space Initialization and Init Scripts:  

Once the init process is running, it executes startup scripts or other initialization tasks to set up the user space environment.

The init process reads configuration files and executes startup scripts, which are responsible for initializing various system services and configurations. These scripts are typically located in directories like /etc/init.d or defined as systemd unit files in /etc/systemd/system. 

This may include mounting additional filesystems, configuring network interfaces, setting up system logging, and initializing other system services.

6.  Runlevels or Target Units: Depending on the system's runlevel (in SysVinit) or target unit (in systemd), different sets of startup scripts or unit files are executed. Runlevels/target units define different system states, such as single-user mode, multi-user mode, or graphical mode.

7. Application Launch:

Once the system is fully initialized, any user applications or services configured to start automatically are launched.

These applications may include custom software specific to the embedded system's functionality, as well as system monitoring or management tools.

8. Service Management: System services are started according to their dependencies and configuration. This includes essential system services like networking, filesystem mounting, and system logging.

9. User Sessions: If the system supports graphical user interfaces, the display manager (e.g., GDM, LightDM) is started, and user sessions are initialized.

10. User Interaction or Login Prompt: 

Finally, the system displays a login prompt (either text-based or graphical), allowing users to log in and interact with the system.

Depending on the embedded system's design, it may provide a user interface for interaction, such as a command-line interface or a graphical user interface.

Users can interact with the system to perform tasks or monitor its operation.

This sequence provides a high-level overview of the startup process on a Linux system. The actual details and specific steps may vary based on the distribution, init system, and configuration.

===== 分割线 =====

rcS脚本介绍

在 Linux 中,rcS 脚本文件是启动过程中使用的初始化脚本之一。该脚本通常位于 /etc/init.d 目录中,由系统在启动过程中执行。

一种说法,"rcS "通常指 "启动时运行命令 "脚本。在包括 Linux 在内的类 Unix 操作系统中,"rcS "脚本通常是系统启动过程中最先执行的脚本之一。它设置初始环境并运行各种系统初始化任务。

还一种说法,"rcS "代表 "单用户模式运行命令"。它是 System V(SysV)init 系统的一部分,是启动类 Unix 操作系统的传统方法。

rcS "脚本在系统启动过程中执行,特别是当系统启动到单用户模式时。单用户模式是一种故障诊断模式,只有单个用户(通常是 root 用户)可以访问系统。它通常用于系统恢复或修复系统问题等维护任务。

rcS "脚本负责初始化启动过程早期所需的基本系统服务和配置,这些服务和配置是单用户模式运行所必需的。这包括挂载基本文件系统、设置基本网络、初始化系统时钟、挂载基本文件系统、配置系统环境以及执行其他必要的设置任务,以使系统达到最低功能状态。

它之所以重要,是因为它为启动过程的其他部分奠定了基础,确保系统在其他服务和应用程序启动前处于正常运行状态。rcS 脚本的内容因 Linux 发行版而异,但其主要目的在大多数发行版中都是一致的。

总的来说,"rcS "脚本在准备系统进入单用户模式时起着至关重要的作用,它能确保基本服务和配置就位,以便进行故障排除和维护。

英文版:

In Linux, the rcS script file is one of the initialization scripts used during the boot process. This script is typically found in the /etc/init.d directory, and it's executed by the system during the boot sequence.

For one explanation, "rcS" typically refers to the "run commands at startup" script. In Unix-like operating systems, including Linux, the "rcS" script is often one of the first scripts executed during the system boot process. It sets up the initial environment and runs various system initialization tasks.

And another explanation, "rcS" stands for "run commands for Single-user mode." It's part of the System V (SysV) init system, which is a traditional method for booting Unix-like operating systems.

The "rcS" script is executed during the system startup process, specifically when the system boots into single-user mode. Single-user mode is a troubleshooting mode where only a single user (typically the root user) has access to the system. It's often used for maintenance tasks such as system recovery or repairing system issues.

The "rcS" script is responsible for initializing essential system services and configurations that are required early in the boot process and are needed for single-user mode operation. This includes tasks like mounting essential filesystems, setting up basic networking, initializing the system clock, mounting essential filesystems, configuring the system environment, and performing other necessary setup tasks to bring the system to a minimal functional state.

It's important because it lays the groundwork for the rest of the boot process, ensuring that the system is in a functional state before other services and applications start up. The contents of the rcS script can vary depending on the Linux distribution, but its primary purpose is consistent across most distributions.

Overall, the "rcS" script plays a crucial role in preparing the system for single-user mode, ensuring that essential services and configurations are in place for troubleshooting and maintenance purposes.

====== 分割线 ======

嵌入式系统执行的启动脚本

通常嵌入式系统启动和重启前执行的脚本是rcS和rcK。

# ls /etc/init.d/rc*

rcK  rcS

启动和重启时调用rcS和rcK脚本,这个是在/etc/inittab 文件里设置的。Linux系统启动进程会读取这个文件。

# cat /etc/inittab

# This is run first except when booting in single-user mode.



# Startup the system

::sysinit:echo "<< inittab >>"

::sysinit:/bin/mount -t proc proc /proc

::sysinit:/bin/mount -t sysfs sysfs /sys

::sysinit:/bin/mount -t devtmpfs devtmpfs /dev

::sysinit:/bin/mount -o remount,rw /

::sysinit:/bin/mkdir -p /dev/pts

::sysinit:/bin/mount -t devpts devpts /dev/pts

::sysinit:/bin/mount -a

::sysinit:/sbin/swapon -a

null::sysinit:/bin/ln -sf /proc/self/fd /dev/fd

null::sysinit:/bin/ln -sf /proc/self/fd/0 /dev/stdin

null::sysinit:/bin/ln -sf /proc/self/fd/1 /dev/stdout

null::sysinit:/bin/ln -sf /proc/self/fd/2 /dev/stderr

# set hostname

null::sysinit:/bin/busybox hostname -F /etc/hostname

::sysinit:/etc/init.d/rcS



# Stuff to do before rebooting

#::ctrlaltdel:/sbin/reboot

::shutdown:/etc/init.d/rcK

::shutdown:/sbin/swapoff -a

::shutdown:/bin/umount -a -r



# Stuff to do when restarting the init process

::restart:/sbin/init



ttyLP0::respawn:/sbin/getty 115200 ttyLP0

# /sbin/getty invocations for the runlevels.

#

# The "id" field MUST be the same as the last

# characters of the device (after "tty").

#

# Format:

#  <id>:<runlevels>:<action>:<process>

#



tty1:12345:respawn:/sbin/getty 38400 tty1




 

启动进程就是系统启动后第一个执行的进程:

# ps

  PID USER       VSZ STAT COMMAND

    1 root      3500 S    /sbin/init

    ...

    ...

具体步骤如下:

1. 初始进程:

    * 内核执行指定的初始进程,通常是 /sbin/init 或其符号链接。

    * 在使用 BusyBox 的嵌入式系统中,/sbin/init 可能是指向 BusyBox 二进制文件的符号链接。

2. 执行初始化脚本

    * 初始进程会读取配置文件(如 /etc/inittab 或 /etc/init/rcS.conf),以确定要执行的脚本。

    * 通常,init 进程会生成配置文件中指定的多个脚本,这些脚本通常位于 /etc/init.d/ 或 /etc/rc.d/ 等目录中。

    * 这些脚本处理各种系统初始化任务,如加载文件系统、配置网络接口、启动基本服务和设置硬件外设。

1. Init Process:

    * The kernel executes the designated init process, which is typically /sbin/init or a symbolic link to it.

    * In embedded systems using BusyBox, /sbin/init may be a symbolic link to the BusyBox binary.

2. Init Script Execution:

    * The init process reads its configuration file (e.g., /etc/inittab or /etc/init/rcS.conf) to determine which scripts to execute.

    * Commonly, the init process spawns multiple scripts specified in the configuration file, usually located in directories like /etc/init.d/ or /etc/rc.d/.

    * These scripts handle various system initialization tasks, such as mounting filesystems, configuring network interfaces, starting essential services, and setting up hardware peripherals.

  • 31
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值