-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-os.sh
More file actions
executable file
·177 lines (144 loc) · 5.22 KB
/
build-os.sh
File metadata and controls
executable file
·177 lines (144 loc) · 5.22 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
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
#!/bin/bash
DATA_PATH=${1:-"data"}
DESTINATION=${2:-"archive.swu"}
prepare-base-os() {
if [ -f "${DATA_PATH}/base-os.zip" ];
then
echo "Base OS present"
cp "${DATA_PATH}/base-os.zip" "base-os.zip"
else
echo "Downloading Base OS"
wget -nv --header="User-Agent: Mozilla/5.0" https://kb.unipi.technology/_media/files:software:os-images:patron-base-os_12.20240917.1.zip -O base-os.zip
fi
unzip base-os.zip -d base-os && rm base-os.zip
cd base-os
mkdir archive && cd archive
cpio -idm --no-absolute-filenames -I ../archive.swu && rm ../archive.swu
mkdir root && cd root
gzip -d ../root.cpio.gz && cpio -idm --no-absolute-filenames -I ../root.cpio && rm ../root.cpio
cd ../
mkdir boot && cd boot
gzip -d ../boot.cpio.gz && cpio -idm --no-absolute-filenames -I ../boot.cpio && rm ../boot.cpio
cd ../../../
}
prepare-packages() {
ROBOPIPE_API_REPO="Robopipe/API"
ROBOPIPE_API_RES=$(wget -qO- "https://api.github.com/repos/${ROBOPIPE_API_REPO}/releases/latest")
ROBOPIPE_API_RELEASE=$(echo "${ROBOPIPE_API_RES}" | jq -r '.assets[] | select(.name | endswith("tar.gz")) | .browser_download_url')
export ROBOPIPE_API="robopipe-api.tar.gz"
wget -nv "${ROBOPIPE_API_RELEASE}" -O "${DATA_PATH}/${ROBOPIPE_API}"
}
cleanup() {
rm "${DATA_PATH}/${ROBOPIPE_API}"
}
archive() {
ARCHIVE_CONTENTS=("sw-description" "clearfs.sh" "root.cpio.gz" "boot.cpio.gz")
cd base-os/archive/root
find . -print -depth -mindepth 1 | cpio -o -H crc | gzip -c > ../root.cpio.gz
cd ../boot
find . -print -depth -mindepth 1 | cpio -o -H newc | gzip -c > ../boot.cpio.gz
cd ../
echo "${ARCHIVE_CONTENTS[@]}" | tr " " "\n" | cpio -o -H crc > "../../archive.swu"
cd ../../
mv archive.swu "${DESTINATION}"
rm -r base-os
}
configure_os() {
_USERNAME="admin"
mv /etc/resolv.conf /etc/resolv.conf.bak
echo "nameserver 8.8.8.8" > /etc/resolv.conf
mkdir -p /opt/robopipe/tools
mkdir -p /etc/robopipe
apt update && apt install -y pipx git nginx owserver avahi-utils
apt install -y --no-install-recommends evok-unipi-data
# User configuration
cp -r /home/unipi/ "/home/${_USERNAME}/"
useradd -d "/home/${_USERNAME}" -s /bin/bash ${_USERNAME}
echo "${_USERNAME}:robopipe.io" | chpasswd
find "/home/${_USERNAME}/" -user root -exec chown ${_USERNAME}:${_USERNAME} {} \;
usermod -aG sudo,unipi,dialout,i2c,spi,gpio ${_USERNAME}
# Hostname service
systemctl disable unipihostname
cp /mnt/robopipehostname.service /etc/systemd/system/
cp /mnt/set-robopipe-hostname.sh /opt/robopipe/tools/
systemctl enable robopipehostname
# Pipx configuration
pipx ensurepath
pipx install pipx
apt purge --autoremove -y pipx
~/.local/bin/pipx install pipx --global
. ~/.profile
pipx uninstall pipx
pipx ensurepath --global
# Robopipe API configuration
pipx install --global "/mnt/${ROBOPIPE_API}"
ln -s /etc/evok/hw_definitions /etc/robopipe/hw_definitions
ln -s /etc/evok/autogen.yaml /etc/robopipe/autogen.yaml
cp /mnt/controller-config.yaml /etc/robopipe/config.yaml
cp /mnt/robopipe-api.env /etc/robopipe/.env
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="03e7", MODE="0666"' > /etc/udev/rules.d/80-movidius.rules
cp /mnt/robopipeapi.service /etc/systemd/system/
systemctl enable robopipeapi
# Nginx configuration
openssl req -x509 -newkey rsa:4096 -keyout /etc/ssl/certs/server.key -out /etc/ssl/certs/server.cert \
-sha256 -days 3650 -nodes \
-subj "/CN=robopipe.local"
cp /mnt/robopipe-api.conf /etc/nginx/sites-available/robopipe-api
rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/robopipe-api /etc/nginx/sites-enabled/robopipe-api
mv /etc/resolv.conf.bak /etc/resolv.conf
}
configure_boot() {
INITRD="initrd.img-6.1.55"
cd boot/altboot
mv "${INITRD}" "${INITRD}.gz"
gzip -d "${INITRD}.gz"
mkdir initrd && cd initrd
cpio -imd -I "../${INITRD}" && rm "../${INITRD}"
cd opt/swu
tar -xzf webapp.tar.gz && rm webapp.tar.gz
cp "${DATA_PATH}/service-index.html" index.html
cp "${DATA_PATH}/robopipe-logo.svg" images/logo.svg
cp "${DATA_PATH}/favicon.png" images/favicon.png
tar --remove-files -czf webapp.tar.gz $(ls | grep -Ev "swupdate|ttyd")
cd ../../
find . | cpio -o -H newc | gzip -9 -n > "../${INITRD}"
cd ../
rm -rf "initrd"
chmod 755 "${INITRD}"
cd ../../
}
if [ ! -f "/etc/debian_version" ];
then
echo "This script needs to be run on Debian OS"
exit 1
fi
for dep in sudo wget unzip cpio jq;
do
if ! command -v "${dep}" >/dev/null 2>&1;
then
if [ "${INSTALL_DEPS}" = "true" ];
then
apt update && apt install -y "${dep}"
else
echo "${dep} dependency is missing"
exit 1
fi
fi
done
prepare-base-os
prepare-packages
cd base-os/archive
mount -t proc /proc root/proc/
mount -t sysfs /sys root/sys/
mount -o bind /dev root/dev/
mount -o bind "${DATA_PATH}" root/mnt/
sudo chroot root /bin/sh -c "ROBOPIPE_API=${ROBOPIPE_API}; $(declare -f configure_os); configure_os"
umount root/proc/
umount root/sys/
umount root/dev/
umount root/mnt/
configure_boot
cd ../../
archive
cleanup