ogboot/installer/ogboot_installer.sh

160 lines
3.8 KiB
Bash

#!/bin/bash
echo "Hello world"
touch "Hello world"
BRANCH=$1
MAC_ADDRESSES=$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 add_tftpboot_files() {
# Navegar al directorio ogboot
cd ogboot
src_dir="tftpboot"
dest_dir="/opt/opengnsys/tftpboot"
# Crear el directorio de destino si no existe
mkdir -p "$dest_dir"
# Copiar los archivos sin sobrescribir los existentes
cp -rn "$src_dir/." "$dest_dir/"
# Comprobar si la copia fue exitosa
if [ $? -eq 0 ]; then
echo "Los archivos se añadieron con éxito."
else
echo "Hubo un error al añadir los archivos."
fi
# Volver al directorio original
}
generate_ipxe_script() {
echo "Generando script IPXE..."
ip_address=$(ifconfig eth0 | awk '/inet / {print $2}')
ls -lha
pwd
template="etc/dhcp_boot.ipxe.tmpl"
ipxe_output="/opt/opengnsys/tftpboot/ipxe_scripts/dhcp_boot.ipxe"
# Reemplazar SERVERIP con la dirección IP en la plantilla y guardarla en el archivo de salida
sed "s/SERVERIP/$ip_address/g" "$template" > "$ipxe_output"
echo "Archivo creado con éxito en $ipxe_output"
template_default="tftpboot/ipxe_scripts/default.ipxe"
default_output="/opt/opengnsys/tftpboot/ipxe_scripts/default.ipxe"
sed "s/SERVERIP/$ip_address/g" "$template_default" > "$default_output"
echo "Creando ficheros MAC script"
ls -lh
for mac_address in $MAC_ADDRESSES; do
mac_address_lower=$(echo "$mac_address" | tr '[:upper:]' '[:lower:]')
filename="01-${mac_address_lower}"
echo "Creando archivo $filename..."
cp /opt/opengnsys/tftpboot/ipxe_scripts/default.ipxe /opt/opengnsys/tftpboot/ipxe_scripts/$filename
done
}
# Definir la función
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
}
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
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