rk3568移植搭建Ubuntu20.04.5根文件系统

发布时间 2023-12-15 20:10:29作者: M-kobe

一.下载ubuntu-base
https://cdimage.ubuntu.com/ubuntu-base/releases/20.04.5/release/
下载固件:buntu-base-20.04.5-base-arm64.tar.gz

根文件系统创建目录ubuntu_rootfs,并解压到该目录:

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

 

二.构建根文件系统

2.1、配置网络配置

cp /etc/resolv.conf  ubuntu_rootfs/etc/

 

2.2、配置仿真开发环境

sudo apt install qemu-user-static
sudo cp /usr/bin/qemu-aarch64-static ubuntu_rootfs/usr/bin/

2.3、更换软件源

1 sudo vim 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.4、挂载根文件系统

  2.4.1、编写mount.sh脚本

  vim mount.h

#!/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 tralling '/')"
    echo ""
    echo "For example: ch-mount -m /media/sdcard"
    echo ""
    echo 1st parameter : ${1}
    echo 2nd parameter : $[2]
fi

 

  2.4.2、增加权限、挂载、卸载

#增加脚本执行权限
sudo chmod +x mount.sh
#挂载根文件系统
./mount.sh -m ubuntu_rootfs/
#退出根文件系统
exit
#卸载根文件系统
./mount.sh -u ubuntu_rootfs/

 

2.5、安装一些必要软件

  先apt-get update,发现有以下报错:

root@m5280:/# apt-get update
        Get:1 http://mirrors.aliyun.com/ubuntu-ports focal InRelease [265 kB]
        Get:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates InRelease [114 kB]
        Err:1 http://mirrors.aliyun.com/ubuntu-ports focal InRelease
          Couldn't create temporary file /tmp/apt.conf.wxDXeG for passing config to apt-key
        Err:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates InRelease
          Couldn't create temporary file /tmp/apt.conf.IPDhqR for passing config to apt-key
        Get:3 http://mirrors.aliyun.com/ubuntu-ports focal-backports InRelease [108 kB]
        Err:3 http://mirrors.aliyun.com/ubuntu-ports focal-backports InRelease
          Couldn't create temporary file /tmp/apt.conf.6tYJ0X for passing config to apt-key
        Get:4 http://mirrors.aliyun.com/ubuntu-ports focal-security InRelease [114 kB]
        Err:4 http://mirrors.aliyun.com/ubuntu-ports focal-security InRelease
          Couldn't create temporary file /tmp/apt.conf.TLSc47 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.wxDXeG 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.IPDhqR 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.6tYJ0X 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.TLSc47 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.

  借鉴网友的经验:https://blog.csdn.net/yinfang1252/article/details/105342449,需要改下tmp的权限:

chmod 777 /tmp

  工具安装:

apt update
apt upgrade
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    

 

2.6、卸载一些不必要的软件

apt-get remove --purge lubuntu-update-notifier 
apt-get remove --purge libreoffice* 

 

2.7、安装轻量级的桌面lubuntu-desktop

apt-get install ubuntu-desktop

 

2.8、添加用户:

adduser ubuntu
密码:123456

 

2.9、sudo权限:

chmod u+w /etc/sudoers
vim /etc/sudoers

然后在root的下一行添加新的用户【developer】,注意中间的间隔是一个TAB键:

# User privilege specification
root    ALL=(ALL:ALL) ALL
ubuntu    ALL=(ALL:ALL) ALL

 

2.10、启用ssh的root帐号登录

vi /etc/ssh/sshd_config
# 将下面这项设置成yes
PermitRootLogin yes

 

2.11、设置主机名称和IP

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

 

2.12、配置DHCP和网卡白名单

RK3568有两个网络端口,相应的在配置的时候我们要将两个网口都配置上
#修改网络配置文件
vim /etc/network/interfaces

以下是该文件具体内容

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
auto eth0
iface eth0 inet dhcp
auto eth1
iface eth1 inet dhcp
source-directory /etc/network/interfaces.d

 

配置网卡:Network-Manager服务会自动配置网卡,但是其默认配置文件将Ethernet加入了黑名单,以至于以太网不能用,找不到网卡

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

 

在实际测试中网口必须接入网线系统才能正常启动,就是在不联网的情况下,每次开机都要等待很久,
卡在网络连接上5分钟,这里我们可以修改下面这个文件:

vim /lib/systemd/system/networking.service
//将里面的TimeoutStartSec=5min修改为
TimeoutStartSec=5sec

 

2.13、修改系统重启默认等待时间

vim /etc/systemd/system.conf
解除注释并将 DefaultTimeoutStopSec=90s 改为:
DefaultTimeoutStopSec=3s

 

2.14、禁用系统休眠:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

实测加了这条指令,系统也会进入休眠。why?

 

2.15、设置开机串口免密登录到图形界面

  2.15.1

vim /etc/gdm3/custom.conf
添加
[daemon]
AutomaticLoginEnable=true
AutomaticLogin=ubuntu
TimedLoginEnable=true
TimedLogin=root
TimedLoginDelay=10

  2.15.2

vim /etc/pam.d/gdm-password 
注释:#auth required pam_succeed_if.so user != root quiet_success

  2.15.3

vim /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
末行添加: greeter
-show-manual-login=true all-guest=false

  2.15.4

vim /etc/pam.d/gdm-autologin
注释:#auth   required        pam_succeed_if.so user != root quiet_success

  2.15.5、

vi /root/.profile
将文件最后一行mesg n 2> /dev/null || true改为以下内容:
tty -s && mesg n || true

  2.15.6、串口自动登录

vim /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.16、添加分区释放的系统服务(非常重要)

  2.16.1、默认是对/dev/mmcblk0p6分区进行扩充,创建一个脚本firstboot.sh和服务来扩充分区:

vim etc/init.d/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

  2.16.2、创建服务firstboot.service去运行脚本

vim lib/systemd/system/firstboot.service
#start
[Unit]
Description=Setup wmc-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
启动服务:systemctl enable firstboot.service
查看服务状态:    systemctl status firstboot

一开始,由于服务service写的有问题,导致脚本没有运行到,扩容没有成功,由于/目录没有磁盘空间了,也就进不了ubuntu桌面环境的,一直卡在开机logo画面,但通过串口实际已经进入到系统了。可以手动执行resize2fs /dev/mmcblk0p6,然后重启就正常进入到桌面了。

 

三.打包根文件系统镜像

3.1、创建空镜像文件,大小为6144MB

dd if=/dev/zero of=ubuntu_rootfs.img bs=1M count=6144

 

3.2、将该文件格式化成ext4文件系统

mkfs.ext4 ubuntu_rootfs.img

 

3.3、将该镜像文件挂载到一个空的文件夹ubuntu_base_rootfs上,然后将ubuntu_rootfs的文件复制到该空文件夹中

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


3.4、复制完后用e2fsck修复及检测镜像文件系统,resize2fs 减小镜像文件的大小

sudo umount ubuntu_base_rootfs/
e2fsck -p -f ubuntu_rootfs.img
resize2fs -M ubuntu_rootfs.img

至此,文件系统镜像制作完成。

四、将文件系统烧录到主板

为了简便,先在主板上烧录调试好的debian系统,然后再单独烧录Ubuntu文件系统