嵌入式中利用ubuntu-base构建根文件系统
在嵌入式環境中,我們最常用busybox、yocto或buildroot來構建根文件系統,這種情況下,當我們需要使用一些組件的時候,往往需要我們使用源碼編譯,移植到開發板中。那有沒有像ubuntu的文件系統一樣,很多工具一個apt命令搞定,答案肯定是可以的。
一、介紹
ubuntu-base 是Ubuntu官方構建的ubuntu最小文件系統,基礎包大小通常只有幾十兆,但是依托包debain軟件包管理器,可以直接使用ubuntu豐富的軟件源,很方便的個性化定制嵌入式環境。本文使用的是arm架構的板子,故下面的介紹都是基于arm的,X86、powerpc、ppc等其他架構類似。
二、環境配置
2.1、下載ubuntu-base
下載ubuntu-base的方式有很多,我們可以從官方的地址下載,也可以其它鏡像地址下載,如清華源,根據需要,下載自己對應的發行版本,這里我們就已20.04為例。
?進入到對應的版本目錄后,根據我們的CPU類型,選擇對應的文件下載,這里,我們使用的是imx6ul,32位的CPU,我們選擇armhf的版本
?下載完成后,在宿主機上選擇建立一個rootfs目錄,將下載的文件ubuntu-base-20.04.4-base-armhf.tar.gz解壓到這個目錄,需要注意的是:解壓后的文件,需要保留ubuntu-base中的文件權限及所有者,解壓時需要root權限或者sudo操作,且使用-p參數保留權限
sudo tar -xpvf ubuntu-base-20.04.4-base-armhf.tar.gz -C rootfs/2.2、安裝qemu
因為我們在x86上掛載配置arm的文件系統,需要使用qemu的環境,ubuntu下安裝命令如下:
sudo apt-get install qemu-user-static將剛剛安裝的qemu-user-static復制到rootfs目錄中的/usr/bin目錄,命令如下:
sudo cp /usr/bin/qemu-arm-static ./usr/bin/2.3、設置軟件源
為了能讓ubuntu-base正確掛載且能上網,將 Ubuntu 主機下的 DNS 配置文件/etc/resolv.conf 拷貝到根文件系統中
sudo cp /etc/resolv.conf ./etc/resolv.conf至于軟件源,默認情況下,ubuntu的軟件源使用的是http的方式,國內存在訪問慢和hash不匹配的情況,影響使用體驗。
這里我們使用清華源?ubuntu-ports | 鏡像站使用幫助 | 清華大學開源軟件鏡像站 | Tsinghua Open Source Mirror
上面的軟件源地址使用的是https,在使用的過程中會報如下的錯誤,
Reading package lists... Done E: The method driver /usr/lib/apt/methods/https could not be found. N: Is the package apt-transport-https installed? E: The method driver /usr/lib/apt/methods/https could not be found. N: Is the package apt-transport-https installed? E: The method driver /usr/lib/apt/methods/https could not be found. N: Is the package apt-transport-https installed? E: The method driver /usr/lib/apt/methods/https could not be found. N: Is the package apt-transport-https installed? E: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/dists/xenial/InRelease E: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/dists/xenial-updates/InRelease E: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/dists/xenial-backports/InRelease E: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/dists/xenial-security/InRelease E: Some index files failed to download. They have been ignored, or old ones used instead.?故需要在切換上面的源之前,先安裝?apt-transport-https。具體步驟如下:
1、掛載文件系統,可以使用下面的兩種方式之一
? ? ? ? a、需要編寫一個掛載腳本和卸載腳本,本文就不列出了,可以參考這個文章
? ? ? ? b、使用arch-chroot
????????linux發行版提供了一個自動化chroot的腳本arch-chroot,包含自動配置DNS文件、自動掛載虛擬文件系統等操作,用來維護linux系統非常方便,chroot時無需掛載等操作直接執行
2、安裝 apt-transport-https 工具
apt update apt install apt-transport-https完成后切換上面的清華源即可,這樣我們就可以使用https的源了,使用方式與PC的ubuntu一致
三、設置用戶及常用軟件安裝
1、軟件安裝
默認下,ubuntu-base基本沒安裝什么工具,如果要正常使用,我們可以根據需要安裝,以下基本的幾個建議都進行安裝
apt update apt install sudo apt install vim apt install net-tools apt install ethtool apt install ifupdown apt install language-pack-en-base apt install iputils-ping apt install openssh-sftp-server2、設置用戶,這個根據具體的情況,嵌入式的環境下,我們可以不用設置,直接使用root的用戶就行了,如果要設置,執行如下的步驟:
# 1、設置root用戶的密碼 passwd root #2、新增用戶imx6ul adduser imx6ul #3、設置用戶使用sudo 權限 前提條件是已經安裝了sudo3、設置本機的名稱和本地的IP信息
echo "imx6ul" > /etc/hostname echo "127.0.0.1 localhost" >> /etc/hosts echo "127.0.0.1 imx6ul" >> /etc/hosts4、配置網絡,啟動DHCP
echo auto eth0 > /etc/network/interfaces.d/eth0 echo iface eth0 inet dhcp >> /etc/network/interfaces.d/eth0 /etc/init.d/networking restart總結
以上是生活随笔為你收集整理的嵌入式中利用ubuntu-base构建根文件系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 文字竖排效果
- 下一篇: ad中按钮开关的符号_电工新手必经之路: