NetBSD: Running Everything with COMPAT_LINUX
NetBSD's portability is legendary, but sometimes you just need to run a proprietary Linux binary (looking at you, Netscape). Instead of a slow VM, NetBSD uses kernel-level binary emulation. This isn't emulation in the "slow" sense; it's a high-performance syscall translation layer.
How it Works
When the kernel loads an ELF binary, it checks the brand. If it's a Linux binary, it switches the process to the Linux emulation vector. Every time the process makes a syscall, the kernel redirects it to a translation function.
Enabling Emulation
First, you need COMPAT_LINUX in your kernel config:
options COMPAT_LINUX
options EXEC_ELF32
And you need the Linux libraries in /emul/linux.
A Look at the Translation
Inside the kernel source (sys/compat/linux/common/linux_socket.c), you'll see how NetBSD maps Linux socket calls to native ones:
/* Linux socketcall syscall */
int
linux_sys_socketcall(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_socketcall_args /* {
syscallarg(int) what;
syscallarg(void *) args;
} */ *uap = v;
switch (SCARG(uap, what)) {
case LINUX_SYS_SOCKET:
return linux_sys_socket(p, SCARG(uap, args), retval);
case LINUX_SYS_BIND:
return linux_sys_bind(p, SCARG(uap, args), retval);
/* ... and so on ... */
}
return (ENOSYS);
}
The overhead is minimal-just a few extra instructions to swap structure layouts. This is why NetBSD is often a better "Linux" than Linux itself when it comes to stability and flexibility. If it exists, NetBSD runs it.
Aunimeda provides DevOps engineering and infrastructure services - CI/CD pipelines, containerization, cloud deployments, and monitoring setups.
Contact us to discuss your infrastructure needs. See also: DevOps Services, Custom Software Development