aiscripts/install_incus_ollama.sh
Antonio de la Rosa d3d3483d0b Fixes in scripts
2025-04-07 17:50:23 +02:00

179 lines
6.1 KiB
Bash

#!/bin/bash
# Script para inicializar Incus (LXD) con configuraciones básicas
# Colores para la salida
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
LOG_FILE="/tmp/incus_ollama_install.log"
# Función para mostrar mensajes de error y salir
die () {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
run_command() {
local cmd="$1"
local description="$2"
echo -e "${YELLOW}Ejecutando: ${description}...${NC}"
echo "Comando: ${cmd}\n" >> "${LOG_FILE}"
eval "${cmd} < /dev/null" >> "${LOG_FILE}" 2>&1
if [ $? -ne 0 ]; then
die "Falló al ejecutar: ${cmd}"
fi
}
echo -e "${YELLOW}Instalando Incus"
# Configuración (puedes modificar estos valores)
STORAGE_POOL="default"
STORAGE_DRIVER="dir" # Puede ser 'btrfs', 'zfs', 'lvm', 'dir', etc.
NETWORK_NAME="incusbr0"
NETWORK_TYPE="bridge"
NETWORK_ADDR="10.0.0.1/24"
NETWORK_DHCP_RANGE="10.0.0.2-10.0.0.254"
NETWORK_DHCP_STATIC="10.0.0.1"
TRUST_PASSWORD="" # Déjalo vacío para no configurar contraseña
echo -e "${YELLOW}Inicializando Incus con la siguiente configuración:${NC}"
echo " - Pool de almacenamiento: $STORAGE_POOL ($STORAGE_DRIVER)"
echo " - Red: $NETWORK_NAME ($NETWORK_TYPE, $NETWORK_ADDR)"
echo -e "${YELLOW}Esto configurará Incus con valores por defecto.${NC}"
# Confirmar antes de continuar
#read -p "¿Deseas continuar? (y/N) " -n 1 -r
#echo
#if [[ ! $REPLY =~ ^[Yy]$ ]]; then
# echo -e "${GREEN}Operación cancelada por el usuario.${NC}"
# exit 0
#fi
# Ejecutar incus admin init con las configuraciones
echo -e "${YELLOW}Inicializando Incus...${NC}"
cat <<EOF | incus admin init --minimal
config:
core.https_address: '[::]:8443'
core.trust_password: ${TRUST_PASSWORD}
storage_pools:
- name: ${STORAGE_POOL}
driver: ${STORAGE_DRIVER}
networks:
- name: ${NETWORK_NAME}
type: ${NETWORK_TYPE}
config:
ipv4.address: ${NETWORK_ADDR}
ipv4.nat: "true"
ipv6.address: none
profiles:
- name: default
devices:
root:
path: /
pool: ${STORAGE_POOL}
type: disk
eth0:
name: eth0
network: ${NETWORK_NAME}
type: nic
EOF
# Verificar si la inicialización fue exitosa
if [ $? -eq 0 ]; then
echo -e "${GREEN}¡Incus se ha inicializado correctamente!${NC}"
echo -e "Puedes ver la información con: ${YELLOW}incus info${NC}"
else
die "Hubo un problema al inicializar Incus"
fi
# Install ollama container
echo "Create ollama container"
IMAGE="images:debian/12"
#incus launch images:debian/12 ollama-container
#incus launch "${IMAGE}" "ollama-container" < /dev/null
run_command "incus launch ${IMAGE} ollama-container" "Crear contenedor Ollama"
echo -e "${YELLOW}Adding gpu to container${NC}"
incus restart ollama-container
incus config device add ollama-container gpu gpu
# Install nvidia
run_command "incus exec ollama-container -- apt install -y software-properties-common gpg wget curl" "Add software properties package and other utilities"
run_command "incus exec ollama-container -- apt-add-repository -y contrib" "Add contrib"
run_command "incus exec ollama-container -- apt-add-repository -y non-free" "Add non-free"
run_command "incus exec ollama-container -- apt-add-repository -y non-free-firmware" "Add non-free-firmware"
run_command "incus exec ollama-container -- bash -c 'echo \"deb http://deb.debian.org/debian bookworm-backports main contrib non-free non-free-firmware\" > /etc/apt/sources.list.d/backports.list'" "Añadir repo backport"
run_command "incus exec ollama-container -- bash -c 'curl -fsSL https://pkgs.zabbly.com/key.asc | gpg --show-keys --fingerprint'" "Añadir gpg incus"
run_command "incus exec ollama-container -- mkdir -p /etc/apt/keyrings/" "Añadir keyrings"
run_command "incus exec ollama-container -- curl -fsSL https://pkgs.zabbly.com/key.asc -o /etc/apt/keyrings/zabbly.asc" "Añadir gpg zabbly en /et/apt/keyrings"
run_command "incus exec ollama-container -- bash -c 'cat <<EOF > /etc/apt/sources.list.d/zabbly-incus-stable.sources
Enabled: yes
Types: deb
URIs: https://pkgs.zabbly.com/incus/stable
Suites: $(. /etc/os-release && echo ${VERSION_CODENAME})
Components: main
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/zabbly.asc
EOF'" "Añadir incus stable repo"
run_command "incus exec ollama-container -- bash -c 'apt-get update && apt-get upgrade'" "Upgrade repos...'"
# Install new kernel.
run_command "incus exec ollama-container -- apt-get -y install linux-image-6.11.10+bpo-amd64 linux-headers-6.11.10+bpo-amd64" "Add ollama container"
# Install cuda nvidia drivers
echo "Install nvidia drivers in container"
run_command "incus exec ollama-container -- apt -y purge \"*nvidia*\"" "Delete nvidia"
run_command "incus exec ollama-container -- wget https://developer.download.nvidia.com/compute/cuda/repos/debian12/x86_64/cuda-keyring_1.1-1_all.deb" "Download cuda keyring"
run_command "incus exec ollama-container -- dpkg -i cuda-keyring_1.1-1_all.deb" "Install cuda keyring..."
run_command "incus exec ollama-container -- apt-get -y -f install" "Update install..."
run_command "incus exec ollama-container -- apt update" "Update repositories..."
run_command "incus exec ollama-container -- bash -c 'echo \"deb [signed-by=/usr/share/keyrings/cuda-archive-keyring.gpg] https://developer.download.nvidia.com/compute/cuda/repos/debian12/x86_64/ /\" | tee /etc/apt/sources.list.d/cuda-debian12-x86_64.list'" "Install GPG cuda"
run_command "incus exec ollama-container -- apt -y update" "Update Cuda drivers repo"
run_command "incus exec ollama-container -- bash -c 'DEBIAN_FRONTEND=noninteractive apt-get -y --force-yes install cuda-drivers'" "Installing cuda drivers"
echo "Install Ollama in container ollama-container"
#run_command "incus exec ollama-container -- apt -y install curl " ""
run_command "incus exec ollama-container -- bash -c 'curl -fsSL https://ollama.com/install.sh | sh'" "Installing Ollama..."
run_command "incus exec ollama-container -- ollama pull gemma3:4b" "Installing model gemma3:4b..."
echo -e "${GREEN}Done!!!${NC}"
sleep 3
echo "You can enter to ollama container with incus exec ollama-container -- /bin/bash"
echo "Use journalctl -u ollama.service command for check if ollama is working"