长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 计算机视觉

  • ESP32开发

  • Linux系统移植

    • Linux系统移植(一)--- 交叉编译工具链的配置
    • Linux系统移植(二)--- Uboot移植
    • Linux系统移植(三)--- Linux kernel移植
    • Linux系统移植(四)--- 使用busybox制作根文件系统(rootfs)
      • 下载源码
      • 配置
      • 构建目录
      • 编译&安装
      • rootfs基础配置
        • 1. /etc/inittab
        • 2. /etc/init.d/rcS
        • 3. /etc/fastab
        • 4. /etc/sysconfig/hostname
        • 5. /etc/profile
        • 6. 用户登录(可选)
        • 7. 添加库文件(可选)
      • 参考文章
    • Linux系统移植(五)--- 制作、烧录镜像并启动Linux
  • 快速开始

  • 编程小知识

  • 技术
  • Linux系统移植
DC Wang
2024-02-05
目录

Linux系统移植(四)--- 使用busybox制作根文件系统(rootfs)

# Linux系统移植(四)--- 使用busybox制作根文件系统(rootfs)

以Orange Pi Zero3为例,使用busybox制作根文件系统。

# 下载源码

官方网站: https://www.busybox.net/

wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2
tar -jxvf busybox-1.36.1.tar.bz2
cd busybox-1.36.1
1
2
3

# 配置

make ARCH=arm64 CROSS_COMPILE=aarch64-livingteam-linux-gnu- menuconfig
1
Busybox Settings--->
    Build Options--->
        [*]Build BusyBox as a static binary(no shared libs)
 
        
Busybox Library Tuning--->
    [*]vi-style line editing commands
    [*]Fancy shell prompts
    
    
Linux Module Utilities--->
    [ ]Simplified modutils
    [*]insmod
    [*]rmmod
    [*]lsmod
    [*]modprobe
    [*]depmod
 
    
Linux System Utilities--->[*]mdev
    [*]Support /etc/mdev.conf
    [*]Support subdirs/symlinks
    [*]Support regular expressions substitutions when renaming dev
    [*]Support command execution at device addition/removal
    [*]Support loading of firmwares
 
Coreutils  --->
    [ ] sync 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 构建目录

mkdir /path/to/rootfs/
cd /path/to/rootfs/
mkdir dev etc lib usr var proc tmp home root mnt bin sbin opt sys media
1
2
3

# 编译&安装

make ARCH=arm64 CROSS_COMPILE=aarch64-livingteam-linux-gnu- -j8 && make ARCH=arm64 CROSS_COMPILE=aarch64-livingteam-linux-gnu- install -j8
1

编译生成的文件安装在_install下面:

$ ls _install/
bin  linuxrc  sbin  usr
1
2

linuxrc作为根文件系统的入口文件。

将生成的文件复制到rootfs:

