Skip to content

Commit d19915f

Browse files
committed
hack: add archive build script and update install.sh to support archive install (#4)
Signed-off-by: Xiaodong Ye <[email protected]>
1 parent 294b45c commit d19915f

File tree

2 files changed

+167
-7
lines changed

2 files changed

+167
-7
lines changed

scripts/build_linux_musa.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
# Script to duplicate the linux-build workflow from GitHub Actions for MUSA builds
6+
# Usage: ./scripts/build_linux_musa.sh [OS] [ARCH] [TARGET]
7+
# Examples:
8+
# ./scripts/build_linux_musa.sh linux amd64 archive
9+
# ./scripts/build_linux_musa.sh linux arm64 archive
10+
11+
# Default values (can be overridden by command line arguments)
12+
OS=${1:-linux}
13+
ARCH=${2:-amd64}
14+
TARGET=${3:-archive}
15+
16+
echo "Building Ollama Linux MUSA Release"
17+
echo "OS: $OS"
18+
echo "ARCH: $ARCH"
19+
echo "TARGET: $TARGET"
20+
echo "Platform: $OS/$ARCH"
21+
22+
# Set up environment variables (similar to setup-environment job)
23+
VERSION=${VERSION:-$(git describe --tags --first-parent --abbrev=7 --long --dirty --always | sed -e "s/^v//g")}
24+
GOFLAGS="'-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=$VERSION\" \"-X=github.com/ollama/ollama/server.mode=release\"'"
25+
26+
echo "VERSION: $VERSION"
27+
echo "GOFLAGS: $GOFLAGS"
28+
29+
# Cleanup previous build files
30+
echo "Cleaning up previous build files..."
31+
DIST_DIR="dist/$OS-$ARCH"
32+
33+
# Remove previous build artifacts
34+
rm -f *.tgz
35+
rm -f *.tar.in
36+
echo "Removed previous .tgz and .tar.in files"
37+
38+
# Remove previous dist directory
39+
if [ -d "$DIST_DIR" ]; then
40+
rm -rf "$DIST_DIR"
41+
echo "Removed previous dist directory: $DIST_DIR"
42+
fi
43+
44+
# Create fresh dist directory
45+
mkdir -p "$DIST_DIR"
46+
echo "Created fresh dist directory: $DIST_DIR"
47+
48+
echo "Building Docker image with buildx..."
49+
50+
# Build using Docker (equivalent to docker/build-push-action@v6)
51+
echo "Starting Docker build..."
52+
docker buildx build \
53+
--platform="$OS/$ARCH" \
54+
--target="$TARGET" \
55+
--build-arg="GOFLAGS=$GOFLAGS" \
56+
--build-arg="CGO_CFLAGS=${CGO_CFLAGS:-}" \
57+
--build-arg="CGO_CXXFLAGS=${CGO_CXXFLAGS:-}" \
58+
--build-arg="FLAVOR=musa" \
59+
--output="type=local,dest=$DIST_DIR" \
60+
.
61+
62+
echo "Build completed. Organizing artifacts..."
63+
64+
# Change to dist directory for artifact organization
65+
cd "$DIST_DIR"
66+
67+
# Organize built components into different archives (equivalent to the shell script in workflow)
68+
if [ -d "bin" ] || [ -d "lib/ollama" ]; then
69+
for COMPONENT in bin/* lib/ollama/*; do
70+
if [ -e "$COMPONENT" ]; then
71+
case "$COMPONENT" in
72+
bin/ollama) echo "$COMPONENT" >> "ollama-$OS-$ARCH.tar.in" ;;
73+
lib/ollama/*.so) echo "$COMPONENT" >> "ollama-$OS-$ARCH.tar.in" ;;
74+
lib/ollama/musa_v4) echo "$COMPONENT" >> "ollama-$OS-$ARCH.tar.in" ;;
75+
*) echo "Skipping $COMPONENT" ;;
76+
esac
77+
fi
78+
done
79+
else
80+
echo "Warning: No bin/ or lib/ollama/ directories found in build output"
81+
echo "Available files/directories:"
82+
ls -la
83+
fi
84+
85+
# Go back to root directory
86+
cd - > /dev/null
87+
88+
# Create compressed archives (equivalent to the tar/pigz command in workflow)
89+
echo "Creating compressed archives..."
90+
91+
for ARCHIVE in "$DIST_DIR"/*.tar.in; do
92+
if [ -f "$ARCHIVE" ]; then
93+
ARCHIVE_NAME=$(basename "${ARCHIVE/.tar.in/.tgz}")
94+
echo "Creating $ARCHIVE_NAME..."
95+
96+
# Use gzip if pigz is not available
97+
if command -v pigz > /dev/null; then
98+
COMPRESS_CMD="pigz -9vc"
99+
else
100+
COMPRESS_CMD="gzip -9c"
101+
fi
102+
103+
tar c -C "$DIST_DIR" -T "$ARCHIVE" --owner 0 --group 0 | $COMPRESS_CMD > "$ARCHIVE_NAME"
104+
echo "Created: $ARCHIVE_NAME"
105+
fi
106+
done
107+
108+
echo "Build artifacts created:"
109+
ls -la *.tgz 2>/dev/null || echo "No .tgz files found"
110+
111+
echo ""
112+
echo "Linux MUSA build completed successfully!"
113+
echo "Artifacts are available in the current directory as .tgz files"
114+
echo "Build output is in: $DIST_DIR"

scripts/install.sh

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,43 @@ esac
4747

4848
VER_PARAM="${OLLAMA_VERSION:+?version=$OLLAMA_VERSION}"
4949

50+
# GitHub repository for MooreThreads Ollama MUSA
51+
GITHUB_REPO="MooreThreads/ollama-musa"
52+
GITHUB_API_URL="https://api.github.com/repos/${GITHUB_REPO}"
53+
54+
# Function to get the latest release version
55+
get_latest_version() {
56+
if [ -n "${OLLAMA_VERSION:-}" ]; then
57+
echo "${OLLAMA_VERSION}"
58+
else
59+
# Try to fetch the latest release from GitHub API
60+
status "Fetching latest release from GitHub..."
61+
LATEST_VERSION=$(curl -s --fail "${GITHUB_API_URL}/releases/latest" 2>/dev/null | grep '"tag_name":' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/' | head -1)
62+
if [ -n "$LATEST_VERSION" ] && [ "$LATEST_VERSION" != "null" ]; then
63+
echo "$LATEST_VERSION"
64+
else
65+
# Fallback: try to get the latest tag
66+
status "Falling back to latest tag..."
67+
LATEST_VERSION=$(curl -s --fail "${GITHUB_API_URL}/tags" 2>/dev/null | grep '"name":' | head -1 | sed 's/.*"name": *"\([^"]*\)".*/\1/')
68+
if [ -n "$LATEST_VERSION" ] && [ "$LATEST_VERSION" != "null" ]; then
69+
echo "$LATEST_VERSION"
70+
else
71+
error "Failed to fetch latest version from GitHub API. Please set OLLAMA_VERSION environment variable."
72+
fi
73+
fi
74+
fi
75+
}
76+
77+
# Get the version to download
78+
VERSION=$(get_latest_version)
79+
status "Using version: $VERSION"
80+
81+
# Function to construct GitHub release download URL
82+
get_download_url() {
83+
local filename="$1"
84+
echo "https://github.com/${GITHUB_REPO}/releases/download/${VERSION}/${filename}"
85+
}
86+
5087
SUDO=
5188
if [ "$(id -u)" -ne 0 ]; then
5289
# Running as root, no need for sudo
@@ -78,9 +115,11 @@ fi
78115
status "Installing ollama to $OLLAMA_INSTALL_DIR"
79116
$SUDO install -o0 -g0 -m755 -d $BINDIR
80117
$SUDO install -o0 -g0 -m755 -d "$OLLAMA_INSTALL_DIR/lib/ollama"
81-
status "Downloading Linux ${ARCH} bundle"
118+
status "Downloading Linux ${ARCH} MUSA bundle"
119+
DOWNLOAD_URL=$(get_download_url "ollama-linux-${ARCH}.tgz${VER_PARAM}")
120+
status "Download URL: $DOWNLOAD_URL"
82121
curl --fail --show-error --location --progress-bar \
83-
"https://ollama.com/download/ollama-linux-${ARCH}.tgz${VER_PARAM}" | \
122+
"$DOWNLOAD_URL" | \
84123
$SUDO tar -xzf - -C "$OLLAMA_INSTALL_DIR"
85124

