自作ファイルシステムをRedHat系で動かす時注意すべきこと

卒論のために作ってるFuseFSを用いたファイルシステムをFedora8とCentOSで動かしてみようとしました。が、うまくいかなかった。今までの開発はDebianでした。


やはりカーネル(?)の知識無しにファイルシステム構築なんて、どこかしらに弊害が生じますね。


原因がわかったので、メモ代わりに書いておく。

環境

サンプルスクリプト hello.rb を用意します。

require 'fusefs'

class HelloDir
  def contents(path)
    ['hello.txt']
  end
  def file?(path)
    path == '/hello.txt'
  end
  def read_file(path)
    "Hello, World!\n"
  end
end

hellodir = HelloDir.new
FuseFS.set_root( hellodir )

# Mount under a directory given on the command line.
FuseFS.mount_under ARGV.shift
FuseFS.run

問題点

上記のスクリプトをFedora8,CentOS5で動かし、構築されたhello.txtに対するコマンドの挙動があやしくなります。
まずはDebian4で動かしてみました。このときは特に問題ありません。

$ ls
hello.rb  mnt
$ ruby hello.rb mnt &
[1] 1972
$ ls -l mnt
合計 0
-r--r--r-- 1 kui kui 0 2007-12-20 20:16 hello.txt
$ cp mnt/hello.txt .
$ ls -l
合計 8
-rw-r--r-- 1 kui kui  330 2007-12-20 20:14 hello.rb
-r--r--r-- 1 kui kui   14 2007-12-20 20:19 hello.txt
dr-xr-xr-x 1 kui kui 4096 2007-12-20 20:16 mnt
$ cat
Hello, World!
$ 

こんな感じ。
しかしFedora8,CentOS5で同様のことをすると、、、

$ ls
hello.rb  mnt
$ ruby hello.rb mnt &
[1] 1972
$ ls -l mnt
合計 0
-r--r--r-- 1 kui kui 0 2007-12-20 20:16 hello.txt
$ cp mnt/hello.txt .
$ ls -l
合計 8
-rw-r--r-- 1 kui kui  330 2007-12-20 20:14 hello.rb
-r--r--r-- 1 kui kui    0 2007-12-20 20:19 hello.txt
dr-xr-xr-x 1 kui kui 4096 2007-12-20 20:16 mnt
$ cat hello.txt
$

となる。
つまり、cp をした時に、RedHatの方は空コピーしかしてくれない。

原因

ファイルシステムとして動いているクラス HelloDir が、hello.txtのファイルサイズを定義していないせいである。どうやら、RedHatな方は、cp をする前にコピー元が空ファイルかどうかチェックするようです。

解法

hello.txtのサイズを与えてあげましょう。

require 'fusefs'

class HelloDir
  def contents(path)
    ['hello.txt']
  end
  def file?(path)
    path == '/hello.txt'
  end
  def read_file(path)
    "Hello, World!\n"
  end
  # ここから!
  def size(path)
    "Hello, World!\n".size # 1 だけでも問題ない
  end
  # ここまで!
end

hellodir = HelloDir.new
FuseFS.set_root( hellodir )

# Mount under a directory given on the command line.
FuseFS.mount_under ARGV.shift
FuseFS.run

コメントにも書きましたが、ようするにマシンに「hello.txt は空じゃない」と認識させればいいため、1 と直接書いてしまっても問題無いです。

結論

カーネルの知識なしにファイルシステム構築なんて、後々面倒になるだけなんだろうなぁ。今回の cp の件はおそらく氷山の一角と思われる。