IMX6ULL - Linux根文件系統(rootfs)構建


Author: zzssdd2

E-mail: zzssdd2@foxmail.com

一、Ubuntu-base20.04.3

Ubuntu官方已經制作好了各架構、各版本的base版根文件系統,只需下載下來做少許改動即可。

  1. 下載Ubuntu Base 20.04.3 LTS (Focal Fossa)

    ubuntu-base-20.04.3-base-armhf.tar.gz	2021-08-19 10:56	22M
    
  2. 解壓

    mkdir rootfs
    sudo chmod 777 rootfs
    tar -zxvf ubuntu-base-20.04.3-base-armhf.tar.gz -C rootfs
    #避免后面更新軟件報錯
    sudo chmod 777 ./rootfs/tmp/
    
  3. 安裝工具

    sudo apt-get install qemu-user-static
    sudo cp /usr/bin/qemu-arm-static ./rootfs/usr/bin/
    
  4. 拷貝主機DNS

    sudo cp /etc/resolv.conf ./rootfs/etc/resolv.conf
    
  5. 更換下載源

    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`
    
  6. 創建掛載腳本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 
    
  7. 掛載

    source mount.sh -m ./rootfs/
    
  8. 更新軟件

    apt update
    apt install sudo vim kmod net-tools ethtool ifupdown rsyslog htop iputils-ping language-pack-en-base ssh
    

    注意:ssh必須安裝,不然后面鏈接串口服務會出錯

  9. 設置用戶名和密碼

    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`
    
  10. 設置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
    
  11. 設置串口終端

    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會創建該目錄,然后重新執行一遍設置串口終端命令即可。

  12. 退出

    #退出qemu模擬器
    exit
    #取消掛載
    source mount.sh -u ./rootfs/
    #打包
    sudo tar -czvf ubuntu-base-20.04.3-rootfs.tar.gz rootfs/*
    

二、Debian 11

  • 直接使用三方制作好的Debian根文件系統,比如Linaro
  • 使用MultistrapDebootstrap工具構建自己的Debian根文件系統

下面我使用debootstrap工具構建Debian11根文件系統(別問為什么不用multistrap,問就是不會︶︹︺)

  1. 安裝工具

    sudo apt install binfmt-support qemu qemu-user-static debootstrap
    
  2. 抽取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
    
  3. 復制qemu-arm-static到剛構建的基本系統中

    sudo cp /usr/bin/qemu-arm-static ./rootfs/usr/bin
    
  4. 完成文件系統引導

    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向用戶詢問任何問題(非交互式),相當於是靜默配置安裝。

      了解更多debconf - Debian Wiki

    • debootstrap

      man debootstrap
      或
      debootstrap --help
      

      了解更多Debootstrap - Debian Wiki

    完成后提示如下:

    I: Base system installed successfully.
    
  5. 更換下載源

    #備份
    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
    
  6. 創建掛載腳本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 
    
  7. 掛載

    source mount.sh -m ./rootfs/
    
  8. 執行以下命令

    # <file system> <mount point> <type> <options> <dump> <pass>
    echo "proc /proc proc defaults 0 0" >> etc/fstab 
    
  9. 設置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
    
  10. 安裝軟件

    apt update
    
    apt install sudo
    apt install net-tools
    apt install ethtool 
    apt install htop 
    apt install ssh
    ......
    
  11. 設置用戶

    #設置root用戶密碼
    passwd root
    #添加新用戶
    adduser zzssdd2
    
  12. 設置以太網

    echo "auto eth0" > /etc/network/interfaces.d/eth0
    echo "iface eth0 inet dhcp" >> /etc/network/interfaces.d/eth0
    
  13. 設置IP和名稱

    echo "zzssdd2-imx6ull" > /etc/hostname
    echo "127.0.0.1 localhost" >> /etc/hosts
    echo "127.0.0.1 zzssdd2-imx6ull" >> /etc/hosts
    
  14. 退出

    #退出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平台進行配置。

  1. 下載:https://buildroot.org/download.html

  2. 解壓 :tar -jxvf buildroot-2021.08.1.tar.bz2

  3. 進入圖形配置界面:make menuconfig

    注:下面貼出來的都是經過手動配置更改的選項,沒有貼出來的則表示使用默認設置

  4. 配置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)
    
  5. 配置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?
    
  6. 配置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 
    
  7. 配置Target packages

    這一步根據自己的需求選擇安裝需要的包即可(這里為了下一步編譯快一些我保持默認選擇)。

  8. 按需配置完成后:Save --> Exit --> 執行make命令等待編譯完成后在Buildroot根目錄下的output/images目錄下可以看到編譯好的根文件系統。

    注意:

    1. 如果編譯之前執行過make則需要先執行make clean清理一下再執行make命令,否則可能報錯。
    2. 如果遇到某些包下載巨慢、下不動、下載失敗的情況,可以根據編譯信息提示的地址復制到瀏覽器手動下載。下載下來后將其復制到Buildroot根目錄下的dl目錄下,然后CTRL+C終止編譯 --> make clean清除上次編譯 --> make重新編譯。

四、驗證

參考開發板直連電腦搭建NFS&TFTP環境第四節


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM