workbench-script/pxe/install-pxe.sh

172 lines
5.4 KiB
Bash
Raw Normal View History

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'
# jump to current dir where the script is so relative links work
cd "$(dirname "${0}")"
# 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
${SUDO} cp -a "${target}" "${target}-bak_${ts}"
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
mkdir -p "${nfs_path}/live"
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-26 12:01:52 +00:00
mkdir -p "/snapshots"
2024-09-25 23:16:03 +00:00
mount --bind "${nfs_path}/snapshots" "/snapshots"
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-09-25 14:10:43 +00:00
if [ ! -f "${nfs_path}/settings.ini" ]; then
if [ -f "settings.ini" ]; then
2024-09-25 16:40:57 +00:00
cp settings.ini "${nfs_path}/settings.ini"
2024-09-25 14:10:43 +00:00
else
2024-09-25 16:47:43 +00:00
echo "ERROR: $(pwd)/settings.ini does not exist yet, cannot read config from there. You can take inspiration with file $(pwd)/settings.ini.example"
2024-09-25 14:10:43 +00:00
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-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-09-25 12:39:41 +00:00
if [ ! -d "${tftp_path}" ] || [ "${FORCE:-}" ]; then
2024-09-25 15:51:47 +00:00
mkdir -p "${tftp_path}/pxelinux.cfg"
2024-09-24 13:51:14 +00:00
cd "${tftp_path}"
2024-09-25 15:57:52 +00:00
if [ ! -f "${tftp_path}/netboot.tar.gz" ]; then
wget http://ftp.debian.org/debian/dists/${VERSION_CODENAME}/main/installer-amd64/current/images/netboot/netboot.tar.gz
2024-09-25 16:00:15 +00:00
tar xvf netboot.tar.gz || true
rm -rf "${tftp_path}/pxelinux.cfg"
2024-09-25 16:02:18 +00:00
mkdir -p "${tftp_path}/pxelinux.cfg"
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-09-27 20:27:12 +00:00
${SUDO} cp /usr/lib/syslinux/memdisk "${tftp_path}/"
${SUDO} cp /usr/lib/syslinux/modules/bios/* "${tftp_path}/"
${SUDO} tee "${tftp_path}/pxelinux.cfg/default" <<END
DEFAULT menu.c32
PROMPT 0
TIMEOUT 50
ONTIMEOUT wb
2024-09-24 13:51:14 +00:00
2024-09-27 20:27:12 +00:00
MENU TITLE PXE Boot Menu
LABEL wb
MENU LABEL Boot Workbench
2024-09-25 12:54:16 +00:00
KERNEL vmlinuz
2024-09-25 16:15:55 +00:00
INITRD initrd
APPEND ip=dhcp netboot=nfs nfsroot=${server_ip}:${nfs_path}/ boot=live text forcepae
2024-09-24 13:51:14 +00:00
END
cd -
2024-09-24 13:51:14 +00:00
fi
2024-09-27 20:27:12 +00:00
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-09-24 15:26:10 +00:00
# get where the script is
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-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-25 16:47:43 +00:00
echo "PXE: WARNING: $(pwd)/.env does not exist yet, cannot read config from there. You can take inspiration with file $(pwd)/.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}"
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; -*-