Creating Ubuntu 20.04 Root Filesystem on RK3399

Click the above“Embedded and Linux Matters”, select“Pin/Star Public Account”

Benefits and resources delivered immediately

The file system that comes with Firefly lacks some basic functional modules, so we can manually create a Ubuntu 20.04 file system.

Download Ubuntu Root Filesystem

http://cdimage.ubuntu.com/ubuntu-base/releases/

Install Virtual Machine

apt-get install qemu-user-static

Extract

mkdir ubuntu-rootfs
tar -xpf ubuntu-base-20.04.5-base-arm64.tar.gz  -C ubuntu-rootfs

Copy the virtual machine’s runtime environment

cp -b /etc/resolv.conf ubuntu-rootfs/etc/resolv.conf
cp /usr/bin/qemu-aarch64-static ubuntu-rootfs/usr/bin/

Copy Official Peripheral Driver Files

Mount the development board to the virtual machine and copy the /vendor, /system, /lib/firmware folders from the development board to our own file system.

Mount Root Filesystem

Create a ch-mount.sh file and write the following script

#!/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
    echo "CHROOT..."
    sudo chroot ${2}
    echo "Success!"
}
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

Place the downloaded script in the parent directory of ubuntu-rootfs and change permissions:

chmod a+x ch-mount.sh
./ch-mount.sh -m ubuntu-rootfs/

Execute the mount action, and remember to execute ./ch-mount.sh -u ubuntu-rootfs/ after exiting, otherwise your local Ubuntu system will have issues and will require a restart to recover.

Copy Bash Dependency Libraries

If you encounter the following error when executing the mount script, you can copy the Bash dynamic libraries.

➜  tools sudo chroot ubuntu-rootfs 
chroot: failed to run command ‘/bin/zsh’: No such file or directory

Check the dynamic link libraries that /bin/bash depends on, and copy them to the corresponding directory. Since I installed zsh, in addition to copying the Bash dynamic libraries, I also need to copy the zsh dynamic libraries. If zsh is not installed, you can skip copying the zsh dynamic libraries.

zhongyi@ubuntu:~$ ldd /bin/zsh
        linux-vdso.so.1 (0x00007ffd5c1dc000)
        libcap.so.2 => /lib/x86_64-linux-gnu/libcap.so.2 (0x00007f4b4d9d7000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4b4d7d3000)
        libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f4b4d5a9000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4b4d20b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4b4ce1a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4b4debc000)
zhongyi@ubuntu:~$ ldd /bin/bash
        linux-vdso.so.1 (0x00007ffd8335a000)
        libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f50a4d2a000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f50a4b26000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f50a4735000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f50a526e000)
$ mkdir lib64
$ cp /lib64/ld-linux-x86-64.so.2 ./lib64/
$ mkdir ./lib/x86_64-linux-gnu
$ cp /lib/x86_64-linux-gnu/libtinfo.so.5 ./lib/x86_64-linux-gnu/
$ cp /lib/x86_64-linux-gnu/libdl.so.2 ./lib/x86_64-linux-gnu/
$ cp /lib/x86_64-linux-gnu/libc.so.6 ./lib/x86_64-linux-gnu/ 
$ cp /lib/x86_64-linux-gnu/libcap.so.2 ./lib/x86_64-linux-gnu/ 
$ cp /lib/x86_64-linux-gnu/libm.so.6 ./lib/x86_64-linux-gnu/ 
$ cp /lib/x86_64-linux-gnu/libc.so.6 ./lib/x86_64-linux-gnu/ 
$cp /bin/bash ubuntu-rootfs/bin
$cp /bin/zsh ubuntu-rootfs/bin

Execute Mount

After a successful mount, you will see that it automatically switches to the root user.

zhongyi@ubuntu:~/tools$ ./ch-mount.sh -m ubuntu-rootfs/
MOUNTING...
CHROOT...
zsh: failed to load module `zsh/zle': /usr/lib/x86_64-linux-gnu/zsh/5.4.2/zsh/zle.so: cannot open shared object file: No such file or directory     
# Successfully mounted
ubuntu# 

Install Necessary Software

apt-get update
apt-get -y install  vim nfs-common  sudo ssh net-tools ethtool wireless-tools  xfce4-power-manager xinit  network-manager iputils-ping rsyslog   bash-completion lxtask htop  synaptic  --no-install-recommends

Change Source

vim /etc/apt/source.list
# Add USTC Source
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe

# Add Aliyun Source
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

# Add Tsinghua Source
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

Set Username and Password

useradd -s '/bin/bash' -m -G adm,sudo firefly
passwd firefly
passwd root

Set Hostname

echo 'ubuntu.firefly' > /etc/hostname

Add Host Entry to /etc/hosts

127.0.0.1 localhost 
127.0.0.1 ubuntu.firefly
127.0.1.1 firefly 

Add Nameserver

vim /etc/resolvconf/resolv.conf.d/head
# Add nameserver
nameserver 114.114.114.114
nameserver 8.8.8.8

Unmount

exit
./ch-mount.sh -u ubuntu-rootfs/

Create mkrootfs.sh file, write the following script, where 2048 depends on the actual size of your root filesystem. For example, if you installed a desktop environment and the size exceeds 2048M, increase it a bit.

#!/bin/bash
# 
dd if=/dev/zero of=ubuntu-rootfs.img bs=1M count=2048
sudo  mkfs.ext4  ubuntu-rootfs.img
rm -r rootfs
mkdir  rootfs
sudo mount ubuntu-rootfs.img rootfs/
sudo cp -rfp ubuntu-rootfs/*  rootfs/
sudo umount rootfs/
e2fsck -p -f ubuntu-rootfs.img
resize2fs  -M ubuntu-rootfs.img

Execute the creation

➜  tools ./mkrootfs.sh
➜  tools ls -al |grep *.img
-rw-rw-r--  1 zhongyi zhongyi 1497706496 Sep 30 21:32 ubuntu-rootfs.img

Flash Image

sudo upgrade_tool di -rootfs ubuntu-rootfs.img

References

https://blog.csdn.net/Alex_Vinci/article/details/122818828

https://dev.t-firefly.com/thread-12739-1-1.html

https://blog.csdn.net/vcsuanfadaima/article/details/112280308

https://www.csdn.net/tags/OtTaIg1sNTAwNTQtYmxvZwO0O0OO0O0O0O.html

end

Previous Recommendations

[Recommended to Collect] How Does MMU Complete Address Translation?

Must-Read Classic Books for Embedded Linux

Detailed Explanation of container_of Macro in Linux Kernel

Recommended Learning Path for Embedded Systems

Creating Ubuntu 20.04 Root Filesystem on RK3399
Creating Ubuntu 20.04 Root Filesystem on RK3399

Scan to Add Me on WeChat

Join Technical Discussion Group

Leave a Comment