linux制作 ext4镜像image 脚本demo

结构如下:
linux制作 ext4镜像image 脚本demo_第1张图片
build_linux_targetfs.sh

#!/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拷贝到镜像包,最后卸载,这样就做出了一个镜像包,
linux制作 ext4镜像image 脚本demo_第2张图片
linux制作 ext4镜像image 脚本demo_第3张图片
然后将文件替换,和md5 hash值,文件大小替换烧录文件里。记录烧录起始地址,结束地址,md5等等
在这里插入图片描述

你可能感兴趣的:(linux,驱动开发,linux内核,BootLoader,linux,服务器,java)