基于ubuntu-base进行根文件系统的修改与打包

对了,我最近开通了微信公众号,计划是两边会同步更新,并逐步的会将博客上的文章同步至公众号中。
感兴趣的朋友可以扫描下方的二维码或者搜索“里先森sements”来关注,欢迎来玩~!
在这里插入图片描述


基础性笔记。本文介绍了如何下载并以ubuntu-base根文件系统为基础,在其上进行自定义的修改,并打包为img文件的过程。

1 - 下载ubuntu-base

去到 http://cdimage.ubuntu.com/ubuntu-base/releases/18.04.4/release/ 选择你平台上使用的 ubuntu-base。
值得注意的是,在嵌入式平台上,ubuntu-base只有armhf与arm64(aarch64)版本,没有arm软浮点处理器版本的支持。

下载完成,解压到目录,这里我们解压到同路径的 raw-rootfs

$mkdir raw-rootfs
$sudo tar -xpf ubuntu-base-18.04-base-arm64.tar.gz -C raw-rootfs/

-x:解压文件
-p:使用原来文件相同的权限来解压文件
-f:指定解压文件
-C:解压到

2 - 准备挂载根文件系统以进行修改

安装 qemu

$sudo apt install qemu-user-static

拷贝 qemu-你要部署到的平台的架构-static 到刚刚解压出来的目录

下载的arm64根文件系统则使用 qemu-aarch64-static
下载的armhf根文件系统则使用 qemu-arm-static

