# 配置启动后设置

启动后设置包括完成初始设置、配置内存以及 CPU 频率参数。这些步骤确保在系统启动后配置正确并且各项功能正常运行。

## 启动后框架概述

后启动脚本文件用于配置 Qualcomm Linux 分发版上的系统参数。

启动后设置通过 *systemd service* 文件进行管理。

有关 systemd 的更多信息，请参见 [systemd(1) - Linux manual page](https://www.man7.org/linux/man-pages/man1/systemd.1.html)。

## Systemd 服务概述

systemd 服务是指根据特定条件启动或停止的后台进程。

写入一个 `systemd service` 文件以允许 systemd 按照指示解析、理解和运行。

有关后台进程的更多信息，请参见 [background process](https://linuxhandbook.com/run-process-background/)。

## systemd 服务文件的基本结构

按以下步骤根据需要修改 systemd 的行为：

以下是 systemd 服务文件的示例：

[Unit]
    SourcePath=/etc/initscripts/log_restrict.sh
    Description=QTI logging service
    
    [Service]
    ExecStart=/etc/initscripts/post_boot.sh
    
    [Install]
    WantedBy=multi-user.target
    Copy to clipboard

### `[Unit]` 部分

在 systemd 中，单元是指系统知道如何操作和管理的任何资源。它包括服务、套接字、设备、挂载点和外部创建的进程组。

每个单元使用名为 `unit` 文件的配置文件定义，其中包含元数据和配置详细信息。Unit 文件中的 `[Unit]` 部分提供有关说明以及与其他单元关系的信息。

`[Unit]` 部分包含以下字段：

表：[Unit] 部分支持的字段

| 字段 | 说明 |
| --- | --- |
| Description<br>    Copy to clipboard | 可读的 systemd 服务标题。 |
| After<br>    Copy to clipboard | 设置对服务的依赖。例如，如果配置的是 WCN3960 Web 服务器，则希望服务器在网络联机后启动。 |
| Before<br>    Copy to clipboard | 在指定服务之前启动当前服务。在此示例中，WCN3960 Web 服务器在启动下一个云的服务之前运行，因为下一个云服务器依赖于 WCN3960 Web 服务器。 |

### `[Service]` 部分

`[Service]` 部分包含服务执行和终止的详细信息。

`[Service]` 部分包含以下字段：

> 
> 
> 表：[Service] 部分支持的字段
> 
> 
> | 字段 | 说明 |
> | --- | --- |
> | ExecStart<br>    Copy to clipboard | 服务启动时必须运行的命令。例如，启动 Web 服务器服务时必须执行的命令。 |
> | ExecReload<br>    Copy to clipboard | 可选字段，用于指定服务重启方式。对于执行磁盘 I/O 的服务，建议终止并重启服务。如果想要使用特定的重启机制，需使用 `ExecReload` 字段。 |
> | Type<br>    Copy to clipboard | 指示给定 systemd 服务的进程的启动类型。可用的选项有 `simple`、`exec`、`forking`、`oneshot`、`dbus`、`notify` 和 `idle`。 |

有关详细信息，参见 [Options](https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html?ref=linuxhandbook.com#Options)。

### `[Install]` 部分

`[Install]` 部分处理 systemd 服务或单元文件的安装。当运行启用或禁用服务的 `systemctl enable` 或 `systemctl disable` 命令时使用此方法。

`[Install]` 部分包含以下字段：

- `WantedBy`：类似于 [\[Unit\] 部分](https://docs.qualcomm.com/doc/80-70022-3SC/topic/post-boot-settings.html#kernelexternaldocumentation-the-unit-section)的 `After` 和 `Before` 字段。不过，该方法用于指定 systemd 等效的**运行级别**。

    `default.target` 用于系统初始化均完成并要求用户登录时。大多数面向用户的服务（如 WCN3960、cron、GNOME-stuff）都使用此目标。

    `shutdown.target` 是在设备关闭之前运行该服务。

    `multi-user.target` 表示在系统启动时以根用户身份运行服务。

    它既可以是目标设备，也可以是服务，例如 `network.target`。
- `RequiredBy`：此字段类似于 `WantedBy`。但是，该字段指定**硬依赖项**。如果依赖项运行失败，服务也会运行失败。

## 安装启动后 systemd 服务

启动后脚本文件位于 `meta-qcom-hwe` 层的核心配方。

将启动后脚本作为前一个 systemd 服务的一部分运行。所有启动后设置均托管在 `meta-qcom-hwe/recipes-core/initscripts/files/post_boot.sh` 脚本中。

#!/bin/sh
    # Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
    # SPDX-License-Identifier: BSD-3-Clause-Clear
    
    # Apply postboot settings
    Copy to clipboard

### 启动后 systemd 服务的配方

使用 `initscripts_1.0.bbappend` 文件了解安装 `post_boot` 脚本的配方配置过程。

在 `/etc/init.d` 中安装启动后 bash 脚本的配方在 `meta-qcom-hwe/recipes-core/initscripts/initscripts_1.0.bbappend` 文件中。

# postboot
    
    inherit systemd externalsrc
    
    FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
    SRC_URI:append = " \
       file://post_boot.sh \
       file://logging-restrictions.sh \
       file://log-restrict.service \
       file://post-boot.service \
       "
    
    do_install:append() {
       # postboot
       install -m 0755 ${WORKDIR}/post_boot.sh ${D}${sysconfdir}/initscripts/post_boot.sh
       install -m 0644 ${WORKDIR}/post-boot.service -D ${D}${systemd_unitdir}/system/post-boot.service
       ln -sf ${systemd_unitdir}/system/post-boot.service ${D}${systemd_unitdir}/system/multi-user.target.wants/post-boot.service
    }
    
    S = "${WORKDIR}"
    
    INITSCRIPT_PACKAGES =+ "${PN}-post-boot"
    INITSCRIPT_NAME:${PN}-post-boot = "post_boot.sh"
    
    PACKAGES =+ "${PN}-post-boot"
    FILES:${PN}-post-boot += "${systemd_unitdir}/system/post-boot.service ${systemd_unitdir}/system/multi-user.target.wants/post-boot.service ${sysconfdir}/initscripts/post_boot.sh"
    Copy to clipboard

Last Published: Nov 04, 2025

[Previous Topic
启动流程和架构概述](https://docs.qualcomm.com/bundle/publicresource/80-70022-3SC/topics/boot-flow-and-architecture-overview.md) [Next Topic
开发内核](https://docs.qualcomm.com/bundle/publicresource/80-70022-3SC/topics/kernel-development.md)