You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
9.8 KiB

\chapter{制作 ubuntu 文件系统}
在 主机的 Ubuntu 系统里面,可以制作 aarch64 的文件系统,这里介绍一下步骤。
\section{准备安装环境}
\subsection{安装依赖软件}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
sudo apt install qemu-user-static debootstrap
\end{minted}
\subsection{下载基础系统}
这里以安装 Ubuntu 22.04 为例:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
mkdir ubuntu
sudo debootstrap --arch=arm64 --foreign jammy ubuntu
sudo cp /usr/bin/qemu-aarch64-static ubuntu/usr/bin
sudo chroot ubuntu /usr/bin/qemu-aarch64-static /bin/sh \
-i /debootstrap/debootstrap --second-stage
\end{minted}
Ubuntu 22.04 的代号是 jammy, Ubuntu 20.04 的代号是 focal。如果要做 Ubuntu 20.04 版本的系统,把命令中的 jammy 换成 focal。
\subsection{切换到安装环境}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
sudo mount -t sysfs sys /sys ubuntu/sys
sudo mount -t proc proc /proc ubuntu/proc
sudo mount -o bind /dev ubuntu/dev
sudo mount -o bind /dev/pts ubuntu/dev/pts
sudo chroot ubuntu /usr/bin/qemu-aarch64-static /bin/bash -i
\end{minted}
\section{构建 rootfs}
\subsection{更新 apt 源}
修改 /etc/apt/sources.list 如下:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
deb http://ports.ubuntu.com/ jammy main universe
deb-src http://ports.ubuntu.com/ jammy main universe
deb http://ports.ubuntu.com/ jammy-security main universe
deb-src http://ports.ubuntu.com/ jammy-security main universe
deb http://ports.ubuntu.com/ jammy-updates main universe
deb-src http://ports.ubuntu.com/ jammy-updates main universe
\end{minted}
\subsection{更新系统}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get update
apt-get dist-upgrade
\end{minted}
\subsection{安装必要软件}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get install -y vim-nox build-essential gdb u-boot-tools rsync \
socat jq tcpdump ifupdown minicom i2c-tools lrzsz tftp-hpa \
net-tools dosfstools pciutils memtool ethtool unzip python3-pip \
parted usbutils command-not-found gdisk openssh-server
apt-get install locales tzdata
\end{minted}
\subsection{安装 rockchip 硬解码器}
添加软件源:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get install software-properties-common
add-apt-repository ppa:george-coolpi/multimedia
apt update
\end{minted}
安装 gstreamer :
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good gstreamer1.0-plugins-bad alsa-utils \
gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools \
gstreamer1.0-alsa gstreamer1.0-pulseaudio ffmpeg v4l-utils
\end{minted}
安装插件gstreamer1.0-rockchip
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get install gstreamer1.0-rockchip
\end{minted}
\subsection{安装图形界面 (可选)}
\subsubsection{安装 ubuntu 桌面环境}
执行如下命令:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get install ubuntu-desktop # 其他可选xubuntu-desktop lubuntu-desktop等
\end{minted}
提示选择桌面管理器时,可以选 `gdm`。
\subsubsection{安装中英文语言包与输入法(可选)}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
# 英文环境
apt-get install language-pack-en-base
apt-get install language-pack-gnome-en-base
# 中文环境
apt-get install language-pack-zh-hans-base
apt install language-pack-gnome-zh-hans-base
# 中文输入法
apt-get install ibus-table-wubi ibus-pinyin ibus-sunpinyin
\end{minted}
\subsubsection{设置 gdm 自动登录}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
vim /etc/gdm3/custom.conf
# 修改下面的内容,ubuntu即账户名
[daemon]
AutomaticLoginEnable=true
AutomaticLogin=ubuntu
TimedLoginEnable=true
TimedLogin=ubuntu
TimedLoginDelay=10
\end{minted}
\subsubsection{开机默认进图形界面}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
systemctl set-default graphical.target
\end{minted}
\subsection{rootfs 设置}
\subsubsection{设置 root 密码}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
passwd root
\end{minted}
\subsubsection{添加用户}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
useradd -s '/bin/bash' -m -G adm,sudo,video,plugdev,dialout ubuntu
\end{minted}
\subsubsection{设置时区}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
\end{minted}
\subsubsection{设置 hostname}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
echo sytc > /etc/hostname
echo "127.0.0.1 sytc" >> /etc/hosts
\end{minted}
\subsubsection{设置bash为默认shell (可选)}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
dpkg-reconfigure dash
\end{minted}
选择 ``NO''。
\subsubsection{配置 locale}
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
dpkg-reconfigure locales
\end{minted}
至少选择 en\_US.UTF-8 和 zh\_CN.UTF-8。
\subsubsection{修改开机检测网络时间}
修改开机检测网络时间,避免开机卡住:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
vim /lib/systemd/system/networking.service
# 将里面的TimeoutStartSec=5min修改为
TimeoutStartSec=1sec
\end{minted}
\subsubsection{ 配置 fstab }
配置/etc/fstab 内容如下:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
# <file system> <mount pt> <type> <options> <dump> <pass>
/dev/root / auto rw,noauto 0 1
tmpfs /tmp tmpfs mode=1777 0 0
tmpfs /run tmpfs mode=0755,nosuid,nodev 0 0
#PARTLABEL=oem /oem ext2 defaults 0 2
PARTLABEL=userdata /userdata ext4 defaults 0 0
proc /proc proc defaults 0 0
devtmpfs /dev devtmpfs defaults 0 0
devpts dev/pts devpts mode=0620,ptmxmode=0666,gid=5 0 0
tmpfs /dev/shm tmpfs nosuid,nodev,noexec 0 0
sysfs /sys sysfs defaults 0 0
debugfs /sys/kernel/debug debugfs defaults 0 0
pstore /sys/fs/pstore pstore defaults 0 0
\end{minted}
\subsubsection{串口自动登录}
修改文件 /usr/lib/systemd/system/serial-getty@.service 的 ExecStart 对应的值:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
[Service]
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.
ExecStart=-/sbin/agetty --autologin root --noclear %I $TERM
#ExecStart=-/sbin/agetty -o '-p -- \\u' --keep-baud 115200,38400,9600 %I $TERM
Type=idle
\end{minted}
然后使能调试串口服务:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
systemctl enable serial-getty@ttyFIQ0.service
\end{minted}
\subsubsection{ 设置 pulseaudio 音频服务 (可选)}
添加文件 /etc/systemd/system/pulseaudio.service 内容如下:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
[Unit]
Description=PulseAudio system server
# DO NOT ADD ConditionUser=!root
[Service]
Type=notify
ExecStart=pulseaudio --daemonize=no --system --realtime --log-target=journal
Restart=on-failure
[Install]
WantedBy=multi-user.target
\end{minted}
然后执行命令:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
systemctl --system enable --now pulseaudio.service
\end{minted}
添加以下内容到 /etc/pulse/client.conf文件中:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
default-server = /var/run/pulse/native
autospawn = no
\end{minted}
将用户添加到 pulse-access组中。
\subsubsection{设置网卡IP地址}
设置网络参数/etc/network/interfaces.d/eth0
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{ini}
auto eth0
iface eth0 inet static
address 192.168.1.80
netmask 255.255.255.0
gateway 192.168.1.1
\end{minted}
修改/etc/network/interfaces \footnote{这个步骤可选,默认的配置应该已经包含这个设置。},执行命令:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
echo "source-directory /etc/network/interfaces.d" > /etc/network/interfaces
\end{minted}
用这里 \ref{sec:fixethx} 的方法将网卡名固定为 ethX 。
\subsubsection{首次开机扩容脚本}
下载并安装附件的\openfilelink{resize-disk.pdf}{resize-disk} 脚本。
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
tar xpvf resize-disk.tar.xz -C /
\end{minted}
\subsubsection{配置 ssh server}
配置允许root登陆并解决登录慢的问题,将/etc/ssh/sshd\_config中的:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
#PermitRootLogin prohibit-password
GSSAPIAuthentication yes
\end{minted}
改为:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
PermitRootLogin yes
GSSAPIAuthentication no
\end{minted}
\subsubsection{清理并退出}
删除临时文件,卸载文件系统:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
apt-get clean
exit
sudo umount ubuntu/dev/pts
sudo umount ubuntu/dev
sudo umount ubuntu/sys
sudo umount ubuntu/proc
\end{minted}
\section{打包成 ext4 文件系统}
执行命令,打包成 ext4 的文件系统文件:
\begin{minted}[bgcolor=lightgray!30,fontsize=\scriptsize]{bash}
IMAGE_SIZE_MB=$(( $(sudo du -sh -m ubuntu | cut -f1) + 300 ))
dd if=/dev/zero of=ubuntu.img bs=1M count=0 seek=${IMAGE_SIZE_MB}
sudo mkfs.ext4 -d ubuntu ubuntu.img
\end{minted}
可以将 ubuntu.img 烧写到 EMMC 的 rootfs 分区上。