嵌入式Linux設備如何添加開機啟動文件
1. 前言
嵌入式Linux設備通常會使用 sysvinit 或 systemd 兩種方式中的一種作為開機啟動的方式。xilinx petalinux 2021.2 默認使用 sysvinit。
在說明如何添加開機啟動文件之前,先說一下 sysvinit 設備中,開機程序被啟動的過程。
整體過程:
(資料圖)
/sbin/init + /etc/inittab -- > /etc/init.d/rcS -- > /etc/init.d/rc S -- > /etc/init.d/rc 5
下面,咱們一個一個地說(著急的可以直接拉到最后看答案)。
2. /sbin/init
系統啟動時,文件系統掛載完畢后會首先執行 /sbin/init程序,它就是 系統中進程號為 1 的進程對應的程序,是系統中所有進程的祖先。
系統啟動 log:
init 進程信息:
3. /etc/inittab
/etc/inittab是一個配置文件,程序 /sbin/init根據該文件的內容對系統進行操作,比如設置系統的運行級別(runlevel),運行各級別下所需啟動的程序等等。
這篇文章不對 inittab 文件內容進行分析。當前,我們只需知道:
/sbin/init會首先執行 /etc/init.d/rcS,然后執行 /etc/init.d/rc 5。
4. /etc/init.d/rcS
這個腳本主要用于掛載文件系統,比如 sysfs。該腳本中,首先執行 /etc/default/rcS,設置一些環境變量,然后執行 exec /etc/init.d/rc S
——執行 /etc/rcS.d/目錄下的文件。
rcS 文件內容:
#!/bin/sh## rcS Call all S??* scripts in /etc/rcS.d in# numerical/alphabetical order.## Version: @(#)/etc/init.d/rcS 2.76 19-Apr-1999 miquels@cistron.nl#PATH=/sbin:/bin:/usr/sbin:/usr/binrunlevel=Sprevlevel=Numask 022export PATH runlevel prevlevel# Make sure proc is mounted#[ -d "/proc/1" ] || mount /proc## Source defaults.#. /etc/default/rcS## Trap CTRL-C &c only in this shell so we caninterrupt subprocesses.#trap ":" INT QUIT TSTP## Call all parts in order.#exec /etc/init.d/rc S
4.1 /etc/default/rcS
這個文件會設置一些環境變量,后面執行的腳本會用到。比如,我們可以將該文件中 VERBOSE的值改為 very。這樣,在系統啟動時我們就可以看到 /etc/init.d/rc執行的詳細過程了。
文件內容:
## Defaults for the boot scripts in /etc/rcS.d## Time files in /tmp are kept in days.TMPTIME=0# Set to yes if you want sulogin to be spawned on bootupSULOGIN=no# Set to no if you want to be able to login over telnet/rlogin# before system startup is complete (as soon as inetd is started)DELAYLOGIN=no# Assume that the BIOSclock is set to UTC time (recommended)UTC=yes# Set VERBOSE to "no" if you would like a more quiet bootup.VERBOSE=no# Set EDITMOTD to "no" if you don"t want /etc/motd to be edited automaticallyEDITMOTD=no# Whether to fsck root on bootENABLE_ROOTFS_FSCK=no# Set FSCKFIX to "yes" if you want to add "-y" to the fsck at startup.FSCKFIX=yes# Set TICKADJ to the correct tick value for this specific machine#TICKADJ=10000# Enable caching in populate-volatile.shVOLATILE_ENABLE_CACHE=yes# Indicate whether the rootfs is intended to be read-only or not.# Setting ROOTFS_READ_ONLY to yes and rebooting will give you a read-only rootfs.# Normally you should not change this value.ROOTFS_READ_ONLY=no
VERBOSE=very:
4.2 exec /etc/init.d/rc S
/etc/init.d/rc,這個是最主要的文件,各個運行級別下的開機啟動程序的執行就發生在這個文件中。
比如,當前執行的是 /etc/init.d/rc S
,那么就會執行 /etc/rcS.d/ 目錄下的程序。
5. exec /etc/init.d/rc 5
和 exec /etc/init.d/rc S
類似,exec /etc/init.d/rc 5
會執行 /etc/rc5.d/目錄下的程序。通常情況下, 這個目錄下的文件在系統啟動的最后階段執行。所以, 我們的啟動文件也應當放在這個目錄下。
6. 啟動文件添加規則
6.1 軟鏈接
如果使用 ls -al
命令查看 /etc/rcX.d/目錄下的文件,你就會發現,這些目錄下的文件都是軟鏈接,鏈接到 /etc/init.d/目錄下的對應文件。
所以,我們也 應當將我們的文件放在 /etc/init.d/目錄下,然后在 /etc/rcX.d/目錄下創建軟鏈接。
6.2 數字和名稱
/etc/rcX.d/目錄下的文件名可以分為 3 部分——S[序號][文件名稱]。
S:start,表示需要開機運行的程序,與之對應的是 K——kill,表示開機需要停止的程序;
序號:一個兩位的數字,范圍為 01~99,表示啟動的順序, 數值小的優先啟動;
文件名稱:英文名稱(我沒見過中文的),也表示啟動順序,按照 a~z 的順序啟動。如果兩個文件有相同的序號,那么就按照文件名稱中字母的順序進行啟動;
比如,在 /etc/rc5.d/目錄下有兩個文件,分別為 S99atest 和 S99btest。那么,系統啟動時會 優先執行 S10atest。
其實,在 /etc/init.d/rcS文件開頭的注釋部分已經表明了文件的啟動順序:
7. 總結
啰里吧嗦一大堆,添加開機啟動文件時只需按照以下步驟即可:
將需要開機啟動程序的啟動命令寫在一個腳本中,比如 test-auto-run.sh;(雖然可以直接使用可執行程序,但還是建議使用腳本的方式)將 test-auto-run.sh放在 /etc/init.d/目錄下;在 /etc/rc5.d/目錄下創建 test-auto-run.sh文件的軟鏈接;根據程序的啟動時機對軟鏈接進行命名。比如,想要最后啟動程序,那么就命名為 S99zxxxx。關鍵詞:
相關文章
精彩推送
國際實業(000159.SZ):擬以簡易程序定增募資不超3億元 投于磷酸鐵鋰儲能電池PACK集成生產線項目等
格隆匯8月22日丨國際實業(000159 SZ)公布2023年度以簡易程序向特定對象
阿里巴巴旗下大灣區基金完成20億港元募資,曾投Animoca Brands、Sandbox VR等
阿里巴巴旗下大灣區基金完成20億港元募資,曾投AnimocaBrands、SandboxVR等
圖南股份(300855)2023年中報點評:下游航發高景氣 2023Q2業績創單季新高
圖南股份(300855)2023年中報點評:下游航發高景氣2023Q2業績創單季新高