logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

In-praise-of-Plan-9.md (6563B)


  1. ---
  2. title: In praise of Plan 9
  3. date: 2022-11-12
  4. ---
  5. [Plan 9][0] is an operating system designed by Bell Labs. It's the OS they wrote
  6. *after* Unix, with the benefit of hindsight. It is the most interesting
  7. operating system that you've never heard of, and, in my opinion, the best
  8. operating system design to date. Even if you haven't heard of Plan 9, the
  9. designers of whatever OS you *do* use have heard of it, and have incorporated
  10. some of its ideas into your OS.
  11. [0]: https://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs
  12. Plan 9 is a research operating system, and exists to answer questions about
  13. ideas in OS design. As such, the Plan 9 experience is in essence an exploration
  14. of the interesting ideas it puts forth. Most of the ideas are small. Many of
  15. them found a foothold in the broader ecosystem — UTF-8, goroutines, /proc,
  16. containers, union filesystems, these all have their roots in Plan 9 — but
  17. many of its ideas, even the good ones, remain unexplored outside of Plan 9. As a
  18. consequence, Plan 9 exists at the center of a fervor of research achievements
  19. which forms a unique and profoundly interesting operating system.
  20. One example I often raise to illustrate the design ideals of Plan 9 is to
  21. compare its approach to network programming with that of the Unix standard,
  22. Berkeley sockets. BSD sockets fly in the face of Unix sensibilities and are
  23. quite alien on the system, though by now everyone has developed stockholm
  24. syndrome with respect to them so they don't notice. When everything is supposed
  25. to be a file on Unix, why is it that the networking API is entirely implemented
  26. with special-purpose syscalls and ioctls? On Unix, creating a TCP connection
  27. involves calling the "socket" syscall to create a magic file descriptor, then
  28. the "connect" syscall to establish a connection. Plan 9 is much more Unix in its
  29. approach: you open /net/tcp/clone to reserve a connection, and read the
  30. connection ID from it. Then you open /net/tcp/n/ctl and write "connect
  31. 127.0.0.1!80" to it, where "n" is that connection ID. Now you can open
  32. /net/tcp/n/data and that file is a full-duplex stream. No magic syscalls, and
  33. you can trivially implement it in a shell script.
  34. This composes elegantly with another idea from Plan 9: the 9P protocol. All file
  35. I/O on the entire system uses the 9P protocol, which defines operations like
  36. read and write. This protocol is network transparent, and you can mount remote
  37. servers into your filesystem namespace and access their files over 9P. You can
  38. do something similar on Unix, but on Plan 9 you get much more mileage from the
  39. idea because everything is *actually* a file, and there are no magic syscalls or
  40. ioctls. For instance, your Ethernet interface is at /net/ether0, and everything
  41. in there is just a file. Say you want to establish a VPN: you simply mount a
  42. remote server's /net/ether0 at /net/ether1, and now you have a VPN. That's *it*.
  43. The mountpoints are interesting as well, because they exist within a per-process
  44. filesystem namespace. Mounting filesystems does not require special permissions
  45. like on Unix, because these mounts only exist within the process tree that
  46. creates them, rather than modifying global state. The filesystems can also be
  47. implemented in userspace rather trivially via the 9P protocol, similar to FUSE
  48. but much more straightforward. Many programs provide a programmable/scriptable
  49. interface via a special filesystem such as this.
  50. Userspace programs can also provide filesystems compatible with those normally
  51. implemented by kernel drivers, like /net/ether0, and provide these to processes
  52. in their namespace. For example, /dev/draw is analogous to a framebuffer device:
  53. you open it to write pixels to the screen. The window manager, Rio, implements
  54. a /dev/draw-like interface in userspace, then mounts it in the filesystem
  55. namespace of its children. All GUI programs can thus be run both on a
  56. framebuffer or in a window, without any awareness of which it's using. The same
  57. is also true over the network: to implement VNC-like functionality, just mount
  58. your local /dev/draw and /dev/kbd on a remote server. Add /dev/audio if you
  59. like.
  60. These ideas can also be built upon to form something resembling a container
  61. runtime, pre-dating even early concepts like BSD jails by several years, and
  62. implementing them much more effectively. Recall that everything *really is* just
  63. a file on Plan 9, unlike Unix. Access to the hardware is provided through normal
  64. files, and per-process namespaces do not require special permissions to modify
  65. mountpoints. Making a container is thus trivial: just unmount all of the
  66. hardware you don't want the sandboxed program to have access to. Done. You don't
  67. even have to be root. Want to forward a TCP port? Write an implementation of
  68. /net/tcp which is limited to whatever ports you need — perhaps with just a
  69. hundred lines of shell scripting — and mount it into the namespace.
  70. The shell, rc, is also wonderful. The debugger is terribly interesting, and its
  71. ideas didn't seem to catch on with the likes of gdb. The editors, acme and sam,
  72. are also interesting and present a unique user interface that you can't find
  73. anywhere else. The plumber is cool, it's like "what if xdg-open was good
  74. actually". The kernel is concise and a pleasure to read. The entire operating
  75. system, kernel *and* userspace, can be built from source code on my 12 year old
  76. laptop in about 5 minutes. The network database, ndb, is brilliant. The entire
  77. OS is stuffed to the brim with interesting ideas, all of them implemented with
  78. elegance, conciseness, and simplicity.
  79. Plan 9 failed, in a sense, because Unix was simply too big and too entrenched by
  80. the time Plan 9 came around. It was doomed by its predecessor. Nevertheless, its
  81. design ideas and implementation resonate deeply with me, and have provided an
  82. endless supply of inspiration for my own work. I think that everyone owes it to
  83. themselves to spend a few weeks messing around with and learning about Plan 9.
  84. The dream is kept alive by [9front][1], which is the most actively maintained
  85. fork of Plan 9 available today. Install it on your ThinkPad and mess around.
  86. [1]: http://9front.org/
  87. I will offer a caveat, however: leave your expectations at the door. Plan 9 is
  88. not Unix, it is not Unix-compatible, and it is certainly not yet another Linux
  89. distribution. Everything you're comfortable and familiar with in your normal
  90. Unix setup will not translate to Plan 9. Come to Plan 9 empty handed, and let it
  91. fill those hands with its ideas. You will come away from the experience as a
  92. better programmer.