-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathimage_from_dir
executable file
·290 lines (245 loc) · 8.26 KB
/
image_from_dir
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
echo ""
date
echo -e "\033[36m==================================================="
echo "Creating Ubuntu/Debian SD Card (image) for OrangePI"
echo -e "===================================================\033[37m"
echo ""
_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $_DIR
# ===================================================
if [ "${1}" = "" ]; then
echo "Source directory not specified."
echo "USAGE: image_from_dir <directory> <img_name | /dev/sdX> [noformat] [opi-2|opi-PC|opi-plus]"
exit 0
fi
if [ "${2}" = "" ]; then
echo "Destination not specified."
echo "USAGE: image_from_dir <directory> <img_name | /dev/sdX> [noformat] [opi-2|opi-PC|opi-plus]"
exit 0
fi
idir=$1
if [ ! -d $idir ]; then
echo "Source directory not found."
echo "USAGE: image_from_dir <directory> <img_name | /dev/sdX> [noformat] [opi-2|opi-PC|opi-plus]"
exit 0
fi
# ===================================================
. params.sh
# ********************************************************
# Linux partition size IS CALCULATED from directory size *
# ********************************************************
_ext4size=`du -s $1 | awk '{print $1}'`
ext4sizeMB=$(expr $_ext4size / 1024)
ext4size=$(expr $ext4sizeMB / 2 + $ext4sizeMB)
linuxsize=$(expr $ext4sizeMB + 500)
_boot_on_ext4="no"
image_name="$2"
odir="_extdir"
bootdir="_fatdir"
if [ "${3}" != "noformat" ] ; then
# =========
. format.sh
# =========
fi
# === Show info ===============
echo ""
echo "==== Info ===="
echo " from: $idir"
echo " to: $sdcard"
#echo "ext4boot: $_boot_on_ext4"
if [ "${_format}" = "btrfs" ] ; then
echo " btrfs: yes"
else
echo " btrfs: no"
fi
echo "dir size: $ext4sizeMB MB"
echo "img size: $linuxsize MB"
echo "=============="
echo ""
# -------------------------------------------------------------------
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
if [ ! -d $bootdir ]; then
mkdir -p $bootdir
fi
rm $bootdir/* > /dev/null 2>&1
sync
umount $bootdir > /dev/null 2>&1
fi
if [ ! -d $odir ]; then
mkdir -p $odir
fi
rm -rf $odir/* > /dev/null 2>&1
sync
umount $odir > /dev/null 2>&1
sleep 1
# ================
# MOUNT PARTITIONS
# ================
if [ "${_format}" = "btrfs" ] ; then
_mntopt="-o compress-force=lzo"
else
_mntopt=""
fi
echo ""
echo "Mounting SD Card partitions..."
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
if ! mount ${sdcard}1 $bootdir; then
echo "ERROR mounting fat partitions..."
exit 1
fi
if ! mount ${_mntopt} ${sdcard}2 $odir; then
echo "ERROR mounting linux partitions..."
umount $bootdir
exit 1
fi
echo "FAT partitions mounted to $bootdir"
else
if ! mount ${_mntopt} ${sdcard}1 $odir; then
echo "ERROR mounting linux partitions..."
exit 1
fi
fi
echo "linux partition mounted to $odir"
#-----------------------------------------------------------------------------------------------
echo ""
echo "Copying file system to image ..."
echo ""
#-------------------------------------------------------------------------------
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats $idir/ $odir/
#-------------------------------------------------------------------------------
sync
# === Copy kernel, modules & scripts =============================================
if [ ! -d $odir/usr/local/bin ]; then
mkdir -p $odir/usr/local/bin
fi
if [ ! -d $odir/boot ]; then
mkdir -p $odir/boot
fi
if [ ! -d $odir/media/boot ]; then
mkdir -p $odir/media/boot
fi
rm -r $odir/boot/* > /dev/null 2>&1
cp orange/uImage_OPI* $odir/boot > /dev/null 2>&1
cp orange/boot0_OPI.fex $odir/boot > /dev/null 2>&1
cp orange/u-boot_OPI*.fex $odir/boot > /dev/null 2>&1
cp fs_resize $odir/usr/local/bin > /dev/null 2>&1
cp update_kernel.sh $odir/usr/local/bin > /dev/null 2>&1
cp install_* $odir/usr/local/bin > /dev/null 2>&1
if [ "${3}" = "noinstallscr" ] || [ "${4}" = "noinstallscr" ] || [ "${5}" = "noinstallscr" ]; then
rm $odir/usr/local/bin/install_*_desktop > /dev/null 2>&1
fi
if [ "${3}" = "opi-2" ] || [ "${4}" = "opi-2" ]; then
cp orange/uImage_OPI-2 $odir/boot/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-2_* $odir/boot > /dev/null 2>&1
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
cp orange/uImage_OPI-2 $bootdir/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-2_1080p60_hdmi $bootdir/script.bin > /dev/null 2>&1
cp orange/script.bin.OPI-2_* $bootdir > /dev/null 2>&1
fi
elif [ "${3}" = "opi-pc" ] || [ "${4}" = "opi-pc" ]; then
cp orange/uImage_OPI-2 $odir/boot/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-PC_* $odir/boot > /dev/null 2>&1
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
cp orange/uImage_OPI-2 $bootdir/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-PC_1080p60_hdmi $bootdir/script.bin > /dev/null 2>&1
cp orange/script.bin.OPI-PC_* $bootdir > /dev/null 2>&1
fi
elif [ "${3}" = "opi-plus" ] || [ "${4}" = "opi-plus" ]; then
cp orange/uImage_OPI-PLUS $odir/boot/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-PLUS_* $odir/boot > /dev/null 2>&1
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
cp orange/uImage_OPI-PLUS $bootdir/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-PLUS_1080p60_hdmi $bootdir/script.bin > /dev/null 2>&1
cp orange/script.bin.OPI-PLUS_* $bootdir > /dev/null 2>&1
fi
else
cp orange/uImage_OPI-2 $odir/boot/uImage > /dev/null 2>&1
cp orange/script.bin.OPI-* $odir/boot > /dev/null 2>&1
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
cp orange/uImage_OPI-2 $bootdir/uImage > /dev/null 2>&1
cp orange/uImage_OPI* $bootdir > /dev/null 2>&1
cp orange/script.bin.OPI-* $bootdir > /dev/null 2>&1
cp orange/script.bin.OPI-2_720p60_hdmi $bootdir/script.bin > /dev/null 2>&1
fi
fi
rm -r $odir/lib/modules/* > /dev/null 2>&1
cp -rf orange/lib/modules $odir/lib > /dev/null 2>&1
rm -r $odir/lib/firmware/* > /dev/null 2>&1
cp -rf orange/lib/firmware $odir/lib > /dev/null 2>&1
chown -R root:root $odir/lib/* > /dev/null 2>&1
# ================================================================================
sync
# ************************
# Create fs_resize warning
# ************************
cat > ${odir}/usr/local/bin/fs_resize_warning << _EOF_
#!/bin/sh
echo -e "\033[31m\033[1m***********************************************"
echo -e "WARNING: TO RESIZE FILESYSTEM RUN:"
echo -e "sudo fs_resize or sudo /usr/local/bin/fs_resize"
echo -e "to remove this message run:"
echo -e "sudo rm /usr/local/bin/fs_resize_warning"
echo -e "***********************************************\033[22m\033[37m"
setterm -default
_EOF_
chmod +x ${odir}/usr/local/bin/fs_resize_warning
# ***************
# Create fstab
# ***************
echo "Creating \"fstab\""
echo "# OrangePI fstab" > $odir/etc/fstab
if [ "${_format}" = "btrfs" ] ; then
echo "/dev/mmcblk0p2 / btrfs subvolid=0,noatime,nodiratime,compress=lzo 0 1" >> $odir/etc/fstab
else
echo "/dev/mmcblk0p2 / ext4 errors=remount-ro,noatime,nodiratime 0 1" >> $odir/etc/fstab
fi
echo "/dev/mmcblk0p1 /media/boot vfat defaults 0 0" >> $odir/etc/fstab
echo "tmpfs /tmp tmpfs nodev,nosuid,mode=1777 0 0" >> $odir/etc/fstab
echo ""
echo "sync, please wait ..."
sync
# UMOUNT
if [ ! "${_boot_on_ext4}" = "yes" ] ; then
if ! umount $bootdir; then
echo "ERROR unmounting fat partition."
exit 0
fi
rm -rf $bootdir/* > /dev/null 2>&1
rmdir $bootdir > /dev/null 2>&1
fi
if ! umount $odir; then
echo "ERROR unmounting linux partitions."
exit 0
fi
rm -rf $odir/* > /dev/null 2>&1
rmdir $odir > /dev/null 2>&1
sync
if [ -f ${sdcard}u ] && [ -f ${sdcard}2 ]; then
echo "Creating SDCard image..."
dd if=${sdcard}u of=${sdcard} > /dev/null 2>&1
if [ -f ${sdcard}1 ]; then
dd if=${sdcard}1 of=${sdcard} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
fi
if [ -f ${sdcard}2 ]; then
dd if=${sdcard}2 of=${sdcard} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
fi
if [ "${_compress}" = "yes" ]; then
echo "Compressing image..."
xz -z -k -9 -f -v $sdcard
md5sum $sdcard > $sdcard.md5sum
md5sum $sdcard.xz > $sdcard.xz.md5sum
fi
fi
echo ""
date
echo ""
echo -e "\033[36m*********************"
echo "SDCard image created."
echo -e "*********************\033[37m"
echo ""
exit 0