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
|
|
|
|
|
2024-09-27 20:27:12 +00:00
|
|
|
detect_user() {
|
|
|
|
userid="$(id -u)"
|
|
|
|
# detect non root user without sudo
|
|
|
|
if [ ! "${userid}" = 0 ] && id ${USER} | grep -qv sudo; then
|
|
|
|
echo "ERROR: this script needs root or sudo permissions (current user is not part of sudo group)"
|
|
|
|
exit 1
|
|
|
|
# detect user with sudo or already on sudo src https://serverfault.com/questions/568627/can-a-program-tell-it-is-being-run-under-sudo/568628#568628
|
|
|
|
elif [ ! "${userid}" = 0 ] || [ -n "${SUDO_USER}" ]; then
|
|
|
|
SUDO='sudo'
|
|
|
|
# working directory to build the iso
|
|
|
|
ISO_PATH="iso"
|
|
|
|
# detect pure root
|
|
|
|
elif [ "${userid}" = 0 ]; then
|
|
|
|
SUDO=''
|
|
|
|
ISO_PATH="/opt/workbench"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:51:14 +00:00
|
|
|
install_dependencies() {
|
2024-09-27 20:27:12 +00:00
|
|
|
${SUDO} apt update
|
|
|
|
${SUDO} apt install -y wget dnsmasq nfs-kernel-server rsync syslinux
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
backup_file() {
|
|
|
|
target="${1}"
|
|
|
|
ts="$(date +'%Y-%m-%d_%H-%M-%S')"
|
2024-09-27 20:27:12 +00:00
|
|
|
|
2024-09-24 13:51:14 +00:00
|
|
|
if [ -f "${target}" ]; then
|
2024-09-27 20:27:12 +00:00
|
|
|
if ! grep -q 'we should do a backup' "${target}"; then
|
2024-11-06 18:31:44 +00:00
|
|
|
${SUDO} cp -v -a "${target}" "${target}-bak_${ts}"
|
2024-09-27 20:27:12 +00:00
|
|
|
fi
|
2024-09-24 13:51:14 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
install_nfs() {
|
2024-09-27 20:27:12 +00:00
|
|
|
# append live directory, which is expected by the debian live env
|
2024-09-27 21:02:19 +00:00
|
|
|
${SUDO} mkdir -p "${nfs_path}/live"
|
|
|
|
${SUDO} mkdir -p "${nfs_path}/snapshots"
|
2024-09-25 23:16:03 +00:00
|
|
|
|
|
|
|
# debian live nfs path is readonly, do a trick
|
|
|
|
# to make snapshots subdir readwrite
|
2024-09-26 12:00:36 +00:00
|
|
|
if ! grep -q "/snapshots" /proc/mounts; then
|
2024-09-27 21:02:19 +00:00
|
|
|
${SUDO} mkdir -p "/snapshots"
|
|
|
|
${SUDO} mount --bind "${nfs_path}/snapshots" "/snapshots"
|
2024-09-25 23:16:03 +00:00
|
|
|
fi
|
|
|
|
|
2024-09-27 20:27:12 +00:00
|
|
|
backup_file /etc/exports
|
|
|
|
|
|
|
|
if [ "${DEBUG:-}" ]; then
|
|
|
|
nfs_debug=' 127.0.0.1(rw,sync,no_subtree_check,no_root_squash,insecure)'
|
|
|
|
fi
|
|
|
|
|
|
|
|
${SUDO} tee /etc/exports <<END
|
|
|
|
${script_header}
|
|
|
|
# we assume that if you remove this line from the file, we should do a backup
|
|
|
|
${nfs_path} ${nfs_allowed_lan}(rw,sync,no_subtree_check,no_root_squash)${nfs_debug:-}
|
|
|
|
/snapshots ${nfs_allowed_lan}(rw,sync,no_subtree_check,no_root_squash)${nfs_debug:-}
|
2024-09-24 13:51:14 +00:00
|
|
|
END
|
2024-09-25 23:16:03 +00:00
|
|
|
# reload nfs exports
|
2024-09-27 20:27:12 +00:00
|
|
|
${SUDO} exportfs -vra
|
2024-09-25 23:16:03 +00:00
|
|
|
|
2024-11-11 13:04:17 +00:00
|
|
|
if [ ! -f ./.settings.ini ]; then
|
|
|
|
cp -v ./.settings.ini.example ./.settings.ini
|
|
|
|
echo "WARNING: settings.ini was not there, settings.ini.example was copied, this only happens once"
|
|
|
|
fi
|
2024-09-25 14:10:43 +00:00
|
|
|
|
2024-09-25 14:52:46 +00:00
|
|
|
if [ ! -f "${nfs_path}/settings.ini" ]; then
|
2024-11-11 13:04:17 +00:00
|
|
|
${SUDO} cp -v settings.ini "${nfs_path}/settings.ini"
|
|
|
|
echo "WARNING: ${nfs_path}/settings.ini was not there, ./settings.ini was copied, this only happens once"
|
2024-09-25 14:10:43 +00:00
|
|
|
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-27 20:27:12 +00:00
|
|
|
${SUDO} tee /etc/dnsmasq.d/pxe-tftp <<END
|
|
|
|
${script_header}
|
2024-09-24 15:14:24 +00:00
|
|
|
port=0
|
2024-09-26 15:18:17 +00:00
|
|
|
# info: https://wiki.archlinux.org/title/Dnsmasq#Proxy_DHCP
|
2024-09-24 15:14:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
install_netboot() {
|
|
|
|
# if you want to refresh install, remove or move dir
|
2024-10-16 20:52:22 +00:00
|
|
|
if [ ! -d "${tftp_path}" ] || [ "${FORCE:-}" = 'true' ]; then
|
2024-09-27 21:02:19 +00:00
|
|
|
${SUDO} mkdir -p "${tftp_path}/pxelinux.cfg"
|
2024-09-25 15:57:52 +00:00
|
|
|
if [ ! -f "${tftp_path}/netboot.tar.gz" ]; then
|
2024-09-27 21:02:19 +00:00
|
|
|
url="http://ftp.debian.org/debian/dists/${VERSION_CODENAME}/main/installer-amd64/current/images/netboot/netboot.tar.gz"
|
|
|
|
${SUDO} wget -P "${tftp_path}" "${url}"
|
|
|
|
${SUDO} tar xvf "${tftp_path}/netboot.tar.gz" -C "${tftp_path}"
|
|
|
|
${SUDO} rm -rf "${tftp_path}/pxelinux.cfg"
|
|
|
|
${SUDO} mkdir -p "${tftp_path}/pxelinux.cfg"
|
2024-09-25 15:55:37 +00:00
|
|
|
fi
|
2024-09-25 16:45:54 +00:00
|
|
|
|
2024-09-27 20:27:12 +00:00
|
|
|
${SUDO} cp -fv "${PXE_DIR}/../iso/staging/live/vmlinuz" "${tftp_path}/"
|
|
|
|
${SUDO} cp -fv "${PXE_DIR}/../iso/staging/live/initrd" "${tftp_path}/"
|
2024-09-25 01:56:47 +00:00
|
|
|
|
2024-11-06 18:31:44 +00:00
|
|
|
${SUDO} cp -v /usr/lib/syslinux/memdisk "${tftp_path}/"
|
|
|
|
${SUDO} cp -v /usr/lib/syslinux/modules/bios/* "${tftp_path}/"
|
|
|
|
if [ ! -f ./pxe-menu.cfg ]; then
|
|
|
|
${SUDO} cp -v ./pxe-menu.cfg.example pxe-menu.cfg
|
|
|
|
echo "WARNING: pxe-menu.cfg was not there, pxe-menu.cfg.example was copied, this only happens once"
|
|
|
|
fi
|
2024-09-27 21:02:37 +00:00
|
|
|
envsubst < ./pxe-menu.cfg | ${SUDO} tee "${tftp_path}/pxelinux.cfg/default"
|
2024-09-24 13:51:14 +00:00
|
|
|
fi
|
2024-09-27 20:27:12 +00:00
|
|
|
|
2024-09-27 21:02:37 +00:00
|
|
|
${SUDO} rsync -av "${PXE_DIR}/../iso/staging/live/filesystem.squashfs" "${nfs_path}/live/"
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init_config() {
|
2024-09-27 20:27:12 +00:00
|
|
|
|
2024-10-16 20:52:02 +00:00
|
|
|
# jump to current dir where the script is so relative links work
|
2024-09-24 15:26:10 +00:00
|
|
|
cd "$(dirname "${0}")"
|
2024-09-27 20:27:12 +00:00
|
|
|
|
|
|
|
# this is what we put in the files we modity
|
|
|
|
script_header='# configuration done through workbench install-pxe script'
|
|
|
|
|
2024-09-25 15:41:49 +00:00
|
|
|
PXE_DIR="$(pwd)"
|
2024-09-24 15:26:10 +00:00
|
|
|
|
2024-11-08 17:20:47 +00:00
|
|
|
if [ ! -f ./.env ]; then
|
2024-11-11 12:58:37 +00:00
|
|
|
cp -v ./.env.example ./.env
|
2024-11-08 17:20:47 +00:00
|
|
|
echo "WARNING: .env was not there, .env.example was copied, this only happens once"
|
2024-09-24 13:51:14 +00:00
|
|
|
fi
|
2024-11-08 17:20:47 +00:00
|
|
|
. ./.env
|
2024-09-24 13:51:14 +00:00
|
|
|
VERSION_CODENAME="${VERSION_CODENAME:-bookworm}"
|
|
|
|
tftp_path="${tftp_path:-/srv/pxe-tftp}"
|
2024-09-27 21:02:37 +00:00
|
|
|
# vars used in envsubst require to be exported:
|
|
|
|
export server_ip="${server_ip}"
|
|
|
|
export nfs_path="${nfs_path:-/srv/pxe-nfs}"
|
2024-09-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2024-09-27 20:27:12 +00:00
|
|
|
detect_user
|
2024-09-24 13:51:14 +00:00
|
|
|
init_config
|
|
|
|
install_dependencies
|
|
|
|
install_tftp
|
|
|
|
install_nfs
|
2024-09-25 16:45:54 +00:00
|
|
|
install_netboot
|
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; -*-
|