summaryrefslogtreecommitdiff
path: root/sites/pmikkelsen.com/plan9/mounting-9p-over-drawterm.md
blob: 414b9f2284f3e7f0953dea16d71b81b3c5c845e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Mounting a 9P connection over drawterm

I sometimes use drawterm on linux to connect to my 9front server.
While it is possible to access the host system's files under `/mnt/term`, there is no builtin way to access the remote system's file under linux.
Now, why would anybody want to do this?
In my case, I often want to write some code under 9front, but for languages which aren't supported such as prolog in this case, so there are three options as I see it:

* Store the files on the host machine, and access them under `/mnt/term`.
* Store the files on the server and somehow mount the server's filesystem on the host.
* Store the files on a third machine that both the host and server can access.

Option number two seems best for me, so I asked around and it seems like the best tool to mount a 9P connection on linux is [9pfs](https://github.com/bunny351/9pfs).

## How it works

Before I could mount the connection, I had to serve it somehow. Normally I already serve 9P directly from my server's filesystem, but that requires some
authentication that 9pfs does not support, so I had to serve it without authentication.
Serving directly to the internet without authentication is of course pretty dumb since everyone can then access my files, so thanks to a hint from hiro, I figured
out that it is actually possible to use the host's network stack on the server by binding `/mnt/term/net` over `/net`.

I'll just show the final script below and explain it afterwards:

	#!/bin/rc
	
	rfork n
	bind /mnt/term/net /net
	aux/listen1 -t tcp!*!12335 /bin/exportfs -r / &
	os mkdir -p /tmp/drawterm
	os 9pfs localhost -p 12335 /tmp/drawterm

So the first thing that happens is that I bind the host's network in, and from that point on, every network connection in this namespace actually goes out
from the host instead of from the server!

Then `exportfs` is started and it is serving the `/` directory over 9P at port 12335.
The `os` command runs a command on the host, so it just creates the folder that the system will be mounted to, and then uses `9pfs` to actually mount it.
The nice thing here is that `9pfs` just connects to localhost.

## Using it

As I said in the beginning, I did this to be able to edit files on the 9front server, and run compilers/interpreters on linux.
The `os` command goes a long way, but the following script makes it even easier (I have this installed as `linux`):

	#!/bin/rc
	
	dir=/tmp/drawterm/`{pwd}
	os -d $dir $*

This means I can just go into any directory on the server, type `linux ghci` and I get a haskell repl running in the correct directory, as seen in the gif below.

[![An animated gif showing ghci loading a file on the 9front server][1]][1]


## Final notes

The bind net trick still blows my mind a bit, since it is so trivial, yet so powerful. It is also fun to think about how one would do this
on other systems than plan9, which I can't even imagine.

[1]: /images/linuxreverse.gif