forked from mad-ady/OdroidC1-tripleboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_openelec
executable file
·248 lines (214 loc) · 6.88 KB
/
extract_openelec
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
. params.sh
. func.sh
echo ""
date
echo "**********************************************"
echo "* Extracting structure of OpenELEC SD Card *"
echo "* and copying partitions data to directories *"
echo "**********************************************"
echo ""
check_sdcard "nocreate"
if [ $? -ne 0 ]; then
exit $?
fi
umount ${sdcard}* > /dev/null 2>&1
sleep 1
mkdir -p ${bkpdir}/openELECimg > /dev/null 2>&1
#=======================================================================
# Calculate partitions offsets and sizes
echo ""
echo "Analyzing sd card ..."
sdcard_part=`fdisk -l $sdcard | grep Linux | awk '{print $1}'`
sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
if [ "${sdcard_sect}" = "" ]; then
sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
fi
sdcard_end=$(expr $sdcard_sect - 1024)
fat=`fdisk -l $sdcard | grep FAT`
if [ "${fat}" = "" ]; then
linuxpart="1"
else
linuxpart="2"
fi
if [ "${linuxpart}" = "2" ]; then
fat_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
fat_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
fat_size=$(( ($fat_end - $fat_start + 1) / 2048 ))
linux_start=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $2}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $3}'`
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
else
linux_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
fi
echo ""
echo " SDCard size: $sdcard_sect blocks, $(expr $sdcard_sect / 2048 ) M"
echo "------------------------------------------------"
if [ "${linuxpart}" = "2" ]; then
printf "%14s" "FAT part:"; printf "%12s" $fat_start; printf "%12s" $fat_end; printf "%10s\n" "$fat_size M"
fi
printf "%14s" "ext4 part:"; printf "%12s" $linux_start; printf "%12s" $linux_end; printf "%10s\n" "$linux_size M"
echo "------------------------------------------------"
echo ""
if [ "${linuxpart}" = "2" ]; then
if [ "${fat_start}" = "" ] || [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${fat_start}" = "4096" ]; then
echo "Bad SDCard partition structure (start) !"
exit 1
fi
else
if [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${linux_start}" = "4096" ]; then
echo "Bad SDCard partition structure (start)!"
exit 1
fi
echo "SDCard is ext4 only card!"
fi
mkdir _mnt > /dev/null 2>&1
umount _mnt > /dev/null 2>&1
mkdir _mnt1 > /dev/null 2>&1
umount _mnt1 > /dev/null 2>&1
echo ""
echo -n "WARNING: OpenElec files in $bkpdir/openELECimg WILL BE UPDATED !, Continue (y/N)? "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
echo "Canceled.."
exit 0
fi
echo ""
# EXTRACTING FILES
echo ""
if [ "${_isimage}" = "yes" ]; then
kpartx -l ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR testing kpartx"
exit 1
fi
loop_dev=`kpartx -l $sdcard | grep 4096 | sed s/"\// "/g | awk '{print $6}'`
kpartx -a -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting with kpartx"
exit 1
fi
sleep 1
if [ "${linuxpart}" = "2" ]; then
mkdir ${bkpdir}/openELECimg/System > /dev/null 2>&1
loop_mapp="/dev/mapper/${loop_dev}p1"
mount ${loop_mapp} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${loop_mapp}"
kpartx -d -s ${sdcard} > /dev/null 2>&1
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
exit 1
fi
echo "Copying FAT (System) partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/openELECimg/System/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/openELECimg/System | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
loop_mapp="/dev/mapper/${loop_dev}p${linuxpart}"
mkdir ${bkpdir}/openELECimg/Storage > /dev/null 2>&1
mount ${loop_mapp} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${loop_mapp}"
kpartx -d -s ${sdcard} > /dev/null 2>&1
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
exit 1
fi
echo ""
echo "Copying ext4 (Storage) partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/openELECimg/Storage/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/openELECimg/Storage | awk '{print $1}'`
echo "File system size: $fs_size"
sleep 2
umount _mnt > /dev/null 2>&1
kpartx -d -s ${sdcard} > /dev/null 2>&1
sleep 1
if [ $? -ne 0 ]; then
echo "WARNING: ERROR unmounting with kpartx"
fi
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
else
if [ "${linuxpart}" = "2" ]; then
mkdir ${bkpdir}/openELECimg/System > /dev/null 2>&1
mount ${sdcard}1 _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}2"
exit 1
fi
echo "Copying FAT (System) partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/openELECimg/System/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/openELECimg/System | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
mkdir ${bkpdir}/openELECimg/Storage > /dev/null 2>&1
mount ${sdcard}${linuxpart} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}${linuxpart}"
exit 1
fi
echo ""
echo "Copying ext4 (Storage) partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/openELECimg/Storage/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/openELECimg/Storage | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
echo ""
echo "Preparing OpenELEC for multi boot..."
cd ${bkpdir}/openELECimg/System
if [ ! -f INITRD ]; then
echo "INITRD not found."
exit 1
fi
cp INITRD INITRD.orig
dd if=INITRD of=initrd.oelec bs=64 skip=1 > /dev/null 2>&1
mkdir init > /dev/null 2>&1
rm initrd.new > /dev/null 2>&1
rm INITRD > /dev/null 2>&1
cd init
cpio -i < ../initrd.oelec > /dev/null 2>&1
if [ ! -f platform_init ]; then
echo "platform_init not found."
exit 1
fi
echo "" >> platform_init
echo "mkdir /mnt" >> platform_init
echo "mount /dev/mmcblk0p1 /mnt" >> platform_init
echo "cp /mnt/bootsel.ini /mnt/boot.ini" >> platform_init
echo "umount /mnt" >> platform_init
echo "rmdir /mnt" >> platform_init
find | cpio -H newc -o > ../initrd.new
cd ..
mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n "RootFS [Odroid]" -d initrd.new INITRD
rm initrd.new > /dev/null 2>&1
sync
echo "OK."
echo ""
echo "=================================="
echo "OpenEELEC prepared for multi boot."
echo "=================================="
echo ""
# -------------------------------------------------------------------