workbench-script/pxe/install-pxe.sh

109 lines
2.9 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
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
${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
mkdir -p "${nfs_path}/live"
mkdir -p "${nfs_path}/snapshots"
2024-09-25 14:10:43 +00:00
if [ ! -f "${nfs_path}/settings.ini" ]; then
if [ -f "settings.ini" ]; then
ln -sfv "$(pwd)/settings.ini"" ${nfs_path}/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 15:41:49 +00:00
ln -sfv "${PXE_DIR}/../iso/staging/live/filesystem.squashfs" "${nfs_path}/live/"
ln -sfv "${PXE_DIR}/../iso/staging/live/vmlinuz" "${tftp_path}/"
2024-09-25 15:50:31 +00:00
ln -sfv "${PXE_DIR}/../iso/staging/live/initrd" "${tftp_path}/"
2024-09-25 01:56:47 +00:00
}
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-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 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
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-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-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}"
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; -*-