diff --git a/etc/dhcp_boot.ipxe.tmpl b/etc/dhcp_boot.ipxe.tmpl new file mode 100644 index 0000000..4df2849 --- /dev/null +++ b/etc/dhcp_boot.ipxe.tmpl @@ -0,0 +1,14 @@ +#!ipxe +echo Booting by ipxe +echo MAC Address: ${net0/mac} +set macaddress ${net0/mac} +set prefix tftp://SERVERIP/ipxe_scripts +echo Prefix: ${prefix} +set configfile ${prefix}/01-${net0/mac} +echo Config File: ${configfile} +ifopen net0 +route +# Intentar cargar la configuración personalizada por MAC +chain ${configfile} || +# Si no se encuentra la configuración personalizada, cargar la configuración por defecto +chain $prefix/default.ipxe diff --git a/etc/mac_script.ipxe.tmpl b/etc/mac_script.ipxe.tmpl new file mode 100644 index 0000000..faf030f --- /dev/null +++ b/etc/mac_script.ipxe.tmpl @@ -0,0 +1,14 @@ +#!ipxe +set timeout 0 +set timeout-style hidden +set ISODIR ogLive +set default 0 +set kernelargs ro boot=oginit quiet splash irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=true ogdebug=true ogtmpfs=15 oglivedir=${ISODIR} LANG=es_ES.UTF-8 ip=IP_ADDRESS:192.168.2.1:192.168.2.1:255.255.255.0:HOSTNAME:eth0:none group=Aula_virtual ogrepo=192.168.2.1 oglive=192.168.2.1 oglog=192.168.2.1 ogshare=192.168.2.1 ogprof=false vga=788 +echo "OgLive $ISODIR" +ifopen net0 +route +kernel tftp://SERVERIP/ogLive/ogvmlinuz ${kernelargs} +initrd tftp://SERVERIP/ogLive/oginitrd.img +boot + + diff --git a/installer/ogboot_installer.sh b/installer/ogboot_installer.sh index 0fbdc49..0d8bde6 100644 --- a/installer/ogboot_installer.sh +++ b/installer/ogboot_installer.sh @@ -1,4 +1,249 @@ #!/bin/bash -echo "Hello world" -touch "Hello world" +BRANCH=$1 +CLIENTS=("${@:2}") +if [[ -z $BRANCH ]]; then + BRANCH="main" +fi +GIT_REPO="ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" +# Obtiene el código fuente del proyecto desde el repositorio de GitHub. +function downloadCode() +{ + if [ $# -ne 1 ]; then + echo "${FUNCNAME}(): invalid number of parameters" + exit 1 + fi + + local url="$1" + + echo "${FUNCNAME}(): downloading code from '$url'..." + + GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=accept-new" git archive --remote=$url --format zip --output ogboot.zip --prefix=ogboot/ $BRANCH && unzip ogboot.zip + if [ $? -ne 0 ]; then + echo "${FUNCNAME}(): error getting OpenGnsys code from $url" + return 1 + fi + rm -f ogboot.zip + echo "${FUNCNAME}(): code was downloaded" + return 0 +} + +function checkDependencies() { + echoAndLog "Checking dependencies..." + + # Lista de dependencias + +local DEPENDENCIES=( + php + php-cli + php-fpm + php-json + php-pdo + php-mysql + php-zip + php-gd + php-mbstring + php-curl + php-xml + php-pear + php-bcmath + composer + unzip + apache2 + libapache2-mod-php + subversion + php-ldap + isc-dhcp-server + bittorrent + tftp-hpa + tftpd-hpa + xinetd + build-essential + g++-multilib + wget + curl + graphviz + bittornado + ctorrent + samba + rsync + netpipes + debootstrap + schroot + squashfs-tools + btrfs-tools + procps + arp-scan + realpath + gettext + moreutils + jq + wakeonlan + udpcast + libev-dev + libjansson-dev + libssl-dev + shim-signed + grub-efi-amd64-signed + gawk + libdbi-dev + libdbi1 + automake + liblz4-tool +) + + # Comprobar cada dependencia + for dep in "${DEPENDENCIES[@]}"; do + if ! dpkg -s "$dep" >/dev/null 2>&1; then + echoAndLog "$dep is not installed. Installing..." + sudo apt-get install -y "$dep" + else + echoAndLog "$dep is already installed." + fi + done + + echoAndLog "Dependencies checked." +} + +function add_tftpboot_files() { + # Navegar al directorio ogboot + cd ogboot + + src_dir="tftpboot" + dest_dir="/opt/opengnsys/tftpboot" + + mkdir -p "$dest_dir" + + cp -rn "$src_dir/." "$dest_dir/" + + if [ $? -eq 0 ]; then + echo "Los archivos se añadieron con éxito." + else + echo "Hubo un error al añadir los archivos." + fi + +} + +replace_in_config_kea() { + local config_file="/etc/kea/kea-dhcp4.conf" + + sed -i 's/"boot-file-name": "shimx64.efi.signed"/"boot-file-name": "ipxe.efi"/g' "$config_file" + sed -i 's/"boot-file-name": "grldr"/"boot-file-name": "undionly.kpxe"/g' "$config_file" +} + + +generate_ipxe_script() { + + echo "Generando script IPXE..." + ip_address_server=$(ifconfig eth0 | awk '/inet / {print $2}') + template="etc/dhcp_boot.ipxe.tmpl" + + ipxe_output="/opt/opengnsys/tftpboot/ipxe_scripts/dhcp_boot.ipxe" + + sed "s/SERVERIP/$ip_address_server/g" "$template" > "$ipxe_output" + + template_default="tftpboot/ipxe_scripts/default.ipxe" + + default_output="/opt/opengnsys/tftpboot/ipxe_scripts/default.ipxe" + + mac_script_template="etc/mac_script.ipxe.tmpl" + + sed "s/SERVERIP/$ip_address_server/g" "$template_default" > "$default_output" + echo "Creando ficheros MAC script" + for client_declaration in "${CLIENTS[@]}"; do + eval "$client_declaration" + + hostname="${CLIENT[hostname]}" + mac_address="${CLIENT[mac_address]}" + ip_address="${CLIENT[ip_address]}" + mac_address_lower=$(echo "$mac_address" | tr '[:upper:]' '[:lower:]') + filename="01-${mac_address_lower}" + sed "s/SERVERIP/$ip_address_server/g; s/IP_ADDRESS/$ip_address/g; s/HOSTNAME/$hostname/g; s/MAC_ADDRESS/$mac_address/g" $mac_script_template > "/opt/opengnsys/tftpboot/ipxe_scripts/$filename" + echo "Archivo $filename creado con los parámetros modificados." + done + + + echo "Archivos creados correctamente." + + + +} + + +mount_NFS() { + if sudo mount -t nfs ognartefactos.evlt.uma.es:/ /mnt; then + echo "Sistema NFS montado correctamente." + else + echo "Error: No se pudo montar el sistema NFS." + exit 1 + fi + + ls /mnt/ + + sudo cp -r /mnt/srv/artefactos/ipxe/ /tmp + + cd /tmp/ipxe/src + if sudo make -j 4; then + echo "Directorio /tmp/ipxe/src montado correctamente." + else + echo "Error: No se pudo montar el directorio /tmp/ipxe/src." + exit 1 + fi + + if sudo make bin/undionly.kpxe EMBED=/opt/opengnsys/tftpboot/ipxe_scripts/dhcp_boot.ipxe; then + echo "Fichero de arranque montado correctamente." + else + echo "Error: No se pudo montar el fichero de arranque." + exit 1 + fi + + sudo cp bin/undionly.kpxe /opt/opengnsys/tftpboot + + if sudo make bin-x86_64-efi/ipxe.efi EMBED=/opt/opengnsys/tftpboot/ipxe_scripts/dhcp_boot.ipxe; then + echo "Fichero EFI construido correctamente." + else + echo "Error: No se pudo construir el fichero EFI." + exit 1 + fi + + sudo cp bin-x86_64-efi/ipxe.efi /opt/opengnsys/tftpboot + +} + +checkDependencies + +downloadCode $GIT_REPO +if [ $? -ne 0 ]; then + echo "Error while getting code from the repository" + exit 1 +fi + +add_tftpboot_files +if [ $? -ne 0 ]; then + echo "Error añadiendo ficheros" + exit 1 +fi + + +replace_in_config_kea +if [ $? -ne 0 ]; then + echo "Error en la sustitución de valores en el archivo de configuración de KEA DHCP" + exit 1 +fi + + +generate_ipxe_script +if [ $? -ne 0 ]; then + echo "Error en la generación de scripts IPXE" + exit 1 +fi + + +mount_NFS +if [ $? -ne 0 ]; then + echo "Error en el montaje NFS" + exit 1 +fi + + + diff --git a/tftpboot/ipxe.efi b/tftpboot/ipxe.efi new file mode 100644 index 0000000..3a4c4a9 Binary files /dev/null and b/tftpboot/ipxe.efi differ diff --git a/tftpboot/ipxe_scripts/01-00:50:56:22:11:11 b/tftpboot/ipxe_scripts/01-00:50:56:22:11:11 new file mode 100644 index 0000000..8591b59 --- /dev/null +++ b/tftpboot/ipxe_scripts/01-00:50:56:22:11:11 @@ -0,0 +1,12 @@ +#!ipxe +set timeout 0 +set timeout-style hidden +set ISODIR ogLive +set default 0 +set kernelargs ro boot=oginit quiet splash irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=true ogdebug=true ogtmpfs=15 oglivedir=${ISODIR} LANG=es_ES.UTF-8 ip=192.168.2.11:192.168.2.1:192.168.2.1:255.255.255.0:pc11:eth0:none group=Aula_virtual ogrepo=192.168.2.1 oglive=192.168.2.1 oglog=192.168.2.1 ogshare=192.168.2.1 ogprof=false vga=788 + +kernel tftp://172.17.8.71/ogLive/ogvmlinuz ${kernelargs} +initrd tftp://172.17.8.71/ogLive/oginitrd.img +boot + + diff --git a/tftpboot/ipxe_scripts/default.ipxe b/tftpboot/ipxe_scripts/default.ipxe new file mode 100644 index 0000000..af00c40 --- /dev/null +++ b/tftpboot/ipxe_scripts/default.ipxe @@ -0,0 +1,14 @@ +#!ipxe +set timeout 0 +set timeout-style hidden +set ISODIR ogLive +set default 0 +set kernelargs ro boot=oginit quiet splash irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=true ogdebug=true ogtmpfs=15 oglivedir=${ISODIR} LANG=es_ES.UTF-8 ip=192.168.2.11:192.168.2.1:192.168.2.1:255.255.255.0:pc11:eth0:none group=Aula_virtual ogrepo=192.168.2.1 oglive=192.168.2.1 oglog=192.168.2.1 ogshare=192.168.2.1 ogprof=false vga=788 +echo "OgLive $ISODIR" +ifopen net0 +route +kernel tftp://SERVERIP/ogLive/ogvmlinuz ${kernelargs} +initrd tftp://SERVERIP/ogLive/oginitrd.img +boot + + diff --git a/tftpboot/undionly.kpxe b/tftpboot/undionly.kpxe new file mode 100644 index 0000000..39d4e78 Binary files /dev/null and b/tftpboot/undionly.kpxe differ