

Nerd Sniped by BINFMT_MISC
source link: https://blog.jessfraz.com/post/nerd-sniped-by-binfmt_misc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nerd Sniped by BINFMT_MISC
This is a story about how I got nerd sniped by a blog post from Cloudflare Engineering. The TLDR on their post is that you can script in Go if you use BINFMT_MISC in the kernel.
BINFMT_MISC is really well documented and awesome. In the end, all they had to do to script in Go was to mount the filesystem:
$ mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
Then, register the Go script binary format:
$ echo ':golang:E::go::/usr/local/bin/gorun:OC' | sudo tee /proc/sys/fs/binfmt_misc/register
:golang:E::go::/usr/local/bin/gorun:OC
Then you can ./
any go file on your host:
$ chmod u+x helloscript.go
$ ./helloscript.go
Hello, world!
They go through all the extraordinary details of exit codes for the shell and blah blah blah. It’s a great post you should really read it. Do it, go read it, then come back here and I will take it to 11.
Okay, cool, you are back. That post was dope right?
I kinda want to do this with all languages. Because I LOVE SCRIPTING. Have you seen my cloud native dotfiles? My bash scripts smell like roses.
Right, so I want to do this with all languages… but what I also hate is installing shit on my host. Ew, we have containers for those silly things. Luckily, I know a thing or two about containers…
A few years ago I made a project called
binctr.
It creates fully static, unprivileged, self-contained, containers as
executable binaries. (Wow that was a lot of words, let’s break it down.) What
binctr
does is embed an entire container image (aka rootfs) into a fully
static binary and when you execute the binary it will unpack the image and run
it as a container. So you get containers without a daemon or privileges and
without even having the image for the rootfs of the container. You just need
this one binary.
(Huge thanks to @lordcyphar who got rootless
containers into runc so I could actually archive my gross hack for binctr
.)
Kinda seems like the perfect match for trying to use all languages with BINFMT_MISC. So I tried it.
(Preface: this post should not be tried at home, which is why I did not
unarchive binctr
, I am merely showing a different, very crazy abstraction).
I put common lisp in a container. Why common lisp? Well I could do this with any language and I’m a bit insane haven’t you noticed…
Then I embedded the image into a binary with binctr
. I made one slight
modification to the spec in binctr
that allowed me to use local files,
basically so I could get the script into the container after the executable is
run pointing to the file.
Then I registered my common lisp binary format with BINFMT_MISC…
$ echo ':clisp:E::lisp::/usr/local/bin/clisp:OC' | sudo tee /proc/sys/fs/binfmt_misc/register
:clisp:E::lisp::/usr/local/bin/clisp:OC
/usr/local/bin/clisp
is just my binctr
generated binary with common lisp.
And boom, now I can “dot slash” any .lisp
file and it will run in my common
lisp container.
Obviously, my container needed to be packaged with any dependencies and packages I needed but I didn’t need to install any of that shit on my host so I consider it a win.
Imagine if an entire OS had all the languages packaged this way so that everything could be “dot slashed” and executed but without actually installing the language to your host operating system.
I think it would be dope.
Thanks for tuning in for this crazy blog post. Catch ya later. Hacker news, you can shove your comments right up your
Recommend
-
63
-
12
Nerd Humor - When You Blog in Bright Sunlight in a Convertible Jul 31, 2019 Disclaimer: There is likely only one person on the planet who will find this funny. Hi! So I found myself last night in a co...
-
29
Best Nerd Humor Ever Bad Horse Nov 18, 2016 Run to a terminal and type in: traceroute bad.horse But in case you're busy then just look below:
-
10
Dated Nerd Humor - Klingon Open Source Aug 31, 2016 I dug up an archive of an old server and found some stuff I wrote back in 2000 (pre 9/11 actually). Mildly amusing at least if you're old enough. I should...
-
13
binfmt概念空间建模准备要做一种新的二进制格式,本文对Linux二进制格式,特别是这个格式和它的执行单元直接的关系做一个概念建模。本分析基于5.5主线内核。Linux对每种二进制格式进行抽象,抽象的接口叫linux_binfmt(以下简称binfmt)...
-
8
Contributing to a Political Campaign as a Nerd As I promised in my previous politics article, I will continue not to advocate for specific poli...
-
12
Jan 25 2019 binfmt-support 2.2.0 I’ve released binfmt-support 2.2.0. These are the...
-
6
How I got nerd sniped into benchmarking legacy x86 instructions Posted on July 27, 2019 ...
-
5
Productivity-sniped by PARA How I’m using the PARA method in Notion April 22, 2022 In February, I got
-
10
利用 qemu user 模式和 binfmt_misc 构建其他架构的 docker 镜像 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK