当我们配置好自己的根文件系统,或者在一块开发板上完善好根文件系统时,可以将根文件系统拷贝下来然后重新打包成镜像,以供后续使用。
主机环境
系统为 Ubuntu,安装以下软件:
sudo apt install openssh-client rsync
开发板环境
安装前请更新源
apt-get update
安装以下软件:
sudo apt install openssh-server rsync
安装失败可以通过 aptitude 安装
apt-get install aptitude
aptitude install openssh-server rsync
修改开发板的 root 登录权限:
sudo vi /etc/ssh/sshd_config
更改下述配置:
PermitRootLogin yes
重启开发板或者重启 ssh 服务:
reboot
# 或者
/etc/init.d/ssh restart
开始制作
在主机上新建文件夹:ubuntu_make 以及 ubuntu_make/ubuntu
mkdir ubuntu_make
mkdir ubuntu_make/ubuntu
进入ubuntu_make,然后执行下述命令将文件系统同步到ubuntu目录下:
sudo rsync -avx root@192.168.xxx.xxx:/ ubuntu/
制作方法(脚本)有两种,任选一个即可。
第一种
在 ubuntu_make 目录下新建以下脚本,假定命名为 mkext4.sh
#!/bin/bash
mkrootfs(){
TARGET_FILE=$1
FILE_SIZE=$2
SOURCE_FILE=$3
if [ -e $TARGET_FILE ];then
rm -rf $TARGET_FILE
echo "-------remove file $TARGET_FILE !--------"
fi
dd of=$TARGET_FILE bs=1M seek=$FILE_SIZE count=0 2>&1 || fatal "Failed to dd image!"
mke2fs -t ext4 $TARGET_FILE
if [ -e /tmp_ext2 ];then
rm /tmp_ext2 -rf
echo "---------remove dir emp_ext2!------------"
fi
mkdir /tmp_ext2
echo "-------make dir tmp_ext2 for mount !-----"
mount $TARGET_FILE /tmp_ext2
cp -rp $SOURCE_FILE/* /tmp_ext2
umount /tmp_ext2
tune2fs -c 0 -i 0 $TARGET_FILE
echo "-------build compilete,remove tmp dir!-----"
rm /tmp_ext2 -rf
}
usage(){
echo "usage: sh mkext4.sh <target> <size> <sourcefile>"
echo "example: sh mkext4.sh ./rootfs.ext4 2048 ./ubuntu16.04"
}
if [ $# -eq 3 ];then
mkrootfs $1 $2 $3
else
usage
fi
然后赋予执行权限
sudo ./mkext4.sh <target> <size> <source>
参数说明:
target : 制作出来的ext4镜像文件名称
size : 文件系统的大小,单位是M,命令 du -sml ubuntu/ 可以计算同步下来的文件系统大小,然后需要额外再扩展至少200M
source: 同步下来的文件系统路径
示例:
sudo ./mkext4.sh rootfs.ext4 2048 ubuntu/
第二种 制作img 文件的方式
进入 ubuntu 目录,创建相关脚本:
sudo vim etc/onlyone.sh
内容如下:
#!/bin/sh
read line < /proc/cmdline
for arg in $line; do
if [ "5" -le "$(expr length $arg)" ]; then
if [ "root=" = "$(expr substr $arg 1 5)" ]; then
{
*debug_arg=$(expr $arg : 'root=\(.\*\)')
resize2fs $debug_arg
}
fi
fi
done
更改执行权限:
sudo chmod 777 etc/onlyone.sh
返回 ubuntu_make 目录,新建脚本 make_ubuntu.sh,内容如下:
#!/bin/bash
#ubuntu(ubuntu-core) build already annotation
#not Often compiled .......too slow and need root
MAKE_THEARD=`cat /proc/cpuinfo| grep "processor"| wc -l`
RESULT="Image-ubuntu"
function creat_result_dir()
{
if [ ! -d $RESULT ];
then
{
mkdir Image-rk3288-ubuntu
}
fi
}
function ubuntu_core_build()
{
dd if=/dev/zero of=linux-rootfs-core.img bs=1M count=1000
sudo mkfs.ext4 -F -L linuxroot linux-rootfs-core.img
if [ ! -d mount ];
then
{
mkdir mount
}
fi
sudo mount linux-rootfs-core.img mount
sudo cp -a ubuntu_core/* mount
sudo umount mount
e2fsck -p -f linux-rootfs-core.img
resize2fs -M linux-rootfs-core.img
rm -rf mount
mv linux-rootfs-core.img $RESULT
}
function ubuntu_build()
{
dd if=/dev/zero of=linux-rootfs.img bs=1M count=5000
sudo mkfs.ext4 -F -L linuxroot linux-rootfs.img
if [ ! -d mount ];
then
{
mkdir mount
}
fi
sudo mount linux-rootfs.img mount
sudo cp -a ubuntu/* mount
sudo umount mount
e2fsck -p -f linux-rootfs.img
resize2fs -M linux-rootfs.img
rm -rf mount
mv linux-rootfs.img $RESULT
}
function ubuntu_clean()
{
rm $RESULT/linux-rootfs.img
}
function ubuntu_core_clean()
{
rm $RESULT/linux-rootfs-core.img
}
function result_clean()
{
rm -rf $RESULT
}
creat_result_dir
if [ $1 == "clean" ]
then
{
if [ ! -n $2 ]
then
{
ubuntu_core_clean
ubuntu_clean
result_clean
echo clean Img oK
}
elif [ $2 == "ubuntu" -o $2 == "ubuntu/" ]
then
{
ubuntu_clean
}
elif [ $2 == "ubuntu_core" -o $2 == "ubuntu_core/" -o $2 == "ubuntu-core" -o $2 == "ubuntu-core/" ]
then
{
ubuntu_core_clean
}
else
{
ubuntu_core_clean
ubuntu_clean
result_clean
echo clean Img oK
}
fi
}
elif [ $1 == "ubuntu_core" -o $1 == "ubuntu_core/" -o $1 == "ubuntu-core" -o $1 == "ubuntu-core/" ]
then
{
ubuntu_core_build
}
elif [ $1 == "ubuntu" -o $1 == "ubuntu/" ]
then
{
ubuntu_build
}
else
{
ubuntu_core_build
ubuntu_build
}
fi
赋予权限:
sudo chmod 777 make_ubuntu.sh
然后执行该脚本制作 ubuntu 或者 ubuntu_core 镜像:
sudo ./make_ubuntu.sh ubuntu
# 或者
sudo ./make_ubuntu.sh ubuntu_core
完成后可在Image-ubuntu下生成对应固件
注意 :
make_ubuntu 脚本限制了文件系统的大小,使用者可以根据文件系统的大小修改脚本,在脚本中找到 dd 命令,修改 count 参数即可,count * bs 即为总体大小。