-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathinstall_to_emmc
executable file
·237 lines (202 loc) · 5.69 KB
/
install_to_emmc
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
echo ""
date
echo -e "\033[36m==============================="
echo "Installing Linux system to emmc"
echo -e "===============================\033[37m"
setterm -default
echo ""
_format=${1}
fatsize=64
sdcard="/dev/mmcblk1"
odir="/tmp/_extdir"
bootdir="/tmp/_fatdir"
if [ ! -b ${sdcard}boot0 ]; then
echo "Error: EMMC not found."
exit 1
fi
if [ ! -f /boot/boot0_OPI.fex ]; then
echo "Error: boot0_OPI.fex not found."
exit 1
fi
if [ ! -f /boot/u-boot_OPI-emmc.fex ]; then
echo "Error: u-boot_OPI-emmc.fex not found."
exit 1
fi
umount ${sdcard}* > /dev/null 2>&1
#----------------------------------------------------------
echo ""
echo -n "WARNING: EMMC WILL BE ERASED !, Continue (y/N)? "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
echo "Canceled.."
exit 0
fi
echo ""
#----------------------------------------------------------
echo "Erasing EMMC ..."
dd if=/dev/zero of=${sdcard} bs=1M count=32 > /dev/null 2>&1
sync
sleep 1
echo "Creating new filesystem on EMMC ..."
echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
sync
echo " New filesystem created on $sdcard."
sleep 1
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR."
exit 1
fi
sleep 1
echo "Partitioning EMMC ..."
sfat=40960
efat=$(( $fatsize * 1024 * 1024 / 512 + $sfat - 1))
echo " Creating boot & linux partitions"
sext4=$(( $efat + 1))
eext4=""
echo -e "n\np\n1\n$sfat\n$efat\nn\np\n2\n$sext4\n$eext4\nt\n1\nb\nt\n2\n83\nw" | fdisk ${sdcard} > /dev/null 2>&1
echo " OK."
sync
sleep 2
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR."
exit 1
fi
sleep 1
echo "Formating fat partition ..."
dd if=/dev/zero of=${sdcard}p1 bs=1M count=1 oflag=direct > /dev/null 2>&1
sync
sleep 1
mkfs.vfat -n EMMCBOOT ${sdcard}p1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " ERROR formating fat partition."
exit 1
fi
echo " fat partition formated."
dd if=/dev/zero of=${sdcard}p2 bs=1M count=1 oflag=direct > /dev/null 2>&1
sync
sleep 1
if [ "${_format}" = "btrfs" ] ; then
echo "Formating linux partition (btrfs), please wait ..."
# format as btrfs
mkfs.btrfs -O ^extref,^skinny-metadata -f -L emmclinux ${sdcard}p2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating btrfs partition."
exit 1
fi
else
echo "Formating linux partition (ext4), please wait ..."
mkfs.ext4 -L emmclinux ${sdcard}p2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating ext4 partition."
exit 1
fi
fi
echo " linux partition formated."
#************************************************************************
echo ""
echo "Instaling u-boot to EMMC ..."
dd if=/boot/boot0_OPI.fex of=${sdcard} bs=1k seek=8 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot."
exit 1
fi
dd if=/boot/u-boot_OPI-emmc.fex of=${sdcard} bs=1k seek=16400 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot."
exit 0
fi
sync
#************************************************************************
# -------------------------------------------------------------------
if [ ! -d $bootdir ]; then
mkdir -p $bootdir
fi
rm $bootdir/* > /dev/null 2>&1
sync
umount $bootdir > /dev/null 2>&1
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 EMMC partitions..."
if ! mount ${sdcard}p1 $bootdir; then
echo "ERROR mounting fat partitions..."
exit 1
fi
if ! mount ${_mntopt} ${sdcard}p2 $odir; then
echo "ERROR mounting linux partitions..."
umount $bootdir
exit 1
fi
echo "FAT partitions mounted to $bootdir"
echo "linux partition mounted to $odir"
#-----------------------------------------------------------------------------------------------
echo ""
echo "Copying file system to EMMC ..."
echo ""
#-----------------------------------------------------------------------------------------
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats / $odir/ > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " ERROR."
fi
#-----------------------------------------------------------------------------------------
sync
rm $odir/usr/local/bin/fs_resize_warning > /dev/null 2>&1
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
sync
#-----------------------------------------------------------------------------------------
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats /media/boot/ $bootdir/ > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " ERROR."
fi
#-----------------------------------------------------------------------------------------
sync
# UMOUNT
if ! umount $bootdir; then
echo "ERROR unmounting fat partition."
exit 1
fi
rm -rf $bootdir/* > /dev/null 2>&1
rmdir $bootdir > /dev/null 2>&1
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
echo ""
echo -e "\033[36m*******************************"
echo "Linux system installed to EMMC."
echo -e "*******************************\033[37m"
setterm -default
echo ""
exit 0