forked from eliasbakken/Kamikaze2
-
Notifications
You must be signed in to change notification settings - Fork 18
/
build-image-in-chroot-end-to-end.sh
executable file
·152 lines (126 loc) · 3.96 KB
/
build-image-in-chroot-end-to-end.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
if [ "$#" -lt 1 ]; then
echo "We need to know which platform we're building for."
echo "Should be one of: {replicape, recore, raspihf, raspi64}"
echo "Usage: build-image-in-chroot-end-to-end.sh platform [system-build-script]"
exit
fi
if [ "$#" -eq 2 ]; then
SYSTEM_ANSIBLE=$2
else
SYSTEM_ANSIBLE=SYSTEM_klipper_octoprint-DEFAULT.yml
fi
if [ ! -f ${SYSTEM_ANSIBLE} ]; then
echo "Could not find the system build playbook ${SYSTEM_ANSIBLE}. Cannot continue."
exit
fi
set -x
set -e
TARGET_PLATFORM=$1
for f in $(ls BaseLinux/${TARGET_PLATFORM}/*)
do
source $f
done
if [ -f "customize.sh" ] ; then
source customize.sh
fi
TARGETIMAGE=refactor-${TARGET_PLATFORM}-rootfs.img
MOUNTPOINT=$(mktemp -d /tmp/umikaze-root.XXXXXX)
REFACTOR_HOME="/usr/src/Refactor"
if [ ! -f $BASEIMAGE ]; then
wget -q $BASEIMAGE_URL -O $BASEIMAGE
fi
rm -f $TARGETIMAGE
decompress || $(echo "check your Linux platform file is correct!"; exit) # defined in the BaseLinux/{platform}/Linux file
echo "preparing the partition layout"
if [ ${TARGET_PLATFORM} == "recore" ]; then
truncate -s 6000M $TARGETIMAGE
DEVICE=`losetup -P -f --show $TARGETIMAGE`
PARTITION=${DEVICE}p2
cat <<EOF > image.layout
# partition table of ${DEVICE}
unit: sectors
${DEVICE}p1 : start=8192, size=524288, type=83
${DEVICE}p2 : start=532480, size=11303520, type=83
EOF
fi
if [ ${TARGET_PLATFORM} == 'replicape' ]; then
truncate -s 3600M $TARGETIMAGE
DEVICE=`losetup -P -f --show $TARGETIMAGE`
PARTITION=${DEVICE}p1
cat <<EOF > image.layout
# partition table of ${DEVICE}
unit: sectors
${DEVICE}p1 : start=8192, size=7364608, type=83
EOF
fi
sfdisk --delete ${DEVICE}
# Perform actual modifications to the partition layout
sfdisk ${DEVICE} < image.layout
echo "checking the filesystem..."
e2fsck -f -p ${PARTITION}
clean_status=$?
if [ "$clean_status" -ne "0" ]; then
echo "had to clear out something on the FS..."
fi
echo "resizing"
resize2fs ${PARTITION}
echo "Beginning the mount sequence."
mount ${PARTITION} ${MOUNTPOINT}
if [ ${TARGET_PLATFORM} == "recore" ]; then
mount ${DEVICE}p1 ${MOUNTPOINT}/boot
fi
mount -o bind /dev ${MOUNTPOINT}/dev
mount -o bind /sys ${MOUNTPOINT}/sys
mount -o bind /proc ${MOUNTPOINT}/proc
mount -o bind /dev/pts ${MOUNTPOINT}/dev/pts
rm ${MOUNTPOINT}/etc/resolv.conf
echo "nameserver 8.8.8.8" >> ${MOUNTPOINT}/etc/resolv.conf
# don't git clone here - if someone did a commit since this script started, Unexpected Things will happen
# instead, do a deep copy so the image has a git repo as well
mkdir -p ${MOUNTPOINT}${REFACTOR_HOME}
shopt -s dotglob # include hidden files/directories so we get .git
shopt -s extglob # allow excluding so we can hide the img files
cp -r `pwd`/!(*.img*|*.7z) ${MOUNTPOINT}${REFACTOR_HOME}
shopt -u extglob
shopt -u dotglob
set +e # allow this to fail - we'll check the return code
chroot ${MOUNTPOINT} su -c "\
export DEBIAN_FRONTEND=noninteractive && \
cd ${REFACTOR_HOME} && \
apt update && \
apt install -y ansible python && \
ansible-playbook ${SYSTEM_ANSIBLE} -T 180 --extra-vars '${ANSIBLE_PLATFORM_VARS}' -i hosts"
status=$?
set -e
rm -rf ${MOUNTPOINT}${REFACTOR_HOME}
rm -rf ${MOUNTPOINT}/root/.ansible
rm ${MOUNTPOINT}/etc/resolv.conf
umount -l ${MOUNTPOINT}/dev/pts
umount -l ${MOUNTPOINT}/dev
umount -l ${MOUNTPOINT}/proc
umount -l ${MOUNTPOINT}/sys
if [ ${TARGET_PLATFORM} == "recore" ]; then
umount -l ${MOUNTPOINT}/boot
fi
umount -l ${MOUNTPOINT}
rmdir ${MOUNTPOINT}
if [ $status -eq 0 ]; then
echo "Looks like the image was prepared successfully - packing it up"
if [ ${TARGET_PLATFORM} == 'replicape' ]; then
if [ ! -f ${UBOOT_SPL} ] ; then
wget ${UBOOT_SPL_URL}
fi
if [ ! -f ${UBOOT_BIN} ] ; then
wget ${UBOOT_BIN_URL}
fi
dd if=${UBOOT_SPL} of=${DEVICE} seek=1 bs=128k
dd if=${UBOOT_BIN} of=${DEVICE} seek=1 bs=384k
fi
./generate-image-from-sd.sh $DEVICE $TARGET_PLATFORM
losetup -d $DEVICE
else
echo "image generation seems to have failed - cleaning up, returning $status"
losetup -d $DEVICE
exit ${status}
fi