diff --git a/deploy-workbench.sh b/deploy-workbench.sh index b14ed73..a0e4d79 100755 --- a/deploy-workbench.sh +++ b/deploy-workbench.sh @@ -198,9 +198,9 @@ create_persistence_partition() { ${SUDO} umount -f -l "${tmp_rw_mount}" >/dev/null 2>&1 || true mkdir -p "${tmp_rw_mount}" ${SUDO} mount "$(pwd)/${rw_img_path}" "${tmp_rw_mount}" - ${SUDO} mkdir -p "${tmp_rw_mount}/settings" + ${SUDO} mkdir -p "${tmp_rw_mount}" if [ -f "settings.ini" ]; then - ${SUDO} cp -v settings.ini "${tmp_rw_mount}/settings/settings.ini" + ${SUDO} cp -v settings.ini "${tmp_rw_mount}/settings.ini" 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 @@ -274,11 +274,11 @@ stty -echo # Do not show what we type in terminal so it does not meddle with our dmesg -n 1 # Do not report *useless* system messages to the terminal # detect pxe env -if [ -d /run/live/medium ]; then +nfs_host="\$(df -hT | grep nfs | cut -f1 -d: | head -n1)" +if [ "\${nfs_host}" ]; then mount --bind /run/live/medium /mnt # debian live nfs path is readonly, do a trick # to make snapshots subdir readwrite - nfs_host="\$(df -hT | grep nfs | cut -f1 -d: | head -n1)" mount \${nfs_host}:/snapshots /run/live/medium/snapshots # reload mounts on systemd systemctl daemon-reload diff --git a/pxe/Makefile b/pxe/Makefile new file mode 100644 index 0000000..edf648f --- /dev/null +++ b/pxe/Makefile @@ -0,0 +1,2 @@ +test_pxe: + qemu-system-x86_64 -m 1G -boot n -netdev user,id=mynet0,tftp=/srv/pxe-tftp,bootfile=pxelinux.0 -device e1000,netdev=mynet0 diff --git a/pxe/README-es.md b/pxe/README-es.md index 7b6d7de..c1cda36 100644 --- a/pxe/README-es.md +++ b/pxe/README-es.md @@ -16,6 +16,34 @@ Este servidor aporta un servicio de arranque por red tipo PXE, y no hace colisi El servidor PXE ofrece a la máquina que arranca un *debian live* a través de [NFS](https://es.wikipedia.org/wiki/Network_File_System). Una vez arrancado, ejecuta el `workbench-script.py` con la configuración remota del servidor PXE. Cuando ha terminado, también guarda en el mismo servidor PXE el snapshot resultante. También lo puede guardar en devicehub si se especifica en la variable `url` de la configuración `settings.ini`. +## Probarlo todo en localhost + +Preparar configuración de `.env` tal como: + +``` +server_ip=10.0.2.2 +nfs_allowed_lan=10.0.2.0/24 +tftp_path='/srv/pxe-tftp' +nfs_path='/srv/pxe-nfs' +``` + +Red y host 10.0.2.2? Esta es la forma en que el programa *qemu* hace red en localhost, 10.0.2.2 es la dirección de localhost que saliendo de qemu es traducida como 127.0.0.1 + +Desplegar servidores TFTP y NFS en el mismo ordenador, para permitir nfs inseguro: + +``` +DEBUG=true ./install-pxe.sh +``` + +Los directorios inseguros contienen configuración y snapshots de workbench, nada importante supongo. Aún así, `DEBUG=true` no se recomienda para un entorno de producción para evitar sorpresas. + + +Y para terminar, probar el cliente PXE con el siguiente comando: + +``` +make test_pxe +``` + ## Recursos El servicio PXE diff --git a/pxe/install-pxe.sh b/pxe/install-pxe.sh index 8ca21cf..17f153c 100755 --- a/pxe/install-pxe.sh +++ b/pxe/install-pxe.sh @@ -8,21 +8,46 @@ 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() { - apt update - apt install -y wget dnsmasq nfs-kernel-server rsync + ${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 - cp -a "${target}" "${target}_bak_${ts}" + if ! grep -q 'we should do a backup' "${target}"; then + ${SUDO} cp -a "${target}" "${target}-bak_${ts}" + fi fi } install_nfs() { - backup_file /etc/exports + # append live directory, which is expected by the debian live env + mkdir -p "${nfs_path}/live" + mkdir -p "${nfs_path}/snapshots" # debian live nfs path is readonly, do a trick # to make snapshots subdir readwrite @@ -31,16 +56,21 @@ install_nfs() { mount --bind "${nfs_path}/snapshots" "/snapshots" fi - cat > /etc/exports < /etc/dnsmasq.d/pxe-tftp < "${tftp_path}/pxelinux.cfg/default" <