#!/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 } if [ "$USER" = 'root' ]; then die "Ejecuta este script como usuario normal con acceso a sudo y no root" 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 < /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 < /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"