86125
if [ "$OLLAMA_INSTALL_DIR/bin/ollama" != "$BINDIR/ollama" ] ; then
@@ -189,12 +228,12 @@ fi
189228

190229
# Install GPU dependencies on Linux
191230
if ! available lspci && ! available lshw; then
192-
warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
231+
warning "Unable to detect NVIDIA/AMD/MTHREADS GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
193232
exit 0
194233
fi
195234

196235
check_gpu() {
197-
# Look for devices based on vendor ID for NVIDIA and AMD
236+
# Look for devices based on vendor ID for NVIDIA, AMD and MTHREADS
198237
case $1 in
199238
lspci)
200239
case $2 in
@@ -205,8 +244,10 @@ check_gpu() {
205244
case $2 in
206245
nvidia) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[10DE\]' || return 1 ;;
207246
amdgpu) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[1002\]' || return 1 ;;
247+
mtgpu) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[1ED5\]' || return 1 ;;
208248
esac ;;
209249
nvidia-smi) available nvidia-smi || return 1 ;;
250+
mthreads-gmi) available mthreads-gmi || return 1 ;;
210251
esac
211252
}
212253

@@ -215,9 +256,14 @@ if check_gpu nvidia-smi; then
215256
exit 0
216257
fi
217258

218-
if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
259+
if check_gpu mthreads-gmi; then
260+
status "MTHREADS GPU installed."
261+
exit 0
262+
fi
263+
264+
if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu && ! check_gpu lshw mtgpu; then
219265
install_success
220-
warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode."
266+
warning "No NVIDIA/AMD/MTHREADS GPU detected. Ollama will run in CPU-only mode."
221267
exit 0
222268
fi
223269

@@ -239,7 +285,7 @@ CUDA_REPO_ERR_MSG="NVIDIA GPU detected, but your OS and Architecture are not sup
239285
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
240286
install_cuda_driver_yum() {
241287
status 'Installing NVIDIA repository...'
242-
288+
243289
case $PACKAGE_MANAGER in
244290
yum)
245291
$SUDO $PACKAGE_MANAGER -y install yum-utils

0 commit comments

Comments
 (0)