#!/bin/bash
# alpine-bootstrap v1.0
#
# a script for quickly installing alpine on Oracle Cloud servers
# tested to work on Ampere (aarch64) and AMD (amd64)
#
# NOTE: running on AMD64 currently requires copying the kernel
#       and modules from RedHat to Alpine. Please note that
#       this may be buggy as heck.
#
# LICENSED UNDER LGPLv3 (--> https://git.sakamoto.pl/laudompat/http.sh/-/blob/master/LICENSE.md <--)
#
# sdomi <ja@sdomi.pl>
# https://sdomi.pl


if [[ "$(whoami)" != "root" ]]; then
	echo "This script needs to be run as root."
	exit 1
fi

if [[ "$1" == '' ]]; then
	swap="/dev/$(lsblk -l | grep -i swap | awk '{print $1}')"
	echo "Annihilating swap on $swap..."
	swapoff "$swap"
else
	swap="$1"
fi

if [[ "$swap" == "/dev/" ]]; then
	echo "No swap partition found?"
	echo "We'll need a partition for sysroot. Specify which with $0 <dev_path>"
	exit 1
fi

gpt="hd0,gpt$(grep -Poh "[0-9]" <<< "$swap")"
efi="/dev/$(lsblk -l | grep -i efi | awk '{print $1}')"

mkfs.ext4 "$swap"
mount "$swap" /mnt


if [[ "$(uname -p)" == "aarch64" ]]; then
	arch="aarch64"
elif [[ "$(uname -p)" == "x86_64" ]]; then
	arch="x86_64"
else
	echo "Unsupported architecture..?"
	exit 1
fi

cd /mnt
wget "https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/$arch/alpine-minirootfs-3.13.5-$arch.tar.gz"

tar xvf *.tar.gz
rm *.tar.gz

chroot /mnt passwd

echo "nameserver 1.1.1.1" > etc/resolv.conf

chroot /mnt apk update
if [[ $arch == "x86_64" ]]; then
	chroot /mnt apk add alpine-base openssh-server util-linux e2fsprogs-extra
else
	chroot /mnt apk add alpine-base openssh-server util-linux e2fsprogs-extra linux-virt
fi

echo "PermitRootLogin yes" >> etc/ssh/sshd_config

mkdir -p boot/efi

if [[ $arch == "x86_64" ]]; then
	kernel="$(uname -r)"
	cp /boot/vmlinuz-$kernel /mnt/boot/vmlinuz-redhat
	cp /boot/initramfs-$kernel.img /mnt/boot/initramfs-redhat
	mkdir -p /mnt/lib/modules/
	cp -R /lib/modules/$kernel /mnt/lib/modules/$kernel
	
	sed -i 's@### END /etc/grub.d/10_linux ###@menuentry '"'Alpine w/ RedHat kernel'"' {\n\tset root='"'$gpt'"'\n\tlinuxefi /boot/vmlinuz-redhat root='"$swap"' modules=sd-mod,usb-storage,ext4 nomodeset loglevel=4 rootfstype=ext4\n\tinitrdefi /boot/initramfs-redhat\n}\ndefault=3\n@' /boot/efi/EFI/redhat/grub.cfg
else
	sed -i 's@### END /etc/grub.d/10_linux ###@menuentry '"'Alpine'"' {\n\tset root='"'$gpt'"'\n\tlinux /boot/vmlinuz-virt root='"$swap"' modules=sd-mod,usb-storage,ext4 nomodeset loglevel=4 rootfstype=ext4\n\tinitrd /boot/initramfs-virt\n}\ndefault=2\n@' /boot/efi/EFI/redhat/grub.cfg
fi

echo -e "$swap\t/\text4\tauto\t0\t0\n$efi\t/boot/efi\tvfat\t0\t0" >> etc/fstab
echo -e "auto lo\niface lo inet loopback\n\nauto eth0\niface eth0 inet dhcp" > etc/network/interfaces

for i in bootmisc hostname hwclock loadkmap loopback modules networking swap sysctl syslog urandom; do
	ln -s /etc/init.d/$i etc/runlevels/boot
done

for i in devfs dmesg hwdrivers mdev; do
	ln -s /etc/init.d/$i etc/runlevels/sysinit
done

for i in acpid sshd; do
	ln -s /etc/init.d/$i etc/runlevels/default
done

for i in killprocs mount-ro savecache; do
	ln -s /etc/init.d/$i etc/runlevels/shutdown
done

echo "We're finished? Try rebooting ^^"
