基于Pico PC RK3588S平台搭建Ubuntu20.04.6根文件系统

文章目录

前言

很多厂商没有吧文件系统的构建方法开源出来,只是提供了一个rootfs.img的固件包,这不利于我们做二次开发,本文章实现一个自己构建的文件系统,并移植到RK的平台上使用。
搭建好用于开发的Ubuntu虚拟机环境,熟练使用一些常用工具如烧录系统的工具RKDevTool、远程连接工具MobaXterm等等。当然,本章只针对根文件系统,uboot、boot的镜像文件和驱动文件需要提前准备好,编译资料提供的Rockchip的SDK即可得到这些文件。

一、官网下载ubuntu-base

http://cdimage.ubuntu.com/ubuntu-base/releases/
本文章使用的是:buntu-base-20.04.5-base-arm64.tar.gz
先寻找一个空白的文件夹放置ubuntu-base-20.04.1-base-arm64.tar.gz,然后以root权限建立一个文件夹,将根文件系统解压到这个文件夹中。

mkdir ubuntu_rootfs
tar -zxvf ubuntu-base-20.04.5-base-arm64.tar.gz -C ubuntu_rootfs

二、挂载并构建文件系统

2.1、配置构建文件系统环境

接下来在Ubuntu虚拟机上模拟运行arm64架构的最小文件系统,为此需要安装qemu-user-static。

sudo apt-get install qemu-user-static
# 适用于aarch64即arm64架构
sudo cp /usr/bin/qemu-aarch64-static ubuntu-rootfs/usr/bin/

为了连接网络,需要拷贝resolv.conf文件以及配置国内的镜像源。

# 拷贝文件
sudo cp -b /etc/resolv.conf ubuntu-rootfs/etc/resolv.conf
# 修改镜像源
sudo gedit ubuntu-rootfs/etc/apt/sources.list

下面是阿里云镜像源,可以直接复制内容覆盖源镜像列表文件。

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

2.2、编写挂载脚本mount.sh并安装相关工具

编写一个挂载脚本mount.sh,方便进入和退出arm64的文件系统
vim 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 -t devpts -o gid=5,mode=620 devpts ${2}dev/pts
  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 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

2.4 、更新与安装

进入arm64的根文件系统后,终端将只能操作arm64的根文件系统,通过exit命令退出并返回Ubuntu虚拟机的终端。

# 进入arm64的根文件系统
./mount.sh -m ubuntu-rootfs/

更新并升级根文件系统。

apt update
apt upgrade

报错:

oot@yang-vm:/# apt update
Get:1 http://mirrors.aliyun.com/ubuntu-ports focal InRelease [265 kB]
Err:1 http://mirrors.aliyun.com/ubuntu-ports focal InRelease
Couldn't create temporary file /tmp/apt.conf.yVGRsi for passing config to apt-key
Get:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates InRelease [128 kB]
Err:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates InRelease
Couldn't create temporary file /tmp/apt.conf.rXxHWr for passing config to apt-key
Get:3 http://mirrors.aliyun.com/ubuntu-ports focal-backports InRelease [128 kB]
Err:3 http://mirrors.aliyun.com/ubuntu-ports focal-backports InRelease
Couldn't create temporary file /tmp/apt.conf.6y4akz for passing config to apt-key
Get:4 http://mirrors.aliyun.com/ubuntu-ports focal-security InRelease [128 kB]
Err:4 http://mirrors.aliyun.com/ubuntu-ports focal-security InRelease
Couldn't create temporary file /tmp/apt.conf.oT6fPG for passing config to apt-key
Reading package lists... Done
W: GPG error: http://mirrors.aliyun.com/ubuntu-ports focal InRelease: Couldn't create temporary file /tmp/apt.conf.yVGRsi for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports focal 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 focal-updates InRelease: Couldn't create temporary file /tmp/apt.conf.rXxHWr for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports focal-updates 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 focal-backports InRelease: Couldn't create temporary file /tmp/apt.conf.6y4akz for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports focal-backports 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 focal-security InRelease: Couldn't create temporary file /tmp/apt.conf.oT6fPG for passing config to apt-key
E: The repository 'http://mirrors.aliyun.com/ubuntu-ports focal-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.

