#!/bin/sh # Copyright (c) 2024 Pedro # SPDX-License-Identifier: AGPL-3.0-or-later set -e set -u # DEBUG set -x 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 } install_dependencies() { ${SUDO} apt update ${SUDO} apt install -y wget dnsmasq nfs-kernel-server rsync syslinux } backup_file() { target="${1}" ts="$(date +'%Y-%m-%d_%H-%M-%S')" if [ -f "${target}" ]; then if ! grep -q 'we should do a backup' "${target}"; then ${SUDO} cp -a "${target}" "${target}-bak_${ts}" fi fi } install_nfs() { # append live directory, which is expected by the debian live env ${SUDO} mkdir -p "${nfs_path}/live" ${SUDO} mkdir -p "${nfs_path}/snapshots" # debian live nfs path is readonly, do a trick # to make snapshots subdir readwrite if ! grep -q "/snapshots" /proc/mounts; then ${SUDO} mkdir -p "/snapshots" ${SUDO} mount --bind "${nfs_path}/snapshots" "/snapshots" fi 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 <