cp _install/* /path/to/rootfs/ -arf
1

# rootfs基础配置

# 1. /etc/inittab

inittab文件内容:

#first:run the system script file
::sysinit:/etc/init.d/rcS
::askfirst:-/bin/sh
::ctrlaltdel:-/sbin/reboot
#umount all filesystem
::shutdown:/bin/umount -a -r
#restart init process
::restart:/sbin/init
1
2
3
4
5
6
7
8

inittab是etc下的配置文件,linuxrc运行时调用并且按格式解析文件。格式为:

id:runlevels:action:process
1
  • :分隔符
  • unlevels是程序运行级别(15)
  • process为可执行程序(shell)
  • action是process的执行条件

action含义解释:

配置项 描述
sysinit 在系统启动阶段执行后续的进程。在给定例子中,执行 rcS 脚本。
respawn 当后续的进程结束时,该进程将被重新启动。
askfirst 类似于 respawn,但在运行进程之前,它会打印提示信息,等待用户按下 Enter 键来启动该进程。通常用于启动终端设备。
wait init 进程会等待该进程执行完毕,然后才继续执行下一项。
once 进程只会执行一次。在给定的例子中,执行 telnetd 守护进程,使用 -l 参数表示连接时使用 login 登录。同时执行 login 程序。
restart 当重新启动 init 进程时执行该进程。
ctrlaltdel 当按下 Ctrl+Alt+Del 三个键时,init 进程收到 SIGINT 信号,然后运行相应的进程。
shutdown 在系统关机时执行相应的进程。在给定的例子中,卸载所有已挂载的设备。

# 2. /etc/init.d/rcS

#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
mount -a
/bin/hostname -F /etc/sysconfig/hostname
1
2
3
4
5
6
7
8

给rcS添加可执行权限

chmod +x rcS
1

# 3. /etc/fastab

# /etc/fstab: static file system information.
#
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#     <file system>     <mount point>     <type>     <options>     <dump>     <pass>
    proc             /proc             proc     defaults     0         0
    sysfs             /sys             sysfs     defaults     0         0
    tmpfs             /var             tmpfs     defaults     0         0
    tmpfs             /tmp             tmpfs     defaults     0         0
    tmpfs             /dev             tmpfs     defaults     0         0
1
2
3
4
5
6
7
8
9
10
11
12

# 4. /etc/sysconfig/hostname

设置主机名称:

livingteam
1

# 5. /etc/profile

修改提示符号,导出环境变量,profile文件内容:

# Ash profile
# vim: syntax=sh

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

USER="`id -un`"
LOGNAME=$USER
PS1='[\u@\h \W]\# '
PATH=$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
HOSTNAME=`/bin/hostname`

export USER LOGNAME PS1 PATH LD_LIBRARY_PATH
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 6. 用户登录(可选)

在inittab执行/bin/sh会直接进入shell界面,故添加/bin/login或/sbin/gettty来实现登录功能。

  • /etc/inittab

    注释掉第3行,添加console::sysinit:/bin/login

    #::askfirst:-/bin/sh
    console::sysinit:/bin/login
    
    1
    2
  • /etc/passwd

    root:x:0:0:root:/root:/bin/sh
    
    1
  • /etc/shadow

    root:$6$xHD0aja9$Bxg0rl3jJCq7RXnUuCaBNZGXC5aZrKWUHCiE0Lgp3vh6.S.4gsMhfPLVSwOwIDVZDNVhjksB0VdcvI7MDTKaz0:19612:0:99999:7:::
    
    1

    密码是123456,删除$6$xHD0aja9$Bxg0rl3jJCq7RXnUuCaBNZGXC5aZrKWUHCiE0Lgp3vh6.S.4gsMhfPLVSwOwIDVZDNVhjksB0VdcvI7MDTKaz0则默认无密码。

# 7. 添加库文件(可选)

未添加库文件可执静态链接行程序,添加库文件可执行动态链接程序。

使用交叉编译工具生成目录中的库文件,参考Linux系统移植(一)--- 交叉编译工具链的配置 | 长生栈 (livingteam.cn) (opens new window)

mkdir rootfs/lib
cp aarch64-livingteam-linux-gnu/aarch64-livingteam-linux-gnu/sysroot/lib/*so* lib/ -rdf
1
2

库文件添加测试

  • rootfs/root/c/hello.c

    #include <stdio.h>
    
    int main()
    {
            printf("hello world\n");
            return 0;
    }
    
    1
    2
    3
    4
    5
    6
    7

使用交叉编译工具编译:

aarch64-livingteam-linux-gcc hello.c -o hello_dynamic
aarch64-livingteam-linux-gcc hello.c -o hello_satic -static
cp ./* rootfs/root
1
2
3

至此,基于busybox制作rootfs完成。

# 参考文章

[busybox] busybox生成一个最精简rootfs(上)_busybox如何精简-CSDN博客 (opens new window)

利用busybox构造linux rootfs - 知乎 (zhihu.com) (opens new window)

根文件系统(二):busybox-CSDN博客 (opens new window)

编辑 (opens new window)
#Linux#Linux系统移植#嵌入式
Linux系统移植(三)--- Linux kernel移植
Linux系统移植(五)--- 制作、烧录镜像并启动Linux

← Linux系统移植(三)--- Linux kernel移植 Linux系统移植(五)--- 制作、烧录镜像并启动Linux→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式