在嵌入式环境中,我们最常用busybox、yocto或buildroot来构建根文件系统,这种情况下,当我们需要使用一些组件的时候,往往需要我们使用源码编译,移植到开发板中。那有没有像ubuntu的文件系统一样,很多工具一个apt命令搞定,答案肯定是可以的。
ubuntu-base 是Ubuntu官方构建的ubuntu最小文件系统,基础包大小通常只有几十兆,但是依托包debain软件包管理器,可以直接使用ubuntu丰富的软件源,很方便的个性化定制嵌入式环境。本文使用的是arm架构的板子,故下面的介绍都是基于arm的,X86、powerpc、ppc等其他架构类似。
下载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-server
2、设置用户,这个根据具体的情况,嵌入式的环境下,我们可以不用设置,直接使用root的用户就行了,如果要设置,执行如下的步骤:
# 1、设置root用户的密码
passwd root
#2、新增用户imx6ul
adduser imx6ul
#3、设置用户使用sudo 权限 前提条件是已经安装了sudo
3、设置本机的名称和本地的IP信息
echo "imx6ul" > /etc/hostname
echo "127.0.0.1 localhost" >> /etc/hosts
echo "127.0.0.1 imx6ul" >> /etc/hosts
4、配置网络,启动DHCP
echo auto eth0 > /etc/network/interfaces.d/eth0
echo iface eth0 inet dhcp >> /etc/network/interfaces.d/eth0
/etc/init.d/networking restart