前言
RK系列的平台提供了buildroot和debian的系统,使用脚本可以直接构建出来,但是没有提供ubuntu的系统,很多厂商只提供一个rootfs.img的固件包,没有将方法开源出来。本文实现了从ubuntu官网开始构建一个ubuntu22.04根文件系统。
ubuntu-base
什么是ubuntu base?, ubuntu针对不同的CPU架构提供相应的ubuntu base 根文件系统,目前提供的架构有amd64, arm64m armhf, i386, s390x, ppc64.
ubuntu base 是用于为特定需求创建自定义映像的最小rootfs, 是ubuntu可以运行的最小环境。
Ubuntu Base 22.04.4 LTS (Jammy Jellyfish)下载地址:
https://cdimage.ubuntu.com/ubuntu-base/releases/22.04/release/
本文最终运行的环境是RK3588,所以下载的是ubuntu-base-22.04-base-arm64.tar.gz的压缩包。
解压:
mkdir rootfs
tar -zxvf ubuntu-base-22.04-base-arm64.tar.gz -C rootfs/
挂载构建文件系统
配置网络
sudo cp /etc/resolv.conf rootfs/etc/
安装qemu,
qemu是用来仿真arm64平台的,使得arm64的系统可以再x86平台运行起来。
$sudo apt install qemu-user-static
sudo cp /usr/bin/qemu-aarch64-static rootfs/usr/bin/
修改源改为阿里源:
ubuntu 阿里 arm源地址:
https://developer.aliyun.com/mirror/ubuntu-ports?spm=a2c6h.13651104.d-1008.9.742b4763eLldW3
sudo vim rootfs/etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu-ports/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu-ports/ jammy-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu-ports/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ jammy-backports main restricted universe multiverse
设置权限
cd rootfs
sudo chmod 777 -R tmp/
cd ..
编写挂载脚本mount.sh,内容如下:
#!/bin/bash
function mnt() {
echo "MOUNTING"
sudo mount -t proc /proc ${2}proc
sudo mount -t sysfs /sys ${2}sys
sudo mount -o bind /dev ${2}dev
sudo mount -o bind /dev/pts ${2}dev/pts
sudo chroot ${2}
}
function umnt(){
echo "unmounting"
sudo umount ${2}proc
sudo umount ${2}sys
sudo umount ${2}dev/pts
sudo umount ${2}dev
}
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 ben one of these: -m(mount) OR -u(umount)"
echo "2'nd parameter is the full path of rootfs directory(with tralling '/')"
echo ""
echo "For example: ch-mount -m /meadia/sdcard"
echo ""
echo 1st parameter : ${1}
echo 2nd parameter : ${2}
fi
挂载
./mount.sh -m rootfs/
挂载完后,就会切换到ubuntu22.04的根文件系统,因为使用了qemu,后面就可以再arm64平台操作执行了。
进入后先执行,apt update,更新最新的源
apt update
如果执行中出现如下错误
W: https://mirrors.aliyun.com/ubuntu-ports/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu-ports/dists/jammy-security/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu-ports/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu-ports/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu-ports/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
这是因为https是加密协议,需要直接使用阿里源会要求安装证书。而我们刚开展做新的系统apt又用不了,所以此处将source.list中的https都替换为http即可。
如果出现下列错误,说明是文件没有权限,在挂载前就需要将rootfs/tmp目录设置可读写权限。
W: GPG error: http://mirrors.aliyun.com/ubuntu-ports jammy InRelease: Couldn't create temporary file /tmp/apt.conf.nNgmYa for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports jammy InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: GPG error: http://mirrors.aliyun.com/ubuntu-ports jammy-security InRelease: Couldn't create temporary file /tmp/apt.conf.cnD2Tw for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports jammy-security InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
安装需要的包
apt install sudo vim udev net-tools ethtool udhcpc netplan.io language-pack-en-base language-pack-zh-han* iputils-ping openssh-sftp-server ntp usbutils alsa-utils libmtp9 ssh htop network-manager
apt install gcc g++
桌面安装
如果需要使用桌面,建议直接安装轻量级的桌面lxde,
apt install lxde
添加用户
adduser ubuntu
adduser ubuntu sudo
退出挂载的文件系统
exit
./mount.sh -u rootfs/
制作rootfs
创建一个空镜像文件
dd if=/dev/zero of=ubuntu2204_rootfs.img bs=1M count=5120
将该文件格式化为ext4文件系统
mkfs.ext4 ubuntu2204_rootfs.img
创建一个空的文件,挂载镜像
mkdir temp
sudo mount ubuntu2204_rootfs.img temp/
cp -rfp rootfs/* temp/
# -r:递归文件夹
# -f:强制
# -p:不拷贝符号链接的目标文件,仅拷贝该符号链接
卸载镜像
umount temp/
e2fsck 检查镜像的文件系统。resize2fs 减小镜像文件的大小
$ e2fsck -p -f ubuntu2204_rootfs.img
$ resize2fs -M ubuntu2204_rootfs.img
至此,最小的ubuntn22.04文件系统做完,可以直接烧录到RK板子上。