文章目录
- 【系统移植篇】ubuntu-base系统构建
-
- [1 ubuntu-base获取](#1 ubuntu-base获取)
- [2 ubuntu根文件系统的构建](#2 ubuntu根文件系统的构建)
-
- [2.1 解压缩ubuntu base 根文件系统](#2.1 解压缩ubuntu base 根文件系统)
- [2.2 安装QEMU](#2.2 安装QEMU)
- [2.3 设置软件源](#2.3 设置软件源)
- [2.4 主机挂载并且配置根文件系统](#2.4 主机挂载并且配置根文件系统)
-
- [2.4.1 主机挂载根文件系统](#2.4.1 主机挂载根文件系统)
- [2.4.2 安装常用命令和软件](#2.4.2 安装常用命令和软件)
- [2.4.3 设置root密码](#2.4.3 设置root密码)
- [2.4.4 设置本机名称和IP地址](#2.4.4 设置本机名称和IP地址)
- [2.4.5 设置串口终端](#2.4.5 设置串口终端)
- [2.4.6 取消挂载](#2.4.6 取消挂载)
- [3 ubuntu根文件系统测试](#3 ubuntu根文件系统测试)
-
- [3.1 nfs挂载测试](#3.1 nfs挂载测试)
- [3.4 ubuntu系统使用](#3.4 ubuntu系统使用)
-
- [3.4.1 添加新用户](#3.4.1 添加新用户)
- [3.4.2 网络DHCP设置](#3.4.2 网络DHCP设置)
- [3.4.3 安装软件](#3.4.3 安装软件)
- [3.4.4 FTP服务器搭建](#3.4.4 FTP服务器搭建)
- [3.4.5 SSH服务器搭建](#3.4.5 SSH服务器搭建)
- [3.4.5 gcc和make工具安装](#3.4.5 gcc和make工具安装)
- [4 保存](#4 保存)
【系统移植篇】ubuntu-base系统构建
上一章节我们采用了busybox构建简单的根文件系统,发现其过程十分复杂并且缺少非常多的库文件,而且安装包起来也非常不方便,这一节我们就来讲讲看如何基于ubuntu-base来制作我们的根文件系统。
1 ubuntu-base获取
首先需要下载ubuntu-base, 这里我们移植ubuntu16.04版本的根文件系统。其下载地址为:ubuntu-base-16.04.6-base-armhf.tar.gz
注:Ubuntu 针对不同的 CPU 架构提供相应的 ubuntu base 根文件系统有 amd64(64 位 X86)、armhf、i386(32 位 X86)、powerpc、ppc64el 等系统的。I.MX6ULL 是 Cortex-A 内核的 CPU,并且有硬件浮点运算单元,因此选择 armhf 版本。
2 ubuntu根文件系统的构建
2.1 解压缩ubuntu base 根文件系统
先在虚拟机的nfs目录下创建一个ubuntu_rootfs目录:
sh
pzs@pzs-jammy:~$ cd ~/linux/nfs/
pzs@pzs-jammy:~/linux/nfs$ mkdir ubuntu_rootfs
将下载的jammy-base-armhf.tar.gz存放到ubuntu_rootfs目录下,然后使用如下命令进行解压:
sh
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo tar -vzxf jammy-base-armhf.tar.gz # 一定要使用sudo命令执行,否则会有权限问题
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ ls
bin dev home lib mnt proc run srv tmp var
boot etc jammy-base-armhf.tar.gz media opt root sbin sys usr
ubuntu base 解压以后就是大家最常见的 linux 根文件系统,但是还需要额外做些配置工作。
2.2 安装QEMU
需要在 PC 的 Ubuntu 上安装 qemu 工具,命令如下:
sh
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo apt-get install -y qemu-user-static
将刚刚安装的 qemu-user-static 拷贝到刚刚解压出来的 ubuntu base 目录中,命令如下:
sh
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo cp /usr/bin/qemu-arm-static ./usr/bin/
2.3 设置软件源
在 ubuntu 下使用 apt-get 安装软件的时候,是从网上下载软件并安装的,因此需要指定软件源。
在设置软件源之前先将 Ubuntu 主机下的 DNS 配置文件/etc/resolv.conf 拷贝到根文件系统中,命令如下:
sh
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo cp /etc/resolv.conf ./etc/resolv.conf
设置软件源:
sh
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo cp ./etc/apt/sources.list ./etc/apt/sources.list.bak
pzs@pzs-jammy:~/linux/nfs/ubuntu_rootfs$ sudo vim ./etc/apt/sources.list
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
2.4 主机挂载并且配置根文件系统
2.4.1 主机挂载根文件系统
将上面制作的根文件系统挂载到主机上,需要挂载 proc、sys、dev、dev/pts 等文件系统,最后使用 chroot 将主机的根文件系统切换到我们前面制作的根文件系统中。通过mount_and_umount.sh脚本来实现:
sh
pzs@pzs-jammy:~/linux/nfs$ vim ./mount_and_umount.sh
#!/bin/bash
set -eu
mount_type=$1
rootfs_dir="/home/pzs/linux/nfs/ubuntu_rootfs"
if [ "$mount_type" != "mount" ] && [ "$mount_type" != "umount" ]; then
echo "Usage: $0 mount|umount"
exit 1
fi
if [ ! -d "$rootfs_dir" ]; then
echo "$rootfs_dir not found"
exit 1
fi
# if not root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
if [ "$mount_type" == "mount" ]; then
mount -t proc /proc "$rootfs_dir"/proc
mount -t sysfs /sys "$rootfs_dir"/sys
mount -t tmpfs /tmp "$rootfs_dir"/tmp
mount -o bind /dev "$rootfs_dir"/dev
mount -o bind /dev/pts "$rootfs_dir"/dev/pts
chroot /home/pzs/linux/nfs/ubuntu_rootfs
elif [ "$mount_type" == "umount" ]; then
umount "$rootfs_dir"/proc
umount "$rootfs_dir"/sys
umount "$rootfs_dir"/tmp
umount "$rootfs_dir"/dev/pts
umount "$rootfs_dir"/dev
fi
赋予权限给改脚本:
sh
pzs@pzs-jammy:~/linux/nfs$ chmod 777 ./mount_and_umount.sh
挂载根文件系统:
pzs@pzs-jammy:~/linux/nfs$ sudo ./mount_and_umount.sh mount
root@pzs-jammy:/# ls
bin dev home lib mnt proc run srv tmp var
boot etc jammy-base-armhf.tar.gz media opt root sbin sys usr
2.4.2 安装常用命令和软件
sh
root@pzs-jammy:/# apt-get update && \
apt-get install -y \
sudo \
vim \
kmod \
net-tools \
ethtool \
ifupdown \
language-pack-en-base \
rsyslog \
htop \
iputils-ping \
util-linux
2.4.3 设置root密码
root 用户密码也设置为"root",相当于用户名和密码一样,命令如下:
sh
root@pzs-jammy:/# passwd root
New password:
Retype new password:
passwd: password updated successfully
2.4.4 设置本机名称和IP地址
sh
root@pzs-jammy:/# echo "alientek_imx6ul" > /etc/hostname
root@pzs-jammy:/# echo "127.0.0.1 localhost" >> /etc/hosts
root@pzs-jammy:/# echo "127.0.0.1 alientek_imx6ul" >> /etc/hosts
root@pzs-jammy:/# cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 alientek_imx6ul
root@pzs-jammy:/# cat /etc/hostname
alientek_imx6ul
2.4.5 设置串口终端
ubuntu 根文件系统在开发板上启动以后我们通常也希望串口终端正常工作,需要创建一个链接。
首先确定自己所使用的串口设备文件,比如ALPHA 开发 板 使 用 的 UART1 对 应 的 串 口 设 备 文 件 为 ttymxc0, 需 要 添 加 一 个 名 为 getty@ttymxc0.service 的链接,链接到 getty@.service 服务上,输入如下命令::
sh
root@pzs-jammy:/# ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttymxc0.service
2.4.6 取消挂载
设置好以后就可以退出根文件系统了,输入如下命令退出
sh
root@pzs-jammy:/# exit
退出以后再执行一下 unmount.sh 脚本取消挂载,命令如下:
sh
pzs@pzs-jammy:~/linux/nfs$ sudo ./mount_and_umount.sh umount
至此,ubuntu base 根文件系统就已经制作好了,接下来就是挂载到开发板上去测试。
3 ubuntu根文件系统测试
3.1 nfs挂载测试
先用 nfs 挂载根文件系统,在 uboot 里面设置 bootargs 环境变量的值如下:
sh
=> setenv ipaddr 192.168.137.3
=> setenv ethaddr b8:ae:1d:01:00:00
=> setenv gatewayip 192.168.137.1
=> setenv netmask 255.255.255.0
=> setenv serverip 192.168.137.2
=> saveenv
==> setenv bootargs 'console=ttymxc0,115200 root=/dev/nfs nfsroot=192.168.137.2:/home/pzs/linux/nfs/ubuntu_rootfs,proto=tcp rw ip=192.168.137.3:192.168.137.2:192.168.137.1:255.255.255.0::eth0:off'
==> setenv bootcmd 'tftp 80800000 zImage; tftp 83000000 imx6ull-alientek-emmc.dtb; bootz 80800000 - 83000000'
==> saveenv
==> boot
设置根文件系统为我们前面制作的 ubuntu_rootfs。设置完成以后重启开发板,这个时候就会进入到 ubuntu 根文件系统中, 并且要求我们输入用户名和密码,用户名和密码都是 root。用户名和密码输入成功以后就会进入到系统, 如下所示:
sh
Ubuntu 16.04.6 LTS alientek_imx6ul ttymxc0
alientek_imx6ul login: root
Password:
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.1.15 armv7l)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@alientek_imx6ul:~#
3.4 ubuntu系统使用
3.4.1 添加新用户
们制作的 ubuntu 根文件系统默认只有一个 root 用户,默认都是用 root 用户登录的,最好创建一个普通用户,默认使用普通用户,当需要 root 权限的时候就使用"sudo"命令即可。使用 adduser 命令添加普通用户:
sh
root@alientek_imx6ul:~# adduser pzs
Adding user `pzs' ...
Adding new group `pzs' (1000) ...
Adding new user `pzs' (1000) with group `pzs' ...
Creating home directory `/home/pzs' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for pzs
Enter the new value, or press ENTER for the default
Full Name []: # 回车
Room Number []: # 回车
Work Phone []: # 回车
Home Phone []: # 回车
Other []: # 回车
Is the information correct? [Y/n] Y
新用户添加完成以后就可以重启开发板,然后使用新用户登录。
此外,需要设置新用户可以使用sudo命令,首先我们需要修改/etc/sudoers 文件,
sh
root@alientek_imx6ul:~# visudo
...
root ALL=(ALL:ALL) ALL
# 下面是新加的一行
pzs ALL=(ALL:ALL) ALL
修改完成以后保存退出,切换到"pzs"用户,使用 fdsik 命令测试一下,fdisk 需要 root 权限。输入如下命令:
sh
root@alientek_imx6ul:~# su pzs
pzs@alientek_imx6ul:/root$ sudo fdisk -l
Disk /dev/ram0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
....
如果 sudo 运行正常的话就会打印出当前开发板的所有存储信息。
3.4.2 网络DHCP设置
默认没有配置 DHCP,因为 ubuntu 启动以后不能直接联网,需要手动启动网卡并设置IP 地址。这里我们配置一下网络 DHCP,这样系统启动以后就会自动设置好网络。
ALPHA 开发板有两个网卡,在 linux 系统下的网卡名字为 eth0 和 eth1,我们以 eth0 为例, 配置其为DHCP
sh
root@alientek_imx6ul:~# echo auto eth0 > /etc/network/interfaces.d/eth0
th0t@alientek_imx6ul:~# echo iface eth0 inet dhcp >> /etc/network/interfaces.d/e
root@alientek_imx6ul:~# /etc/init.d/networking restart
Restarting networking (via systemctl): networking.service
设置好以后重启开发板,eth0 网卡就会默认打开,可以直接上网。eth1 网卡同理。
3.4.3 安装软件
如果要安装什么软件的话直接使用 apt 工具即可,我们需要安装一些必要的软件:
sh
root@alientek_imx6ul:~# sudo apt-get install vim systemd
3.4.4 FTP服务器搭建
我们可以在开发板上搭建一个 FTP 服务器,这样就可以通过 FileZilla 这样的 FTP 客户端软件和开发板之间进行文件传输。
执行如下命令来安装 FTP 服务:
sh
pzs@alientek_imx6ul:~$ sudo apt-get install vsftpd
等待软件自动安装,安装完成以后使用 VI 命令打开/etc/vsftpd.conf,命令如下:
sh
pzs@alientek_imx6ul:~$ sudo vim /etc/vsftpd.conf
# 找到如下两行使能:
local_enable=YES
write_enable=YES
修改完 vsftpd.conf 以后保存退出,使用如下命令重启 FTP 服务:
sh
pzs@alientek_imx6ul:~$ sudo /etc/init.d/vsftpd restart
Restarting vsftpd (via systemctl): vsftpd.service.
之后就可以在windows上通过FileZilla通过FTP协议连接到开发板了。
3.4.5 SSH服务器搭建
在开发板中执行如下命令进行安装:
sh
pzs@alientek_imx6ul:~$ sudo rm -rf /var/run/sshd
pzs@alientek_imx6ul:~$ sudo mkdir /var/run/sshd
pzs@alientek_imx6ul:~$ sudo apt-get install ssh
3.4.5 gcc和make工具安装
可以直接用 I.MX6ULL 来编译代码,只需要安装 gcc 和 make 工具,这两个工具的安装很简单,输入如下命令即可:
sh
pzs@alientek_imx6ul:~$ sudo apt-get install -y gcc make
当 gcc 和 make 工具都安装成功以后就可以编写一个小程序编译测试一下,比如新建一个main.c 文件,然后在 main.c 里面输入如下内容:
c
#include <stdio.h>
int main(void)
{
printf("hello world!\r\n");
return 0;
}
完成以后直接使用 I.MX6ULL 编译,输入如下命令:
sh
pzs@alientek_imx6ul:~$ gcc main.c -o main
编译完成以后就会生成一个名为"main"的可执行文件,使用"file"命令查看此文件信息,如下所示:
sh
pzs@alientek_imx6ul:~$ file main
main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=ec74c5cc5eb97e304742b82ae29c3c778435d0fc, not stripped
main 可执行文件是一个 32 位的 ARM 可执行文件,因为我们安装的 gcc 工具就是 armhf 的。执行 main 文件,输出"hello world!"字符串:
sh
pzs@alientek_imx6ul:~$ ./main
hello world!
至此,关于如何在ALPHA 开发板上搭建 ubuntu 系统就完成了,后续我们可以将和这个文件系统烧录到SD卡或者EMMC中。
4 保存
最后,将此时的rootfs文件系统打包一份, 并自己保存起来,以便后续可以直接使用:
sh
pzs@pzs-jammy:~/linux/nfs$ sudo tar -czf ubuntu_rootfs_alientek_emmc.tar.gz -C ubuntu_rootfs .
注:有需要的话,可以从百度网盘上直接下载已经移植好的内核:
sh
通过网盘分享的文件:ubuntu_rootfs_alientek_emmc.tar.gz
链接: https://pan.baidu.com/s/1lyaW4bnjz5FxElkS26jlHA?pwd=pzs6 提取码: pzs6