forked from Moxa-Linux/resize-image
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresize-img
executable file
·155 lines (128 loc) · 3.26 KB
/
resize-img
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
#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
#
# Name:
# Resize image utility
#
# Authors:
# 2018 Wes Huang <Wes.Huang@moxa.com>
# 2019 Ken CJ Chou <KenCJ.Chou@moxa.com>
#
set -e
VERSION=1.1.0
ORI_MNT_DIR="/tmp/original_mnt"
RES_MNT_DIR="/tmp/resized_mnt"
RES_IMAGE_NAME="resize.img"
usage() {
echo -e "Usage:"
echo -e " # ${0} <image_type> <image_file>"
echo -e ""
echo -e "Parameters:"
echo -e " image_type"
echo -e " Image type must be A, B or C"
echo -e " Further informationcan be found at README"
echo -e ""
echo -e " image_file"
echo -e " A image format file, usually with \".img\" file extension"
echo -e ""
echo -e "Example:"
echo -e " Resize a type A image which named \"example.img\""
echo -e " The output image file will be \"resize.img\""
echo -e " # ./resize-img A example.img"
echo -e ""
return 0
}
log_msg() {
local msg=${1}
echo -ne "\e[1;33m[resize-img]\e[0m "
echo "${msg}"
return 0
}
reset_image() {
local image_type=${1}
local mnt_dir=${2}
if [ "${image_type}" == "A" ]; then
if [ -f "${mnt_dir}/p2/sbin/expand2fs.bak" ]; then
mv ${mnt_dir}/p2/sbin/expand2fs.bak ${mnt_dir}/p2/sbin/expand2fs
fi
else
rm -f ${mnt_dir}/p3/v1/sbin_rw/upper/expand2fs.bak
rm -f ${mnt_dir}/p3/v1/sbin_rw/upper/expand2fs
fi
return 0
}
resize_image() {
local image_type=${1}
local image=${2}
local p1_size=32
local p2_size
local p3_size
if ! [ -f "${image}" ]; then
echo "image not found: ${image}" >&2
return 2
fi
log_msg "Mounting original image"
./mount-img mount "${image}" "${ORI_MNT_DIR}"
log_msg "Calculating original image's partitions size"
p2_size="$(du -s -BM "${ORI_MNT_DIR}/p2/")"
p2_size="${p2_size%%M*}"
p3_size="$(du -s -BM "${ORI_MNT_DIR}/p3/")"
p3_size="${p3_size%%M*}"
p2_size="$(( p2_size + p2_size / 5 ))"
if [ "${image_type}" == "B" ] || [ "${image_type}" == "C" ]; then
p3_size="$(( p3_size + p3_size / 5 ))"
fi
p3_size="$(( p3_size + 32 ))"
log_msg "Creating new image"
if [ "${image_type}" == "A" ]; then
./create-img "${RES_IMAGE_NAME}" "1,${p1_size},y,b,vfat" "3,${p3_size},n,83,ext4" "2,${p2_size},n,83,ext4"
elif [ "${image_type}" == "B" ]; then
./create-img "${RES_IMAGE_NAME}" "1,${p1_size},y,b,vfat" "2,${p2_size},n,83,ext4" "3,${p3_size},n,83,ext4"
else
./create-img "${RES_IMAGE_NAME}" "1,${p1_size},y,83,ext4" "2,${p2_size},n,83,ext4" "3,${p3_size},n,83,ext4"
fi
log_msg "Mounting new image"
./mount-img mount "${RES_IMAGE_NAME}" "${RES_MNT_DIR}"
log_msg "Copying files"
cp -a ${ORI_MNT_DIR}/p1/* ${RES_MNT_DIR}/p1/
cp -a ${ORI_MNT_DIR}/p2/* ${RES_MNT_DIR}/p2/
cp -a ${ORI_MNT_DIR}/p3/* ${RES_MNT_DIR}/p3/
log_msg "Unmounting original image"
./mount-img umount "${ORI_MNT_DIR}"
log_msg "Resetting image"
reset_image "${image_type}" "${RES_MNT_DIR}"
log_msg "Unmounting new image"
./mount-img umount "${RES_MNT_DIR}"
log_msg "OK"
return 0
}
main() {
local option1=${1}
local image=${2}
local image_type
case "${option1}" in
"-v"|"--version")
echo "${VERSION}"
;;
"a"|"A"|"b"|"B"|"c"|"C")
if [ $# -ne 2 ]; then
usage >&2
return 1
fi
image_type="${option1^^}"
resize_image "${image_type}" "${image}"
;;
*)
usage >&2
return 1
;;
esac
return 0
}
if [ "${EUID}" -ne 0 ]; then
echo "Please run as root" >&2
exit 1
fi
main "$@"
exit 0