workbench-script/pxe/install-pxe.sh

109 lines
2.8 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
2024-09-24 13:51:14 +00:00
${nfs_path} ${nfs_allowed_lan}(rw,sync,no_subtree_check,no_root_squash)
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"
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() {
# src https://www.debian.org/CD/faq/#newest
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
mount -o loop live.iso /mnt/
cp /mnt/live/vmlinuz "${tftp_path}/vmlinuz-live"
cp /mnt/live/initrd.img "${tftp_path}/initrd-live.img"
umount /mnt
}
2024-09-24 13:51:14 +00:00
install_netboot() {
# if you want to refresh install, remove or move dir
2024-09-24 16:44:05 +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 01:56:47 +00:00
if [ -f /tmp/live.iso ]; then
extract_live_parts_for_tftp
fi
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-24 16:41:39 +00:00
KERNEL linux
INITRD initrd.gz
2024-09-24 13:51:14 +00:00
APPEND ip=dhcp netboot=nfs nfsroot=${server_ip}:${nfs_path}/ boot=live text forcepae
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}"
nfs_path="${nfs_path:-/srv/pxe-images}"
}
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; -*-