-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_sd_card.sh
executable file
·128 lines (97 loc) · 2.31 KB
/
build_sd_card.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
#!/bin/bash
if [ ${EUID} -ne 0 ]; then
echo "this tool must be run as root"
exit 1
fi
device=$1
if ! [ -b ${device} ]; then
echo "${device} is not a block device"
exit 1
fi
if [ "${deb_local_mirror}" == "" ]; then
deb_local_mirror=${deb_mirror}
fi
bootsize="64M"
relative_path=`dirname $0`
# locate path of this script
absolute_path=`cd ${relative_path}; pwd`
# cd ${absolute_path}
rootfs="./sdos"
bootfs="./sdboot"
today=`date +%Y%m%d`
image=""
if [ "${device}" == "" ]; then
echo "no block device given, just creating an image"
mkdir -p ${buildenv}
image="./img/openfpgaduino_${today}.img"
dd if=/dev/zero of=${image} bs=1MB count=3800
device=`losetup -f --show ${image}`
echo "image ${image} created and mounted as ${device}"
else
dd if=/dev/zero of=${device} bs=512 count=1
fi
fdisk ${device} << EOF
n
p
1
+${bootsize}
t
c
n
p
2
w
EOF
if [ "${image}" != "" ]; then
losetup -d ${device}
device=`kpartx -va ${image} | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
device="/dev/mapper/${device}"
bootp=${device}p1
rootp=${device}p2
else
if ! [ -b ${device}1 ]; then
bootp=${device}p1
rootp=${device}p2
if ! [ -b ${bootp} ]; then
echo "uh, oh, something went wrong, can't find bootpartition neither as ${device}1 nor as ${device}p1, exiting."
exit 1
fi
else
bootp=${device}1
rootp=${device}2
fi
fi
mkfs.vfat ${bootp}
mkfs.ext4 ${rootp}
mkdir -p ${rootfs}
mount ${rootp} ${rootfs}
mkdir -p ${rootfs}/proc
mkdir -p ${rootfs}/sys
mkdir -p ${rootfs}/dev
mkdir -p ${rootfs}/dev/pts
mkdir -p ${rootfs}/usr/src/delivery
mount -t proc none ${rootfs}/proc
mount -t sysfs none ${rootfs}/sys
mount -o bind /dev ${rootfs}/dev
mount -o bind /dev/pts ${rootfs}/dev/pts
sync
sleep 15
# Make sure we're out of the root fs. We won't be able to unmount otherwise, and umount -l will fail silently.
cp -rpf boot/* ${bootfs}/
cp -rpf os/* ${rootfs}/
umount -l ${bootp}
umount -l ${rootfs}/usr/src/delivery
umount -l ${rootfs}/dev/pts
umount -l ${rootfs}/dev
umount -l ${rootfs}/sys
umount -l ${rootfs}/proc
umount -l ${rootfs}
umount -l ${rootp}
# Remove device mapper bindings. Avoids running out of loop devices if run repeatedly.
dmsetup remove_all
echo "finishing ${image}"
if [ "${image}" != "" ]; then
kpartx -d ${image}
echo "created image ${image}"
fi
echo "done."