This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.sh
executable file
·69 lines (55 loc) · 2.08 KB
/
build.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
#!/bin/bash
set -ex
# this script should be run only inside of a Docker container
if [ ! -f /.dockerenv ]; then
echo "ERROR: script works only in a Docker container!"
exit 1
fi
### setting up some important variables to control the build process
BUILD_RESULT_PATH="/workspace"
IMAGE_PATH="rpi-raw.img"
SD_CARD_SIZE=1400
BOOT_PARTITION_SIZE=100
# create empty BOOT/ROOTFS image file
# - SD_CARD_SIZE in MByte
# - DD uses 256 Bytes
# - sector block size is 512Bytes
# - MBR size is 512 Bytes, so we start at sector 2048 (1MByte reserved space)
BOOTFS_START=2048
BOOTFS_SIZE=$((BOOT_PARTITION_SIZE * 2048))
ROOTFS_START=$((BOOTFS_SIZE + BOOTFS_START))
SD_MINUS_DD=$((SD_CARD_SIZE * 1024 * 1024 - 256))
ROOTFS_SIZE=$((SD_MINUS_DD / 512 - ROOTFS_START))
dd if=/dev/zero of=${IMAGE_PATH} bs=1MiB count=${SD_CARD_SIZE}
DEVICE=$(losetup -f --show ${IMAGE_PATH})
echo "Image ${IMAGE_PATH} created and mounted as ${DEVICE}."
# create partions
sfdisk --force "${DEVICE}" <<PARTITION
unit: sectors
/dev/loop0p1 : start= ${BOOTFS_START}, size= ${BOOTFS_SIZE}, Id= c, bootable
/dev/loop0p2 : start= ${ROOTFS_START}, size= ${ROOTFS_SIZE}, Id=83
/dev/loop0p3 : start= 0, size= 0, Id= 0
/dev/loop0p4 : start= 0, size= 0, Id= 0
PARTITION
losetup -d "${DEVICE}"
DEVICE=$(kpartx -va ${IMAGE_PATH} | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1)
dmsetup --noudevsync mknodes
BOOTP="/dev/mapper/${DEVICE}p1"
ROOTP="/dev/mapper/${DEVICE}p2"
DEVICE="/dev/${DEVICE}"
# give some time to system to refresh
sleep 3
# create file systems
mkfs.vfat "${BOOTP}" -n HypriotOS
mkfs.ext4 "${ROOTP}" -L root -i 4096 # create 1 inode per 4kByte block (maximum ratio is 1 per 1kByte)
echo "### remove dev mapper devices for image partitions"
kpartx -vds ${IMAGE_PATH} || true
# ensure that the travis-ci user can access the sd-card image file
umask 0000
# compress image
zip "${BUILD_RESULT_PATH}/${IMAGE_PATH}.zip" "${IMAGE_PATH}"
sleep 2
cd ${BUILD_RESULT_PATH} && sha256sum "${IMAGE_PATH}.zip" > "${IMAGE_PATH}.zip.sha256" && cd -
fdisk -l /rpi-raw.img
# test raw image that we have built
rspec --format documentation --color /builder/rpi/test