一、前言
第一版简单镜像已经上传到 xhr4412 百度云,获取方式请看 [2020.09.12 - xhr4412] 移植 u-boot-2020.07 & linux-5.8.5 & BusyBox-1.31.1 到 iTOP-4412 汇总 。(ubuntu 文件夹内)
众所周知,busybox 是最小的文件系统,很多常用应用都没有支持,需要自己下载源码进行安装,费时费力,搞不好还不知道有些要怎么build,要怎么放那些文件,太麻烦了(主要是因为我太菜了),所以就想使用一个方便的文件系统。
二、准备
通过上面链接下载官方镜像,下载主页能找到 ubuntu 所有的镜像,但是我们这里移植 ubuntu-base-20.04.2 armhf 的镜像,这是支持 exynos4412 cpu 的镜像。
(1) 解压
sudo tar xvf ubuntu-base-20.04.2-base-armhf.tar.gz -C rootfs/
(2) 安装 qemu
对任何机器运行操作系统的全系统仿真。
sudo apt-get install qemu-user-static
sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin/
qemu 是一个通用的、开源的机器仿真器和虚拟机,拷贝它是为了可以模拟 arm cpu 进行文件系统的配置。
(3) 配置文件系统网络
sudo cp /etc/resolv.conf rootfs/etc/resolv.conf
将新文件系统的网络设置和本机一样,方便等一下对新文件系统进行配置。
(4) 设置镜像源
sudo vim rootfs/etc/apt/sources.list
使用 aliyun 的 arm 镜像源,修改文件为:
deb http://mirrors.aliyun.com/ubuntu-ports/ focal main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security multiverse
PC ubuntu 的镜像源将 ubuntu-ports
改为 ubuntu
即可,这里没有使用清华源是由于之后 apt update
时会报错,大概意思是说清华源不被信任之类的,按照这里的办法有尝试过,似乎起了一定作用,但是最后还是没有成功(虽然那个变量是改成了 true),还好后面用了阿里云没有这个问题。
(5) 切换到新文件系统
- 挂载
sysfs
、proc
、/dev
等 sudo chroot
到新文件系统
主要原理我就不讲了,网上有很多,可以查查,我就讲讲我提供的源码如何使用(前面解压的时候需要文件夹名为 rootfs
):
./do_mount.sh
然后输入 root 密码,就会进入新文件系统了,此时做任何操作都是在修改新文件系统- 需要退出时使用
exit
三、配置
./do_mount.sh
进入新文件系统后:
(1) 更新源然后装软件
apt update
apt install -y sudo
apt install -y language-pack-en-base
apt install -y ssh
apt install -y net-tools
apt install -y ethtool
apt install -y ifupdown
apt install -y iputils-ping
apt install -y rsyslog
apt install -y htop
apt install -y vim
apt install -y bash-completion
apt install -y systemd
上面软件可以根据自己需求进行安装,不过网络相关的一般都需要安装,因为需要配置网卡这些,当然也可以装更多你需要的软件。而且经我发现,ubuntu-20.04 必须装最后一个 systemd
服务,但是在比较早的 16.04 似乎下载的镜像就有安装它,20.04 不安装它的话无法正确设置串口服务。
Ubuntu Tab自动补全
先安装软件
apt install bash-completion -y
vim .bashrc
注释这几行
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
然后
source .bashrc
(2) 基础配置
设置密码、添加用户
passwd root
adduser xhr
这里我设置的密码都是 xhr
设置主机名称和本机IP
echo "xhr4412" > /etc/hostname
echo "127.0.0.1 localhost" >> /etc/hosts
(3) 配置串口
ubuntu-20.04 必须装 systemd
服务后,使用该命令才有效果。
ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttySAC2.service
由于我用的是exynos4412的平台,需要添加一个名为getty@ttySAC2.service
的链接,连接到getty@.service
服务。exynos4412的平台是ttySAC2,其他平台的名字就不清楚了。如果确定不了名字,一个比较笨的方法就是先把制作好的根文件系统下载到开发板上,超级终端上查看打印出来的log,由此就可以知道了。
Timed out waiting for device dev-ttySAC2.device.
[DEPEND] Dependency failed for Serial Getty on ttySAC2.
(4) 退出
exit
回到 PC ubuntu
四、制作、烧写
(1) 制作镜像脚本 ext4.sh
#! /bin/bash
FSDIR=fstmp
IMG=sysimg
sudo mkdir -p $FSDIR
sudo dd if=/dev/zero of=$IMG bs=1024 count=1048576
sudo mkfs.ext4 $IMG
sudo mount $IMG $FSDIR
sudo cp -rfp rootfs/* $FSDIR
sudo umount $FSDIR
sudo e2fsck -p -f $IMG
sudo resize2fs -M $IMG
使用脚本后,就可以看到生成了 sysimg
文件
xhr@ubuntu:~/study/test/xhr4412-ubuntu-20.04.2$ ll -h sysimg
-rw-r--r-- 1 root root 490M Mar 17 21:27 sysimg
(2) 烧写镜像
已经写好脚本 sdflush.sh
,将 sysimg
烧写到 /dev/sdb
的U盘内。然后将U盘插到 xhr4412,启动之前的 busybox 文件系统,使用 dd 命令在烧写到分区 /dev/mmcblk1p3
,
dd if=/dev/sda of=/dev/mmcblk1p3 bs=16M count=40
然后重启开发板,使用启动 /dev/mmcblk1p3
的 dtb 文件来启动文件系统,就可以看到成功启动 ubuntu-20.04 了。
配置一下网络,启动 sshd 服务,就可以从远端进行 ssh 的登录,就可以享受丝滑了,非常爽,特别是用上前面移植的无线网卡,从此开发板只需要电源线了!
五、其他问题
(1) ls 始终没有颜色
即使使用 ls --color
,在串口登录时始终没有颜色,不过 ssh 登录的话是有颜色的,我只能猜测可能是 ubuntu 对于串口的支持可能不是很完善。
(2) 长命令无法自动换行
我认为这也可能是串口支持不完善导致的。虽然我有发现下面这些文章似乎在解决这个问题,但是并没有效果,他们更像是说 pc 端无法换行的问题,不过并不影响使用,而且 ssh 登录仍然是正常的。
- linux 输入长命令行 会无缘无故的回到行开始,本来应该在下一行继续的!
- How do I get long command lines to wrap to the next line?
- I have a fancy prompt with colors, and now bash doesn’t seem to know how wide my terminal is. Lines wrap around incorrectly.
都是说有颜色的提示符才导致了这个问题,那些不显示的字符应该使用 \[ \[
括起来,不过我这个并没有颜色,没有效果。
(3) vim 无法自动全屏
同样是 ssh 没有该问题,但是串口有,可以看这里或这里简单解决,但是也不能解决得很好。vi ~/.vimrc
后添加:
if has("gui_running")
" GUI is running or is about to start.
" Maximize gvim window (for an alternative on Windows, see simalt below).
set lines=999 columns=999
else
" This is console Vim.
if exists("+lines")
set lines=50
endif
if exists("+columns")
set columns=100
endif
endif
(4) 无法保存文件
使用 vim 后无法保存,无法创建新文件,看起来报错是由于分区没有空间,可是 mmcblk1p3
应该是有 1G 空间的,sysimg 只有 490M,应该有够的啊?这是由于制作的 sysimg 镜像缩小到了尽可能的最小,所以打开文件系统后看起来占用了 100%,所以没有办法保存,需要在第一次打开文件系统时切换到 root 使用命令 resize2fs /dev/mmcblk1p3
来调整空间到实际大小,之后就可以保存了。
(5) 如何切换到 mmcblk1p3 分区
前面移植的 uboot 有 fdisk
命令,会分出该区,移植的 kernel 需要修改 dts 文件的 bootargs
属性中路径为 mmcblk1p3
,然后编译烧写到 uboot 的 dtb2
分区,修改环境变量 defdtb
= dtb2
, 然后 boot
就可以切换到新文件系统了。
有没有其他办法?为什么不能用 uboot 直接烧写?问得很好,我觉得应该有,uboot 也可以烧,不过由于时间关系还没有研究,uboot 也没有改好,就先这样吧 @_@ 。
平时用 ubuntu 还是觉得挺方便的,安装啥都是一条命令,何不搞个 ubuntu ?在公司也因为一些项目,花两天时间把 python 掌握得比较好了,觉得这个“垃圾”语言还是有可取之处的(python 在一些使用场景上确实方便太多太多了,给 python 道歉),搞得我一些简单的东西也想用 python ,这也是装 ubuntu 的一个原因,研究了一下,移植 ubuntu 也比较简单。
这次更新距离上次更新有两个月了,主要还是春节玩得过于不亦乐乎。最近不知道做些什么,不过有些东西做得不够完美想优化一下,比如 uboot,但是现在思路还不够全。