解决:
chmod 1777 /tmp
再更新即可
安装一些必要的软件,如vim、sudo、网络管理、蓝牙管理等等。

apt install apt-utils dialog
apt install vim sudo
apt install bash-completion 
apt install net-tools iputils-ping ifupdown ethtool
apt install wireless-tools network-manager
apt install ssh rsync udev htop rsyslog

配置系统的时区、文字编码,此时终端有图形界面可选。

apt install locales tzdata
# 时区选择
# Asia/Shanghai
dpkg-reconfigure locales
# 勾选英文和中文环境
# en_US.UTF-8 UTF-8
# zh_CN.UTF-8 UTF-8

2.3、桌面环境 ubuntu-desktop(可选,我没选跳到2.4,相当于服务器版)

安装图形环境,依赖非常多,占用空间3289M。
apt install ubuntu-desktop
开机默认切换到图形界面。
systemctl set-default graphical.target
安装中英文语言包和中文输入法。

# 英文环境
apt install language-pack-en-base 
apt install language-pack-gnome-en-base 
 
# 中文环境
apt install language-pack-zh-hans-base 
apt install language-pack-gnome-zh-hans-base
 
# 中文输入法
apt install ibus-table-wubi ibus-pinyin ibus-sunpinyin

2.4、网卡配置

Network-Manager服务会自动配置网卡,但是其默认配置文件将Ethernet加入了黑名单,以至于Ubuntu提示unmanned。

vi /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf
# 文件内容改为如下内容
[keyfile]
unmanaged-devices=*,except:type:ethernet,except:type:wifi,except:type:gsm,except:type:cdma

2.5、添加用户

设置主机名,增加用户,修改账户密码。

echo "RK3588" > /etc/hostname
echo "127.0.0.1 localhost" >> /etc/hosts
echo "127.0.0.1 RK3588" >> /etc/hosts

# 添加用户ubuntu
useradd -s '/bin/bash' -m -G adm,sudo ubuntu
# 设置密码
passwd ubuntu
passwd root

2.6 、允许root用户登录桌面

Ubuntu默认不能用root登录界面,可以配置启用以root登录界面。

vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
# 添加下面的内容
greeter-show-manual-login=true
all-guest=false
 
vi /etc/pam.d/gdm-autologin
# 注释掉下面这行
#auth   required        pam_succeed_if.so user != root quiet_success
 
vi /etc/pam.d/gdm-password
# 注释掉下面这行
#auth   required        pam_succeed_if.so user != root quiet_success
 
vi /root/.profile
# 添加下面的内容,替换掉 mesg n || true 这一行
tty -s && mesg n || true

2.7、串口自动登录

vi /lib/systemd/system/serial-getty\@.service
# 注释 ExecStart=-/sbin/agetty -o ‘-p – \u’ --keep-baud 115200,38400,9600 %I $TERM这行
# 添加 ExecStart=-/sbin/agetty --autologin root --noclear %I $TERM

2.8、修改系统配置

  • 网络超时时间
    默认等待网络接入后才开机,无网络会等待5分钟网络连接超时才跳过该步骤。体验感非常差,建议改为5s。
vim /lib/systemd/system/networking.service

TimeoutStartSec=5min 修改为TimeoutStartSec=5sec。
  • 开机等待时间
    此外还有系统默认等待时间90s,建议改为2s。方法解除相关注释,改为2秒。
vim /etc/systemd/system.conf
解除 DefaultTimeoutStopSec=90s 注释,改为DefaultTimeoutStopSec=2s。
  • 禁用系统休眠
    sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

