• linux制作 ext4镜像image 脚本demo


    结构如下:
    在这里插入图片描述
    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	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. The target image:
      filesystem/targetfs-images

    将需要打包进镜像的文件丢进target的对应文件下,先生成ext4文件进行,然后挂载,cp拷贝到镜像包,最后卸载,这样就做出了一个镜像包,
    在这里插入图片描述
    在这里插入图片描述
    然后将文件替换,和md5 hash值,文件大小替换烧录文件里。记录烧录起始地址,结束地址,md5等等
    在这里插入图片描述

  • 相关阅读:
    陌生人随意进出?学校宿舍这样管理更安全
    ISO14708-3:2017中关于有源植入物对非电离辐射的防护
    2024-2-22 学习笔记(Yolo-World, Yolov8-OBB,小样本分类,CNN/Transfomer选择)
    ROS基础学习
    Windows取证——学习笔记(二)
    怎样把1.ts-10.ts的文件拼接成一个MP4文件
    数据结构(超详细讲解!!)第二十二节 广义表
    Lsm树学习笔记
    x64 番外篇——保护模式相关
    代码随想录Day_52打卡
  • 原文地址:https://blog.csdn.net/rjszcb/article/details/134535999