Skip to content

Instantly share code, notes, and snippets.

@FrantisekSidak
Created January 21, 2024 15:37
Show Gist options
  • Save FrantisekSidak/029e01ab67ed4eadfce79392c9a9ee86 to your computer and use it in GitHub Desktop.
Save FrantisekSidak/029e01ab67ed4eadfce79392c9a9ee86 to your computer and use it in GitHub Desktop.
vpsFree k3s install script (ubuntu 22.04)
#!/bin/sh
K3S_SERVICE_NAME="k3s.service"
K3S_FAKE_SERVICE_NAME="k3s-fake.service"
# helper functions for output messages
info()
{
echo '[INFO] ' "$@"
}
warn()
{
echo '[WARN] ' "$@" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
# check installation is not processed yet
check_status() {
ensure_unit_not_exists $K3S_SERVICE_NAME
ensure_unit_not_exists $K3S_FAKE_SERVICE_NAME
# check curl presence
if ! type "curl" > /dev/null; then
fatal "curl command is missing."
fi
}
# install k3s, do not start
k3s_install() {
info 'Installing k3s...'
curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_START=true sh -
}
# apply system prerequisities
k3s_fakes_create() {
info 'Applying system fakes...'
ln -s /bin/true /usr/local/sbin/modprobe
mkdir -p /opt/k3s/fake
cat > /opt/k3s/fake.sh <<EOF
#!/bin/bash
cd /opt/k3s/fake
echo 0 > panic
mount --bind panic /proc/sys/kernel/panic
echo 0 > panic_on_oops
mount --bind panic_on_oops /proc/sys/kernel/panic_on_oops
echo 0 > overcommit_memory
mount --bind overcommit_memory /proc/sys/vm/overcommit_memory
mkdir block
mount -o bind block/ /sys/block/
EOF
chmod +x /opt/k3s/fake.sh
cat > /etc/systemd/system/k3s-fake.service <<EOF
[Unit]
Before=k3s.service
[Service]
ExecStart=/opt/k3s/fake.sh
[Install]
WantedBy=default.target
EOF
chmod 644 /etc/systemd/system/k3s-fake.service
info 'Faked.'
}
# updates k3s service to use fakes
k3s_modify() {
cp /etc/systemd/system/k3s.service /etc/systemd/system/k3s.service.orig
sed -i 's/ExecStartPre=.*br_netfilter.*/ExecStartPre=-\/usr\/local\/sbin\/modprobe br_netfilter/g' /etc/systemd/system/k3s.service
sed -i 's/ExecStartPre=.*overlay.*/ExecStartPre=-\/usr\/local\/sbin\/modprobe overlay/g' /etc/systemd/system/k3s.service
}
# enables and starts services
enable_and_start() {
systemctl daemon-reload
systemctl enable k3s-fake.service
systemctl start k3s-fake.service
systemctl start k3s.service
}
# checks if unit exists, usage: unit_exists "k3s.service"
unit_exists() {
[ $(systemctl list-unit-files "${1}" | wc -l) -gt 3 ]
}
# exit script if unit exists, usage: ensure_unit_not_exists "k3s.service"
ensure_unit_not_exists() {
if unit_exists "$1" ; then
fatal "Unit $1 already exist."
fi
}
# process installation
{
check_status
k3s_install
k3s_fakes_create
k3s_modify
enable_and_start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment