forked from OpenELEC/OpenELEC.tv
/
create_sdcard
executable file
·266 lines (232 loc) · 9.6 KB
/
create_sdcard
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
#!/bin/sh
################################################################################
# This file is part of OpenELEC - http://www.openelec.tv
# Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenELEC.tv; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
# http://www.gnu.org/copyleft/gpl.html
################################################################################
# usage: sudo ./create_sdcard <drive>
# example: sudo ./create_sdcard /dev/sdb
# loop example: sudo ./create_sdcard /dev/loop0 ~/vSD.img
# create an empty image file for use with loop device like this:
# dd if=/dev/zero of=~/vSD.img bs=1M count=910
if [ "$(id -u)" != "0" ]; then
clear
echo "#########################################################"
echo "# please execute with 'sudo' or -DANGEROUS!!!- as root #"
echo "# example: sudo ./create_sdcard <drive> #"
echo "#########################################################"
exit 1
fi
if [ -z "$1" ]; then
clear
echo "#########################################################"
echo "# please execute with your drive as option #"
echo "# example: sudo ./create_sdcard /dev/sdb #"
echo "# or: sudo ./create_sdcard /dev/mmcblk0 #"
echo "# or: sudo ./create_sdcard /dev/loop0 ~/vSD.img #"
echo "# to create an image file for /dev/loop0 option: #"
echo "# sudo dd if=/dev/zero of=~/vSD.img bs=1M count=910 #"
echo "#########################################################"
exit 1
fi
DISK="$1"
if [ "$DISK" = "/dev/mmcblk0" ]; then
PART1="${DISK}p1"
PART2="${DISK}p2"
elif [ "$DISK" = "/dev/loop0" ]; then
PART1="${DISK}p1"
PART2="${DISK}p2"
IMGFILE="$2"
losetup $DISK $IMGFILE
else
PART1="${DISK}1"
PART2="${DISK}2"
fi
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv USB Installer #"
echo "# #"
echo "#########################################################"
echo "# #"
echo "# This will wipe any data off your chosen drive #"
echo "# Please read the instructions and use very carefully.. #"
echo "# #"
echo "#########################################################"
# check for some required tools
# this is needed to partion the drive
which parted > /dev/null
if [ "$?" = "1" ]; then
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv missing tool - Installation will quit #"
echo "# #"
echo "# We can't find the required tool \"parted\" #"
echo "# on your system. #"
echo "# Please install it via your package manager. #"
echo "# #"
echo "#########################################################"
exit 1
fi
# this is needed to format the drive
which mkfs.vfat > /dev/null
if [ "$?" = "1" ]; then
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv missing tool - Installation will quit #"
echo "# #"
echo "# We can't find the required tool \"mkfs.vfat\" #"
echo "# on your system. #"
echo "# Please install it via your package manager. #"
echo "# #"
echo "#########################################################"
exit 1
fi
# this is needed to format the drive
which mkfs.ext4 > /dev/null
if [ "$?" = "1" ]; then
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv missing tool - Installation will quit #"
echo "# #"
echo "# We can't find the required tool \"mkfs.ext4\" #"
echo "# on your system. #"
echo "# Please install it via your package manager. #"
echo "# #"
echo "#########################################################"
exit 1
fi
# this is needed to tell the kernel for partition changes
which partprobe > /dev/null
if [ "$?" = "1" ]; then
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv missing tool - Installation will quit #"
echo "# #"
echo "# We can't find the required tool \"partprobe\" #"
echo "# on your system. #"
echo "# Please install it via your package manager. #"
echo "# #"
echo "#########################################################"
exit 1
fi
# this is needed to tell the kernel for partition changes
which md5sum > /dev/null
if [ "$?" = "1" ]; then
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv missing tool - Installation will quit #"
echo "# #"
echo "# We can't find the required tool \"md5sum\" #"
echo "# on your system. #"
echo "# Please install it via your package manager. #"
echo "# #"
echo "#########################################################"
exit 1
fi
# check MD5 sums
echo "checking MD5 sum..."
md5sumFailed()
{
clear
echo "#########################################################"
echo "# #"
echo "# OpenELEC.tv failed md5 check - Installation will quit #"
echo "# #"
echo "# Your original download was probably corrupt. #"
echo "# Please visit www.openelec.tv and get another copy #"
echo "# #"
echo "#########################################################"
exit 1
}
md5sum -c target/KERNEL.md5
if [ "$?" = "1" ]; then
md5sumFailed
fi
md5sum -c target/SYSTEM.md5
if [ "$?" = "1" ]; then
md5sumFailed
fi
# (TODO) umount everything (if more than one partition)
umount ${DISK}*
# remove all partitions from the drive
echo "writing new disklabel on $DISK (removing all partitions)..."
parted -s "$DISK" mklabel msdos
# create a single partition
echo "creating partitions on $DISK..."
parted -s "$DISK" unit cyl mkpart primary fat32 -- 0 16
parted -s "$DISK" unit cyl mkpart primary ext2 -- 16 -2
# make partition active (bootable)
echo "marking partition active..."
parted -s "$DISK" set 1 boot on
# tell kernel we have a new partition table
echo "telling kernel we have a new partition table..."
partprobe "$DISK"
# create filesystem
echo "creating filesystem on $PART1..."
mkfs.vfat "$PART1" -I -n System
echo "creating filesystem on $PART2..."
mkfs.ext4 "$PART2" -L Storage
# remount loopback device
if [ "$DISK" = "/dev/loop0" ]; then
sync
losetup -d $DISK
losetup $DISK $IMGFILE -o 1048576 --sizelimit 131071488
PART1=$DISK
fi
# mount partition
echo "mounting partition $PART1 ..."
if [ -d /dev/shm ]; then
rm -rf /dev/shm/openelec_install
mkdir -p /dev/shm/openelec_install
mount -t vfat "$PART1" /dev/shm/openelec_install
MOUNTPOINT=/dev/shm/openelec_install
else
rm -rf /tmp/openelec_install
mkdir -p /tmp/openelec_install
mount -t vfat "$PART1" /tmp/openelec_install
MOUNTPOINT=/tmp/openelec_install
fi
# create bootloader configuration
echo "creating bootloader configuration..."
echo "boot=/dev/mmcblk0p1 disk=/dev/mmcblk0p2 ssh quiet" > $MOUNTPOINT/cmdline.txt
# copy files
echo "copying files to $MOUNTPOINT..."
cp target/KERNEL $MOUNTPOINT/kernel.img
cp target/SYSTEM $MOUNTPOINT
cp 3rdparty/bootloader/* $MOUNTPOINT
cp openelec.ico $MOUNTPOINT
cp README.md $MOUNTPOINT
# sync disk
echo "syncing disk..."
sync
# unmount partition
echo "unmounting partition $MOUNTPOINT ..."
umount $MOUNTPOINT
# cleaning
echo "cleaning tempdir..."
rmdir $MOUNTPOINT
# unmount loopback device
if [ "$DISK" = "/dev/loop0" ]; then
losetup -d $DISK
fi
echo "...installation finished"