2.9、添加分区释放的系统服务(重要)

Ubuntu根文件系统打包成镜像并烧录到emmc后,所占分区大小和镜像的大小一样,为了充分利用emmc的空间,需要在第一次运行时扩充分区大小。根据parameter.txt中rootfs分区对应的名称配置, 默认是对/dev/mmcblk0p6分区进行扩充。创建一个脚本和服务来扩充分区。
vi etc/init.d/firstboot.sh


# 以下是firstboot.sh的内容
#!/bin/bash -e
# first boot configure
# resize filesystem mmcblk0p6
if [ ! -e "/usr/local/first_boot_flag" ] ;
then
  echo "Resizing /dev/mmcblk0p6..."
  resize2fs /dev/mmcblk0p6
  touch /usr/local/first_boot_flag
fi

添加运行权限。
chmod +x etc/init.d/firstboot.sh
创建服务脚本
vi lib/systemd/system/firstboot.service

# 以下是firstboot.service的内容
#start
[Unit]
Description=Setup rockchip platform environment
Before=lightdm.service
After=resize-helper.service
[Service]
Type=simple
ExecStart=/etc/init.d/firstboot.sh
[Install]
WantedBy=multi-user.target
#end

启动firstboot.service服务。
systemctl enable firstboot.service

2.9、退出

#退出根文件系统
exit
#卸载挂载的根文件系统
./mount -u ubuntu-rootfs

三、制作文件系统rootfs分区固件

创建一个空镜像文件,将镜像文件挂载到空文件,修复及检测镜像文件系统
创建一个脚本
vi mkimage.sh
内容如下


#!/bin/bash
rootfs_dir=$1
rootfs_file=$2
rootfs_mnt="mnt"
if [ ! $rootfs_dir ] || [ ! $rootfs_file ];
then
  echo "Folder or target is empty."
  exit 0
fi
if [ -f "$rootfs_file" ]; then
  echo "-- Delete exist $rootfs_file ..."
  rm -f "$rootfs_file"
fi
echo "-- Create $rootfs_file ..."
dd if=/dev/zero of="$rootfs_file" bs=1M count=6144
sudo mkfs.ext4 -F -L linuxroot "$rootfs_file"
if [ ! -d "$rootfs_mnt" ]; then
  mkdir $rootfs_mnt
fi
echo "-- Copy data to $rootfs_file ..."
sudo mount $rootfs_file $rootfs_mnt
sudo cp -rfp $rootfs_dir/* $rootfs_mnt
sudo sync
sudo umount $rootfs_mnt
rm -r $rootfs_mnt
echo "-- Resize $rootfs_file ..."
/sbin/e2fsck -p -f "$rootfs_file"
/sbin/resize2fs -M "$rootfs_file"
echo "-- Done."

运行,制作镜像,制作完成后即可进行烧录。
./mkimage.sh ubuntu-rootfs rootfs.img

分布制作文件系统rootfs分区固件

3.1、创建一个空镜像文件,大小为8G
dd if=/dev/zero of=ubuntu_rootfs.img bs=1M count=8192‬
将该文件格式化为ext4文件系统
mkfs.ext4 ubuntu_rootfs.img
3.2、将镜像文件挂载到空文件

mkdir ubuntu_base_rootfs
chmod 777 ubuntu_base_rootfs
mount ubuntu_rootfs.img ubuntu_base_rootfs
cp -rfp ubuntu_rootfs/* ubuntu_base_rootfs/
umount ubuntu_base_rootfs/

3.3、修复及检测镜像文件系统
修复及检测镜像文件系统
e2fsck -p -f ubuntu_rootfs.img
resize2fs减小镜像文件的大小,上述的2.8小节中的释放rootfs分区大小与这里减少文件大小相关。
resize2fs -M ubuntu_rootfs.img
查看大小
du -sh ubuntu_rootfs.img

有问题尽请留言,有时间会回复大家。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值