-
Notifications
You must be signed in to change notification settings - Fork 1
/
copylinux2img
126 lines (108 loc) · 5.09 KB
/
copylinux2img
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
#!/bin/bash
#
#将当前的运行的armbian-linux复制到一个镜像文件里面
#
#
#参考:shell 输出颜色的字体 https://www.cnblogs.com/nanruosen/p/16246506.html
#参考: linux下挂载镜像文件 https://blog.csdn.net/weixin_29668721/article/details/116813447
#参考:多行注释 https://zhuanlan.zhihu.com/p/54494213
#更改分区UUID https://www.dandelioncloud.cn/article/details/1517735553230565378
#export LC_ALL="en_US.UTF-8"
#BOOT_DEV="/dev/mmcblk0"
#ROOT_FS_DEV="/dev/mmcblk0p1"
#ROOT_FS_PATH="/" #末尾必须带上'/'
#DEST_IMG="/tmp/OPH6PC3LTS-EVMV4-$(date +"%m.%d.%H%M").img" #"/media/tony/usb/OPH6PC3LTS-EVMV4-20221023-01.img"
BOOT_DEV="/dev/sdb"
ROOT_FS_DEV="/dev/sdb1"
ROOT_FS_PATH="/media/tony/3ad1e1b0-e5b3-45cd-9357-e3777e0723d6/" #末尾必须带上'/'
DEST_IMG="/dev/shm/OPH6PC3LTS-EVMV4-20221025-01.img" #"/media/tony/usb/OPH6PC3LTS-EVMV4-20221023-01.img"
DEST_IMG_SIZE=7168 #具体需要再计算,单位:MB $((7 * 1024)) = 7168MB =7GB
DEST_ROOTFS_TYPE="ext4"
OFFSET=4 #4MB 单位:MB
#echo $BOOT_DEV
#echo $ROOT_FS_DEV
#echo $ROOT_FS_PATH
#echo $DEST_IMG
#echo $DEST_IMG_SIZE
#echo $DEST_ROOTFS_TYPE
#echo $EXCLUDE
#echo $OFFSET
echo -e "Creating blank image for rootfs \033[31m $DEST_IMG_SIZE\033[0m MiB"
rm /tmp/OPH6PC3LTS-EVMV4* -fr
#DEST_IMG_SIZE=1024 #测试一下 用100M数据
#实测复制前面的4MB数据会导致镜像文件的尺寸也变成4MB,所以尝试一下将写入空数据的命令放在后面
#单独复制boot设备的前面4M的数据,就是为了复制uboot到目标镜像文件上,实际uboot并没有这么大,只是管它呢,多复制点也不会有什么后果.这么做就是为了简单
#文件实际尺寸是DEST_IMG_SIZE + OFFSET
dd if=$BOOT_DEV bs=1M count=$OFFSET of=$DEST_IMG
echo -e "Write bootdev fisrt $OFFSET MB to image file \033[31m $DEST_IMG\033[0m success."
dd if=/dev/zero bs=1M status=none count=$DEST_IMG_SIZE | pv -p -b -r -s $(( $DEST_IMG_SIZE * 1024 * 1024 )) -N "[ .... ] dd" | dd status=none bs=1M seek=$OFFSET of=$DEST_IMG
echo -e "Write blank data to image file \033[31m $DEST_IMG\033[0m success."
#create rootfs partation type ext4.
parted -s $DEST_IMG -- mklabel "msdos"
rootstart=$(($OFFSET * 2048)) #4*2048=8192个扇区,每个扇区的尺寸是2048=2K,2K*2K = 4MB
#parted -s $DEST_IMG -- mkpart primary ext4 ${rootstart}s 100%
parted -s $DEST_IMG -- mkpart primary ext4 $OFFSET 100%
#echo -e "\033[32m 绿色字\033[0m"
echo -e "\033[32m partation\033[0m image file \033[31m $DEST_IMG\033[0m success."
LOOP=$(losetup -f)
echo -e "\033[32m losetup\033[0m $LOOP - \033[31m $DEST_IMG\033[0m ."
losetup $LOOP $DEST_IMG
echo -e "\033[32m partprobe\033[0m - \033[31m $LOOP\033[0m ."
partprobe $LOOP
#check_loop_device ${LOOP}p1
# mkfs.${mkfs[$ROOTFS_TYPE]} ${mkopts[$ROOTFS_TYPE]} $rootdevice
mkfs.ext4 -q -m 2 -O ^64bit,^metadata_csum ${LOOP}p1
tune2fs -o journal_data_writeback ${LOOP}p1 > /dev/null
#mount -o loop,offset=8192 disk.img /tmp/looprootfs
#mount -o loop,offset=8192 ./OPH6PC3LTS-EVMV4-10.24.2017.img /tmp/looprootfs
MOUNT="/dev/shm/looprootfs"
rm ${MOUNT} -fr
mkdir ${MOUNT}
mount ${LOOP}p1 ${MOUNT}/
#ls ${MOUNT} -la
mkdir -p ${MOUNT}/dev
mkdir -p ${MOUNT}/proc
mkdir -p ${MOUNT}/sys
mkdir -p ${MOUNT}/media
mkdir -p ${MOUNT}/mnt
mkdir -p ${MOUNT}/run
mkdir -p ${MOUNT}/tmp
mkdir -p ${MOUNT}/srv
mkdir -p ${MOUNT}/var
mkdir -p ${MOUNT}/var/log
mkdir -p ${MOUNT}/var/log.hdd
chown root:syslog ${MOUNT}/var/log
chown root:syslog ${MOUNT}/var/log.hdd
cp -r ${ROOT_FS_PATH}boot ${MOUNT}
cp -r ${ROOT_FS_PATH}bin ${MOUNT}
cp -r ${ROOT_FS_PATH}etc ${MOUNT}/
cp -r ${ROOT_FS_PATH}root ${MOUNT}/
cp -r ${ROOT_FS_PATH}lib ${MOUNT}/
cp -r ${ROOT_FS_PATH}opt ${MOUNT}/
cp -r ${ROOT_FS_PATH}sbin ${MOUNT}/
cp -r ${ROOT_FS_PATH}selinux ${MOUNT}/
cp -r ${ROOT_FS_PATH}usr ${MOUNT}/
cp -r ${ROOT_FS_PATH}sbin ${MOUNT}/
cp -r ${ROOT_FS_PATH}home ${MOUNT}/
cp -r ${ROOT_FS_PATH}var/backups ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/cache ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/crash ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/lib ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/local ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/lock ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/mail ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/opt ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/run ${MOUNT}/var/
# snap
cp -r ${ROOT_FS_PATH}var/spool ${MOUNT}/var/
cp -r ${ROOT_FS_PATH}var/tmp ${MOUNT}/var/
echo -e "\033[32m cp files to \033[0m - \033[31m ${MOUNT}\033[0m ."
#ls ${MOUNT} -la
umount -l "${MOUNT}" >/dev/null 2>&1
# umount /dev/sdb1
# tune2fs /dev/sdb1 -U random /dev/sdb1 # blkid | grep sdb1
# tune2fs -U random /dev/loop0p1
# tune2fs -U $(blkid -s UUID -o value /dev/mmcblk0p1) /dev/loop0p1
#将镜像上的分区UUID修改成和系统的ROOT分区UUID一样,就不用修改uboot和fstab的参数了.
tune2fs -U $(blkid -s UUID -o value $ROOT_FS_DEV) ${LOOP}p1
losetup -d "${LOOP}" >/dev/null 2>&1