1.准备工作
1.1下载最小文件系统
到ubuntu网址 下载相应的最小系统(本文以 ubuntu-base-16.04.5-base-arm64.tar.gz 为例)
1.2安装模拟器工具
$sudo apt-get install qemu-user-static
2.解压最小系统
$mkdir ubuntu-rootfs
$sudo tar -xvpf ubuntu-base-16.04.5-base-armhf.tar.gz -C ubuntu-rootfs
3.复制模拟器工具到根文件系统
(1) arm
$sudo cp /usr/bin/qemu-arm-static ubuntu-rootfs/usr/bin/
(2)arm64
$sudo cp /usr/bin/qemu-aarch64-static ubuntu-rootfs/usr/bin/
4.复制本地DNS配置到根文件系统
$sudo cp /etc/resolv.conf ubuntu-rootfs/etc/resolv.conf
5.添加开机启动脚本
$sudo vim ubuntu-rootfs/etc/init.d/firstboot.sh
#!/bin/bash -e
# first boot configure
#根据实际分区情况修改mmcblk2p3
if [ ! -e "/usr/local/first_boot_flag" ] ;
then
echo "Resizing /dev/mmcblk2p3..."
resize2fs /dev/mmcblk2p3
touch /usr/local/first_boot_flag
fi
$sudo vim ubuntu-rootfs/lib/systemd/system/firstboot.service
#start
[Unit]
Description=Setup rockchip platform environment
Before=lightdm.service
After=resize-helper.service
[Service]
Type=simple
ExecStart=/etc/init.d/firstboot.sh
[Install]
WantedBy=multi-user.target
#end
6.挂载根文件系统,进入根文件系统
$./mount.sh -m linux-rootfs
mount.sh的内容
#!/bin/bash
function 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 chroot ${2}
}
function umnt() {
echo "UNMOUNTING"
sudo umount ${2}/proc
sudo umount ${2}/sys
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
7.对根文件系统进行配置
(1) 更新源
$apt update
ps:如果更新速度慢,可以修改/etc/apt/source.list文件的内容,修改成国内的更新源
(2) 更新系统
$apt upgrade
(3)安装常用的工具
$apt install bash-completion // shell自动补全
$apt install net-tools //没有这个,ifconfig命令没有
$apt install inetutils-ping //没有这个,ping命令没有
$apt install vim //vim编辑器
(其它工具及软件,请自行安装)
(4)安装xubuntu桌面环境(耗时较长,耐心等待)
$apt install ubuntu-session xubuntu-desktop
(5)设置桌面环境启动选项
$update-alternatives --config x-session-manager // xubuntu选择startxfce4
(6)设置默认登录方式
$dpkg-reconfigure lightdm
(7)设置系统默认语言及编码方式
$apt install locales //系统语言及编码格式
$dpkg-reconfigure locales //根据提示,空格键选择"en_US.UTF-8 UTF-8" "zh_CN GB2312" "zh_CN.GB18030 GB18030" "zh_CN.GBK GBK" "zh_CN.UTF-8 UTF-8";
(8)设置主机名
$echo "localhost" > /etc/hostname
$echo "127.0.0.1 localhost" > /etc/hosts
(9)添加用户并设置密码
$useradd -s '/bin/bash' -m -G adm,sudo admin
$passwd admin
$passwd root
(10)卸载一些没用的软件
$apt remove thunderbird //邮箱客户端
$apt remove gnome-mines //游戏
$apt remove gnome-sudoku //游戏
$apt remove pidgin //聊天客户端
$apt remove transmission-* //下载工具
$apt remove gnome-software //软件中心
$apt autoremove
(11)安装qt5支持
$apt install qt5-default
(12) [endif]重新配置时区
$dpkg-reconfigure tzdata //选择亚洲、上海
(13)禁止apport报错
$vim etc/default/apport
设置enabled=0
(14) 设置开机启动脚本
$systemctl enable firstboot.service
(15)设置蓝牙
$apt remove bluetooth* bluez* blueman*
$apt autoremove
$apt install bluetooth* bluez* blueman*
$vim /usr/bin/bt-attach
#!/usr/bin/env bash
start-stop-daemon --start --oknodo --pidfile /var/run/hciattach.pid --background --startas /usr/bin/hciattach -- -s 115200 -n /dev/ttyS0 bcm43xx 1500000 -t 200
exit 0
$vim /etc/systemd/system/bt-attach.service
[Unit]
Description=bluetooth-toggle
[Service]
Type=forking
ExecStart=/usr/bin/bt-attach
$vim /lib/udev/rules.d/50-bluetooth.rules
SUBSYSTEM=="rfkill", ACTION=="change", ATTR{name}=="bt_default", ATTR{type}=="bluetooth", ATTRS{state}=="1", RUN+="/bin/systemctl start bt-attach.service"
SUBSYSTEM=="rfkill", ACTION=="change", ATTR{name}=="bt_default", ATTR{type}=="bluetooth", ATTRS{state}=="0", RUN+="/bin/systemctl stop bt-attach.service"
(16) 清理工作
$apt clean
$rm -rf /var/lib/apt/lists/* //这里是更新源保存的临时文件,可删除
(17)退出根文件系统
$exit
8.卸载根文件系统
$./mount.sh -u ubuntu-rootfs
(如果无法正常退出,请重启电脑。)
9.打包镜像
$./mkimage.sh ubuntu-rootfs system.img
mkimage.sh的内容:
#!/bin/bash
rootfs_dir=$1
rootfs_file=$2
rootfs_mnt="mnt"
if [ ! $rootfs_dir ] || [ ! $rootfs_file ];
then
echo "Folder or target is empty."
exit 0
fi
if [ -f "$rootfs_file" ]; then
echo "-- Delete exist $rootfs_file ..."
rm -f "$rootfs_file"
fi
echo "-- Create $rootfs_file ..."
dd if=/dev/zero of="$rootfs_file" bs=1M count=4096
sudo mkfs.ext4 -F -L linuxroot "$rootfs_file"
if [ ! -d "$rootfs_mnt" ]; then
mkdir $rootfs_mnt
fi
echo "-- Copy data to $rootfs_file ..."
sudo mount $rootfs_file $rootfs_mnt
sudo cp -rfp $rootfs_dir/* $rootfs_mnt
sudo sync
sudo umount $rootfs_mnt
rm -r $rootfs_mnt
echo "-- Resize $rootfs_file ..."
/sbin/e2fsck -p -f "$rootfs_file"
/sbin/resize2fs -M "$rootfs_file"
echo "-- Done."