2021年Linux技术总结(三):根文件系统(rootfs)

本文介绍了Linux根文件系统的概念及重要性,并详细阐述了使用BusyBox、Buildroot、Yocto及Ubuntu-base等工具搭建根文件系统的过程。同时,还介绍了在Linux系统中常用的APT软件管理工具、VIM文本编辑器及Shell脚本的相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 一、根文件系统简介
Linux系统三大块:U-boot、kernel以及最后这个rootfs,跟文件系统。在kernel中,启动流程的最后会调用 prepare_namespace 函数,挂载根文件系统,这里就是挂载的本篇要说的。

根文件系统保存了内核代码映射文件,并对它们进行组织,方便用户与操作系统进行交互。但嵌入式里的内核代码保存在flash专用分区里面。内核会调用根文件系统的脚本文件进行初始化操作,如果没有根文件系统,则会出现内核崩溃。

首先,我们要了解一个根文件系统的结构,这才是以后经常打交道的对象,下面是常用的目录:

  • /bin 存放着系统需要的可执行文件,一般都是一些命令,在终端中用的ls cp 什么的,都在里面;
  • /dev 各种设备文件,像ttymxc ttyusb video sda等
  • /etc 存放着各种配置文件,开机密码了什么的在这里面
  • /lib 存放着 Linux所必须的库文件
  • /mnt 临时挂载文件,好比windows下,你插一个U盘,蹦出来的那个盘,Linux下都挂载在这个目录里
  • /proc 虚拟文件系统,里面的文件都是临时存在的,一般用来存储系统运行信息文件。
  • /usr 全称Unix Software Resource,就是一个放软件的地方,一般下载的都安装在这里

这些是本人经常看的目录,其它的基本没怎么用,如果有兴趣可以自己查下。

二、根文件系统的搭建

根文件系统搭建是一个很麻烦很麻烦很麻烦的事,这些文件夹你可以一个个从网上下,哈哈哈哈,但是有专门的工具帮我们搭建。下面是几种常用的搭建方式。

1.1 busybox搭建

busybox是最简单的搭建工具,但它也集成了大量的 Linux命令和工具的软件,像 ls、 mv、 ifconfig,可以搭建一个最小的根文件系统。
busybox下载官网:https://busybox.net/,简陋的一批。
在这里插入图片描述
左面那一块,Get BusyBox里面第一个,点开,里面是各种版本

这里本人不建议用最新版的,还是倒退一两个版本比较好。
下载后解压,然后在Makefile里面改一下ARCH ?= arm 和 CROSS_COMPILE = 交叉编译器路径。
接着 make menuconfig 进行配置,这些配置务必要进行:

Location: 
	-> Settings 
		-> [ ] Build static binary (no shared libs) 

Location: 
	-> Settings 
		-> [*] vi-style line editing commands

Location: 
	-> Linux Module Utilities 
		-> [ ] Simplified modutils

Location: 
	-> Linux System Utilities 
		-> [*] mdev (16 kb) //确保下面的全部选中,默认都是选中的

Location: 
	-> Settings 
		-> [*] Support Unicode 
			-> [*] Check $LC_ALL, $LC_CTYPE and $LANG environment variables

最后编译安装,指定路径
make 
make install CONFIG_PREFIX=这里是路径

ok 还没完,虽然busybox搭建完了,可,还有特别多库需要移植才能用,接下来需要移植busybox的库。

  • 在根目录下建立一个lib文件夹(busybox搭建的根文件系统没有),然后将交叉编译器里面的库(路径是交叉编译器目录下arm-linux-gnueabihf/libc/lib和arm-linux-gnueabihf/lib,用的其它交叉编译器的话可以搜一下在哪)文件: * so* (*是通配符 )和 .a文件,cp到lib下;
  • 在根目录下建立一个usr/lib文件夹(busybox搭建的根文件系统没有),然后将交叉编译器里面的库(路径是交叉编译器目录下arm-linux-gnueabihf/libc/usr/lib和arm-linux-gnueabihf/lib,用的其它交叉编译器的话可以搜一下在哪)文件: * so* (*是通配符 )和 .a文件,cp到usr/lib下;
  • 创建dev、 proc、 mnt、 sys、 tmp 和 root 等文件夹;
  • 创建 /etc/init.d/rcS文件,输入如下内容:
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH

mount -a
mkdir /dev/pts
mount -t devpts devpts /dev/pts

echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
  • 创建 /etc/fstab文件
#<file system>	<mount point>	<type>		<options> <dump> <pass>
proc			/proc proc 		defaults 	0 0
tmpfs			/tmp tmpfs 		defaults 	0 0
sysfs			/sys sysfs 		defaults 	0 0
  • 创建 /etc/inittab文件
#etc/inittab
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a

1.2 buildroot搭建

可以看出,busybox的搭建还是很麻烦的,下面写一下buildroot这种搭建,这个也是我目前用的搭建方法,效果还不戳。
首先嘞,先下一个buildroot的安装包,然后嘞,解压呀,再然后嘞,进到目录里面,打开终端。输入make menuconfig 打开图形化配置界面,然后进行如下系列配置:

