Files
comfyui-nvidia/deployments/ai-stack/mirror-ollama-model.sh
T
57_WolveandClaude Opus 4.7 5a34ced8f1 Add S3 mirror path for Ollama models + mirror-ollama-model.sh helper
Three pieces:

1. mirror-ollama-model.sh — run on any machine that has the model
   pulled. Parses the manifest at
   ~/.ollama/models/manifests/registry.ollama.ai/<ns>/<name>/<tag>,
   greps every sha256:* digest, tars manifest + referenced blobs into
   one .tgz. Output is portable — extract over any other Ollama
   data dir and the model is immediately visible.

2. init-models.sh gains an s3_pull function that curls a tarball from
   $S3_OLLAMA_BASE and extracts into /root/.ollama/models/. Falls back
   to ollama pull when S3_OLLAMA_BASE is unset, so s3_pull lines are
   safe to commit before the bucket is ready. huihui_ai/qwen3.5-
   abliterated:9b promoted to s3_pull as the example.

3. docker-compose.yml model-init service propagates S3_OLLAMA_BASE
   from .env. Curl auto-installs at script start because ollama/ollama
   doesn't always ship it.

README documents the mirror workflow under "Mirroring models to S3".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:43:26 -05:00

67 lines
2.4 KiB
Bash

#!/bin/bash
# Mirror an Ollama model into a portable tarball you can upload to S3
# (or any HTTPS host) and re-fetch via init-models.sh's s3_pull.
#
# Run on any machine that already has the model pulled locally — the
# script reads ~/.ollama/models/, parses the manifest to find the
# referenced blobs, and tars them together.
#
# Usage: ./mirror-ollama-model.sh <model:tag> <output.tgz>
# Example: ./mirror-ollama-model.sh huihui_ai/qwen3.5-abliterated:9b qwen3.5-abliterated-9b.tgz
#
# Upload the tarball to S3, then add to init-models.sh:
# s3_pull "huihui_ai/qwen3.5-abliterated:9b" "qwen3.5-abliterated-9b.tgz"
# and set S3_OLLAMA_BASE in .env to your bucket's HTTPS base URL.
set -euo pipefail
MODEL="${1:?Usage: $0 <model:tag> <output.tgz>}"
OUT="${2:?Usage: $0 <model:tag> <output.tgz>}"
OLLAMA_HOME="${OLLAMA_HOME:-$HOME/.ollama}"
MODELS="$OLLAMA_HOME/models"
if ! ollama list | awk 'NR>1 {print $1}' | grep -qx "$MODEL"; then
echo "Model $MODEL not found locally; pulling first..."
ollama pull "$MODEL"
fi
# huihui_ai/qwen3.5-abliterated:9b → manifests/registry.ollama.ai/huihui_ai/qwen3.5-abliterated/9b
ns_and_name="${MODEL%:*}"
tag="${MODEL##*:}"
manifest_rel="manifests/registry.ollama.ai/$ns_and_name/$tag"
manifest_abs="$MODELS/$manifest_rel"
if [ ! -f "$manifest_abs" ]; then
echo "ERROR: manifest not found at $manifest_abs" >&2
exit 1
fi
# Pull every sha256:* digest out of the manifest JSON. Each maps to
# blobs/sha256-<hex>.
blob_files=""
for digest in $(grep -oE 'sha256:[a-f0-9]+' "$manifest_abs" | sort -u); do
blob_rel="blobs/${digest/:/-}"
if [ ! -f "$MODELS/$blob_rel" ]; then
echo "WARNING: missing blob $blob_rel — skipping" >&2
continue
fi
blob_files="$blob_files $blob_rel"
done
count=$(echo "$blob_files" | wc -w | tr -d ' ')
echo "Archiving manifest + $count blob(s)..."
tar -czf "$OUT" -C "$MODELS" "$manifest_rel" $blob_files
size=$(du -h "$OUT" | cut -f1)
echo "Done: $OUT ($size)"
echo
echo "Next:"
echo " 1. Upload to your bucket, e.g."
echo " aws s3 cp $OUT s3://YOUR-BUCKET/ollama-models/ --acl public-read"
echo " (or whatever exposes it over HTTPS)"
echo " 2. Set S3_OLLAMA_BASE in .env to the bucket's HTTPS base, e.g."
echo " S3_OLLAMA_BASE=https://YOUR-BUCKET.s3.amazonaws.com/ollama-models"
echo " 3. Add to init-models.sh:"
echo " s3_pull \"$MODEL\" \"$(basename "$OUT")\""