-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathubuntu20-preconfig.sh
518 lines (383 loc) · 10 KB
/
ubuntu20-preconfig.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env bash
# Brett Kelly <[email protected]>
# Josh Boudreau <[email protected]>
# Dawson Della Valle <[email protected]>
# Ubuntu 20.04 LTS System Configuration Tweaks
OS=$(cat /etc/os-release | grep -w VERSION_ID)
if [ "$OS" != 'VERSION_ID="20.04"' ] ; then
echo "OS is not Ubuntu20"
exit
fi
interpreter=$(ps -p $$ | awk '$1 != "PID" {print $(NF)}' | tr -d '()')
if [ "$interpreter" != "bash" ]; then
echo "Please run with bash. (\`./ubuntu-preconfig.sh\` or \`bash ubuntu-preconfig.sh\`)"
echo "Current interpreter: $interpreter"
exit 1
fi
euid=$(id -u)
if [[ "$euid" != 0 ]]; then
echo "Please run as root or with sudo."
exit 1
fi
welcome() {
local response
cat <<EOF
Welcome to the
/## /## /####### /####### /##
| ## | ##| ##____/ | ##__ ## |__/
| ## | ##| ## | ## \ ## /###### /## /## /## /###### /#######
| ########| ####### | ## | ## /##__ ##| ##| ## /##//##__ ## /##_____/
|_____ ##|_____ ##| ## | ##| ## \__/| ## \ ##/##/| ########| ######
| ## /## \ ##| ## | ##| ## | ## \ ###/ | ##_____/ \____ ##
| ##| ######/| #######/| ## | ## \ #/ | ####### /#######/
|__/ \______/ |_______/ |__/ |__/ \_/ \_______/|_______/
Ubuntu Preconfiguration Script.
This will set up the root login password, enable root login over SSH,
add the 45Drives apt repository, replace systemd-networkd with network-manager,
remove cloud-init and snapd, and add the Houston Management UI.
This script should *not* be run in an SSH session, as the network will be
modified and you may be disconnected. Run this script from the console or IPMI
remote console.
EOF
read -p "Are you sure you want to continue? [y/N]: " response
case $response in
[yY]|[yY][eE][sS])
echo
;;
*)
echo "Exiting..."
exit 0
;;
esac
return 0
}
enable_root_user() {
local ROOTPASSWD=""
local ROOTPASSWD2=""
local res
echo "ENABLING ROOT LOGIN"
while true; do
read -sp "Enter password for root user: " ROOTPASSWD
echo
if [[ "$ROOTPASSWD" == "" ]]; then
echo "Root password cannot be empty! Try again."
continue
fi
read -sp "Confirm password for root user: " ROOTPASSWD2
echo
if [[ "$ROOTPASSWD" != "$ROOTPASSWD2" ]]; then
echo "Passwords do not match! Try again."
continue
fi
echo "root:$ROOTPASSWD" | chpasswd
res=$?
if [[ $res != 0 ]]; then
echo "Setting password failed! Exit code: $res"
continue
fi
echo "Successfully set root password."
break
done
return 0
}
enable_root_ssh() {
local res
echo "ENABLING ROOT LOGIN VIA SSH"
cat > /etc/ssh/sshd_config.d/45drives.conf <<EOF
PermitRootLogin yes
PasswordAuthentication yes
EOF
# Restart sshd
systemctl restart sshd
res=$?
if [[ $res != 0 ]]; then
echo "Restarting sshd failed!"
exit $res
fi
# test root login
echo "Adding localhost's ECDSA Key Fingerprint to $HOME/.ssh/known_hosts"
ssh-keyscan -H localhost >> $HOME/.ssh/known_hosts
echo "Enter password to test root login to localhost:"
ssh root@localhost exit
res=$?
if [[ $res != 0 ]]; then
echo "Root ssh login failed!"
exit $res
fi
echo "Successfully enabled ssh login."
return 0
}
update_system() {
local res
## Update system
# Install 45drives repository
echo "UPDATING SYSTEM"
echo "Downloading 45Drives Repo Setup Script"
curl -sSL https://repo.45drives.com/setup -o setup-repo.sh
res=$?
if [[ $res != 0 ]]; then
echo "Failed to download repo setup script! (https://repo.45drives.com/setup)"
exit $res
fi
echo "Running 45Drives Repo Setup Script"
bash setup-repo.sh
res=$?
if [[ $res != 0 ]]; then
echo "Failed to run the setup script! (https://repo.45drives.com/setup)"
exit $res
fi
# apt upgrade
echo "Upgrading packages"
apt upgrade -y
res=$?
if [[ $res != 0 ]]; then
echo "apt upgrade failed!"
exit $res
fi
echo "Successfully updated system."
return 0
}
init_network() {
local res
echo "SETTING UP NETWORK MANAGER"
# Install network packages
apt update
res=$?
if [[ $res != 0 ]]; then
echo "apt update failed!"
exit $res
fi
apt install -y network-manager firewalld
res=$?
if [[ $res != 0 ]]; then
echo "Installing network manager and/or firewalld failed!"
exit $res
fi
systemctl enable --now network-manager
res=$?
if [[ $res != 0 ]]; then
echo "Enabling network manager failed!"
exit $res
fi
# Disable ufw and enable firewalld
systemctl enable --now firewalld
res=$?
if [[ $res != 0 ]]; then
echo "Enabling firewalld failed!"
exit $res
fi
ufw disable
res=$?
if [[ $res != 0 ]]; then
echo "Disabling ufw failed!"
exit $res
fi
echo "Successfully set up network manager."
return 0
}
remove_garbage() {
local res
echo "REMOVING CLOUD-INIT AND SNAPD"
# Disable cloud-init
touch /etc/cloud/cloud-init.disabled
# Remove snapd
apt autoremove --purge -y snapd
res=$?
if [[ $res != 0 ]]; then
echo "Disabling snapd failed!"
exit $res
fi
echo "Successfully removed cloud-init and snapd."
return 0
}
add_cockpit() {
local res
echo "INITIALIZING HOUSTON"
# Install cockpit and cockpit related things
echo "Installing dependencies"
apt update
res=$?
if [[ $res != 0 ]]; then
echo "apt update failed!"
exit $res
fi
apt install -y cockpit cockpit-benchmark cockpit-navigator cockpit-file-sharing cockpit-45drives-hardware cockpit-identities cockpit-machines \
cockpit-sosreport realmd tuned udisks2-lvm2 zfs-dkms samba winbind nfs-kernel-server nfs-client 45drives-tools cockpit-scheduler cockpit-zfs
res=$?
if [[ $res != 0 ]]; then
echo "Installing Houston dependencies failed!"
exit $res
fi
# Open firewall for cockpit
echo "Opening firewall for cockpit"
firewall-cmd --permanent --add-service=cockpit
res=$?
if [[ $res != 0 ]]; then
echo "Adding cockpit to firewall failed!"
exit $res
fi
firewall-cmd --reload
res=$?
if [[ $res != 0 ]]; then
echo "Reloading firewall failed!"
exit $res
fi
# Install cockpit override manifests for 45ddrives-hardware and apps
cat >> /usr/share/cockpit/45drives-disks/override.json <<EOF
{
"menu": {
"45drives-disks": {
"order": 110
}
}
}
EOF
cat >> /usr/share/cockpit/45drives-motherboard/override.json <<EOF
{
"menu": {
"45drives-motherboard": {
"order": 111
}
}
}
EOF
cat >> /usr/share/cockpit/45drives-system/override.json <<EOF
{
"menu": {
"45drives-system": {
"order": 112
}
}
}
EOF
cat >> /usr/share/cockpit/apps/override.json <<EOF
{
"tools": {
"index": null
}
}
EOF
systemctl enable --now cockpit.socket
res=$?
if [[ $res != 0 ]]; then
echo "Enabling cockpit.socket failed!"
exit $res
fi
echo "Successfully initialized Houston."
return 0
}
use_nm_not_systemd-networkd() {
local res
echo "ENABLING NETWORK MANAGER"
# Use Network Manager instead of systemd-networkd
cat > /etc/netplan/00-networkmanager.yaml <<EOF
network:
version: 2
renderer: NetworkManager
EOF
[[ -f /etc/netplan/00-installer-config.yaml ]] && mv /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.backup
netplan try
res=$?
if [[ $res != 0 ]]; then
echo "netplan try failed."
exit $res
fi
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
mv /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf /usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf.backup
sed -i '/^managed/s/false/true/' /etc/NetworkManager/NetworkManager.conf
systemctl restart network-manager
res=$?
if [[ $res != 0 ]]; then
echo "Reloading network-manager failed."
exit $res
fi
echo "Successfully enabled network manager."
return 0
}
setup_done() {
local response=""
echo "SETUP COMPLETE"
read -p "Reboot system now? [y/N]: " response
case $response in
[yY]|[yY][eE][sS])
reboot now
;;
*)
echo "Reboot soon to finish configuration."
;;
esac
return 0
}
progress=""
if [[ -f .ubuntu-preconfig.progress ]]; then
progress=$(cat .ubuntu-preconfig.progress)
fi
if [[ $progress != "" ]]; then
echo "Found progress from previous time running this script. ($PWD/.ubuntu-preconfig.progress)"
echo "1. Continue from last successful step."
echo "2. Start from beginning."
echo "3. Exit. (default)"
read -p "[1-3]: " response
case $response in
1)
echo "Starting from last successful step."
;;
2)
echo "Starting from beginning."
progress=""
;;
*)
echo "Exiting..."
exit 0
;;
esac
fi
case $progress in
"")
welcome
;& # fallthrough
0)
echo "################################################################################"
enable_root_user
echo 1 > .ubuntu-preconfig.progress
;&
1)
echo "################################################################################"
enable_root_ssh
echo 2 > .ubuntu-preconfig.progress
;&
2)
echo "################################################################################"
update_system
echo 3 > .ubuntu-preconfig.progress
;&
3)
echo "################################################################################"
init_network
echo 4 > .ubuntu-preconfig.progress
;&
4)
echo "################################################################################"
remove_garbage
echo 5 > .ubuntu-preconfig.progress
;&
5)
echo "################################################################################"
add_cockpit
echo 6 > .ubuntu-preconfig.progress
;&
6)
echo "################################################################################"
use_nm_not_systemd-networkd
echo 7 > .ubuntu-preconfig.progress
;&
7)
echo "################################################################################"
setup_done
echo 8 > .ubuntu-preconfig.progress
;;
8)
echo "Setup successfully finished the previous time running this script."
;;
esac
exit 0