The Plan 9 Namespace for Dummies

It took me a while to wrap my head around the Plan 9 namespace concept, and it is one of the most important aspects of the system.

I guess I couldn’t find a nice, concise description of the Plan 9 namespace that really explained what was going on. The man pages are okay, but sometimes fail to provide extensive examples and explanations.

If you run

; ns

…you get a list of binds and mounts for the namespace of your process.

Binding directories

bind [ option ... ] new old

The directory “new” is bound to “old”.

The -a (after) and -b (before) options specify whether the files on the original directory are opened or the ones in the mounted union directory or bound directory are opened if there’s conflicts with filenames.

With the -a flag, files in the original directory will be read.

With the -b flag, files in the bound directory will be read.

An example of bind:

; pwd
/usr/glenda
; lc
acme.dump   lib         readme.rio
bin     readme.acme     tmp
; bind -b tmp /bin
; ed tmp/lc
?tmp/lc
a
#!/bin/rc
echo glenda
.
w
22
q
; chmod +x tmp/lc
; lc
glenda
; unmount /bin
; lc
lc: '/bin/lc' does not exist
; ns
ns: '/bin/ns' does not exist

Oops, we just screwed up our namespace!

Note that, when you change your namespace, only the view of the current process and its children on the namespace is changed, so you could simply open a new window and your namespace won’t be broken.

9front has shr(3) as a global mount registry to help alleviate any annoyances of having private namespaces, but private namespaces can also be useful.

By default, /$cputype/bin is bound to /bin, as is /rc/bin:

; ns | grep '/bin'
bind  /386/bin /bin 
bind -a /rc/bin /bin 
bind -a /usr/glenda/bin/rc /bin 
bind -a /usr/glenda/bin/386 /bin 

This particular machine is x86, hence the 386 $cputype. By not just having binaries on /bin, one Plan 9 file server can be used by machines of many architectures, and all binaries are rooted in /bin:

; echo $path
. /bin

By the way these directories are bound, the directory bound first has precedence over all the ones after it (files on /386/bin are opened/read rather than in any of the other ones if there’s a conflict), unless you do a bind -b, so then those take precedence.

This sort of linked list represents a namespace where /386/bin and /rc/bin are bound to /bin:

/bin: /386/bin -> /rc/bin.

So, if the system were to look in a file, it’d look first in /386/bin and then on /rc/bin.

You can do a bind of another directory before, or after.

If you bind before, the directory you’re binding acquires the highest precedence in conflicts:

/bin: /newdir -> /386/bin -> /rc/bin

If you bind after, you get lowest precedence.

/bin: /386/bin -> /rc/bin -> /newdir

So any conflicting files would end up being the ones on /386/bin.

Directories are just files, so there are no ‘recursive’ union mounts. If you have a /usr/you/bin/386/aux and you do

; bind -b /usr/you/bin/386 /bin

… /bin/aux would only contain the files in /usr/you/bin/386/aux. :(

The -c flag allows creating files with union mounts/binds (which are any binds using the -b or -a flags). When a new file is created, the system looks for the first directory with a -c flag and tries to put it there.

Todo: Add experiments with -c.

Binding files

You can also bind files instead of directories. For example:

; touch blackhole.log
; bind /dev/null blackhole.log
; echo efoinbawgfoweagoiw > blackhole.log
; cat blackhole.log
;

mount(1)

mount [ option ... ] servename old [spec]

mount(1) alters the name space of the target “old” just like bind(1), but, it mounts an existing fileserver connection, in /srv, which is a file that represents a 9P connection to a file server. It takes -a and -b just like bind(1).

The third argument is “spec”, which is passed on to the server being mounted, selecting “among the file trees served by the server”. The “spec” argument effectively works as the path to the disk partition (just a file) to mount when mounting from dossrv.

To make a dos filesystem on a file and mount it somewhere:

(note that the xd command is/was broken in Plan 9, but not on 9front at the time of this writing. This is what should be the correct output.)

cpu% dd -if /dev/zero -of muh.fs -bs 1024 -count 128
128+0 records in
128+0 records out
cpu% disk/format -d muh.fs
Initializing FAT file system
type 3½HD, 80 tracks, 2 heads, 18 sectors/track, 512 bytes/sec
used 512 bytes
cpu% ls /srv/dos
ls: /srv/dos: '/srv/dos' file does not exist
cpu% dossrv
dossrv: serving #s/dos
cpu% mkdir notafile
cpu% mount -c /srv/dos notafile muh.fs
cpu% echo i am some text!!!!! > notafile/afile
cpu% unmount notafile
cpu% rm /srv/dos
cpu% rm /srv/dos
rm: /srv/dos: '/srv/dos' file does not exist
cpu% ps -a | grep dossrv
glenda       86237    0:00   0:00      156K Pread    grep dossrv
cpu% xd -c -r muh.fs
0000000  eb  < 90  P  l  a  n  9  .  0  0 00 02 01 01 00
0000010  02 e0 00  @ 0b f0 \t 00 12 00 02 00 00 00 00 00
0000020   @ 0b 00 00 00 00  ) 13 00 00 00  C  Y  L  I  N
0000030   D  R  I  C  A  L  F  A  T  1  2          fa fc
0000040  8c c8 8e d8 8e d0 bc 00  | be  w  | e8 19 00  3
0000050  c0 cd 16 bb  @ 00 8e c3 bb  r 00 b8  4 12  & 89
0000060  07 ea 00 00 ff ff eb d6 ac \n c0  t \t b4 0e bb
0000070  07 00 cd 10 eb f2 c3  N  o  t     a     b  o  o
0000080   t  a  b  l  e     d  i  s  c     o  r     d  i
0000090   s  c     e  r  r  o  r \r \n  P  r  e  s  s   
00000a0   a  l  m  o  s  t     a  n  y     k  e  y     t
00000b0   o     r  e  b  o  o  t  .  .  . 00 00 00 00 00
00000c0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
00001f0  00 00 00 00 00 00 00 00 00 00 00 00 00 00  U aa
0000200  f0 ff ff ff 0f 00 00 00 00 00 00 00 00 00 00 00
0000210  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0001400  f0 ff ff ff 0f 00 00 00 00 00 00 00 00 00 00 00
0001410  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0002600   A  a 00  f 00  i 00  l 00  e 00 0f 00  ) 00 00
0002610  ff ff ff ff ff ff ff ff ff ff 00 00 ff ff ff ff
0002620   A  F  I  L  E                   00 00 00 00 00
0002630  00 00 00 00 00 00 07 13 01  E 02 00 14 00 00 00
0002640  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0004200   i     a  m     s  o  m  e     t  e  x  t  !  !
0004210   !  !  ! \n 00 00 00 00 00 00 00 00 00 00 00 00
0004220  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0167ff0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  9
0168000 
0168000 
cpu% strings muh.fs
       3: Plan9.00
      43: CYLINDRICALFAT12   
     119: Not a bootable disc or disc error
     154: Press almost any key to reboot...
    9760: AFILE      
   16896: i am some text!!!!!

Comments

By: Glenda (Mon Aug 31 15:58:07 EDT 2015)
asd