-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-clone-first-boot.sh
executable file
·133 lines (101 loc) · 4.01 KB
/
post-clone-first-boot.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
#!/usr/bin/env bash
# Post Clone First Boot OS Initialization Bash Script
# Description: Post Clone First Boot OS Initialization Bash Script
# Verson: 1.0.0
# Version_Date: 2024-03-25
# Author: John Haverlack ([email protected])
# License: MIT (Proposed/Pending) / UAF Only
# Source: https://github.com/acep-uaf/seal-os-lnx
# Check if dependancy binaries are installed.
req_binaries=(awk cat cut date df egrep grep jq lsblk mount sed stat tail tr uname uptime wc which)
for i in "${req_binaries[@]}"; do
if ! which $i > /dev/null 2>&1; then
echo "Error: $i binary not found or not executable. Please install $i"
exit 1
fi
done
# Verify that this script is being run as root.
if [ "$EUID" -ne 0 ]; then
echo "ERROR: This script must be run as root."
exit 1
fi
# Determine the directory full path where this seal-os.sh file is located.
rundir=$(realpath $(dirname $0))
# Check to see if the losd-lib.sh file exists and is readable.
if [ ! -r $rundir/losd/losd-lib.sh ]; then
echo "Error: $rundir/losd/losd-lib.sh file not found or not readable."
exit 1echo "WARNING: Do not run this script on production systems!!!"
fi
# Defined supported OS
supported_os=("Ubuntu" "Debian")
# Source the losd-lib.sh file.
source $rundir/losd/losd-lib.sh
losd_json=$(losd)
os_name=$(echo $losd_json | jq '.DISTRO.NAME' | sed -r 's/"//g')
os_version=$(echo $losd_json | jq '.DISTRO.VERSION' | sed -r 's/"//g')
hw_platform=$(echo $losd_json | jq '.HARDWARE.HOSTNAMECTL.Chassis' | tr -dc '[:print:]' | sed -r 's/\s//g' | sed -r 's/"//g')
ts=$(echo $losd_json | jq '.OS.NOW' | sed -r 's/"//g')
# echo "OS Name: $os_name"
# echo "OS Version: $os_version"
# echo "Hardware Platform: $hw_platform"
# Check if the hardware platform is a virtual machine.
if [ "$hw_platform" != "vm" ]; then
echo "ERROR: This script is intended to be run on a virtual machine. [$hw_platform] detected."
exit 1
fi
# Check if the OS is supported
if [[ ! " ${supported_os[@]} " =~ " ${os_name} " ]]; then
echo "ERROR: Unsupported OS detected: $os_name $os_version"
exit 1
fi
# Case statement to determine the OS version to Initialize.
case $os_name in
"Ubuntu")
# Check if SSH keys are already present befor generating new ones.
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
# Generate the SSH keys
dpkg-reconfigure openssh-server
# Regenerate a new machine-id
systemd-machine-id-setup
systemctl restart dbus
# If hostname is 'localhost' then notify users to change it upon logging in.
# if [ "$(hostname)" == "localhost" ]; then
# echo "WARNING: Hostname is set to 'localhost'. Please edit /etc/hostname."
# fi
# systemctl disable post-clone-first-boot.service
systemctl disable post-clone-first-boot.service
# rm /etc/systemd/system/post-clone-first-boot.service
# systemctl daemon-reload
# Logging Setup
$rundir/losd/losd.sh > $rundir/post-clone-first-boot.$ts.json
# Reboot the system
shutdown -r now
fi
;;
"Debian")
# Check if SSH keys are already present befor generating new ones.
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
# Generate the SSH keys
dpkg-reconfigure openssh-server
# Regenerate a new machine-id
systemd-machine-id-setup
systemctl restart dbus
# If hostname is 'localhost' then notify users to change it upon logging in.
# if [ "$(hostname)" == "localhost" ]; then
# echo "WARNING: Hostname is set to 'localhost'. Please edit /etc/hostname."
# fi
# systemctl disable post-clone-first-boot.service
systemctl disable post-clone-first-boot.service
# rm /etc/systemd/system/post-clone-first-boot.service
# systemctl daemon-reload
# Logging Setup
$rundir/losd/losd.sh > $rundir/post-clone-first-boot.$ts.json
# Reboot the system
shutdown -r now
fi
;;
*)
echo "UnSupported OS detected: $os_name $os_version"
exit 1
;;
esac