linux制作 ext4镜像image 脚本demo

结构如下:

build_linux_targetfs.sh

c 复制代码
#!/bin/bash

CHECK_MARK="\033[0;32m\xE2\x9C\x94\033[0m"
X_MARK="\033[0;1;31mX\033[0m"

export TOP_DIR=$PWD
export TARGET_IMAGE_PATH=$TOP_DIR/filesystem/targetfs-images
export BSP_IMAGE_PATH=${TOP_DIR}/../bsp_images
build_app=mpd

#usage
function usage() {
	echo "Usage:
               percp :                	build percp image
               pnc :                 		build pnc image
               map :                		build map image
               model :          		build model image
               platform_service :      	build platform_service image
               platform :          		build platform image
               parameter :     		build parameter image
	       all :				build all above images

	       example :
	       1. build one image:
	       ./build_linux_targetfs.sh percp
	       
	       2. build any numbers images:
	       ./build_linux_targetfs.sh percp pnc
	       or
	       ./build_linux_targetfs.sh percp pnc map
	       ......
	       
	       3. build all partitions image:
	       ./build_linux_targetfs.sh all
               "
}

#update bsp_images
function update_bsp_images() {

	FLASH_IMAGE_PATH=${BSP_IMAGE_PATH}/642-63663-0001-001_TS2/flash-images
	FLASH_FILE=${FLASH_IMAGE_PATH}/FileToFlash.txt	
	TEMP_FILE=${FLASH_IMAGE_PATH}/temp.txt
	FLASH_IMAGE_NAME=$TARGET_IMAGE_PATH/$1.img

	grep $1_ $FLASH_FILE > $TEMP_FILE

	NEW_MD5=$(md5sum ${FLASH_IMAGE_NAME} | awk -F " " {'print $1'})
	while read LINE
	do
		IMAGE_NAME=$(echo $LINE | awk -F " " {'print $3'})
		cp $FLASH_IMAGE_NAME $FLASH_IMAGE_PATH/$IMAGE_NAME
		sync

		BAK_MD5=$(echo $LINE | awk -F " " {'print $11'})
		if [ "${BAK_MD5}" != "${NEW_MD5}" ];then
   			sed -i 's/'${BAK_MD5}'/'${NEW_MD5}'/g' ${FLASH_FILE}
		fi
	done < $TEMP_FILE

	rm -f $TEMP_FILE

	echo -e "\033[2K\\r${CHECK_MARK} update $1.img to bsp_images successed!"
	sync
}

#create linux extend targetfs
function build_linux_extend_targetfs() {

	if [ ! -d $TARGET_IMAGE_PATH ];then
		mkdir -p $TARGET_IMAGE_PATH
		sync
	fi

	sudo dd if=/dev/zero of=$TARGET_IMAGE_PATH/$1.img bs=16384 count=$(($2/16384))
	ret_val_mkxfs=$?
	if [ $ret_val_mkxfs -ne 0 ]; then
		printf "${X_MARK} ***ERROR*** Failed to create the filesystem/targetfs/$1.img . error code = $ret_val_mkxfs ...\n"
		exit ${ret_val_mkxfs}
	fi

	sudo mkfs.ext4 $TARGET_IMAGE_PATH/$1.img
	sudo mkdir -p $3_bak
	sudo mount $TARGET_IMAGE_PATH/$1.img $3_bak
	sudo cp -raf $3/* $3_bak
	sudo umount -f $3_bak
	sudo rm -rf $3_bak
	sync

	echo -e "\033[2K\\r${CHECK_MARK} build $1.img successed!"

	if [ -d $BSP_IMAGE_PATH ];then
		update_bsp_images $1
	else
		echo -e "\033[2K\\r${CHECK_MARK} update $1.img to bsp_images error!"
	fi
}

#build parameter.img
function build_parameter() {

	IMAGE_SIZE=16777216
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/vehicle_parameter
	TARGET_IMAGE_NAME=parameter

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build model.img
function build_model() {

	IMAGE_SIZE=6442450944
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/model
	TARGET_IMAGE_NAME=model

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build platform_service.img
function build_platform_service() {

	IMAGE_SIZE=1073741824
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/platform_service
	TARGET_IMAGE_NAME=platform_service

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build platform.img
function build_platform() {

	IMAGE_SIZE=2684354560
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/platform
	TARGET_IMAGE_NAME=platform

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build map.img
function build_map() {

	IMAGE_SIZE=1289748480
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/map
	TARGET_IMAGE_NAME=map

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build pnc.img
function build_pnc() {

	IMAGE_SIZE=1610612736
        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/pnc
	TARGET_IMAGE_NAME=pnc

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

#build percp.img
function build_percp() {

	IMAGE_SIZE=1289748480

        ROOTFS_LOCAL_DIR=${TOP_DIR}/filesystem/targetfs/percp
	TARGET_IMAGE_NAME=percp

	build_linux_extend_targetfs $TARGET_IMAGE_NAME $IMAGE_SIZE $ROOTFS_LOCAL_DIR
}

if [ $# -ge 1 ];then

	for i in "$@";do
		case "$i" in
			percp)
				build_percp
				;;
			pnc)
				build_pnc
				;;
			map)
				build_map
				;;
			platform)
				build_platform
				;;
			platform_service)
				build_platform_service
				;;
			model)
				build_model
				;;
			parameter)
				build_parameter
				;;
			all)
				build_percp
				build_pnc
				build_map
				build_platform
				build_platform_service
				build_model
				build_parameter
				;;
			?)
				usage
				;;
		esac
	done

else
	usage
fi	

readme 使用方法

example :

a. build one image:

./build_linux_targetfs.sh percp

b. build any numbers images:
       ./build_linux_targetfs.sh percp pnc
       or
       ./build_linux_targetfs.sh percp pnc platform
       ......
       
c. build all partitions image:
       ./build_linux_targetfs.sh all
  1. The target image:
    filesystem/targetfs-images

将需要打包进镜像的文件丢进target的对应文件下,先生成ext4文件进行,然后挂载,cp拷贝到镜像包,最后卸载,这样就做出了一个镜像包,

然后将文件替换,和md5 hash值,文件大小替换烧录文件里。记录烧录起始地址,结束地址,md5等等

相关推荐
花姐夫Jun6 分钟前
在 CentOS 8 系统上安装 Jenkins 的全过程
linux·centos·jenkins
Code成立7 分钟前
《Java核心技术I》Swing的网格包布局
java·开发语言·swing
中草药z13 分钟前
【Spring】深入解析 Spring 原理:Bean 的多方面剖析(源码阅读)
java·数据库·spring boot·spring·bean·源码阅读
地球资源数据云14 分钟前
全国30米分辨率逐年植被覆盖度(FVC)数据集
大数据·运维·服务器·数据库·均值算法
信徒_21 分钟前
常用设计模式
java·单例模式·设计模式
是店小二呀23 分钟前
【Linux】Linux开发利器:make与Makefile自动化构建详解
linux·运维·自动化
神仙别闹26 分钟前
基于C#实现的(WinForm)模拟操作系统文件管理系统
java·git·ffmpeg
小爬虫程序猿27 分钟前
利用Java爬虫速卖通按关键字搜索AliExpress商品
java·开发语言·爬虫
组合缺一32 分钟前
Solon v3.0.5 发布!(Spring 可以退休了吗?)
java·后端·spring·solon
程序猿零零漆34 分钟前
SpringCloud 系列教程:微服务的未来(二)Mybatis-Plus的条件构造器、自定义SQL、Service接口基本用法
java·spring cloud·mybatis-plus