forked from NodeOS-Legacy/nodeos-rootfs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild
executable file
·356 lines (251 loc) · 7.67 KB
/
build
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env bash
# We prepare the ROOT filesystem with dependencies from NPM
# Since there is no functional NPM on the system yet, we use NPM from the
# downloaded Node.js source code to install NPM packages into the container
set -o pipefail
GRN="\e[32m"
CLR="\e[0m"
__dirname=`dirname "$(readlink -f "$0")"`
while getopts ":B:D:F:I:K:O:U:" opt; do
case $opt in
B)
BAREBONES="$OPTARG" # Location of the barebones image
;;
D)
DTS="$OPTARG" # Device Tree Specs directory
;;
F)
FORMAT="$OPTARG" # Output format
;;
I)
INITRAMFS="$OPTARG" # Location of the initramfs image
;;
K)
KERNEL="$OPTARG" # Location of the barebones kernel
;;
O)
OUTPUT="$OPTARG" # Output location
;;
U)
USERSFS="$OPTARG" # Usersfs location to be mounted from
;;
esac
done
if [[ -z "$OUTPUT" ]]; then
echo "-O is mandatory"
exit 1
fi
if [[ "$USERSFS" ]]; then
USERSFS_UUID=`blkid $USERSFS -o export | grep UUID`
fi
TOOLCHAIN=`node -p "require('nodeos-cross-toolchain')"`
source $TOOLCHAIN/scripts/adjustEnvVars.sh || exit $?
if [[ -d $OBJECTS ]]; then
chmod -R u+w $OBJECTS &&
rm -rf $OBJECTS || exit 2
fi
#
# Define steps paths
#
EFIBOOT_DIR=$OBJECTS/efiboot
EFIBOOT_IMG=$EFIBOOT_DIR.img
DISK_DIR=$OBJECTS/disk
IMG_DIR=$OBJECTS/img
ISO_DIR=$OBJECTS/iso
#
# SysLinux
#
SRC_DIR=$__dirname/../deps/syslinux
#
# Wrap the system up and pack it
#
# Disk images are a composition of bootfs and usersfs
function createDiskImage() {
BOOTFS=$1
STEP_DIR=$DISK_DIR
# ToDo: sizes would need to be on cylinders and retain DOS compatibility
# http://elinux.org/RPi_Advanced_Setup#Advanced_SD_card_setup
SIZE_MBR=$((2*1024)) # Reserve first megabyte for raspi. Is this needed?
SIZE_BOOTFS=$(stat -L -c%s "$BOOTFS")
SIZE_BOOTFS=$(($SIZE_BOOTFS/512))
START_USERSFS=$(($SIZE_MBR+$SIZE_BOOTFS))
dd if=/dev/zero of=$OUTPUT count=$SIZE_MBR &&
dd if=$BOOTFS of=$OUTPUT seek=$SIZE_MBR &&
dd if=$USERSFS of=$OUTPUT seek=$START_USERSFS || exit 10
eval "echo \"$(<$__dirname/../resources/sfdisk.txt)\"" | \
/sbin/sfdisk -uS --force $OUTPUT || exit 11
}
function createPartitionImage() {
BOOTFS=$1
# Copy kernel and initramfs on the partition image
cp $INITRAMFS $STEP_DIR/initram.gz || exit 20
# Create partition image
mkdir -p `dirname $BOOTFS` || exit 21
# Size in KB
# DISK_SIZE=`du -ks $STEP_DIR | cut -f1`
DISK_SIZE=$((32*1024))
genfatfs -b $DISK_SIZE \
--root $STEP_DIR \
$BOOTFS || exit 22
}
function createEfiImage() {
STEP_DIR=$EFIBOOT_DIR
rm -rf $STEP_DIR &&
mkdir -p $STEP_DIR || exit 30
# Copy SysLinux files
eval "echo \"$(<$__dirname/../resources/syslinux.cfg)\"" \
> $STEP_DIR/syslinux.cfg || exit 31
cp $SRC_DIR/efi$BITS/efi/syslinux.efi \
$SRC_DIR/efi$BITS/com32/elflink/ldlinux/ldlinux.e$BITS \
$STEP_DIR || exit 32
# Copy kernel and initramfs on the ISO image
cp $KERNEL $STEP_DIR &&
cp $INITRAMFS $STEP_DIR/initram.gz || exit 33
# Create EFI disk image
# DISK_SIZE=`du -ks $STEP_DIR | cut -f1`
DISK_SIZE=$((32*1024))
genfatfs -b $DISK_SIZE \
--root $STEP_DIR \
$EFIBOOT_IMG || exit 34
}
function createIsoImage() {
STEP_DIR=$ISO_DIR
mkdir -p $STEP_DIR || exit 40
eval "echo \"$(<$__dirname/../resources/syslinux.cfg)\"" \
> $STEP_DIR/syslinux.cfg || exit 41
# Create EFI boot image
createEfiImage || exit $?
cp $EFIBOOT_IMG $STEP_DIR || exit 42
# Copy IsoLinux files
cp $SRC_DIR/bios/core/isolinux.bin \
$SRC_DIR/bios/com32/elflink/ldlinux/ldlinux.c32 \
$STEP_DIR || exit 43
# Copy kernel and initramfs on the ISO image
cp $KERNEL $STEP_DIR &&
cp $INITRAMFS $STEP_DIR/initram.gz || exit 44
# Create ISO image
mkdir -p `dirname $OUTPUT` || exit 45
# http://wiki.osdev.org/Bootable_El-Torito_CD_with_GRUB_Legacy#Mkisofs_says_Uh_oh.2C_I_cant_find_the_boot_image
# genisoimage looks for its boot image as a subdirectory of the filesystem on the CD
genisoimage -o $OUTPUT \
-c boot.cat \
-V NodeOS \
-b isolinux.bin \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-efi-boot efiboot.img \
-no-emul-boot \
$STEP_DIR || exit 46
$SRC_DIR/bios/utils/isohybrid $OUTPUT || exit 47
}
function createTar() {
STEP_DIR=$OUTPUT
mkdir -p `dirname $STEP_DIR` || err 50
gunzip $BAREBONES -c | tar --delete init | gzip > $STEP_DIR &&
cat $INITRAMFS >> $STEP_DIR || err 51
}
function img_pc() {
STEP_DIR=$IMG_DIR
mkdir -p $STEP_DIR || err 60
BOOTFS=$1
# Copy SysLinux files
eval "echo \"$(<$__dirname/../resources/syslinux.cfg)\"" \
> $STEP_DIR/syslinux.cfg || exit 61
cp $KERNEL $STEP_DIR || exit 62
createPartitionImage $BOOTFS || exit $?
$SRC_DIR/bios/mtools/syslinux --install $BOOTFS || exit 63
}
function img_raspi() {
STEP_DIR=$IMG_DIR
mkdir -p $STEP_DIR || err 70
BOOTFS=$1
# Copy Raspberry PI extra files
# http://elinux.org/RPi_Advanced_Setup#Setting_up_the_boot_partition
#
# bootcode.bin, fixup.dat, start.elf, cmdline.txt
(
cd $STEP_DIR || exit 71
wget https://github.com/raspberrypi/firmware/raw/master/boot/bootcode.bin || exit 72
wget https://github.com/raspberrypi/firmware/raw/master/boot/fixup.dat || exit 73
wget https://github.com/raspberrypi/firmware/raw/master/boot/start.elf || exit 74
) || err $?
cp $__dirname/../resources/config.txt $DTS $STEP_DIR || exit 75
case $MACHINE in
raspi)
cp $KERNEL $STEP_DIR/kernel.img || exit 76
;;
raspi2)
cp $KERNEL $STEP_DIR/kernel7.img || exit 77
;;
raspi3)
echo arm_control=0x200 >> $STEP_DIR/config.txt &&
cp $KERNEL $STEP_DIR/kernel7.img || exit 78
;;
*)
echo -e "${RED}Unknown MACHINE '$MACHINE'${CLR}"
exit 79
;;
esac
createPartitionImage $BOOTFS || exit $?
}
case $MACHINE in
pc)
case $FORMAT in
disk)
if [[ -z "$USERSFS" ]]; then
echo "-U is mandatory"
exit 80
fi
BOOTFS=$DISK_DIR/bootfs.img
img_pc $BOOTFS &&
createDiskImage $BOOTFS || exit $?
dd bs=440 count=1 conv=notrunc \
if=$SRC_DIR/bios/mbr/mbr.bin of=$OUTPUT || err 81
;;
img) # FAT partition
img_pc $OUTPUT || exit $?
;;
iso) # Hybrid ISO image for CDs and USB pendrives
createIsoImage || exit $?
;;
tar) # Combine both barebones and initramfs tarfiles in a single one
createTar || exit $?
;;
*)
echo -e "${RED}Unknown FORMAT '$FORMAT'${CLR}"
exit 90
;;
esac
;;
raspi|raspi2|raspi3) # FAT partition for Raspberry PI
if [[ -z "$DTS" ]]; then
echo "-D is mandatory for MACHINE '$MACHINE'"
exit 100
fi
case $FORMAT in
disk)
if [[ -z "$USERSFS" ]]; then
echo "-U is mandatory"
exit 110
fi
BOOTFS=$DISK_DIR/bootfs.img
img_raspi $BOOTFS &&
createDiskImage $BOOTFS || exit $?
;;
img)
img_raspi $OUTPUT || exit $?
;;
*)
echo -e "${RED}Unknown FORMAT '$FORMAT'${CLR}"
exit 120
;;
esac
;;
*)
echo -e "${RED}Unknown MACHINE '$MACHINE'${CLR}"
exit 130
;;
esac
echo -e "${GRN}Successfully built 'bootfs'${CLR}"