Author: zzssdd2
E-mail: zzssdd2@foxmail.com
一、Ubuntu-base20.04.3
Ubuntu官方已經制作好了各架構、各版本的base版根文件系統,只需下載下來做少許改動即可。
-
下載Ubuntu Base 20.04.3 LTS (Focal Fossa)
ubuntu-base-20.04.3-base-armhf.tar.gz 2021-08-19 10:56 22M
-
解壓
mkdir rootfs sudo chmod 777 rootfs tar -zxvf ubuntu-base-20.04.3-base-armhf.tar.gz -C rootfs #避免后面更新軟件報錯 sudo chmod 777 ./rootfs/tmp/
-
安裝工具
sudo apt-get install qemu-user-static sudo cp /usr/bin/qemu-arm-static ./rootfs/usr/bin/
-
拷貝主機DNS
sudo cp /etc/resolv.conf ./rootfs/etc/resolv.conf
-
更換下載源
cp ./rootfs/etc/apt/source.list ./rootfs/etc/apt/source.list.bak vim ./rootfs/etc/apt/source.list `Esc` + `:` %s/ports.ubuntu.com/mirror.tuna.tsinghua.edu.cn/g `Enter`
-
創建掛載腳本
vim mount.sh
內容如下:
#!/bin/bash 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} } 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
更改權限:
sudo chmod +x mount.sh
-
掛載
source mount.sh -m ./rootfs/
-
更新軟件
apt update apt install sudo vim kmod net-tools ethtool ifupdown rsyslog htop iputils-ping language-pack-en-base ssh
注意:
ssh
必須安裝,不然后面鏈接串口服務會出錯 -
設置用戶名和密碼
root@zsd-virtual-machine:/# passwd root New password: Retype new password: passwd: password updated successfully root@zsd-virtual-machine:/# adduser zsd New password: Retype new password: `Enter` `Enter` ...... `Enter` `Enter`
-
設置IP和名稱
root@zsd-virtual-machine:/# echo "zzssdd2-imx6ull" > /etc/hostname root@zsd-virtual-machine:/# echo "127.0.0.1 localhost" >> /etc/hosts root@zsd-virtual-machine:/# echo "127.0.0.1 zzssdd2-imx6ull" >> /etc/hosts
-
設置串口終端
ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttymxc0.service
注意:
如果提示報錯提示如下:
root@zsd-virtual-machine:/# ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttymxc0.service /usr/bin/ln: failed to create symbolic link '/etc/systemd/system/getty.target.wants/getty@ttymxc0.service': No such file or directory
說明
/etc/systemd/system/
目錄下沒有getty.target.wants
目錄。解決方法:安裝
ssh
會創建該目錄,然后重新執行一遍設置串口終端命令即可。 -
退出
#退出qemu模擬器 exit #取消掛載 source mount.sh -u ./rootfs/ #打包 sudo tar -czvf ubuntu-base-20.04.3-rootfs.tar.gz rootfs/*
二、Debian 11
- 直接使用三方制作好的Debian根文件系統,比如Linaro
- 使用Multistrap或Debootstrap工具構建自己的Debian根文件系統
下面我使用debootstrap
工具構建Debian11根文件系統(別問為什么不用multistrap,問就是不會︶︹︺)
-
安裝工具
sudo apt install binfmt-support qemu qemu-user-static debootstrap
-
抽取Debain文件系統
sudo debootstrap --arch=armhf --foreign bullseye rootfs https://mirrors.tuna.tsinghua.edu.cn/debian/
參數說明:
arch
:CPU架構bullseye
:debian版本名,這是版本11的名稱foreign
:在與主機架構不相同時需要指定此參數,僅做初始化的解包rootfs
:要存放文件系統的文件夾https://mirrors.tuna.tsinghua.edu.cn/debian/
:下載源,這里使用國內的清華源抽取成功后如下:
$ ls ./rootfs/ bin debootstrap etc lib root sbin tmp var boot dev home proc run sys usr
-
復制
qemu-arm-static
到剛構建的基本系統中sudo cp /usr/bin/qemu-arm-static ./rootfs/usr/bin
-
完成文件系統引導
sudo DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true chroot rootfs debootstrap/debootstrap --second-stage
參數說明:
-
chroot rootfs
更改根目錄為
rootfs
:就是變更當前進程及其子進程的可見根路徑。變更后,程序無法訪問可見根目錄外文件和命令。 -
DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
避免
debconf
向用戶詢問任何問題(非交互式),相當於是靜默配置安裝。 -
debootstrap
man debootstrap 或 debootstrap --help
完成后提示如下:
I: Base system installed successfully.
-
-
更換下載源
#備份 sudo cp ./rootfs/etc/apt/sources.list ./rootfs/etc/apt/sources.list.bak #編輯 sudo vim ./rootfs/etc/apt/sources.list #換成如下內容: # 默認注釋了源碼鏡像以提高 apt update 速度,如有需要可自行取消注釋 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
-
創建掛載腳本
vim mount.sh
內容如下:
#!/bin/bash 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} } 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
更改權限:
sudo chmod +x mount.sh
-
掛載
source mount.sh -m ./rootfs/
-
執行以下命令
# <file system> <mount point> <type> <options> <dump> <pass> echo "proc /proc proc defaults 0 0" >> etc/fstab
-
設置Linux中的
locale
環境參數export LC_ALL=C
如果不設置在后面安裝軟件時會有如下提示:
perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = "zh_CN:en_US:en", LC_ALL = (unset), LC_PAPER = "zh_CN.UTF-8", LC_NUMERIC = "zh_CN.UTF-8", LC_IDENTIFICATION = "zh_CN.UTF-8", LC_MEASUREMENT = "zh_CN.UTF-8", LC_NAME = "zh_CN.UTF-8", LC_TELEPHONE = "zh_CN.UTF-8", LC_ADDRESS = "zh_CN.UTF-8", LC_MONETARY = "zh_CN.UTF-8", LC_TIME = "zh_CN.UTF-8", LANG = "zh_CN.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). /usr/bin/locale: Cannot set LC_CTYPE to default locale: No such file or directory /usr/bin/locale: Cannot set LC_MESSAGES to default locale: No such file or directory /usr/bin/locale: Cannot set LC_ALL to default locale: No such file or directory
-
安裝軟件
apt update apt install sudo apt install net-tools apt install ethtool apt install htop apt install ssh ......
-
設置用戶
#設置root用戶密碼 passwd root #添加新用戶 adduser zzssdd2
-
設置以太網
echo "auto eth0" > /etc/network/interfaces.d/eth0 echo "iface eth0 inet dhcp" >> /etc/network/interfaces.d/eth0
-
設置IP和名稱
echo "zzssdd2-imx6ull" > /etc/hostname echo "127.0.0.1 localhost" >> /etc/hosts echo "127.0.0.1 zzssdd2-imx6ull" >> /etc/hosts
-
退出
#退出qemu環境 exit # 取消掛載 source mount.sh -u ./rootfs/ #打包 sudo tar -zcvf debian_bullseye-rootfs.tar.gz ./rootfs/*
三、Buildroot
Buildroot是一個簡單、高效且易於使用的工具,可以通過交叉編譯生成嵌入式Linux系統。更詳細的了解參考官方手冊The Buildroot user manual
下面使用buildroot-2021.08.1
版本對IMX6ULL平台進行配置。
-
解壓 :
tar -jxvf buildroot-2021.08.1.tar.bz2
-
進入圖形配置界面:
make menuconfig
注:下面貼出來的都是經過手動配置更改的選項,沒有貼出來的則表示使用默認設置
-
配置
Target options
Target options ---> Target Architecture (ARM (little endian)) Target Binary Format (ELF) Target Architecture Variant (cortex-A7) Target ABI (EABIhf) Floating point strategy (NEON/VFPv4) ARM instruction set (ARM)
-
配置
Toolchain
Toolchain ---> Toolchain type (External toolchain) Toolchain (Custom toolchain) Toolchain origin (Pre-installed toolchain) (/usr/local/arm/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf) Toolchain path ($(ARCH)-none-linux-gnueabihf) Toolchain prefix External toolchain gcc version (10.x) External toolchain kernel headers series (4.20.x) External toolchain C library (glibc/eglibc) [ ] Toolchain has RPC support? [*] Toolchain has C++ support?
-
配置
System configuration
System configuration ---> (zzssdd2) System hostname (Hello zzssdd2) System banner /dev management (Dynamic using devtmpfs + mdev) (root) Root password (eth0) Network interface to configure through DHCP
-
配置
Target packages
這一步根據自己的需求選擇安裝需要的包即可(這里為了下一步編譯快一些我保持默認選擇)。
-
按需配置完成后:
Save
-->Exit
--> 執行make
命令等待編譯完成后在Buildroot根目錄下的output/images
目錄下可以看到編譯好的根文件系統。注意:
- 如果編譯之前執行過
make
則需要先執行make clean
清理一下再執行make
命令,否則可能報錯。 - 如果遇到某些包下載巨慢、下不動、下載失敗的情況,可以根據編譯信息提示的地址復制到瀏覽器手動下載。下載下來后將其復制到Buildroot根目錄下的
dl
目錄下,然后CTRL+C
終止編譯 -->make clean
清除上次編譯 -->make
重新編譯。
- 如果編譯之前執行過
四、驗證
參考開發板直連電腦搭建NFS&TFTP環境第四節