「車輪の再実装」って言葉が好き(実践はできてない)

linux namespacesのファイルと永続化について

ip netnsコマンドを使うと、/var/run/netnsディレクトリにファイルを足してネットワーク名前空間を足してくれたり、
/proc/[pid]/ns以下に、そのプロセスの名前空間に対応するファイルを見せてくれるのは知っていたけれど、そのあたりの扱いをうろ覚えなので少し整理.

以下、namespaces(7)より抜粋。

Bind mounting (see mount(2)) one of the files in this directory to somewhere else in the filesystem keeps the corresponding namespace of the process specified by pid alive even if all processes currently in the namespace terminate.

というわけで、/proc/[pid]/ns/ 以下のファイルをバインドマウントすればいいらしい。

ちなみに、network namespace向けのmanも読んだが、/var/run/netns/ についての記述はなかったので、このディレクトリはkernelの規定というよりは、ipコマンドが使っているだけな気がしている。

ところで、

In Linux 3.7 and earlier, these files were visible as hard links. Since Linux 3.8, they appear as symbolic links. If two processes are in the same namespace, then the device IDs and inode numbers of their /proc/[pid]/ns/xxx symbolic links will be the same; an application can check this using the stat.st_dev and stat.st_ino fields returned by stat(2). The content of this symbolic link is a string containing the namespace type and inode number as in the following example:

らしい。 /proc/[pid]/ns/シンボリックリンクを作ればいい...みたいに勘違いていたのはここら辺を混同した気がする。

参考