2024-09-24 13:51:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Copyright (c) 2024 Pedro <copyright@cas.cat>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
set -e
|
|
|
|
set -u
|
|
|
|
# DEBUG
|
|
|
|
set -x
|
|
|
|
|
|
|
|
install_dependencies() {
|
|
|
|
apt update
|
|
|
|
apt install -y wget dnsmasq nfs-kernel-server
|
|
|
|
}
|
|
|
|
|
|
|
|
backup_file() {
|
|
|
|
target="${1}"
|
|
|
|
ts="$(date +'%Y-%m-%d_%H-%M-%S')"
|
|
|
|
if [ -f "${target}" ]; then
|
|
|
|
cp -a "${target}" "${target}_bak_${ts}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
install_nfs() {
|
|
|
|
backup_file /etc/exports
|
2024-09-24 15:27:11 +00:00
|
|
|
cat > /etc/exports <<END
|
2024-09-25 14:52:46 +00:00
|
|
|
${nfs_path} ${nfs_allowed_lan}(rw,sync,no_subtree_check,no_root_squash)
|
2024-09-24 13:51:14 +00:00
|
|
|
END
|
2024-09-25 01:56:47 +00:00
|
|
|
# append live directory, which is expected by the debian live env
|
2024-09-25 14:52:46 +00:00
|
|
|
mkdir -p "${nfs_path}/live"
|
|
|
|
mkdir -p "${nfs_path}/snapshots"
|
2024-09-25 14:10:43 +00:00
|
|
|
|
2024-09-25 14:52:46 +00:00
|
|
|
if [ ! -f "${nfs_path}/settings.ini" ]; then
|
|
|
|
if [ -f "settings.ini" ]; then
|
|
|
|
ln -sv "${nfs_path}/settings.ini" "settings.ini"
|
2024-09-25 14:10:43 +00:00
|
|
|
else
|
|
|
|
echo "ERROR: ../settings.ini does not exist yet, cannot read config from there. You can take inspiration with file ../settings.ini.example"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
install_tftp() {
|
2024-09-24 15:14:24 +00:00
|
|
|
|
2024-09-25 01:56:47 +00:00
|
|
|
# from https://wiki.debian.org/PXEBootInstall#Simple_way_-_using_Dnsmasq
|
2024-09-24 15:14:24 +00:00
|
|
|
cat > /etc/dnsmasq.d/pxe-tftp <<END
|
|
|
|
port=0
|
|
|
|
dhcp-range=${nfs_allowed_lan%/*},proxy
|
|
|
|
dhcp-boot=pxelinux.0
|
|
|
|
pxe-service=x86PC,"Network Boot",pxelinux
|
|
|
|
enable-tftp
|
2024-09-24 15:36:23 +00:00
|
|
|
tftp-root=${tftp_path}
|
2024-09-24 13:51:14 +00:00
|
|
|
END
|
|
|
|
}
|
|
|
|
|
2024-09-25 01:56:47 +00:00
|
|
|
extract_live_parts_for_tftp() {
|
2024-09-25 14:11:21 +00:00
|
|
|
# this is slow, so it is not enforced, reboot or remove the
|
|
|
|
# file to redownload the live iso
|
2024-09-25 12:43:11 +00:00
|
|
|
if [ ! -f /tmp/live.iso ]; then
|
2024-09-25 01:56:47 +00:00
|
|
|
# src https://www.debian.org/CD/faq/#newest
|
2024-09-25 12:43:11 +00:00
|
|
|
DEBIAN_VERSION="$(wget https://www.debian.org/CD/ -O- \
|
|
|
|
| grep -o '<strong>[0-9.]*</strong>' \
|
|
|
|
| grep -o '[0-9.]*')"
|
|
|
|
url="https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/debian-live-${DEBIAN_VERSION}-amd64-standard.iso"
|
|
|
|
wget "${url}" -O /tmp/live.iso
|
|
|
|
fi
|
2024-09-25 12:50:13 +00:00
|
|
|
mount -o loop /tmp/live.iso /mnt/
|
2024-09-25 12:52:26 +00:00
|
|
|
cp /mnt/live/vmlinuz "${tftp_path}/"
|
|
|
|
cp /mnt/live/initrd.img "${tftp_path}/"
|
2024-09-25 01:56:47 +00:00
|
|
|
umount /mnt
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:51:14 +00:00
|
|
|
install_netboot() {
|
|
|
|
# if you want to refresh install, remove or move dir
|
2024-09-25 12:39:41 +00:00
|
|
|
if [ ! -d "${tftp_path}" ] || [ "${FORCE:-}" ]; then
|
2024-09-24 13:51:14 +00:00
|
|
|
mkdir -p "${tftp_path}"
|
|
|
|
cd "${tftp_path}"
|
2024-09-25 12:43:11 +00:00
|
|
|
extract_live_parts_for_tftp
|
2024-09-25 01:56:47 +00:00
|
|
|
|
2024-09-24 15:14:33 +00:00
|
|
|
cat > "${tftp_path}/pxelinux.cfg/default" <<END
|
2024-09-24 13:51:14 +00:00
|
|
|
default wb
|
|
|
|
|
|
|
|
label wb
|
2024-09-25 12:54:16 +00:00
|
|
|
KERNEL vmlinuz
|
|
|
|
INITRD initrd.img
|
2024-09-25 14:52:46 +00:00
|
|
|
APPEND ip=dhcp netboot=nfs nfsroot=${server_ip}:${nfs_path}/ boot=live text forcepae
|
2024-09-24 13:51:14 +00:00
|
|
|
END
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
init_config() {
|
2024-09-24 15:26:10 +00:00
|
|
|
# get where the script is
|
|
|
|
cd "$(dirname "${0}")"
|
|
|
|
|
2024-09-24 13:51:14 +00:00
|
|
|
if [ -f ./.env ]; then
|
2024-09-24 13:57:01 +00:00
|
|
|
. ./.env
|
2024-09-24 13:51:14 +00:00
|
|
|
else
|
2024-09-24 15:28:28 +00:00
|
|
|
echo 'PXE: WARNING: .env does not exist yet, cannot read config from there. You can take inspiration with file .env.example'
|
2024-09-24 13:51:14 +00:00
|
|
|
fi
|
|
|
|
VERSION_CODENAME="${VERSION_CODENAME:-bookworm}"
|
|
|
|
tftp_path="${tftp_path:-/srv/pxe-tftp}"
|
|
|
|
server_ip="${server_ip}"
|
2024-09-25 14:52:46 +00:00
|
|
|
nfs_path="${nfs_path:-/srv/pxe-nfs}"
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
init_config
|
|
|
|
install_dependencies
|
|
|
|
install_netboot
|
|
|
|
install_tftp
|
|
|
|
install_nfs
|
2024-09-24 15:36:30 +00:00
|
|
|
echo "PXE: Installation finished"
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main "${@}"
|
|
|
|
|
|
|
|
# written in emacs
|
|
|
|
# -*- mode: shell-script; -*-
|