forked from mindboards/ev3dev-rootfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdcard-img-create
More file actions
executable file
·150 lines (114 loc) · 5.7 KB
/
sdcard-img-create
File metadata and controls
executable file
·150 lines (114 loc) · 5.7 KB
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
#!/bin/sh
# ------------------------------------------------------------------------------
# sdcard-img-create - Create the SD Card image using a file instead of the
# physical card - faster and easier to distribute!
#
# ------------------------------------------------------------------------------
die() { echo -n >&2 "\n$0: $@\n"; exit 1; }
# ------------------------------------------------------------------------------
# Make sure we're running as root, and that we have found the right sdcard!
[ "$(whoami)" != "root" ] && die "Sorry, you are not root!"
# ------------------------------------------------------------------------------
# Make sure we want to overwrite the file if it already exists!
echo "-------------------------------------------------------------------------------"
echo "WARNING - If you type \"Yes\" to the prompt, this script"
echo " will OVERWRITE the ev3dev.img file!"
echo "-------------------------------------------------------------------------------"
echo -n " Type \"Yes\" to continue ... "
read YesNo
[ ! "${YesNo}" = "Yes" ] && die " .. aborting, you typed \"${YesNo}\""
# ------------------------------------------------------------------------------
# If we find a pre-existing file, then ask if we want to recreate a blank one
if [ -e ev3dev.img ]; then
echo "-------------------------------------------------------------------------------"
echo -n " Found an existing ev3dev.img, type \"Yes\" to create a new blank image ... "
read YesNo
fi
if [ ! -e ev3dev.img ] || [ -e ev3dev.img ] && [ "${YesNo}" = "Yes" ]; then
echo "-------------------------------------------------------------------------------"
echo -n " Creating a blank ev3dev.img file - should only take a few seconds ... "
if ! type "fallocate" > /dev/null; then
dd bs=512 if=/dev/zero of=ev3dev.img count=1M > /dev/null 2>&1
else
fallocate -l $((512*1024*1024)) ev3dev.img
fi
echo "done."
fi
# ------------------------------------------------------------------------------
# Repartition ev3dev.img
echo "-------------------------------------------------------------------------------"
echo -n " Type \"Yes\" to repartition ev3dev.img ... "
read YesNo
if [ "${YesNo}" = "Yes" ]; then
sector_size=$((512))
sectors=$((1024 * 1024))
part1_start=$((2048))
part1_end=$((((48 * 1024 * 1024) / ${sector_size}) - 1 ))
part2_start=$((((48 * 1024 * 1024) / ${sector_size}) ))
part2_end=$((${sectors}-1 ))
echo "-------------------------------------------------------------------------------"
echo
echo "ev3dev.img partition characteristics"
echo
echo "Sector size -> ${sector_size}"
echo "Sectors -> ${sectors}"
echo
echo "Partition 1 start -> ${part1_start}"
echo "Partition 1 end -> ${part1_end}"
echo "Partition 2 start -> ${part2_start}"
echo "Partition 2 end -> ${part2_end}"
echo
echo "-------------------------------------------------------------------------------"
echo -n " Creating new disk label for ev3dev.img ... "
parted -s --align minimal ev3dev.img mklabel msdos \
mkpart primary fat32 ${part1_start}s ${part1_end}s \
mkpart primary ext3 ${part2_start}s ${part2_end}s
echo "done."
fi
echo "-------------------------------------------------------------------------------"
echo
# ------------------------------------------------------------------------------
# Display the ev3dev.img partitions
parted ev3dev.img print
# ------------------------------------------------------------------------------
# Now build up the file systems - first we mount the image using kpartx. The
# option for -p is there so we can identify the device later on like this:
#
# /dev/mapper/loop?-ev3dev-1 This will be the fat32 partition
# /dev/mapper/loop?-ev3dev-2 This will be the ext3 partition
#
echo "-------------------------------------------------------------------------------"
echo " Mapping partitions from ev3dev.img ... "
echo
kpartx -vlsa -p"-ev3dev-" ./ev3dev.img
echo "-------------------------------------------------------------------------------"
echo -n " Type \"Yes\" to create a fresh fat32 file system on Partition 1 ... "
read YesNo
if [ "${YesNo}" = "Yes" ]; then
echo -n " Checking for ev3dev-fat32 mountpoint, will create if needed ... "
[ -e ev3dev-fat32 ] && [ ! -d ev3dev-fat32 ] && die "ev3dev-fat32 exists, but it's a file!"
[ ! -e ev3dev-fat32 ] && mkdir ev3dev-fat32
echo "done."
echo -n " Creating fat32 filesystem in ev3dev.img ... "
eval mkfs.msdos -n EV3DEV_FAT32 "/dev/mapper/loop?-ev3dev-1" > /dev/null 2>&1
echo "done"
fi
echo "-------------------------------------------------------------------------------"
echo -n " Type \"Yes\" to create a fresh ext3 file system on Partition 2 ... "
read YesNo
if [ "${YesNo}" = "Yes" ]; then
echo -n " Checking for ev3dev-ext3 mountpoint, will create if needed ... "
[ -e ev3dev-ext3 ] && [ ! -d ev3dev-ext3 ] && die "ev3dev-ext3 exists, but it's a file!"
[ ! -e ev3dev-ext3 ] && mkdir ev3dev-ext3
echo "done."
echo -n " Creating ext3 filesystem in ev3dev.img ... "
eval mkfs.ext3 -L EV3DEV_EXT3 "/dev/mapper/loop?-ev3dev-2" > /dev/null 2>&1
echo "done"
fi
sync
echo "-------------------------------------------------------------------------------"
echo " Unmapping partitions from ev3dev.img ... "
echo
kpartx -vlsd -p"-ev3dev-" ./ev3dev.img
echo "-------------------------------------------------------------------------------"
exit