forked from cnxsoft/a10-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeSD.sh
executable file
·174 lines (161 loc) · 3.88 KB
/
makeSD.sh
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
#!/bin/sh
# Usage ./makeSD.sh /dev/sdx image.bin
checkSyntax () {
if [ -z $1 ] | [ -z $2 ]; then
echo "Usage: $0 [device] [image]"
exit 1
fi
if [ ! -e $1 ]; then
echo "Invalid device: $1"
exit 1
fi
if [ ! -f $2 ]; then
echo "File $2 missing"
exit 1
fi
}
umountSD () {
partlist=`mount | grep $1 | awk '{ print $1 }'`
for part in $partlist
do
sudo umount $part
done
}
partitionSD () {
echo "Delete Existing Partition Table"
sudo dd if=/dev/zero of=$1 bs=1M count=1 >> makesd.log
echo "Creating Partitions"
sudo parted $1 --script mklabel msdos >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to create label for $1"
exit 1
fi
echo "Partition 1 - ${1}1"
sudo parted $1 --script mkpart primary fat32 2048s 16MB >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to create ${1}1 partition"
exit 1
fi
vfat_end=` sudo fdisk -lu $1 | grep ${1}1 | awk '{ print $3 }' `
ext4_offset=`expr $vfat_end + 1`
echo "Partition 2 (Starts at sector No. $ext4_offset)"
sudo parted $1 --script mkpart primary ext4 ${ext4_offset}s -- -1 >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to create ${1}2 partition"
exit 1
fi
echo "Format Partition 1 to VFAT"
sudo mkfs.vfat ${1}1 >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to format ${1}1 partition"
exit 1
fi
echo "Format Partition 2 to EXT-4"
sudo mkfs.ext4 ${1}2 >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to format ${1}2 partition"
exit 1
fi
}
copyUboot ()
{
echo "Copy U-Boot to SD Card"
sudo dd if=$2 bs=1024 skip=8 count=1000 of=$1 seek=8
}
mountPartitions ()
{
echo "Mount image partitions"
loop_device=`sudo kpartx -l $2 | grep p1 | awk '{ print $5 }' | cut -c10`
sudo kpartx -a $2 >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to run kpart"
exit 1
fi
mkdir -p mntIMGvfat mntIMGrootfs >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to create image mount points"
goto exit
fi
echo "Mount VFAT Parition (IMG)"
sudo mount -o loop /dev/mapper/loop${loop_device}p1 mntIMGvfat >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to mount VFAT partition (IMG)"
goto cleanup
fi
echo "Mount EXT4 Parition (IMG)"
sudo mount -o loop /dev/mapper/loop${loop_device}p2 mntIMGrootfs >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to mount EXT4 partition (IMG)"
goto cleanup
fi
echo "Mount SD card partitions"
mkdir -p mntSDvfat mntSDrootfs >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to create SD card mount points"
goto cleanup
fi
echo "Mount VFAT Parition (SD)"
sudo mount ${1}1 mntSDvfat >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to mount VFAT partition (SD)"
goto cleanup
fi
echo "Mount EXT4 Parition (SD)"
sudo mount ${1}2 mntSDrootfs >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to mount EXT4 partition (SD)"
goto cleanup
fi
}
umountPart() {
if [ -d $1 ]; then
mounted=`mount | grep $1`
if [ ! -z mounted ]; then
echo "Umount $2"
sudo umount $1 >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to umount $2)"
else
echo "Delete $1"
rm -rf $1 >> makesd.log
fi
else
echo "Delete $1"
rm -rf $1 >> makesd.log
fi
fi
}
copyData ()
{
echo "Copy VFAT partition files to SD Card"
sudo cp -a mntIMGvfat/* mntSDvfat >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to copy VFAT partition data to SD Card"
goto cleanup
fi
echo "Copy rootfs partition files to SD Card"
sudo cp -a mntIMGrootfs/* mntSDrootfs >> makesd.log
if [ $? -ne 0 ]; then
echo "Failed to copy rootfs partition data to SD Card"
goto cleanup
fi
}
cleanup ()
{
umountPart mntIMGvfat "VFAT Partition (IMG)"
umountPart mntIMGrootfs "EXT4 Partition (IMG)"
sudo kpartx -d $imgfile
umountPart mntSDvfat "VFAT Partition (SD)"
umountPart mntSDrootfs "EXT4 Partition (SD)"
exit
}
# "main"
echo "makeSD log file" > makesd.log
checkSyntax $1 $2
imgfile=$2
umountSD $1
partitionSD $1
copyUboot $1 $2
mountPartitions $1 $2
copyData
cleanup