基于 Ubuntu Base 制作 rootfs
Posted Li-Yongjun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于 Ubuntu Base 制作 rootfs相关的知识,希望对你有一定的参考价值。
前言
BananaPi 的 wiki 上只提供了 uboot 和 kernel 的制作方法(包含在 BSP 中),没有提供 rootfs 的制作方法。我从 RK3399 制作 rootfs 的方法中借鉴经验,制作了 BananaPi 的 rootfs(基于 Ubuntu 16.04)。
Ubuntu Base 介绍
wiki
Ubuntu Base is a minimal rootfs for use in the creation of custom images for specific needs.
It is available for the i386, amd64, armhf, arm64, powerpc and ppc64el architectures.
硬件
BananaPi R64
下载
从 ubuntu 官网下载 Ubuntu Base 压缩包,
http://cdimage.ubuntu.com/ubuntu-base/releases/16.04.6/release/,
BPI-R64 是 arm64 架构,所以我下载的是 【ubuntu-base-16.04.6-base-arm64.tar.gz】。
制作
mkdir raw-rootfs
sudo tar -xpf ubuntu-base-16.04.6-base-arm64.tar.gz -C raw-rootfs/
sudo apt-get install qemu-user-static
sudo cp /usr/bin/qemu-aarch64-static ./raw-rootfs/usr/bin/
sudo cp /etc/resolv.conf raw-rootfs/etc/resolv.conf
如果宿主机的架构和目标架构不一致,需要使用 qemu 模拟目标架构,所以需要安装 qemu。
修改源 ./raw-rootfs/etc/apt/sources.list 为:
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial main restricted
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates main restricted
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates main restricted
## Uncomment the following two lines to add software from the 'universe'
## repository.
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial universe
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial universe
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-backports main restricted
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-backports main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security main restricted
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security universe
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security universe
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security multiverse
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security multiverse
创建一个挂载脚本,命名为 mount.sh,脚本内容如下:
#!/bin/bash
mnt()
echo "MOUNTING"
sudo mount -t proc /proc $2proc
sudo mount -t sysfs /sys $2sys
sudo mount -o bind /dev $2dev
sudo mount -o bind /dev/pts $2dev/pts
sudo chroot $2
umnt()
echo "UNMOUNTING"
sudo umount $2proc
sudo umount $2sys
sudo umount $2dev/pts
sudo umount $2dev
if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
umnt $1 $2
else
echo ""
echo "Either 1'st, 2'nd or both parameters were missing"
echo ""
echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
echo ""
echo "For example: ch-mount -m /media/sdcard/"
echo ""
echo 1st parameter : $1
echo 2nd parameter : $2
fi
挂载
source mount.sh -m raw-rootfs/
执行上述命令后,会将 raw-rootfs/ 下的 proc、sys、dev 挂载到当前系统的对应目录上,并且使用 chroot 命令将根目录更改为 raw-rootfs。要保证该目录下存在 /bin/bash,不然会报错:
$ sudo chroot tmp
chroot: failed to run command ‘/bin/bash’: No such file or directory
挂载过后,相当于进入了 raw-rootfs/ 这个文件系统。
此后在这个文件系统下进行客制化改造:
liyongjun@Box:~/project/board/rk3399_0305/rootfs$ source mount.sh -m raw-rootfs/
MOUNTING
[sudo] liyongjun 的密码:
root@Box:/# uname -a
Linux Box 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
root@Box:/# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
root@Box:/# mount
/proc on /proc type proc (rw,relatime)
/sys on /sys type sysfs (rw,relatime)
udev on /dev type devtmpfs (rw,nosuid,noexec,relatime,size=6965676k,nr_inodes=1741419,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
root@Box:/# apt install vim ssh
添加用户:
useradd -s '/bin/bash' -m -G adm,sudo lyj
passwd lyj # 给新用户设置密码
passwd root # 给 root 用户设置密码
允许用户使用超级用户权限
chmod +w /etc/sudoers
vi /etc/sudoers
# User privilege specification
root ALL=(ALL:ALL) ALL
lyj ALL=(ALL:ALL) ALL # 添加这行
退出 chroot
exit
解除挂载
source mount.sh -u raw-rootfs/
制作根文件系统映像:
dd if=/dev/zero of=linuxroot.img bs=1M count=1024 #根据自己的根文件系统大小分配
sudo mkfs.ext4 linuxroot.img
mkdir rootfs
sudo mount linuxroot.img rootfs/
sudo cp -rfp raw-rootfs/* rootfs/
sudo umount rootfs/
e2fsck -p -f linuxroot.img
resize2fs -M linuxroot.img
烧录根文件系统:
sudo dd if=linuxroot.img of=/dev/sdb seek=262144
测试
原本 SD 卡烧录的是一个 debian 系统,在 SD 卡的第二个分区(ext4)。第一个分区为 FAT16,存放 uEnv.txt、设备树、内核镜像。
我们使用 fdisk 再新建一个 ext4 分区,将我们制作的镜像拷贝到这个分区,并将 uEvn.txt 中的 rootfs 参数修改成该分区
# root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
root=/dev/mmcblk0p3 rootfstype=ext4 rootwait
启动
Partition Map for MMC device 1 -- Partition Type: DOS
Part Start Sector Num Sectors UUID Type
1 204800 524288 674a82c6-01 0c
2 729088 14211072 674a82c6-02 83
3 14940160 16175104 674a82c6-03 83
mmc1 is available
Interface: MMC
Device 1: Vendor: Man 000003 Snr eed20501 Rev: 10.3 Prod: SC16G
Type: Removable Hard Disk
Capacity: 15193.5 MB = 14.8 GB (31116288 x 512)
Filesystem: FAT16 "BPI-BOOT "
Boot from SD
reading bananapi/bpi-r64/linux/uEnv.txt
978 bytes read in 5 ms (190.4 KiB/s)
Loaded environment from uEnv.txt
Banana Pi bpi-r64 chip: mt7622 Service: linux
reading bananapi/bpi-r64/linux/dtb/mt7622-bananapi-r64.dtb
26848 bytes read in 8 ms (3.2 MiB/s)
reading bananapi/bpi-r64/linux/uImage
17556603 bytes read in 740 ms (22.6 MiB/s)
reading bananapi/berryboot.img
** Unable to read file bananapi/berryboot.img **
bootm flag=0, states=70f
## Loading kernel from FIT Image at 44000000 ...
Using 'conf@1' configuration
Trying 'kernel@1' kernel subimage
Description: Kernel Image
Type: Kernel Image
Compression: uncompressed
Data Start: 0x440000d8
Data Size: 17555016 Bytes = 16.7 MiB
Architecture: ARM
OS: Linux
Load Address: 0x40080000
Entry Point: 0x40080000
Verifying Hash Integrity ... OK
## Flattened Device Tree blob at 47000000
Booting using the fdt blob at 0x47000000
Loading Kernel Image ... OK
Loading Device Tree to 5cf08000, end 5cf118df ... OK
Starting kernel ...
[ATF][ 96.452570]save kernel info
[ATF][ 96.455772]Kernel_EL2
[ATF][ 96.458528]Kernel is 64Bit
[ATF][ 96.461716]pc=0x40080000, r0=0x5cf08000, r1=0x0
INFO: BL3-1: Preparing for EL3 exit to normal world, Kernel
INFO: BL3-1: Next image address = 0x40080000
INFO: BL3-1: Next image spsr = 0x3c9
[ATF][ 96.479983]el3_exit
[ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.4.92-BPI-R64-Kernel (liyongjun@liyongjun-VirtualBox) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9) ) #1 SMP PREEMPT Wed Mar 8 01:01:45 CST 2023
[ 0.000000] Boot CPU: AArch64 Processor [410fd034]
[ 0.000000] On node 0 totalpages: 131024
[ 0.000000] DMA zone: 2048 pages used for memmap
[ 0.000000] DMA zone: 0 pages reserved
[ 0.000000] DMA zone: 131024 pages, LIFO batch:31
[ 0.000000] psci: probing for conduit method from DT.
[ 0.000000] psci: PSCIv0.2 detected in firmware.
[ 0.000000] psci: Using standard PSCI v0.2 function IDs
[ 0.000000] psci: Trusted OS migration not required
[ 0.000000] PERCPU: Embedded 21 pages/cpu @ffffffc01ffa5000 s45720 r8192 d32104 u86016
[ 0.000000] pcpu-alloc: s45720 r8192 d32104 u86016 alloc=21*4096
[ 0.000000] pcpu-alloc: [0] 0 [0] 1
[ 0.000000] Detected VIPT I-cache on CPU0
[ 0.000000] CPU features: enabling workaround for ARM erratum 845719
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 128976
[ 0.000000] Kernel command line: board=bpi-r64 console=ttyS0,115200n1 earlyprintk root=/dev/mmcblk0p3 rootfstype=ext4 rootwait service=linux debug=7 initcall_debug=0 androidboot.hardware=mt7622 swiotlb=512
[ 0.000000] PID hash table entries: 2048 (order: 2, 16384 bytes)
......
[ OK ] Started OpenBSD Secure Shell server.
[ OK ] Reached target Multi-User System.
[ OK ] Reached target Graphical Interface.
Ubuntu 16.04.7 LTS localhost.localdomain ttyS0
localhost login: lyj
Password:
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.92-BPI-R64-Kernel aarch64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
-bash: cannot create temp file for here-document: Read-only file system
lyj@localhost:~$
lyj@localhost:~$ ls
lyj@localhost:~$ uname -a
Linux localhost.localdomain 4.4.92-BPI-R64-Kernel #1 SMP PREEMPT Wed Mar 8 01:01:45 CST 2023 aarch64 aarch64 aarch64 GNU/Linux
lyj@localhost:~$
lyj@localhost:~$ ls /dev/mmcblk0*
/dev/mmcblk0 /dev/mmcblk0p1 /dev/mmcblk0p2 /dev/mmcblk0p3
lyj@localhost:~$ mount
/dev/mmcblk0p3 on / type ext4 (ro,relatime,data=ordered)
......
是我们制作的 Ubuntu 16.04 根文件系统 😉
FAQs
根文件系统加载后,大小不正常,未占满整个分区:
在系统正确加载后执行扩展文件系统命令:
sudo resize2fs /dev/sdb5
参考
【原创】从Ubuntu-base构建ubuntu rootfs系统(以x86_64和arm为例)
以上是关于基于 Ubuntu Base 制作 rootfs的主要内容,如果未能解决你的问题,请参考以下文章
i.MX6ULL系统移植 | 基于 ubuntu base 20.04 构建根文件系统
如何制作一个vagrant的base box 及安装 additions
如何制作一个vagrant的base box 及安装 additions
基于ubuntu base安装Xfce桌面环境(arm64)