Skip to content

Commit 89a11e1

Browse files
authored
fix(single-binary): bundle ld.so (#2602)
* debug * fix copy command/silly muscle memory Signed-off-by: Ettore Di Giacinto <[email protected]> * remove tmate * Debugging * Start binary with ld.so if present in libdir Signed-off-by: Ettore Di Giacinto <[email protected]> * small refactor Signed-off-by: Ettore Di Giacinto <[email protected]> --------- Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 06de542 commit 89a11e1

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

.github/workflows/release.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ jobs:
102102
export PATH=/usr/local/cuda/bin:$PATH
103103
sudo rm -rf /usr/aarch64-linux-gnu/lib/libstdc++.so.6
104104
sudo cp -rf /usr/aarch64-linux-gnu/lib/libstdc++.so* /usr/aarch64-linux-gnu/lib/libstdc++.so.6
105+
sudo cp /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 ld.so
105106
GO_TAGS=p2p \
106-
BACKEND_LIBS="./grpc/cmake/cross_build/third_party/re2/libre2.a ./grpc/cmake/cross_build/libgrpc.a ./grpc/cmake/cross_build/libgrpc++.a ./grpc/cmake/cross_build/third_party/protobuf/libprotobuf.a /usr/aarch64-linux-gnu/lib/libc.so.6 /usr/aarch64-linux-gnu/lib/libstdc++.so.6 /usr/aarch64-linux-gnu/lib/libgomp.so.1 /usr/aarch64-linux-gnu/lib/libm.so.6 /usr/aarch64-linux-gnu/lib/libgcc_s.so.1 /usr/aarch64-linux-gnu/lib/libdl.so.2 /usr/aarch64-linux-gnu/lib/libpthread.so.0" \
107+
BACKEND_LIBS="./grpc/cmake/cross_build/third_party/re2/libre2.a ./grpc/cmake/cross_build/libgrpc.a ./grpc/cmake/cross_build/libgrpc++.a ./grpc/cmake/cross_build/third_party/protobuf/libprotobuf.a /usr/aarch64-linux-gnu/lib/libc.so.6 /usr/aarch64-linux-gnu/lib/libstdc++.so.6 /usr/aarch64-linux-gnu/lib/libgomp.so.1 /usr/aarch64-linux-gnu/lib/libm.so.6 /usr/aarch64-linux-gnu/lib/libgcc_s.so.1 /usr/aarch64-linux-gnu/lib/libdl.so.2 /usr/aarch64-linux-gnu/lib/libpthread.so.0 ./ld.so" \
107108
GOOS=linux \
108109
GOARCH=arm64 \
109110
CMAKE_ARGS="-DProtobuf_INCLUDE_DIRS=$CROSS_STAGING_PREFIX/include -DProtobuf_DIR=$CROSS_STAGING_PREFIX/lib/cmake/protobuf -DgRPC_DIR=$CROSS_STAGING_PREFIX/lib/cmake/grpc -DCMAKE_TOOLCHAIN_FILE=$CMAKE_CROSS_TOOLCHAIN -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++" make dist-cross-linux-arm64
@@ -212,8 +213,9 @@ jobs:
212213
export PATH=/usr/local/cuda/bin:$PATH
213214
export PATH=/opt/rocm/bin:$PATH
214215
source /opt/intel/oneapi/setvars.sh
216+
sudo cp /lib64/ld-linux-x86-64.so.2 ld.so
215217
GO_TAGS=p2p \
216-
BACKEND_LIBS="/usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libm.so.6 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 /usr/lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libgomp.so.1" \
218+
BACKEND_LIBS="./ld.so /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libm.so.6 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 /usr/lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libgomp.so.1" \
217219
make -j4 dist
218220
- uses: actions/upload-artifact@v4
219221
with:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ build: prepare backend-assets grpcs ## Build the project
315315
$(info ${GREEN}I LD_FLAGS: ${YELLOW}$(LD_FLAGS)${RESET})
316316
ifneq ($(BACKEND_LIBS),)
317317
$(MAKE) backend-assets/lib
318-
cp -r $(BACKEND_LIBS) backend-assets/lib/
318+
cp $(BACKEND_LIBS) backend-assets/lib/
319319
endif
320320
CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o $(BINARY_NAME) ./
321321

core/cli/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (mi *ModelsInstall) Run(ctx *cliContext.Context) error {
5757
if err := json.Unmarshal([]byte(mi.Galleries), &galleries); err != nil {
5858
log.Error().Err(err).Msg("unable to load galleries")
5959
}
60+
6061
for _, modelName := range mi.ModelArgs {
6162

6263
progressBar := progressbar.NewOptions(

pkg/library/dynaload.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ import (
55
"os"
66
"path/filepath"
77
"runtime"
8+
9+
"github.com/rs/zerolog/log"
810
)
911

12+
/*
13+
This file contains functions to load libraries from the asset directory to keep the business logic clean.
14+
*/
15+
16+
// skipLibraryPath checks if LOCALAI_SKIP_LIBRARY_PATH is set
17+
var skipLibraryPath = os.Getenv("LOCALAI_SKIP_LIBRARY_PATH") != ""
18+
19+
// LoadExtractedLibs loads the extracted libraries from the asset dir
1020
func LoadExtractedLibs(dir string) {
11-
// Skip this if LOCALAI_SKIP_LIBRARY_PATH is set
12-
if os.Getenv("LOCALAI_SKIP_LIBRARY_PATH") != "" {
21+
if skipLibraryPath {
1322
return
1423
}
1524

@@ -18,9 +27,38 @@ func LoadExtractedLibs(dir string) {
1827
}
1928
}
2029

30+
// LoadLDSO checks if there is a ld.so in the asset dir and if so, prefixes the grpc process with it.
31+
// In linux, if we find a ld.so in the asset dir we prefix it to run with the libs exposed in
32+
// LD_LIBRARY_PATH for more compatibility
33+
// If we don't do this, we might run into stack smash
34+
// See also: https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/851229#851229
35+
// In this case, we expect a ld.so in the lib asset dir.
36+
// If that's present, we use it to run the grpc backends as supposedly built against
37+
// that specific version of ld.so
38+
func LoadLDSO(assetDir string, args []string, grpcProcess string) ([]string, string) {
39+
if skipLibraryPath {
40+
return args, grpcProcess
41+
}
42+
43+
if runtime.GOOS != "linux" {
44+
return args, grpcProcess
45+
}
46+
47+
// Check if there is a ld.so file in the assetDir, if it does, we need to run the grpc process with it
48+
ldPath := filepath.Join(assetDir, "backend-assets", "lib", "ld.so")
49+
if _, err := os.Stat(ldPath); err == nil {
50+
log.Debug().Msgf("ld.so found")
51+
// We need to run the grpc process with the ld.so
52+
args = append(args, grpcProcess)
53+
grpcProcess = ldPath
54+
}
55+
56+
return args, grpcProcess
57+
}
58+
59+
// LoadExternal sets the LD_LIBRARY_PATH to include the given directory
2160
func LoadExternal(dir string) {
22-
// Skip this if LOCALAI_SKIP_LIBRARY_PATH is set
23-
if os.Getenv("LOCALAI_SKIP_LIBRARY_PATH") != "" {
61+
if skipLibraryPath {
2462
return
2563
}
2664

pkg/model/initializers.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
grpc "github.com/go-skynet/LocalAI/pkg/grpc"
14+
"github.com/go-skynet/LocalAI/pkg/library"
1415
"github.com/go-skynet/LocalAI/pkg/xsysinfo"
1516
"github.com/klauspost/cpuid/v2"
1617
"github.com/phayes/freeport"
@@ -326,8 +327,13 @@ func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string
326327
return "", fmt.Errorf("failed allocating free ports: %s", err.Error())
327328
}
328329

329-
// Make sure the process is executable
330-
if err := ml.startProcess(grpcProcess, o.model, serverAddress); err != nil {
330+
args := []string{}
331+
332+
// Load the ld.so if it exists
333+
args, grpcProcess = library.LoadLDSO(o.assetDir, args, grpcProcess)
334+
335+
// Make sure the process is executable in any circumstance
336+
if err := ml.startProcess(grpcProcess, o.model, serverAddress, args...); err != nil {
331337
return "", err
332338
}
333339

pkg/model/process.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (ml *ModelLoader) GetGRPCPID(id string) (int, error) {
6969
return strconv.Atoi(p.PID)
7070
}
7171

72-
func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string) error {
72+
func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string, args ...string) error {
7373
// Make sure the process is executable
7474
if err := os.Chmod(grpcProcess, 0700); err != nil {
7575
return err
@@ -82,7 +82,7 @@ func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string
8282
grpcControlProcess := process.New(
8383
process.WithTemporaryStateDir(),
8484
process.WithName(grpcProcess),
85-
process.WithArgs("--addr", serverAddress),
85+
process.WithArgs(append(args, []string{"--addr", serverAddress}...)...),
8686
process.WithEnvironment(os.Environ()...),
8787
)
8888

0 commit comments

Comments
 (0)