Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I package a Go program so that it is self sufficient?

Tags:

linux

go

cisco

I have a Go Program and I want to run it on a switch. Since I cannot install Go on the switch itself, I just copy the executable and try to run. But I get the following error.

runtime: panic before malloc heap initialized
fatal error: runtime: cannot reserve arena virtual address space

runtime stack:
runtime.throw(0x8149b8b)
        /usr/local/go/src/pkg/runtime/panic.c:520 +0x71
runtime.mallocinit()
        /usr/local/go/src/pkg/runtime/malloc.goc:552 +0xf2
runtime.schedinit()
        /usr/local/go/src/pkg/runtime/proc.c:150 +0x3a
_rt0_go()
        /usr/local/go/src/pkg/runtime/asm_386.s:95 +0xf6`

How do I package the Go executable with all it's dependencies?

EDIT 1: Here is the ulimit -a dump.

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 40960
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) 395067
file locks                      (-x) unlimited
like image 941
Rahul Avatar asked Dec 09 '14 04:12

Rahul


People also ask

How do I run a .go file?

To run a Go program (assuming you have installed Go on your system), you need to instruct the Go compiler to compile and run a program using go run command with the relative or absolute path of the Go program file.

How do you run a go binary?

The Go binaries are installed per default under /usr/local/go (Mac). You can verify the installation via the execution of the command go version in a fresh terminal. If go isn't already being found by the CLI you have to manually add /usr/local/go/bin to the PATH variable of your system.


Video Answer


1 Answers

TL;DR

Your Go app is not being able to allocate virtual memory to run. I've never developed for a switch before but if it's running linux or a unix variant, check group/user permissions and ulimit values to check if that user has any kind of restriction. Maybe this question might be of help

Longer version

So, your problem here is not go not being able to run without the go development environment because you really don't need it. Go is known for generating static binaries that by definition are self contained and don't depend on other libraries to run.

If you take a better look at your error message, you'll notice that it says:

"cannot reserve arena virtual address space"

You might be asking yourself "what is this arena?"

I quick look at malloc's source code gives us a hint:

Set up the allocation arena, a contiguous area of memory where allocated data will be found. The arena begins with a bitmap large enough to hold 4 bits per allocated word.

If you go through that source code you'll find your error message around here.

The runtime·SysReserve C function is the one that actually tries to reserve the virtual address space for the arena. If it can't allocate that, it will throw that error.

You can find code for the Linux implementation of it here.

As go normally tries to avoid big allocations as the might fail right away, if your user can't allocate something as small as 64K, it means your user has tight restrictions. As I have no idea which OS your switch is running and have no experience developing for them I can't go any further than this.

If you can provide more information, I can try to update this answer accordingly.

like image 145
DallaRosa Avatar answered Oct 17 '22 02:10

DallaRosa