TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Writing my own init with Go

149 pointsby CSDudeover 9 years ago

13 comments

jabagonutsover 9 years ago
&gt; The reason that I wrote sleep 3 seconds is that I want to see how the kernel reacts when init dies.<p>That was my favorite line in the entire post. I love seeing this kind of fearless experimentation.<p>When I first learned to program, I desperately wanted everything to work and would try very hard not to break things. As I&#x27;ve matured into senior roles, I spend of a lot of effort creating &quot;safe&quot; development and testing environments like the author did here, just so that I can break things and make observations and then throw the environment away when I&#x27;ve learned what I needed to learn. Tools like vagrant and cheap cloud computing have made this easier to do than ever.
评论 #11066284 未加载
chubotover 9 years ago
NOTE: You can&#x27;t write an init without threads in Go, as you can in C. (I tried awhile back)<p>At least you can&#x27;t with its portable interfaces such as exec.Command() and cmd.Wait():<p><a href="http:&#x2F;&#x2F;www.mustafaak.in&#x2F;2016&#x2F;02&#x2F;09&#x2F;forking-process-in-myinit-go.html" rel="nofollow">http:&#x2F;&#x2F;www.mustafaak.in&#x2F;2016&#x2F;02&#x2F;09&#x2F;forking-process-in-myinit...</a><p>You end burning a thread <i>per process</i>, which is pretty lame for an init. The wait() call blocks a goroutine AND OS thread, so the go runtime will have to start a new thread for the next wait().<p>Whereas in C you can do the entire thing from a simple thread (I call it &quot;async processes&quot;). You set SIGCHLD handler and get async exit notifications, and then process those in a single-threaded loop. I&#x27;m pretty sure there is a problem doing this in Go (besides resorting to the syscall module)... I think it has to do with the fact that Go turns signals into messages on a channel, but I don&#x27;t remember right now...<p>EDIT: Now I remember. The problem with using the syscall module is that Go does not export Fork and Exec. It exports ForkExec, because it needs to ensure they run on the same thread.<p><a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;syscall&#x2F;#ForkExec" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;syscall&#x2F;#ForkExec</a><p>This severely limits the usefulness of your init program, because in Unix there are all sorts of important things that happen in between. It&#x27;s how you set the child process state. You basically can&#x27;t do anything with file descriptors or containers at all with this model.
评论 #11067232 未加载
评论 #11067410 未加载
评论 #11067772 未加载
评论 #11067244 未加载
评论 #11069528 未加载
falcolasover 9 years ago
The author might look at how a minimal (yet fully functional) init works:<p><a href="http:&#x2F;&#x2F;git.suckless.org&#x2F;sinit&#x2F;tree&#x2F;" rel="nofollow">http:&#x2F;&#x2F;git.suckless.org&#x2F;sinit&#x2F;tree&#x2F;</a> (look at config.def.h and sinit.c)<p>and build up from there. Given how minimal sinit is, it&#x27;s a great place to start your functionality from, and see the power (and limitations) of a basic SystemV init system is.
评论 #11068801 未加载
_yyover 9 years ago
Nice project! Even if you believe that systemd is too complicated, it&#x27;s a great example on how to build an asynchronous, modern init system. Upstart uses some highly questionable tricks (ptrace!) to track processes across forks whereas systemd uses cgroups.
评论 #11065752 未加载
评论 #11065877 未加载
teh_klevover 9 years ago
Part two is now up on his website:<p><a href="http:&#x2F;&#x2F;www.mustafaak.in&#x2F;2016&#x2F;02&#x2F;09&#x2F;forking-process-in-myinit-go.html" rel="nofollow">http:&#x2F;&#x2F;www.mustafaak.in&#x2F;2016&#x2F;02&#x2F;09&#x2F;forking-process-in-myinit...</a>
tkinomover 9 years ago
Writing init in Go probably have size issue compare to typical C implementation. Last golang helloworld I built was 1.4MB. The &#x2F;sbin&#x2F;init in my ubuntu dist is only 194k. Busybox version of init optimized for size&#x2F;features can be much smaller.<p>Tough to use it in OpenWRT, DDWrt type of embedded system where system might only have a few megabytes of RAM&#x2F;ROM.
评论 #11068827 未加载
vogover 9 years ago
@CSDude: Thanks for sharing this very interesting project!<p>From a technical point of view, what are the reasons you chose Go over alternatives, such as C, C++, OCaml or Rust?<p>(Just curious, as all those languages have compilers that produce fast, optimized, self-contained binaries.)
评论 #11065871 未加载
joshwgetover 9 years ago
Although very different than a traditional Linux distribution, the init system for RancherOS might be worth taking a look at. It&#x27;s written entirely in Go as well. <a href="https:&#x2F;&#x2F;github.com&#x2F;rancher&#x2F;os&#x2F;tree&#x2F;master&#x2F;init" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rancher&#x2F;os&#x2F;tree&#x2F;master&#x2F;init</a>
zettahashover 9 years ago
Using Go even earlier in the boot stage in an initramfs (as a replacement for tools like dracut) crossed my mind recently. Go&#x27;s statically linked binaries and fast builds make this particularly appealing to me. With (a lot of) work you could forego busybox entirely.
评论 #11067023 未加载
SixSigmaover 9 years ago
&gt; Linux is mostly perfect as it is<p>Your critical thinking has not been engaged. Linux is a useful bit of code but &quot;perfect&quot; is not what it is known as everywhere.<p>My side of the room like this little ditty<p>Linux, by amateurs, for amateurs -- Dave Presotto [1]<p>Or<p>i’ve wondered whether Linux sysfs should be called syphilis -- Charles Forsyth [2]<p>Or if you like your detractors with a bit more fame<p>Unix has retarded OS research by 10 years and linux has retarded it by 20. -- Dennis Ritchie as quoted by by Boyd Roberts in 9fans.<p>I won&#x27;t go on<p>[1] <a href="http:&#x2F;&#x2F;research.google.com&#x2F;pubs&#x2F;author4927.html" rel="nofollow">http:&#x2F;&#x2F;research.google.com&#x2F;pubs&#x2F;author4927.html</a><p>[2] <a href="http:&#x2F;&#x2F;www.terzarima.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.terzarima.net&#x2F;</a>
评论 #11066171 未加载
daveloyallover 9 years ago
@CSDude, I recommend putting a link to part 2 on the part 1 page.
评论 #11068858 未加载
sotaanover 9 years ago
If you improve the perf&#x2F;functionalities of init using Go, I will give this language a go right after. It seems promising tho.
评论 #11066398 未加载
mmarxover 9 years ago
The kernel already supports acquiring a DHCP lease via the “ip=dhcp” boot option. Why does this need to be a feature of init?
评论 #11069642 未加载
评论 #11069306 未加载