否则后续进行chroot的时候可能会提示:
chroot: cannot run command `/bin/bash’: No such file or directory

$sudo cp /usr/bin/qemu-aarch64-static ./raw-rootfs/usr/bin/

修改根文件系统软件源

$ sudo gvim raw-rootfs/etc/apt/sources.list

然后将软件源的地址全部替换,这里我使用的是中科大的软件源

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic main restricted
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-updates main restricted
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic universe
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-updates universe
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-updates multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-security main restricted
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-security main restricted
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-security universe
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-security universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ bionic-security multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/  bionic-security multiverse

拷贝本机的dns配置文件到根文件系统

$ sudo cp /etc/resolv.conf raw-rootfs/etc/resolv.conf

编写挂载脚本 mount.sh,注意缩进使用tab而不使用空格

#!/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 raw-rootfs/

我们便切换进入了ubuntu-base的根文件系统中
在这里插入图片描述
如果要退出并取消挂载可使用

root@ubuntu:/# exit
exit
$ source mount.sh -u raw-rootfs/

3 - 在ubuntu-base中安装些需要用的包

挂载根文件系统后

#apt update
#apt install sudo language-pack-en-base ssh net-tools ethtool ifupdown iputils-ping htop vim kmod network-manager xorg qt5-default openbox make
#apt upgrade
#apt install gcc g++
  • kmod: 是为了之后使用insmod与lsmod

  • net-tools: ifconfig命令

  • network-manager:使用nmcli之类的命令进行网络管理

  • xorg:安装好X window system环境

  • qt5-default:为了之后跑qt程序

  • openbox:窗口管理

  • make:用以执行Makefile

  • gcc/g++:安装编译器

    这里若是aarch64架构会自动匹配安装对应平台的编译器。例如我这里安装完后,gcc指向的是 aarch64-linux-gnu-gcc-7。查看 gcc -v 可知其生成对象目标平台

    在这里插入图片描述

4 - 添加一个管理员账号

先修改目前root账户的密码

# passwd

添加一个用户

# adduser 用户名

在 /etc/sudoers 文件中赋予权限

# vim /etc/sudoers

在这里插入图片描述
退出挂载的根文件系统

#exit
$source mount.sh -u raw-rootfs/

在这里插入图片描述

到这一步为止,我们已经修改好了一份根文件系统。可以将其使用下面的命令,复制到需要的磁盘位置进行使用

$ sudo cp -rfp raw-rootfs/* LeeUbuntuBase/

而如果你需要通过 img 镜像文件之类的方式,对板卡进行根文件系统的烧录,继续进行后续的镜像打包流程。

5 - 镜像打包

首先用 dd 工具创建一个空文件,这里conut的大小参考你的根文件系统(我这里即raw-rootfs)的大小,这里生成 2G

$ dd if=/dev/zero of=LeeUbuntuBase.img bs=1M count=2048

使用 mkfs.ext4 来格式化镜像文件,加入卷标 “rootfs”(卷标非必要?)

$ sudo mkfs.ext4 -F -L rootfs LeeUbuntuBase.img

-L:设置卷标
-F:强制格式化

将该镜像文件挂载到一个文件夹上,来读写。将你定制好的根文件系统(raw-rootfs文件夹内)内的文件拷贝到挂载的文件夹内以写入到 img 文件内。

$ mkdir LeeUbuntuBase
$ sudo mount LeeUbuntuBase.img LeeUbuntuBase
$ sudo cp -rfp raw-rootfs/* LeeUbuntuBase/

-r:递归文件夹
-f:强制
-p:不拷贝符号链接的目标文件,仅拷贝该符号链接

卸载镜像。e2fsck 检查镜像的文件系统。resize2fs 减小镜像文件的大小

$ sudo umount LeeUbuntuBase/
$ e2fsck -p -f LeeUbuntuBase.img
$ resize2fs -M LeeUbuntuBase.img

附录

1 - 开机启动rc.local并唤起图形桌面环境

在/etc/目录下建立 rc.local 文件,赋予其 +x 属性,然后在rc.local中添加

#!/bin/bash

#ifconfig eth0 up														#可选,唤起你的网卡
#dhclient	&																#可选,自带从dhcp获取ip地址

startx&
exit 0

这样,在linux启动后,会执行/etc/rc.local下的命令,即 startx

startx最终会调用/etc/X11/xinit/xinitrc下的命令

我们在X的配置文件(这里我用的是startx默认最终会选择的/etc/X11/xinit/xinitrc)修改如下

#!/bin/sh
# /etc/X11/xinit/xinitrc
#
# global xinitrc file, used by all X sessions started by xinit (startx)
# invoke global X session script

echo "LEE:xset turen off screensave DPMS"
xset s off																#关闭 X Screen Save
xset dpms 0 0 0														#关闭 DPMS
xset -dpms
          
xhost +  																	#允许任意host连接到x服务
          
export DISPLAY=:0.0 & /home/sements/developarea/qeyes/qeyes/qeyesArm64 &															#DISPLAY变量 并 #后台执行自己的一个小程序
        
#. /etc/X11/Xsession
openbox-session									#唤起 openbox

在Linux/Unix类操作系统上, DISPLAY用来设置将图形显示到何处. 直接登陆图形界面或者登陆命令行界面后使用startx启动图形, DISPLAY环境变量将自动设置为:0:0, 此时可以打开终端, 输出图形程序的名称(比如xclock)来启动程序, 图形将显示在本地窗口上

这里 xinitrc 中部分代码解释可以看这里: https://blog.csdn.net/sements/article/details/88123894

2 - 指定X系统所使用的framebuffer设备

为了让 X 界面系统启动时使用设定的framebuffer进行绘制,我们需要在相应配置文件中加入下面的语句:

Section "Device"  
  Identifier "myfb"
  Driver "fbdev"
  Option "fbdev" "/dev/fb0"
EndSection

有两种可选的加入位置。

  • 第一种在目录(/etc/X11/xorg.conf.d 没有这个目录就创建即可)下已有的 配置文件(如:40-libinput.conf )中加入上面的语句。

  • (推荐)第二种就是在目录(/etc/X11/xorg.conf.d 没有这个目录就创建即可)新建一个 .conf 文件,这里叫它 99-fbdev.conf (前面的数字影响着 X 界面系统启动加载这些配置文件时的顺序),然后在这个文件中加入上面的语句。

3 - 挂载U盘

插入u盘,使用 fdisk -l 查看当前给u盘分配的盘号

在这里插入图片描述

创建一个文件夹用于接收挂载,这里是 sandisk16,然后使用mount命令挂载u盘上的分区

# mount /dev/sda1 sandisk16/

在这里插入图片描述
即可在 sandisk16 文件夹下看到挂载的u盘分区1内的内容了

4 - 在嵌入式系统上直接编译qt程序

既然前面在根文件系统里面安装好了qt5-default以及编译器,我们便可以考虑在该根文件系统中直接进行qt程序的编译。
既可以在主机上使用qemu进行虚拟化后挂载该文件系统进行编译,也可以在嵌入式系统上挂载该根文件系统,然后进行编译。

要求事先在根文件系统里安装了 qt5-default

安装编译器

$sudo apt install gcc g++

安装完毕后记得查看编译器的目标平台,我这里因为是 aarch64 架构的系统,所以gcc和g++自动匹配的是aarch64的编译器

值得注意的是,如果直接用qmake来产生Makefile的话,其默认写入Makefile的是使用 “gcc” 进行编译。这里若你的 “gcc” 命令指向的并非你期望的编译器,可能会造成后面make时编译失败。
解决这个问题可以通过下面在pro文件中添加指令来指定使用什么编译器,这样qmake后产生的Makefile中会修改使用你指定的编译器来进行编译。
有意思的是,在aarch64平台下,有一个 aarch64_linux_gnu_qmake 可执行脚本(应该是叫这个,不记得了),可以使用该命令来替换qmake指令使用。感兴趣的可以看看该脚本内容。同样也是指定使用特定编译器的方式来生成Makefile

在这里插入图片描述

还可以在 pro 文件中添加下面指令来指定使用什么编译器进行编译,例如这里指定使用 arm-linux-gnueabihf

QMAKE_CC=arm-linux-gnueabihf-gcc
QMAKE_CXX=arm-linux-gnueabihf-g++
QMAKE_LINK=arm-linux-gnueabihf-gcc
QMAKE_AR=arm-linux-gnueabihf-ar
QMAKE_STRIP=arm-linux-gnueabihf-strip

运行qmake来产生Makefile,然后make执行Makefile

$qmake
$make

执行程序

./你的程序

  • 12
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值