\system\core\init\README.md
Android Init Language
---------------------
The Android Init Language consists of five broad classes of statements:
Actions, Commands, Services, Options, and Imports.
Android Init语言由五大类语句组成:操作、命令、服务、选项和导入。
All of these are line-oriented, consisting of tokens separated by whitespace.
The c-style backslash escapes may be used to insert whitespace into a token.
Double quotes may also be used to prevent whitespace from breaking text into multiple tokens.
The backslash, when it is the last character on a line, may be used for line-folding.
所有这些都是面向行的,由空格分隔的标记组成。
c样式的反斜杠转义可用于将空白插入令牌。
双引号,也可用于防止,空白将文本分成多个标记。
当反斜杠是一行的最后一个字符时,可以用于折线。
Lines which start with a `#` (leading whitespace allowed) are comments.
以`#`(允许前导空格)开头的行是注释。
System properties can be expanded using the syntax `${property.name}`.
This also works in contexts where concatenation is required, such as `import /init.recovery.${ro.hardware}.rc`.
可以使用语法`${property.name}`扩展系统属性。
这也适用于需要连接的上下文,例如“import/init.recovery.${ro.hardware}.rc”。
Actions and Services implicitly declare a new section.
All commands or options belong to the section most recently declared.
Commands or options before the first section are ignored.
操作和服务隐式声明新的节。
所有命令或选项都属于最近声明的部分。
忽略第一节之前的命令或选项。
Services have unique names.
If a second Service is defined with the same name as an existing one,
it is ignored and an error message is logged.
服务有唯一的名称。
如果第二个服务被定义为,与现有的相同的名称,则忽略它并记录错误消息。
Init .rc Files
--------------
The init language is used in plain text files that take the .rc file extension.
There are typically multiple of these in multiple locations on the system, described below.
init语言用于扩展名为.rc的纯文本文件。
在系统的多个位置通常有多个这样的节点,如下所述。
/init.rc is the primary .rc file and is loaded by the init executable at the beginning of its execution.
It is responsible for the initial set up of the system.
/init.rc是主.rc文件,由init可执行文件,在其执行开始时加载。
它负责系统的初始设置。
Devices that mount /system, /vendor through the first stage mount mechanism load all of the files contained within the
/{system,vendor,odm}/etc/init/ directories immediately after loading the primary /init.rc.
This is explained in more details in the Imports section of this file.
通过第一阶段装载机制,装载/system、/vendor的设备,在加载primary/init.rc之后装载/{system,vendor,odm}/etc/init/目录。
在这个文件的Imports部分中有更详细的解释。
Legacy devices without the first stage mount mechanism do the following:
1. /init.rc imports /init.${ro.hardware}.rc which is the primary vendor supplied .rc file.
2. During the mount\_all command, the init executable loads all of the files contained within the /{system,vendor,odm}/etc/init/ directories.
These directories are intended for all Actions and Services used after file system mounting.
不带第一阶段装载机制的旧设备,执行以下操作:
1. /init.rc导入/init.${ro.hardware}.rc,它是供应商提供的主要.rc文件。
2. 在mount-all命令期间,init可执行文件加载/{system,vendor,odm}/etc/init/目录中包含的所有文件。
这些目录,用于安装文件系统后,所使用的所有操作和服务。
One may specify paths in the mount\_all command line to have it import .rc files at the specified paths instead of the default ones listed above.
This is primarily for supporting factory mode and other non-standard boot modes.
The three default paths should be used for the normal boot process.
您可以在mount \_all命令行中指定路径,让它在指定的路径导入.rc文件,而不是上面列出的默认路径。
这主要是为了支持工厂模式和其他非标准引导模式。
三个默认路径,应该用于正常引导过程。
The intention of these directories is:
1. /system/etc/init/ is for core system items such as SurfaceFlinger, MediaService, and logcatd.
2. /vendor/etc/init/ is for SoC vendor items such as actions or daemons needed for core SoC functionality.
3. /odm/etc/init/ is for device manufacturer items such as actions or daemons needed for motion sensor or other peripheral functionality.
这些目录的目的是:
1. /system/etc/init/用于核心系统项,如SurfaceFlinger、MediaService和logcatd。
2. /vendor/etc/init/用于SoC供应商项,例如核心SoC功能所需的操作或守护进程。
3. /odm/etc/init/用于设备制造商的项目,例如运动传感器或其他外围功能所需的操作或守护程序。
All services whose binaries reside on the system, vendor, or odm partitions should have their service entries placed into a corresponding init .rc file,
located in the /etc/init/ directory of the partition where they reside.
There is a build system macro, LOCAL\_INIT\_RC, that handles this for developers.
Each init .rc file should additionally contain any actions associated with its service.
其二进制文件位于系统、供应商或odm分区上的所有服务,都应将其服务条目放入对应的init.rc文件,位于它们所在的分区的/etc/init/目录中。
有一个构建系统宏LOCAL-INIT-RC,为开发人员处理这个问题。
每个init.rc文件,还应该包含与其服务相关联的任何操作。
An example is the logcatd.rc and Android.mk files located in the system/core/logcat directory.
The LOCAL\_INIT\_RC macro in the Android.mk file places logcatd.rc in /system/etc/init/ during the build process.
Init loads logcatd.rc during the mount\_all command and allows the service to be run and the action to be queued when appropriate.
一个例子是,位于system/core/logcat目录中的logcatd.rc和Android.mk文件。
在构建过程中,Android.mk文件中的LOCAL\_INIT\_RC宏,将logcatd.rc放在/system/etc/INIT/中。
在mount \_all命令期间,Init加载logcatd.rc,并允许在适当时运行服务和队列操作。
This break up of init .rc files according to their daemon is preferred to the previously used monolithic init .rc files.
This approach ensures that the only service entries that init reads and the only actions that init performs correspond to services
whose binaries are in fact present on the file system, which was not the case with the monolithic init .rc files.
This additionally will aid in merge conflict resolution when multiple services are added to the system, as each one will go into a separate file.
与以前使用的整个init.rc文件相比,这种根据守护进程对init.rc文件的分解更为可取。
这种方法确保,init读取的唯一服务项和init执行的唯一操作,对应于文件系统中实际存在二进制文件的服务,
而对于独个init.rc文件则不是这样。
另外,当向系统添加多个服务时,这将有助于合并冲突的解决,因为每个服务,都将进入一个单独的文件。
There are two options "early" and "late" in mount\_all command which can be set after optional paths.
With "--early" set, the init executable will skip mounting entries with "latemount" flag and triggering fs encryption state event.
With "--late" set, init executable will only mount entries with "latemount" flag but skip importing rc files.
By default, no option is set, and mount\_all will process all entries in the given fstab.
mount\_all命令中,有两个选项“early”和“late”,可以在可选路径之后设置。
设置“--early”后,init可执行文件,将跳过带有“latemount”标志的装入条目,并触发fs encryption state事件。
若设置了“--late”,init可执行文件,将只装载带有“latemount”标志的条目,而跳过导入rc文件。
默认情况下,未设置任何选项,mount\_all将处理给定fstab中的所有条目。
Actions
-------
Actions are named sequences of commands.
Actions have a trigger which is used to determine when the action is executed.
When an event occurs which matches an action's trigger, that action is added to the tail of a to-be-executed queue
(unless it is already on the queue).
动作被命名为命令序列。
操作有一个触发器,用于确定何时执行操作。
当发生与某个操作的触发器匹配的事件时,该操作将被添加到待执行队列的尾部(除非它已经在排队)。
Each action in the queue is dequeued in sequence and each command in that action is executed in sequence.
Init handles other activities(device creation/destruction, property setting, process restarting) "between" the execution of the commands in activities.
队列中的每个操作,按顺序出列,该操作中的每个命令,按顺序执行。
Init处理“活动”命令执行之间的其他活动(设备创建/销毁、属性设置、进程重新启动)。
动作采取的格式:
Actions take the form of:
on <trigger> [&& <trigger>]*
<command>
<command>
<command>
Actions are added to the queue and executed based on the order that the file that contains them was parsed (see the Imports section),
then sequentially within an individual file.
动作将添加到队列中,并根据包含这些动作的文件的解析顺序执行(请参阅导入部分),
然后在单个文件中按顺序进行。
For example if a file contains:
例如,如果文件包含如下:
启动时
on boot
setprop a 1
setprop b 2
on boot && property:true=true
setprop c 1
setprop d 2
on boot
setprop e 1
setprop f 2
Then when the `boot` trigger occurs and assuming the property `true` equals `true`, then the order of the commands executed will be:
然后,当“boot”触发器发生触发,并假定属性“true”等于“true”时,执行命令的顺序将为:
setprop a 1
setprop b 2
setprop c 1
setprop d 2
setprop e 1
setprop f 2
Services
--------
Services are programs which init launches and (optionally) restarts when they exit.
Services(服务)是程序,他在初始化时启动,并在退出时 重启(可选)。
Services take the form of:
Services(服务)采取的格式如下:
service <name> <pathname> [ <argument> ]*
<option>
<option>
...
Options
-------
Options are modifiers to services.
They affect how and when init runs the service.
Options(选项)是一个Services(服务)的修正者。
他们影响Services(服务)在何时,并以何种方式运行。
`console [<console>]`
> This service needs a console. The optional second parameter chooses a specific console instead of the default.
The default "/dev/console" can be changed by setting the "androidboot.console" kernel parameter.
In all cases the leading "/dev/" should be omitted, so "/dev/tty0" would be specified as just "console tty0".
>此服务需要一个控制台。可选的第二个参数选择特定控制台而不是默认控制台。
默认“/dev/console”可以通过设置“androidboot.console”内核参数进行更改。
在前面的“/dev/”应该省略,因此“/dev/tty0”将是仅指定为“控制台tty0”。
`critical`
> This is a device-critical service.
If it exits more than four times in four minutes, the device will reboot into recovery mode.
这是一项设备关键服务。如果超过四次四分钟后,设备将重新启动到恢复模式。
`disabled`
> This service will not automatically start with its class.
It must be explicitly started by name.
此服务不会自动从其类启动。它必须按名称显式启动。
`setenv <name> <value>`
> Set the environment variable _name_ to _value_ in the launched process.
在启动的进程中将环境变量“name”设置为“value”。
`socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]`
> Create a unix domain socket named /dev/socket/_name_ and pass its fd to the launched process.
_type_ must be "dgram", "stream" or "seqpacket".
User and group default to 0.
'seclabel' is the SELinux security context for the socket.
It defaults to the service security context,
as specified by seclabel or computed based on the service executable file security context.
For native executables see libcutils android\_get\_control\_socket().
创建一个名为/dev/socket/_name_的unix域套接字,并将其fd传递给启动进程。
_type_必须是“dgram”、“stream”或“seqpacket”。
用户和组默认值为0。
“seclabel”是套接字的SELinux安全上下文。
它默认为服务安全上下文,由seclabel指定或基于服务可执行文件安全上下文计算。
有关本机可执行文件,请参见libcutils android \_get\_control\_socket()。
`enter_namespace <type> <path>`
> Enters the namespace of type _type_ located at _path_.
Only network namespaces are supported with _type_ set to "net".
Note that only one namespace of a given _type_ may be entered.
输入位于 _path_的类型“_type_ ”为的命名空间。
只有网络命名空间才受支持,且“_type_ ”设置为“net”。
请注意,只能输入给定类型的一个命名空间。
`file <path> <type>`
> Open a file path and pass its fd to the launched process.
_type_ must be "r", "w" or "rw".
For native executables see libcutils android\_get\_control\_file().
打开文件路径并将其fd传递给启动的进程。
_type_类型必须是“r”、“w”或“rw”。
有关本机可执行文件,请参见libcutils android \_get\_control\_file()。
`user <username>`
> Change to 'username' before exec'ing this service.
Currently defaults to root. (??? probably should default to nobody)
As of Android M, processes should use this option even if they require Linux capabilities.
Previously, to acquire Linux capabilities, a process would need to run as root, request the capabilities, then drop to its desired uid.
There is a new mechanism through fs\_config that allows device manufacturers to add Linux capabilities to specific binaries on a file system that should be used instead.
This mechanism is described on <http://source.android.com/devices/tech/config/filesystem.html>.
When using this new mechanism, processes can use the user option to select their desired uid without ever running as root.
As of Android O, processes can also request capabilities directly in their .rc files.
See the "capabilities" option below.
在执行此服务之前更改为“用户名”。
当前默认为root。(?? 可能应该默认为任何人)
从Android M开始,进程应该使用这个选项,即使它们需要Linux功能。
以前,要获得Linux功能,进程需要以root用户身份运行,请求这些功能,然后降级到所需的uid。
这是一种通过fs\_config的新机制,允许设备制造商将Linux功能添加到文件系统上的特定二进制文件中,而这些二进制文件应该被使用。
这个机制在<http://source.android.com/devices/tech/config/filesystem.html>中有描述。
当使用这个新机制时,进程可以使用用户选项来选择所需的uid,而不必以根用户身份运行。
对于Android O,进程也可以直接在其.rc文件中请求功能。
请参阅下面的“功能”选项。
`group <groupname> [ <groupname>\* ]`
> Change to 'groupname' before exec'ing this service.
Additional groupnames beyond the (required) first one are used to set the supplemental groups of the process (via setgroups()).
Currently defaults to root. (??? probably should default to nobody)
在执行此服务之前,更改为“groupname”。
除了(必需的)第一个组名之外的,其他组名用于设置进程的补充组(通过set groups())。
当前默认为root。(?? 可能应该默认为任何人)
`capabilities <capability> [ <capability>\* ]`
> Set capabilities when exec'ing this service.
'capability' should be a Linux capability without the "CAP\_" prefix, like "NET\_ADMIN" or "SETPCAP".
See http://man7.org/linux/man-pages/man7/capabilities.7.html for a list of Linux capabilities.
执行此服务时设置功能。
“capability”应该是一个没有“CAP”前缀的Linux功能,比如“NET ADMIN”或“SETPCAP”。
有关linux功能的列表,请参见http://man7.org/linux/man-pages/man7/capabilities.7.html。
`setrlimit <resource> <cur> <max>`
> This applies the given rlimit to the service. rlimits are inherited by child processes,
so this effectively applies the given rlimit to the process tree started by this service.
It is parsed similarly to the setrlimit command specified below.
这将把给定的rlimit应用于服务。rlimit是由子进程继承的,
因此,这有效的将给定的rlimit,应用于由该服务启动的进程树。
它的解析类似于下面指定的setrlimit命令。
`seclabel <seclabel>`
> Change to 'seclabel' before exec'ing this service.
Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
Services on the system partition can instead use policy-defined transitions based on their file security context.
If not specified and no transition is defined in policy, defaults to the init context.
在执行此服务之前更改为“seclabel”。
主要用于从rootfs运行的服务,例如ueventd、adbd。
系统分区上的服务,可以使用基于其文件安全上下文的策略定义的转换。
如果未指定且策略中未定义转换,则默认为init上下文。
`oneshot`
> Do not restart the service when it exits.
退出时不要重新启动服务。
`class <name> [ <name>\* ]`
> Specify class names for the service. All services in a named class may be started or stopped together.
A service is in the class "default" if one is not specified via the class option.
Additional classnames beyond the (required) first one are used to group services.
指定服务的类名。命名类中的所有服务,可以一起启动或停止。
如果未通过类选项指定服务,则该服务处于类“默认”中。
除了(必需的)第一个类名之外的其他类名,用于对服务进行分组。
`animation class`
> 'animation' class should include all services necessary for both boot animation and shutdown animation.
As these services can be launched very early during bootup and can run until the last stage of shutdown, access to /data partition is not guaranteed.
These services can check files under /data but it should not keep files opened and should work when /data is not available.
“animation”类,应该包括启动动画和关闭动画所需的所有服务。
由于这些服务,可以在启动期间很早启动,并且可以一直运行到关闭的最后阶段,因此无法保证对/数据分区的访问。
这些服务可以检查/data下的文件,但不应保持文件处于打开状态,并且应在/data不可用时工作。
`onrestart`
> Execute a Command (see below) when service restarts.
当服务重新启动时执行命令(见下文)。
`writepid <file> [ <file>\* ]`
> Write the child's pid to the given files when it forks. Meant for cgroup/cpuset usage.
If no files under /dev/cpuset/ are specified,
but the system property 'ro.cpuset.default' is set to a non-empty cpuset name (e.g.'/foreground'),
then the pid is written to file /dev/cpuset/_cpuset\_name_/tasks.
当子进程分叉时,将其pid写入给定的文件。用于cgroup/cpuset用法。
如果未指定/dev/cpuset/下的文件,
但系统属性“ro.cpuset.default”设置为非空的cpuset名称(例如“/foreground”),
然后将pid写入文件/dev/cpuset/_cpuset\_name_/tasks。
`priority <priority>`
> Scheduling priority of the service process.
This value has to be in range -20 to 19.
Default priority is 0. Priority is set via setpriority().
服务进程的调度优先级。
这个值必须在-20到19之间。
默认优先级为0。通过setpriority()设置优先级。
`namespace <pid|mnt>`
> Enter a new PID or mount namespace when forking the service.
在分叉服务时输入新的PID或mount命名空间。
`oom_score_adjust <value>`
> Sets the child's /proc/self/oom\_score\_adj to the specified value,
which must range from -1000 to 1000.
将子项的/proc/self/oom\u score\u adj设置为指定值,
必须在-1000到1000之间。
`memcg.swappiness <value>`
> Sets the child's memory.swappiness to the specified value (only if memcg is mounted),
which must be equal or greater than 0.
将子级的memory.swappiness设置为指定值(仅当memcg已装入时),
必须等于或大于0。
`memcg.soft_limit_in_bytes <value>`
> Sets the child's memory.soft_limit_in_bytes to the specified value (only if memcg is mounted),
which must be equal or greater than 0.
将子级的memory.soft_limit_(以字节为单位)设置为指定值(仅当memcg已装入时),
必须等于或大于0。
`memcg.limit_in_bytes <value>`
> Sets the child's memory.limit_in_bytes to the specified value (only if memcg is mounted),
which must be equal or greater than 0.
将子级的memory.limit_字节设置为指定值(仅当memcg已装入时),
必须等于或大于0。
`shutdown <shutdown_behavior>`
> Set shutdown behavior of the service process.
When this is not specified,the service is killed during shutdown process by using SIGTERM and SIGKILL.
The service with shutdown_behavior of "critical" is not killed during shutdown until shutdown times out.
When shutdown times out, even services tagged with "shutdown critical" will be killed.
When the service tagged with "shutdown critical" is not running when shut down starts, it will be started.
设置服务进程的关闭行为。
如果未指定,则在关机过程中使用SIGTERM和SIGKILL终止服务。
在关机期间,关机行为为“严重”的服务,不会被终止,直到关机超时。
当关闭超时时,即使标记为“关闭关键”的服务,也将被终止。
当标记为“shut down critical”的服务,在shutdown启动时未运行时,它将启动。
Triggers
--------
Triggers are strings which can be used to match certain kinds of events and used to cause an action to occur.
Triggers are subdivided into event triggers and property triggers.
触发器是字符串,可用于匹配某些类型的事件,用于导致操作发生。
触发器分为事件触发器和属性触发器。
Event triggers are strings triggered by the 'trigger' command or by the QueueEventTrigger() function within the init executable.
These take the form of a simple string such as 'boot' or 'late-init'.
事件触发器是由“trigger”命令或init可执行文件中的QueueEventTrigger()函数触发的字符串。
它们采用简单字符串的形式,如“boot”或“late init”。
Property triggers are strings triggered when a named property changes value to a given new value or
when a named property changes value to any new value.
属性触发器,是当命名属性将值更改为给定的新值,或当命名属性将值更改为任何新值时。
These take the form of 'property:<name>=<value>' and 'property:<name>=\*' respectively.
它们分别采用“property:<name>=<value>”和“property:<name>=\*”的形式。
Property triggers are additionally evaluated and triggered accordingly during the initial boot phase of init.
属性触发器在init的初始启动阶段进行额外的计算并相应地触发。
An Action can have multiple property triggers but may only have one event trigger.
一个操作可以有多个属性触发器,但只能有一个事件触发器。
For example:
`on boot && property:a=b`
defines an action that is only executed when the 'boot' event trigger happens and the property a equals b.
例如:
`启动时属性:a=b`
定义仅在“启动”事件触发器发生,且属性a等于b时执行的操作。
`on property:a=b && property:c=d`
defines an action that is executed at three times:
1. During initial boot if property a=b and property c=d.
2. Any time that property a transitions to value b, while property c already equals d.
3. Any time that property c transitions to value d, while property a already equals b.
定义三次执行的操作:
1. 在初始引导期间,如果属性a=b和属性c=d。
2. 当属性a转换为值b时,而属性c已经等于d。
3. 当属性c转换为值d时,而属性a已经等于b。
Commands
--------
`bootchart [start|stop]`
> Start/stop bootcharting.
These are present in the default init.rc files,
but bootcharting is only active if the file /data/bootchart/enabled exists;
otherwise bootchart start/stop are no-ops.
启动/停止引导图表。
它们出现在默认的init.rc文件中,
但是,如果存在文件/数据/引导程序/启用,BooCcTcT仅是激活的;
否则,启动/停止引导图不起作用。
`chmod <octal-mode> <path>`
> Change file access permissions.
更改文件访问权限。
`chown <owner> <group> <path>`
> Change file owner and group.
更改文件所有者和组。
`class_start <serviceclass>`
> Start all services of the specified class if they are not already running.
See the start entry for more information on starting services.
如果指定类的所有服务尚未运行,请启动它们。
有关启动服务的详细信息,请参阅“开始”项。
`class_stop <serviceclass>`
> Stop and disable all services of the specified class if they are currently running.
停止并禁用指定类的所有服务(如果它们当前正在运行)。
`class_reset <serviceclass>`
> Stop all services of the specified class if they are currently running,
without disabling them. They can be restarted later using `class_start`.
如果指定类的所有服务当前正在运行,则停止这些服务,而不禁用它们。
以后可以使用“class_start”重新启动它们。
`class_restart <serviceclass>`
> Restarts all services of the specified class.
重新启动指定类的所有服务。
`copy <src> <dst>`
> Copies a file. Similar to write, but useful for binary/large amounts of data.
Regarding to the src file, copying from symbolic link file and world-writable or group-writable files are not allowed.
Regarding to the dst file, the default mode created is 0600 if it does not exist.
And it will be truncated if dst file is a normal regular file and already exists.
复制文件。类似于写入,但对二进制/大量数据有用。
对于src文件,不允许从符号链接文件,和全局可写或组可写文件,进行复制。
关于DST文件,如果不存在,则创建的默认模式为0600。
如果DST文件,是一个正常的普通文件,并且已经存在,它将被截断。
`domainname <name>`
> Set the domain name.
设置域名。
`enable <servicename>`
> Turns a disabled service into an enabled one as if the service did not specify disabled.
If the service is supposed to be running, it will be started now.
Typically used when the bootloader sets a variable that indicates a specific service should be started when needed. E.g.
将已禁用的服务,转换为已启用的服务,就像该服务未指定“已禁用”一样。
如果服务需要即将运行,它将立即启动。
通常在bootloader设置变量时使用,该变量指示在需要时应启动特定服务。
例如:
on property:ro.boot.myfancyhardware=1
enable my_fancy_service_for_my_fancy_hardware
`exec [ <seclabel> [ <user> [ <group>\* ] ] ] -- <command> [ <argument>\* ]`
> Fork and execute command with the given arguments.
The command starts after "--" so that an optional security context, user, and supplementary groups can be provided.
No other commands will be run until this one finishes.
_seclabel_ can be a - to denote default.
Properties are expanded within _argument_.
Init halts executing commands until the forked process exits.
使用给定参数执行Fork和execute命令。
该命令在“-”之后启动,以便可以提供可选的安全上下文、用户和补充组。
在此命令完成之前,不会运行其他命令。
_seclabel可以是-表示默认值。
属性在"_argument_"中展开。
init停止执行命令,直到已forked进程退出。
`exec_background [ <seclabel> [ <user> [ <group>\* ] ] ] -- <command> [ <argument>\* ]`
> Fork and execute command with the given arguments.
This is handled similarly to the `exec` command.
The difference is that init does not halt executing commands until the process exits for `exec_background`.
使用给定参数执行Fork和execute命令。
这与“exec”命令的处理方式类似。
不同之处在于,在进程退出“exec_background”之前,init不会停止执行命令。
`exec_start <service>`
> Start a given service and halt the processing of additional init commands until it returns.
The command functions similarly to the `exec` command,
but uses an existing service definition in place of the exec argument vector.
启动给定的服务,并停止处理其他init命令,直到它返回。
命令的功能类似于“exec”命令,
但是使用现有的服务定义,代替Exchange参数向量。
`export <name> <value>`
> Set the environment variable _name_ equal to _value_ in the global environment
(which will be inherited by all processes started after this command is executed)
在全局环境中,将环境变量“name”设置为“value”
(将由执行此命令后,启动的所有进程继承)
`hostname <name>`
> Set the host name.
设置主机名。
`ifup <interface>`
> Bring the network interface _interface_ online.
使网络接口_interface_ online 启动联机
`insmod [-f] <path> [<options>]`
> Install the module at _path_ with the specified options.
-f: force installation of the module even if the version of the running kernel
and the version of the kernel for which the module was compiled do not match.
使用指定的选项,在路径处安装模块。
-f: 强制安装模块,即使运行的内核版本
并且编译模块的内核版本不匹配。
`load_all_props`
> Loads properties from /system, /vendor, et cetera.
This is included in the default init.rc.
从/system、/vendor等处加载属性。
这包含在默认的init.rc中。
`load_persist_props`
> Loads persistent properties when /data has been decrypted.
This is included in the default init.rc.
解密/数据后加载持久性属性。
这包含在默认的init.rc中。
`loglevel <level>`
> Sets the kernel log level to level. Properties are expanded within _level_.
将内核日志级别设置为级别。属性在_level_级别内展开。
`mkdir <path> [mode] [owner] [group]`
> Create a directory at _path_, optionally with the given mode, owner, and group.
If not provided, the directory is created with permissions 755 and owned by the root user and root group.
If provided, the mode, owner and group will be updated if the directory exists already.
在路径创建目录,可以选择使用给定的模式、所有者和组。
如果未提供,则使用权限755创建目录,该目录归根用户和根组所有。
如果提供,则如果目录已经存在,则将更新模式、所有者和组。
`mount_all <fstab> [ <path> ]\* [--<option>]`
> Calls fs\_mgr\_mount\_all on the given fs\_mgr-format fstab and imports .rc files at the specified paths
(e.g., on the partitions just mounted) with optional options "early" and "late".
Refer to the section of "Init .rc Files" for detail.
在给定的fs\_mgr-format fstab上调用fs\_mgr\_mount\_all,并在指定路径导入.rc文件
(例如,在刚刚安装的分区上)带有可选的“early”和“late”。
有关详细信息,请参阅“Init.rc文件”一节。
`mount <type> <device> <dir> [ <flag>\* ] [<options>]`
> Attempt to mount the named device at the directory _dir_
_flag_s include "ro", "rw", "remount", "noatime", ...
_options_ include "barrier=1", "noauto\_da\_alloc", "discard", ...
as a comma separated string, eg: barrier=1,noauto\_da\_alloc
尝试在 _dir_目录下,挂载命名设备
_标志包括“ro”、“rw”、“remount”、“noatime”。。。
_选项包括“barrier=1”、“noauto-da-alloc”、“discard”。。。
用逗号分隔字符串,例如:barrier=1,noauto-da-alloc
`restart <service>`
> Stops and restarts a running service,
does nothing if the service is currently restarting, otherwise, it just starts the service.
停止并重新启动正在运行的服务,如果服务当前正在重新启动,则不执行任何操作,否则,它只是启动服务。
`restorecon <path> [ <path>\* ]`
> Restore the file named by _path_ to the security context specified in the file\_contexts configuration.
Not required for directories created by the init.rc as these are automatically labeled correctly by init.
将由路径_path_ 命名的文件,还原到文件上下文配置中,指定的安全上下文。
由init.rc创建的目录不需要,因为它们是由init自动正确标记。
`restorecon_recursive <path> [ <path>\* ]`
> Recursively restore the directory tree named by _path_ to the security contexts specified in the file\_contexts configuration.
递归地将由路径_path_ 命名的目录树,还原到在文件上下文配置中,指定的安全上下文。
`rm <path>`
> Calls unlink(2) on the given path. You might want to
use "exec -- rm ..." instead (provided the system partition is already mounted).
调用给定路径上的取消链接unlink(2) 。
您可能希望改用“exec--rm…”(前提是系统分区已经挂载)。
`rmdir <path>`
> Calls rmdir(2) on the given path.
在给定路径上调用rmdir(2)。
`readahead <file|dir> [--fully]`
> Calls readahead(2) on the file or files within given directory.
Use option --fully to read the full file content.
对给定目录中的一个或多个文件,调用readahead(2)。
使用option--fully读取完整的文件内容。
`setprop <name> <value>`
> Set system property _name_ to _value_. Properties are expanded within _value_.
将系统属性“_name_”设置为“_value_”。
属性在_name_值内展开。
`setrlimit <resource> <cur> <max>`
> Set the rlimit for a resource.
This applies to all processes launched after the limit is set.
It is intended to be set early in init and applied globally.
_resource_ is best specified using its text representation ('cpu', 'rtio', etc or 'RLIM_CPU', 'RLIM_RTIO', etc).
It also may be specified as the int value that the resource enum corresponds to.
为资源设置rlimit。
这适用于设置限制后,启动的所有进程。
它打算在init的早期设置并全局应用。
_resource_ 资源,最好使用其文本表示(“cpu”、“rtio”等或“RLIM-cpu”、“RLIM-rtio”等)来指定。
它也可以指定为,资源枚举对应的int值。
`start <service>`
> Start a service running if it is not already running.
Note that this is _not_ synchronous, and even if it were,
there is no guarantee that the operating system's scheduler will execute the service sufficiently to guarantee anything about the service's status.
如果服务尚未运行,请启动该服务。
注意这不是同步的,即使是,
无法保证操作系统的调度程序,将充分执行服务,以保证有关服务状态的任何内容。
> This creates an important consequence that if the service offers functionality to other services, such as providing a communication channel,
simply starting this service before those services is _not_ sufficient to guarantee that the channel has been set up before those services ask for it.
There must be a separate mechanism to make any such guarantees.
这就产生了一个重要的结果,如果服务向其他服务提供功能,例如提供通信信道,
仅在这些服务之前启动此服务,不足以保证,在这些服务请求之前,已设置频道。
必须有一个单独的机制,来作出这种保证。
`stop <service>`
> Stop a service from running if it is currently running.
如果服务当前正在运行,请停止其运行。
`swapon_all <fstab>`
> Calls fs\_mgr\_swapon\_all on the given fstab file.
在给定的fstab文件上,调用fs\_mgr\_swapon\_all。
`symlink <target> <path>`
> Create a symbolic link at _path_ with the value _target_
用目标值_target_在路径_path_ 创建符号链接
`sysclktz <mins_west_of_gmt>`
> Set the system clock base (0 if system clock ticks in GMT)
设置系统时钟基数(如果系统时钟以GMT计时,则为0)
格林尼治标准时间
`trigger <event>`
> Trigger an event. Used to queue an action from another action.
触发事件。
用于将一个操作与另一个动作队列。
`umount <path>`
> Unmount the filesystem mounted at that path.
卸载在该路径上装载的文件系统。
`verity_load_state`
> Internal implementation detail used to load dm-verity state.
用于加载dm-verity状态的内部实现详细信息。
`verity_update_state <mount-point>`
> Internal implementation detail used to update dm-verity state and set the partition._mount-point_.
verified properties used by adb remount because fs\_mgr can't set them directly itself.
内部实现详细信息,用于更新dm verity state并设置adb remount使用的分区装入点验证属性,因为fs\_mgr不能直接自己设置这些属性。
`wait <path> [ <timeout> ]`
> Poll for the existence of the given file and return when found, or the timeout has been reached.
If timeout is not specified it currently defaults to five seconds.
对给定文件的存在进行投票,并在找到时返回,或已达到超时。
如果未指定超时,则当前默认为5秒。
`wait_for_prop <name> <value>`
> Wait for system property _name_ to be _value_. Properties are expanded within _value_.
If property _name_ is already set to _value_, continue immediately.
等待系统属性“名称”为“值”。属性在值内展开。
如果属性名称已设置为值,请立即继续。
`write <path> <content>`
> Open the file at _path_ and write a string to it with write(2).
If the file does not exist, it will be created.
If it does exist, it will be truncated. Properties are expanded within _content_.
打开路径为的文件,并用write(2)向其写入字符串。
如果文件不存在,它将被创建。
如果它确实存在,它将被截断。属性在内容中展开。
Imports
-------
`import <path>`
> Parse an init config file, extending the current configuration.
If _path_ is a directory, each file in the directory is parsed as a config file.
It is not recursive, nested directories will not be parsed.
解析init配置文件,扩展当前配置。
如果路径是一个目录,则该目录中的每个文件,都被解析为配置文件。
它不是递归的,嵌套的目录将不会被解析。
The import keyword is not a command, but rather its own section,meaning that it does not happen as part of an Action,
but rather,imports are handled as a file is being parsed and follow the below logic.
import关键字不是命令,而是它自己的部分,这意味着它不会作为Action的一部分发生,
但是,import是作为正在解析的文件处理的,并遵循以下逻辑。
There are only three times where the init executable imports .rc files:
只有三次init可执行文件导入.rc文件:
1. When it imports /init.rc or the script indicated by the property `ro.boot.init_rc` during initial boot.
2. When it imports /{system,vendor,odm}/etc/init/ for first stage mount devices immediately after importing /init.rc.
3. When it imports /{system,vendor,odm}/etc/init/ or .rc files at specified paths during mount_all.
1.当它在初始启动时导入/init.rc或属性“ro.boot.init_rc”指示的脚本时。
2.当它在导入/init.rc之后立即导入/{system,vendor,odm}/etc/init/用于第一级装载设备时。
3.当它在mount all期间在指定路径导入/{system,vendor,odm}/etc/init/或.rc文件时。
The order that files are imported is a bit complex for legacy reasons and to keep backwards compatibility.
It is not strictly guaranteed.
由于传统原因和保持向后兼容性,导入文件的顺序有点复杂。
没有严格的保证。
The only correct way to guarantee that a command has been run before a different command is to either
1) place it in an Action with an earlier executed trigger, or
2) place it in an Action with the same trigger within the same file at an earlier line.
保证命令,在另一个命令之前,运行的唯一正确方法是
1) 将它放在,一个具有先前执行的,触发器的操作中,或者
2) 将它放在,前面一行的,同一文件中,具有相同触发器的操作中。
Nonetheless, the defacto order for first stage mount devices is:
1. /init.rc is parsed then recursively each of its imports are parsed.
2. The contents of /system/etc/init/ are alphabetized and parsed sequentially,
with imports happening recursively after each file is parsed.
3. Step 2 is repeated for /vendor/etc/init then /odm/etc/init
尽管如此,第一阶段安装设备的实际顺序是:
1. /解析init.rc,然后递归地解析其每个导入。
2. /system/etc/init/的内容按字母顺序排列和分析,在解析每个文件之后递归地进行导入。
3. 对/vendor/etc/init然后/odm/etc/init重复步骤2
The below pseudocode may explain this more clearly:
下面的伪代码可以更清楚地解释这一点:
fn Import(file)
Parse(file)
for (import : file.imports)
Import(import)
Import(/init.rc)
Directories = [/system/etc/init, /vendor/etc/init, /odm/etc/init]
for (directory : Directories)
files = <Alphabetical order of directory's contents>
for (file : files)
Import(file)
fn导入(文件)
解析(文件)
for(导入:file.imports)
导入(导入)
导入(/init.rc)
目录=[/system/etc/init,/vendor/etc/init,/odm/etc/init]
for(目录:目录)
files=<目录内容的字母顺序>
for(文件:文件)
导入(文件)
Properties
----------
Init provides information about the services that it is responsible for via the below properties.
Init通过以下属性,提供有关它负责的服务的信息。
`init.svc.<name>`
> State of a named service ("stopped", "stopping", "running", "restarting")
命名服务的状态(“已停止”、“正在停止”、“正在运行”、“正在重新启动”)
Boot timing
-----------
Init records some boot timing information in system properties.
Init在系统属性中,记录一些启动计时信息。
`ro.boottime.init`
> Time after boot in ns (via the CLOCK\_BOOTTIME clock) at which the first stage of init started.
在ns中启动后的时间(通过时钟启动时间时钟),初始化的第一阶段在该时间开始。
`ro.boottime.init.selinux`
> How long it took the first stage to initialize SELinux.
第一阶段初始化SELinux花了多长时间。
`ro.boottime.init.cold_boot_wait`
> How long init waited for ueventd's coldboot phase to end.
init等待ueventd的coldboot阶段结束的时间。
`ro.boottime.<service-name>`
> Time after boot in ns (via the CLOCK\_BOOTTIME clock) that the service was first started.
在ns中启动后(通过时钟启动时间时钟)服务首次启动的时间。
Bootcharting
------------
This version of init contains code to perform "bootcharting":
generating log files that can be later processed by the tools provided by <http://www.bootchart.org/>.
此版本的init包含执行“引导图表”的代码:
生成可由<http://www.bootchart.org/>提供的工具稍后处理的日志文件。
On the emulator, use the -bootchart _timeout_ option to boot with bootcharting activated for _timeout_ seconds.
在emulator上,使用-bootchart\u timeout_u选项,在启动引导图表的情况下,启动超时_u秒。
On a device:
adb shell 'touch /data/bootchart/enabled'
Don't forget to delete this file when you're done collecting data!
The log files are written to /data/bootchart/.
收集完数据后,不要忘记删除此文件!
日志文件,被写入/data/bootchart/。
A script is provided to retrieve them and create a bootchart.tgz file that can be used with the
bootchart command-line utility:
提供了一个脚本,来检索它们,并创建可与bootchart命令行实用程序,一起使用的bootchart.tgz文件:
sudo apt-get install pybootchartgui
# grab-bootchart.sh uses $ANDROID_SERIAL.
$ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
One thing to watch for is that the bootchart will show init as if it started running at 0s.
You'll have to look at dmesg to work out when the kernel actually started init.
需要注意的一点是,引导图表将显示init,就像它在0点开始运行一样。
您必须查看dmesg,才能计算出内核何时真正启动init。
Comparing two bootcharts
比较两个引导图
------------------------
A handy script named compare-bootcharts.py can be used to compare the start/end time of selected processes.
The aforementioned grab-bootchart.sh will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart.
If two such barballs are preserved on the host machine under different directories,
the script can list the timestamps differences. For example:
一个名为compare-bootcharts.py的方便脚本,可以用来比较所选进程的开始/结束时间。
前面提到的grab-bootchart.sh将在/tmp/android bootchart上,留下一个名为bootchart.tgz的bootchart tarball。
如果两个这样的barballs被保存在主机上的不同目录下,
脚本可以列出时间戳差异。例如:
Usage: system/core/init/compare-bootcharts.py _base-bootchart-dir_ _exp-bootchart-dir_
process: baseline experiment (delta) - Unit is ms (a jiffy is 10 ms on the system)
------------------------------------
/init: 50 40 (-10)
/system/bin/surfaceflinger: 4320 4470 (+150)
/system/bin/bootanimation: 6980 6990 (+10)
zygote64: 10410 10640 (+230)
zygote: 10410 10640 (+230)
system_server: 15350 15150 (-200)
bootanimation ends at: 33790 31230 (-2560)
Systrace
--------
Systrace (<http://developer.android.com/tools/help/systrace.html>) can be
used for obtaining performance analysis reports during boot time on userdebug or eng builds.
可以用于在用户调试,或工程生成的启动期间,获取性能分析报告。
Here is an example of trace events of "wm" and "am" categories:
以下是“wm”和“am”类别的跟踪事件示例:
$ANDROID_BUILD_TOP/external/chromium-trace/systrace.py \
wm am --boot
This command will cause the device to reboot.
After the device is rebooted and the boot sequence has finished,
the trace report is obtained from the device and written as trace.html on the host by hitting Ctrl+C.
此命令,将导致设备重新启动。
设备重新启动,并完成引导序列后,
跟踪报告,是从设备获取的,并通过按Ctrl+C,在主机上以trace.html形式写入。
Limitation: recording trace events is started after persistent properties are loaded,
so the trace events that are emitted before that are not recorded.
Several services such as vold, surfaceflinger,
and servicemanager are affected by this limitation since they are started before persistent properties are loaded.
Zygote initialization and the processes that are forked from the zygote are not affected.
限制:记录跟踪事件,是在加载 持久性属性 之后,启动的,
所以之前发出的跟踪事件,不会被记录。
几项服务,如vold,surfaceflinger,
和servicemanager受此限制的影响,因为它们是,在加载持久性属性之前,启动的。
Zygote初始化和从Zygote分叉的进程不受影响。
Debugging init
--------------
By default, programs executed by init will drop stdout and stderr into /dev/null.
To help with debugging, you can execute your program via the Android program logwrapper.
This will redirect stdout/stderr into the Android logging system (accessed via logcat).
默认情况下,init执行的程序,会将stdout和stderr放入/dev/null。
为了帮助调试,可以通过Android程序日志包装器,执行程序。
这将把stdout/stderr,重定向到Android日志系统(通过logcat访问)。
For example
service akmd /system/bin/logwrapper /sbin/akmd
For quicker turnaround when working on init itself, use:
为了在处理init本身时更快地周转,请使用:
mm -j &&
m ramdisk-nodeps &&
m bootimage-nodeps &&
adb reboot bootloader &&
fastboot boot $ANDROID_PRODUCT_OUT/boot.img
Alternatively, use the emulator:
或者,使用模拟器:
emulator -partition-size 1024 \
-verbose -show-kernel -no-window
本文详细介绍了Android Init语言的组成部分,包括操作、命令、服务、选项和导入。讲解了Init.rc文件的结构、如何处理系统启动和服务管理。同时,探讨了触发器、属性和命令的使用,以及系统属性和启动时间的记录。
504

被折叠的 条评论
为什么被折叠?



