From 91811347d8cbdcafcec3ff9f90d0e5b7020fcbdf Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Tue, 28 Apr 2020 22:39:45 +0200 Subject: [PATCH] added default image for all clients (known and unkown) --- README.md | 11 ++++ libreschool-pxeserver.spec | 5 +- .../defaults/pxeserver-httpd/defaultimage | 1 + root/srv/ipxe/boot.php | 57 +++++++++++++------ 4 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 root/etc/e-smith/db/configuration/defaults/pxeserver-httpd/defaultimage diff --git a/README.md b/README.md index 5062e12..74b62dc 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,17 @@ db hosts setprop lab01 \ By running the above command, the **memtest** image will be used for one boot only. +## default image ## + +When there's no specific setting for the host, the host is not in the hosts db, or the image specified for the host is not available, the *default image* is used. + +To set the default image, you can use this command: + +```bash +db configuration setprop pxeserver-httpd \ + defaultimage memtest +``` + ## TODO / ideas ## * Create a menu, for interactive selection diff --git a/libreschool-pxeserver.spec b/libreschool-pxeserver.spec index b2d8eda..fe6c695 100644 --- a/libreschool-pxeserver.spec +++ b/libreschool-pxeserver.spec @@ -1,6 +1,6 @@ Summary: LibreSchool PXE server Name: libreschool-pxeserver -Version: 1.1.3 +Version: 1.1.4 Release: 1.ns7 License: GPL BuildArch: noarch @@ -112,6 +112,9 @@ if [ "$1" = 0 ]; then fi %changelog +* Tue Apr 28 2020 Paolo Asperti - 1.1.4-1.ns7 +- Added default image setting + * Sat Apr 25 2020 Paolo Asperti - 1.1.3-1.ns7 - Added KUbuntu 20.04 (live only) - Added LUbuntu 20.04 (live only) diff --git a/root/etc/e-smith/db/configuration/defaults/pxeserver-httpd/defaultimage b/root/etc/e-smith/db/configuration/defaults/pxeserver-httpd/defaultimage new file mode 100644 index 0000000..7902b7b --- /dev/null +++ b/root/etc/e-smith/db/configuration/defaults/pxeserver-httpd/defaultimage @@ -0,0 +1 @@ +localhd diff --git a/root/srv/ipxe/boot.php b/root/srv/ipxe/boot.php index 5f0defa..148c158 100644 --- a/root/srv/ipxe/boot.php +++ b/root/srv/ipxe/boot.php @@ -5,7 +5,7 @@ * * This script manages the loading of the PXE image, based on specific * host preferences. - * + * * php version 5.4.16 * * @category Client_Management @@ -21,15 +21,16 @@ require_once "EsmithDatabase.php"; $hosts_db = new EsmithDatabase('hosts'); +$config_db = new EsmithDatabase('configuration'); -/** +/** * Check if specified mac address is valid - * + * * We should've used filter_var($mac, FILTER_VALIDATE_MAC) but it's available * from PHP 5.5.0 and we have 5.4.16. Too bad. - * + * * @param string $mac the mac address to examine - * + * * @return bool if the macaddress is valid or not */ function isValidMAC($mac) { @@ -50,11 +51,11 @@ function isValidMAC($mac) { } } -/** +/** * Get the client corresponding a specific mac address - * + * * @param string $mac the mac address of the client - * + * * @return host||boolean client if found, false otherwise */ function getClientFromMAC($mac=false) { @@ -67,11 +68,11 @@ function getClientFromMAC($mac=false) { } } -/** +/** * Default exit string - * This string will instruct to reload the script + * This string will instruct to reload the script * passing the mac address of the calling machine - * + * * @return null */ function defaultExit() { @@ -83,23 +84,43 @@ chain http://".$_SERVER['HTTP_HOST'].$_SERVER["SCRIPT_NAME"]."?mac=\${mac} } -/** +/** * Check if specified PXE image exists - * + * * @param string $i image name - * + * * @return bool if the image exists or not */ function imageExists($i) { $pwd = dirname(__FILE__); $dir = $pwd . '/' . $i; - $bootfile = $dir . '/boot'; - $iso_is_mounted = $dir . '/iso_is_mounted'; - $iso_ok = ! file_exists($iso_is_mounted) || + $bootfile = $dir . '/boot'; + $iso_is_mounted = $dir . '/iso_is_mounted'; + $iso_ok = ! file_exists($iso_is_mounted) || (is_link($iso_is_mounted) && is_readable($iso_is_mounted)); return is_dir($dir) && is_readable($bootfile) && $iso_ok; } +/** + * Try to boot the default PXE Image + * + * @return void + */ +function try_defaultimage() { + global $config_db; + + $defaultimage = $config_db->getProp('pxeserver-httpd','defaultimage'); + if (imageExists($defaultimage)) { + print("#!ipxe +# Loading PXEImage +chain " . $defaultimage . "/boot +"); + exit(); + } +} + + + if (!isset($_REQUEST['mac'])) { defaultExit(); } @@ -112,7 +133,7 @@ if (!isValidMAC($_REQUEST['mac'])) { $mac = $_REQUEST['mac']; $client = getClientFromMAC($mac); if (!$client) { - // TODO: make up a default for unknown clients + try_defaultimage(); defaultExit(); }