Target options 
	-> Target Architecture = ARM (little endian) 
	-> Target Binary Format = ELF 
	-> Target Architecture Variant = cortex-A7 
	-> Target ABI = EABIhf 
	-> Floating point strategy = NEON/VFPv4 
	-> ARM instruction set = ARM

Toolchain 
	-> Toolchain type = External toolchain 
	-> Toolchain = Custom toolchain //用户自己的交叉编译器
	-> Toolchain origin = Pre-installed toolchain //预装的编译器
	-> Toolchain path =/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf 
	-> Toolchain prefix = $(ARCH)-linux-gnueabihf //前缀
	-> External toolchain gcc version = 4.9.x 
	-> External toolchain kernel headers series = 4.1.x 
	-> External toolchain C library = glibc/eglibc 
	-> [*] Toolchain has SSP support? (NEW) //选中
	-> [*] Toolchain has RPC support? (NEW) //选中
	-> [*] Toolchain has C++ support? //选中
	-> [*] Enable MMU support (NEW) //选中

System configuration 
	-> System hostname = alpha_imx6ull //平台名字,自行设置
	-> System banner = Welcome to alpha i.mx6ull //欢迎语
	-> Init system = BusyBox //使用 busybox 
	-> /dev management = Dynamic using devtmpfs + mdev //使用 mdev 
	-> [*] Enable root login with password (NEW) //使能登录密码
	-> Root password = 123456 //登录密码为 123456

然后返回到终端,输入make (如果make错误,再试试sudo make 我个人不太喜欢sudo make 权限很烦人),有时候会遇到半天下载很慢的,在图中那个位置可以找到那个下载网址,再跑到windows下下载。
在这里插入图片描述
前面是搭建最小系统的方法,其它第三方软件buildroot也含有,这里建议先搭建个最小的,后面搭建其它的也可能会出现各种意外错误,其它软件添加在:Target packages 里面找,也可以直接按 / 全局搜索,有的软件安装是有依赖的,每次添加完编译都需要先输入 sudo make clean all,再编译,所以建议一次性添加完。

1.3 Yocto搭建

这里直接放一个之前写过的链接吧,没写完,因为最后家里网不行,东西就是下不下来,由于不可描述原因,外网是别想了。

1.4 Ubuntu-base搭建

这个我也用过,但是后面不知道为什么,用过一段时间后,网络挂载就挂不上了,搞得我很心累,唉,先记下来,万一什么时候知道原因了再来改一下,下面先描述一下大致的搭建过程。
首先嘞,先下一个ubuntu-base的安装包,ubuntu版本提供很多,记得别选错,然后嘞,解压呀,再然后,安装 qemu工具,命令:sudo apt-get install qemu-user-static 将刚刚安装的 qemu-user-static拷贝到刚刚解压出来的 ubuntu base目录中,也就是ubuntu_rootfs/usr/bin目录下,命令如下:

cd /home/zuozhongkai/linux/nfs/ubuntu_rootfs 	//进入到 ubuntu_rootfs目录下
sudo cp /usr/bin/qemu-arm-static ./usr/bin/ 	//拷贝 qemu-arm-static

用过ubuntu的都知道,apt软件管理工具需要提供一个软件源,这个源在 /etc/resolv.conf 下,但是arm架构和X86架构的源是不一样的,所以在网上应该找ARM源,一般情况下ARM源的网址比X86源多了一个-ports,例如:

X86架构阿里源

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

ARM架构阿里源

deb http://mirrors.aliyun.com/ubuntu-ports/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ bionic-backports main restricted universe multiverse

换源的同时也需要注意,不同版本的Ubuntu源是不一样的,要找对应版本的源。
源确定后就要在主机挂载并配置根文件系统,脚本如下:

#!/bin/bash
echo "MOUNTING"
sudo mount -t proc /proc /ubuntu-base根目录/proc
sudo mount -t sysfs /sys /ubuntu-base根目录/sys
sudo mount -o bind /dev /ubuntu-base根目录/dev
sudo mount -o bind /dev/pts /ubuntu-base根目录/dev/pts
sudo chroot /ubuntu-base根目录

卸载脚本如下:

#!/bin/bash
echo "UNMOUNTING"
sudo umount /ubuntu-base根目录/proc
sudo umount /ubuntu-base根目录/sys
sudo umount /ubuntu-base根目录/dev/pts
sudo umount /ubuntu-base根目录/dev

挂载后,输入以下安装指令:

apt update
apt install language-pack-en-base 
apt install sudo 
apt install vim 
apt install kmod 
apt install net-tools 
apt install ethtool 
apt install ifupdown 
apt install rsyslog 
apt install htop
apt install iputils-ping

然后执行卸载脚本,就完成了。

三、操作系统常用指令和工具

3.1 APT软件管理工具

​ APT下载工具可以实现软件自动下载、配置、安装二进制或者源码的功能。 APT下载工具和“ install”命令结合在一起构成了 Ubuntu下最常用的下载和安装软件方法。它解决了 Linux平台下安装软件的一个缺陷,即软件之间相互依赖。