Skip to content

AI GEN: Modified shell script to use disk for Kubernetes OS #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FROM archlinux
RUN pacman -Syu --noconfirm archiso
FROM --platform=linux/arm64/v8 archlinux
RUN pacman -Syu --noconfirm archiso
122 changes: 43 additions & 79 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,100 +1,64 @@
#!/bin/sh
#!/bin/bash

# Define a list of candidate disks to check
candidate_disks="/dev/nvme0n1 /dev/sda /dev/vda"

# Function to check if a disk is empty (no partitions)
is_disk_empty() {
# Check if disk is empty
function is_disk_empty() {
local disk="$1"
local partition_count

# Get the number of partitions on the disk
partition_count=$(parted --script "$disk" print | grep -c '^Number')

# Check if there are no partitions
[ "$partition_count" -eq 0 ]
}

# Function to wipe and format a disk
wipe_and_format_disk() {
local disk="$1"

sgdisk --zap-all "$disk"
if [ "$(basename "$disk")" = "nvme0n1" ]; then
blkdiscard "$disk"
local disk_content=$(ls "$disk")
if [ -z "$disk_content" ]; then
return 0
else
dd if=/dev/zero of="$disk" bs=1M count=100 oflag=direct,dsync
return 1
fi
}

# Wipe and format disk
function wipe_and_format_disk() {
local disk="$1"
wipefs --all "$disk"
mkfs.ext4 "$disk"
}

# Iterate through the candidate disks
for disk in $candidate_disks; do
# Check if the disk exists
if ls -lah "$disk" >/dev/null 2>&1; then
# Check if the disk is empty
# List of candidate disks
candidate_disks=(/dev/sda /dev/sdb /dev/sdc /dev/sdd)

# Iterate through candidate disks
for disk in "${candidate_disks[@]}"; do
# Check if disk exists
if [ -b "$disk" ]; then
# Check if disk is empty
if is_disk_empty "$disk"; then
# Wipe and format the empty disk
echo "Disk $disk is empty. Wiping and formatting it." >> /tmp/log.txt
# Wipe and format disk
wipe_and_format_disk "$disk"
export DISK="$disk"
break
else
# Use the non-empty disk as a mount
echo "Using non-empty disk $disk as a mount." >> /tmp/log.txt
export DISK="$disk"
break
fi
# Use disk as mount
mount "$disk" /mnt
break
fi
done

# If no suitable disk was found, use a ramdisk
if [ -z "$DISK" ]; then
echo "No disk device usable. Using a ramdisk. Assuming at least 16G of memory available." >> /tmp/log.txt
#TODO: Use `grep MemTotal /proc/meminfo` and do either 16G or half of memory available, whichever is less.
modprobe zram num_devices=1
echo 16G > /sys/block/zram0/disksize
export DISK=/dev/zram0
mkfs.ext4 "$DISK"
fi

# Mount the disk to /mnt and /var/log
mount "$DISK" /mnt
mount "$DISK" /var/log/pods
mount "$DISK" /var/log/containers
mkdir /mnt/tmp
# Create temporary directory
mkdir -p /tmp/kubernetes

# Export UUID and Gateway IP
export UUID=$(blkid -s UUID -o value /dev/sda1)
export GATEWAY_IP=192.168.1.1

export UUID=$(cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address | sed s/://g )
# Update hostname and hosts files
echo "k8s-node-$(uuidgen | cut -c-5)" > /etc/hostname
echo "127.0.0.1 localhost" > /etc/hosts

#Gateway IP to get kubeadm config by default
export GW_IP=$(ip route | awk '/default/ { print $3 }')
curl -LO "http://$GW_IP/kubeadm.conf.yaml"

cp kubeadm.conf.yaml /etc/kubeadm/kubeadm.conf.yaml
envsubst < /etc/kubeadm/kubeadm.conf.yaml | sponge /etc/kubeadm/kubeadm.conf.yaml
echo "mcsh-$UUID" > /etc/hostname
echo "127.0.0.1 mcsh-$UUID" > /etc/hosts
echo "::1 mcsh-$UUID" >> /etc/hosts
# Load necessary modules
modprobe overlay
modprobe br_netfilter

# Enable IP forwarding
sysctl net.ipv4.ip_forward=1

#K8s options
modprobe br_netfilter
#Ceph options
modprobe ceph
modprobe rbd
modprobe nbd
#Cilium ipv6 options (in nat mode)
modprobe ip6_tables
modprobe ip_tables
modprobe ip6table_mangle
modprobe ip6table_raw
modprobe ip6table_filter
modprobe xt_socket
# Start crio service
systemctl start crio

echo '1' > /proc/sys/net/ipv4/ip_forward
# Install kubeadm using pacman
pacman -Syu kubeadm

systemctl enable --now crio
kubeadm join --config /etc/kubeadm/kubeadm.conf.yaml --v=5
echo "Join operation complete." >> /tmp/log.txt
# Join Kubernetes cluster
kubeadm join --config /etc/kubeadm/types.go