-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-once.sh
84 lines (68 loc) · 2.35 KB
/
init-once.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
#!/bin/bash
# init-once.sh: This script will run on the first boot of the Raspberry Pi.
# Function to add GitHub keys
add_github_keys() {
local user_home=$1
local github_token=$2
if [ -z "$github_token" ]; then
echo "No Github token provided."
return
fi
KEYS=$(curl -H "Authorization: token $github_token" -s https://api.github.com/user/keys)
SSH_KEYS=$(echo "$KEYS" | jq -r '.[].key')
if [ ! -z "$SSH_KEYS" ]; then
echo "$SSH_KEYS" >> "${user_home}/.ssh/authorized_keys"
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
else
echo "No valid SSH keys found for the provided GitHub token!"
fi
}
# Get parameters from special files
if [ -f "/boot/setup_params.json" ]; then
SETUP_PARAMS=$(cat /boot/setup_params.json)
HOSTNAME=$(echo "$SETUP_PARAMS" | jq -r '.hostname')
GITHUB_ACCESS_TOKEN=$(echo "$SETUP_PARAMS" | jq -r '.github_access_token')
CUSTOM_USER=$(echo "$SETUP_PARAMS" | jq -r '.custom_user')
DISABLE_ROOT=$(echo "$SETUP_PARAMS" | jq -r '.disable_root')
REMOVE_ALARM=$(echo "$SETUP_PARAMS" | jq -r '.remove_alarm')
fi
# Set the hostname
if [ -n "$HOSTNAME" ]; then
echo $HOSTNAME > /etc/hostname
fi
# Install necessary tools
pacman -Syu curl jq sudo base-devel git --noconfirm
# Initialize the pacman keyring
pacman-key --init
pacman-key --populate archlinuxarm
# Update all packages
pacman -Syu --noconfirm
# Compile and install Paru
git clone https://aur.archlinux.org/paru.git
cd paru
makepkg -si --noconfirm
cd ..
rm -rf paru
# User-specific customizations
if [ "$DISABLE_ROOT" == "true" ]; then
passwd -l root
fi
if [ "$REMOVE_ALARM" == "true" ]; then
userdel -r alarm
fi
if [ -n "$CUSTOM_USER" ]; then
useradd -m $CUSTOM_USER
echo "$CUSTOM_USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$CUSTOM_USER
mkdir -p /home/$CUSTOM_USER/.ssh
chown $CUSTOM_USER:$CUSTOM_USER /home/$CUSTOM_USER/.ssh
add_github_keys "/home/$CUSTOM_USER" "$GITHUB_ACCESS_TOKEN"
else
add_github_keys "/home/alarm" "$GITHUB_ACCESS_TOKEN"
f
if [ -n "$CUSTOM_USER" && -n "$INITIAL_PASSWORD" ]; then
echo '$CUSTOM_USER:$INITIAL_PASSWORD' | sudo chpasswd
fi
# Disable this script from running again
sed -i '/\/root\/init-once.sh/d' /etc/rc.local
# Remove the setup parameters to prevent re-customization
rm -f /boot/setup_params.json