-
Notifications
You must be signed in to change notification settings - Fork 3
/
preinstall.sh
executable file
·266 lines (231 loc) · 7.67 KB
/
preinstall.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
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
#!/bin/sh
ARCH=""
ROOTFS=""
BOOTFS=""
IS_PI=0
MOUNTS=""
TEMPDIR=""
TARBALL=""
TARBALL_ONLY=0
SERVICE_FILE=$(cat <<EOF
[Unit]
Description=Viam Services Agent
After=NetworkManager.service
StartLimitIntervalSec=0
[Service]
Type=exec
Restart=always
RestartSec=5
User=root
TimeoutSec=240
ExecStart=/opt/viam/bin/viam-agent --config /etc/viam.json
FinalKillSignal=SIGQUIT
[Install]
WantedBy=multi-user.target
EOF
)
find_mountpoints_linux() {
if [ "$MOUNTS" = "" ]; then
MOUNTS=$(findmnt -o TARGET -l | grep -v TARGET | grep -vE '^/$')
fi
}
find_mountpoints_macos() {
if [ "$MOUNTS" = "" ]; then
volsplist=$(mktemp)
diskutil list -plist > "$volsplist"
vols=$(/usr/libexec/PlistBuddy -c "Print :VolumesFromDisks" "$volsplist" | grep -vE '[{}]' | awk '{$1=$1};1')
while read -r vol; do
volplist=$(mktemp)
diskutil info -plist "$vol" > "$volplist"
newMount=$(/usr/libexec/PlistBuddy -c "Print :MountPoint" "$volplist")
if [ "$newMount" != "" ]; then
MOUNTS=$(echo "$newMount\n$MOUNTS")
fi
rm "$volplist"
done <<-EOF
$vols
EOF
rm "$volsplist"
fi
return 0
}
check_fs() {
echo && echo
while read -r mount; do
if stat "$mount/etc/systemd/system" >/dev/null 2>&1; then
ROOTFS="$mount"
if stat "$mount/lib64/ld-linux-x86-64.so.2" >/dev/null 2>&1 || stat "$mount/lib/ld-linux-x86-64.so.2" >/dev/null 2>&1; then
ARCH=x86_64
fi
if stat "$mount/lib/ld-linux-aarch64.so.1" >/dev/null 2>&1; then
ARCH=aarch64
fi
echo "Found target filesystem mounted at $ROOTFS with $ARCH"
fi
if stat "$mount/bootcode.bin" >/dev/null 2>&1; then
if ! stat "$mount/firstrun.sh" >/dev/null 2>&1; then
echo "Found possible Raspberry Pi bootfs mounted at $mount, but it is missing firstrun.sh"
echo "Please re-image using the offical Raspberry Pi Imager and choose 'yes' when asked to apply OS customisation settings."
echo "At minimum, you should set a hostname to uniquely identify the device."
echo "Then re-run this script BEFORE booting the SD card."
echo
continue
fi
BOOTFS="$mount"
IS_PI=1
ARCH=aarch64
echo "Found Raspberry Pi bootfs mounted at $BOOTFS"
fi
done <<-EOF
$MOUNTS
EOF
if [ "$ARCH" != "" ] && ([ "$ROOTFS" != "" ] || [ "$BOOTFS" != "" ]); then
return 0
fi
return 1
}
create_tarball() {
echo "Creating tarball for install."
URL="https://storage.googleapis.com/packages.viam.com/apps/viam-agent/viam-agent-stable-$ARCH"
if [ -n "$VIAM_AGENT_PATH" ]; then
VIAM_AGENT_PATH=$(eval echo "$VIAM_AGENT_PATH")
if ! [ -f "$VIAM_AGENT_PATH" ]; then
echo "Custom binary path provided, but file ($VIAM_AGENT_PATH) was not found."
return 1
fi
echo "Using custom binary: $VIAM_AGENT_PATH"
fi
if [ -n "$PROVISIONING_PATH" ]; then
PROVISIONING_PATH=$(eval echo "$PROVISIONING_PATH")
if ! [ -f "$PROVISIONING_PATH" ]; then
echo "Provisioning file path provided, but file ($PROVISIONING_PATH) was not found."
return 1
fi
echo "Installing $PROVISIONING_PATH as /etc/viam-provisioning.json"
fi
if [ -n "$VIAM_JSON_PATH" ]; then
VIAM_JSON_PATH=$(eval echo "$VIAM_JSON_PATH")
if ! [ -f "$VIAM_JSON_PATH" ]; then
echo "viam.json file path provided, but file ($VIAM_JSON_PATH) was not found."
return 1
fi
echo "Installing $VIAM_JSON_PATH as /etc/viam.json"
fi
TEMPDIR=$(mktemp -d)
mkdir -p "$TEMPDIR/usr/local/lib/systemd/system/multi-user.target.wants/"
echo "$SERVICE_FILE" > "$TEMPDIR/usr/local/lib/systemd/system/viam-agent.service"
ln -s ../viam-agent.service "$TEMPDIR/usr/local/lib/systemd/system/multi-user.target.wants/viam-agent.service"
mkdir -p "$TEMPDIR/opt/viam/cache"
if [ -f "$VIAM_AGENT_PATH" ]; then
cp "$VIAM_AGENT_PATH" "$TEMPDIR/opt/viam/cache/viam-agent-factory-$ARCH" || return 1
else
curl -fsSL "$URL" -o "$TEMPDIR/opt/viam/cache/viam-agent-factory-$ARCH" || return 1
fi
chmod 755 "$TEMPDIR/opt/viam/cache/viam-agent-factory-$ARCH"
mkdir -p "$TEMPDIR/opt/viam/bin"
ln -s "/opt/viam/cache/viam-agent-factory-$ARCH" "$TEMPDIR/opt/viam/bin/viam-agent"
mkdir -p "$TEMPDIR/etc"
if [ -f "$PROVISIONING_PATH" ]; then
cp "$PROVISIONING_PATH" "$TEMPDIR/etc/viam-provisioning.json"
fi
if [ -f "$VIAM_JSON_PATH" ]; then
cp "$VIAM_JSON_PATH" "$TEMPDIR/etc/viam.json"
fi
TARBALL="$TEMPDIR/viam-preinstall-$ARCH.tar.xz"
tar -cJvpf "$TARBALL" -C "$TEMPDIR" opt/ etc/ usr/ || return 1
}
if [ "$(id -u)" -ne 0 ]; then
echo
echo "This install script must be run as root. Try running via sudo."
exit 1
fi
if [ -n "$1" ]; then
if [ "$1" = "--aarch64" ]; then
ARCH=aarch64
TARBALL_ONLY=1
elif [ "$1" = "--x86_64" ]; then
ARCH=x86_64
TARBALL_ONLY=1
elif [ -d "$1" ]; then
MOUNTS="$1"
fi
else
if [ "$(uname)" = "Linux" ]; then
find_mountpoints_linux
elif [ "$(uname)" = "Darwin" ]; then
find_mountpoints_macos
else
echo "This script only supports auto-detection on Linux and MacOS."
echo "Please specify the image root/mountpoint."
echo "Or see the project README for manual install instructions."
exit 1
fi
fi
if [ "$TARBALL_ONLY" -ne 1 ] && ! check_fs ; then
echo "Error: no valid image found at mountpoints (or manually provided path)"
echo "If installing on a Pi via sd card, please make sure it's freshly imaged (never booted) and customized with a unique hostname."
echo "If the imager auto-ejected the disk, you may need to remove and reinsert it to make it visible again."
echo "Alternately, re-run this script with either '--x86_64' or '--aarch64' options to create a portable package to extract manually,"\
"or explicitly specify the root path (/) if you want to install to the live/running system."
exit 1
fi
if [ "$TARBALL_ONLY" -ne 1 ]; then
echo && echo
if [ "$IS_PI" -eq 1 ]; then
echo "A Raspberry Pi boot partition has been found mounted at $BOOTFS"
echo "This script will modify firstrun.sh on that partition to install Viam agent."
else
echo "A systemd install was found installed in $ROOTFS"
echo "Viam agent will be directly installed there."
fi
read -p "Continue pre-install? (y/n): " CONTINUE
if [ "$CONTINUE" != "y" ]; then
echo "Pre-install aborted."
exit 1
fi
if [ -z "$VIAM_AGENT_PATH" ]; then
read -p "Path to custom viam-agent binary (leave empty to download default): " VIAM_AGENT_PATH
fi
if [ -z "$PROVISIONING_PATH" ]; then
read -p "Path to custom viam-provisioning.json (leave empty to skip): " PROVISIONING_PATH
fi
if [ -z "$VIAM_JSON_PATH" ]; then
read -p "Path to custom viam.json (leave empty to skip): " VIAM_JSON_PATH
fi
fi
if ! create_tarball; then
echo "Error creating preinstall package."
exit 1
fi
if [ "$TARBALL_ONLY" -eq 1 ]; then
echo && echo
echo "Tarball package available at:"
echo "$TARBALL"
echo "Extract it manually with 'sudo tar -xJvpf $TARBALL -C <PATH_TO_ROOT_FS>'"
exit 0
fi
if [ "$IS_PI" -eq "1" ]; then
cp "$TARBALL" "$BOOTFS/viam-preinstall.tar.xz"
if grep -q viam-preinstall.tar.xz "$BOOTFS/firstrun.sh"; then
echo "It appears firstrun.sh has already been modified."
echo "If you ran this script more than once WITHOUT booting the target SD card, then it should not cause issues."
echo "If you did boot, you should make a fresh image before running this installer."
else
sed 's/rm -f \/boot\/firstrun.sh/tar -xJpf \/boot\/firmware\/viam-preinstall.tar.xz -C \/\nrm -f \/boot\/firstrun.sh/' "$BOOTFS/firstrun.sh" > "$BOOTFS/firstrun.sh.new"
mv "$BOOTFS/firstrun.sh.new" "$BOOTFS/firstrun.sh"
fi
elif [ "$ROOTFS" != "" ]; then
tar -xJpf "$TARBALL" -C "$ROOTFS"
else
echo "Refusing to install to unknown/unset ROOTFS ($ROOTFS)"
fi
if [ "$TEMPDIR" != "" ]; then
rm -rf "$TEMPDIR"
fi
sync
echo && echo
if [ "$ROOTFS" = "/" ]; then
echo "Install complete! Reboot, or manually start the service with 'systemctl start viam-agent'"
else
echo "Install complete! You can eject/unmount and boot the image now."
fi