install OS from template

This commit is contained in:
ghe0 2024-11-08 23:10:19 +02:00
parent 6e3d689f45
commit cc2e025ae0
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
4 changed files with 66 additions and 11 deletions

@ -13,6 +13,10 @@ install_binary $(which switch_root)
install_binary $(which bash)
install_binary $(which mount)
install_binary $(which openssl) && cp -r /etc/ssl "${ROOT}/etc/"
install_binary $(which cryptsetup)
install_binary $(which blkid)
install_binary $(which fdisk)
install_binary $(which mkfs.ext4)
install_kmod
install_busybox
@ -23,11 +27,18 @@ echo_cyan "Installing kernel modules..."
# # Uncomment this section if you want to grab modules from the guest OS
# scan_modules
install_module virtio_net
install_module btrfs
install_module ext4
install_module virtio_blk
install_module msr
install_module sev-guest
install_module dm_crypt
install_module hid-generic
install_module dm-integrity
install_module cbc
install_module hmac
install_module sha256
install_module rng
install_module aes
echo_cyan "Building module dependency tree..."
cp /lib/modules/${KERNEL}/modules.{order,builtin,builtin.modinfo} "${ROOT}/lib/modules/${KERNEL}/"

@ -25,6 +25,7 @@ create_dirs() {
mkdir -p "${ROOT}/etc"
mkdir -p "${ROOT}/proc"
mkdir -p "${ROOT}/run"
mkdir -p "${ROOT}/sys"
mkdir -p "${ROOT}/tmp"
mkdir -p "${ROOT}/var"
ln -s usr/bin "${ROOT}/bin"
@ -77,12 +78,12 @@ install_binary() {
install_kmod() {
echo_cyan "Installing kmod (depmod, insmod, lsmod, modinfo, modprobe, rmmod)..."
install_binary /usr/bin/kmod || return 1
ln -s kmod ${ROOT}/usr/bin/lsmod
ln -s kmod ${ROOT}/usr/bin/rmmod
ln -s kmod ${ROOT}/usr/bin/insmod
ln -s kmod ${ROOT}/usr/bin/modinfo
ln -s kmod ${ROOT}/usr/bin/modprobe
ln -s kmod ${ROOT}/usr/bin/depmod
ln -s kmod "${ROOT}/usr/bin/lsmod"
ln -s kmod "${ROOT}/usr/bin/rmmod"
ln -s kmod "${ROOT}/usr/bin/insmod"
ln -s kmod "${ROOT}/usr/bin/modinfo"
ln -s kmod "${ROOT}/usr/bin/modprobe"
ln -s kmod "${ROOT}/usr/bin/depmod"
}
install_busybox() {
@ -99,8 +100,8 @@ install_busybox() {
}
install_init_script() {
cp ../init.sh ${ROOT}/init
cp ../init_functions.sh ${ROOT}/
cp ../init.sh "${ROOT}/init"
cp ../init_functions.sh "${ROOT}/"
}
install_module() {

12
init.sh

@ -4,5 +4,15 @@ create_mounts
load_modules
create_certs
setup_network
mount_root
# TODO: replace hardcoded URL with guest_api
echo "http://192.168.122.226/base_arch2.tar.xz" > /tmp/install_url
if [[ -f "/tmp/install_url" ]]; then
install_os
else
mount_root
fi
github_ssh_key
exec switch_root /mnt /sbin/init "$@"

@ -65,7 +65,40 @@ setup_network() {
echo nameserver $nameserver > /etc/resolv.conf
}
install_os() {
local url="$(cat /tmp/install_url)"
blkid | grep vda1 | grep ext4 && {
mount_root
return 0
}
fdisk /dev/vda <<EOF
n
p
w
EOF
mkfs.ext4 /dev/vda1
mount_root
cd /mnt/
wget -O template.tar.xz "$url"
tar --numeric-owner -xpJf /mnt/template.tar.xz -C /mnt/
rm /mnt/template.tar.xz
rm /mnt/etc/fstab
}
# detee_ghu stands for GitHub user and expects format detee_ghu=ghe0
github_ssh_key() {
github_user=$(cat /proc/cmdline | grep -oE 'detee_ghu=[0-9a-z\_\.\-]+' | cut -d '=' -f2)
[[ -z "$github_user" ]] && return 0
mkdir -p /mnt/root/.ssh
cd /mnt/root/.ssh
wget -O authorized_keys https://github.com/${github_user}.keys
chmod 600 authorized_keys
}
mount_root() {
mkdir /mnt
mount /dev/vda3 /mnt
mount /dev/vda1 /mnt
}