logo logo_atte 日記 随筆 何処
git in fuse2fs
1 問題
fuse2fsでマウントしたファイルシステムでは、 gitリポジトリを作成できない。
2 結論
最新のfuse2fs(e2fsprogs-1.45.6)をインストールして、 fakerootオプションをつけてマウントしろ。
3 経緯(と言うかここから覚書)
ChromebookではLinuxコンテナ(Crostini)を利用できる。しかしながら、内蔵ストレージが小さすぎる(16GBしかない)。当然、外部SDcardによる増量が必要になるが、UNIX系ファイルシステムが使えない。外部SDcardの上に作成したいgitリポジトリには、 shellやpython等のスクリプトも存在し、ファイルのアクセス権も管理したいので、 exfat(fat)ファイルシステムしか使えないのは困る。
3.1 ChromebookのSDcardでext2(ext4)を使う
  1. ChromebookのSDcardではexfatしか使えない。
  2. ext2のイメージファイルを作成してloopbackデバイスでマウントすればいいや。
  3. loopbackデバイスは不能。調査してないけど、多分、コンテナ(lxc)ゆえの制限。
  4. user-spaceのファイルシステムなら大丈夫だろう。
以上の経緯からfuse2fsの利用で解決。ここまではすぐ。
3.2 gitリポジトリをcloneできない
さぁ、gitだ。
  • $ git clone ...
  • ...
  • unable to create temporary file
  • ...
あり?他の方法も試してみる。
  • $ git init
  • $ git remote ...
  • $ git pull
  • ...
  • insufficient permission for adding an object to repository database ...
  • ...
これも、ダメか。
3.2.1 gitソースの調査
さて、困った。ウェブの情報を探すが、どうも同様の事例が見つからない。 fuse2fsでマウントしたファイルシステムに作ったgitリポジトリに特有の現象だ。ニッチ過ぎる。仕方ないのでソースを読む。エラーメッセージから該当部分は以下。
  1. static int write_loose_object(const struct object_id *oid, char *hdr,
  2. int hdrlen, const void *buf, unsigned long len,
  3. time_t mtime)
  4. {
  5. int fd, ret;
  6. unsigned char compressed[4096];
  7. git_zstream stream;
  8. git_hash_ctx c;
  9. struct object_id parano_oid;
  10. static struct strbuf tmp_file = STRBUF_INIT;
  11. static struct strbuf filename = STRBUF_INIT;
  12. loose_object_path(the_repository, &filename, oid);
  13. fd = create_tmpfile(&tmp_file, filename.buf);
  14. if (fd < 0) {
  15. if (errno == EACCES)
  16. return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
  17. else
  18. return error_errno(_("unable to create temporary file"));
  19. }
  20. ...
sha1-file.c in git-2.26.0
create_tmpfileから追いかけていく。根本でエラーを返していると思われるのは以下45行目の部分。
  1. int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
  2. {
  3. static const char letters[] =
  4. "abcdefghijklmnopqrstuvwxyz"
  5. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6. "0123456789";
  7. static const int num_letters = ARRAY_SIZE(letters) - 1;
  8. static const char x_pattern[] = "XXXXXX";
  9. static const int num_x = ARRAY_SIZE(x_pattern) - 1;
  10. uint64_t value;
  11. struct timeval tv;
  12. char *filename_template;
  13. size_t len;
  14. int fd, count;
  15. len = strlen(pattern);
  16. if (len < num_x + suffix_len) {
  17. errno = EINVAL;
  18. return -1;
  19. }
  20. if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
  21. errno = EINVAL;
  22. return -1;
  23. }
  24. /*
  25. * Replace pattern's XXXXXX characters with randomness.
  26. * Try TMP_MAX different filenames.
  27. */
  28. gettimeofday(&tv, NULL);
  29. value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
  30. filename_template = &pattern[len - num_x - suffix_len];
  31. for (count = 0; count < TMP_MAX; ++count) {
  32. uint64_t v = value;
  33. int i;
  34. /* Fill in the random bits. */
  35. for (i = 0; i < num_x; i++) {
  36. filename_template[i] = letters[v % num_letters];
  37. v /= num_letters;
  38. }
  39. fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
  40. if (fd >= 0)
  41. return fd;
  42. /*
  43. * Fatal error (EPERM, ENOSPC etc).
  44. * It doesn't make sense to loop.
  45. */
  46. if (errno != EEXIST)
  47. break;
  48. /*
  49. * This is a random value. It is only necessary that
  50. * the next TMP_MAX values generated by adding 7777 to
  51. * VALUE are different with (module 2^32).
  52. */
  53. value += 7777;
  54. }
  55. /* We return the null string if we can't find a unique file name. */
  56. pattern[0] = '\0';
  57. return -1;
  58. }
wrapper.c in git-2.26.0
引数として、mode=0444が与えられてopenシステムコールが呼ばれているので、次のようなテストプログラムを書いてfuse2fsでマウントしたファイルシステムの上で実行。
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. int
  7. main(int argc, char *argv[])
  8. {
  9. char *fn = (1 == argc) ? "ttt" : argv[1];
  10. int fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0444);
  11. fprintf(stderr, "fd=%d, errno=%d\n", fd, errno);
  12. return 0;
  13. }
create444.c
  • $ gcc -o create444 create444.c
  • $ ./create444 test.txt
  • fd=-1, errno=13
  • $ ls
  • test.txt
なんだこりゃ。 openシステムコールは確かに失敗している。そのくせ、しっかりファイルは作成されている。
3.2.2 fuse2fsソースの調査
やれやれ、仕方ない。fuse2fsのソースを読む。 EACCES(errno=13)を返す処理はどうやら次の部分のみのようだ。
  1. static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
  2. {
  3. struct fuse_context *ctxt = fuse_get_context();
  4. struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
  5. struct ext2_inode inode;
  6. mode_t perms;
  7. errcode_t err;
  8. /* no writing to read-only or broken fs */
  9. if ((mask & W_OK) && !fs_writeable(fs))
  10. return -EROFS;
  11. err = ext2fs_read_inode(fs, ino, &inode);
  12. if (err)
  13. return translate_error(fs, ino, err);
  14. perms = inode.i_mode & 0777;
  15. dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
  16. "uid=%d gid=%d\n", ino,
  17. (mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
  18. (mask & X_OK ? "x" : ""), perms, inode_uid(inode),
  19. inode_gid(inode), ctxt->uid, ctxt->gid);
  20. /* existence check */
  21. if (mask == 0)
  22. return 0;
  23. /* is immutable? */
  24. if ((mask & W_OK) &&
  25. (inode.i_flags & EXT2_IMMUTABLE_FL))
  26. return -EACCES;
  27. /* Figure out what root's allowed to do */
  28. if (ff->fakeroot || ctxt->uid == 0) {
  29. /* Non-file access always ok */
  30. if (!LINUX_S_ISREG(inode.i_mode))
  31. return 0;
  32. /* R/W access to a file always ok */
  33. if (!(mask & X_OK))
  34. return 0;
  35. /* X access to a file ok if a user/group/other can X */
  36. if (perms & 0111)
  37. return 0;
  38. /* Trying to execute a file that's not executable. BZZT! */
  39. return -EACCES;
  40. }
  41. /* allow owner, if perms match */
  42. if (inode_uid(inode) == ctxt->uid) {
  43. if ((mask & (perms >> 6)) == mask)
  44. return 0;
  45. return -EACCES;
  46. }
  47. /* allow group, if perms match */
  48. if (inode_gid(inode) == ctxt->gid) {
  49. if ((mask & (perms >> 3)) == mask)
  50. return 0;
  51. return -EACCES;
  52. }
  53. /* otherwise check other */
  54. if ((mask & perms) == mask)
  55. return 0;
  56. return -EACCES;
  57. }
fuse2fs.c in e2fsprogs-1.45.6
51行目から68行目で、ファイルのアクセスモード(mask:O_RDWRなど)と、パーミション(perms:0444など)を比較して不整合をチェックしている。ただし、33行目から49行目で、userがrootであるか、 fakerootオプションが指定されていれば、パーミションチェックは回避される。
3.2.3 解決(workaround)
  • $ fuse2fs -o allow_other,fakeroot,uid=1000,gid=1000 image-file mount-point
3.2.4 余談
fuseモジュールなんて書いたことないです。実際にはfuse2fs.cにデバッグコードを埋め込んではコンパイルを繰り返し、 fuse2fsの挙動を確認しながら進めていたので、結構な時間がかかりました。やっぱ、Linuxのソース(特にスケジューラとファイルシステムまわり)と、メジャーなデバイスドライバくらいは読んでおく必要があると再確認した次第。
3.3 Return to Chromebook
現在のCrostini(debian buster)のfuse2fsは古いので、fakerootオプションが利用できない。 buster-backportsを使え。でも、一番良い解決法はgitにパッチをあてることだと思われる。
  1. *** sha1-file.c 2020-03-23 09:19:47.000000000 +0900
  2. --- sha1-file-patched.c 2020-04-11 02:26:44.803375581 +0900
  3. ***************
  4. *** 1803,1809 ****
  5. strbuf_reset(tmp);
  6. strbuf_add(tmp, filename, dirlen);
  7. strbuf_addstr(tmp, "tmp_obj_XXXXXX");
  8. ! fd = git_mkstemp_mode(tmp->buf, 0444);
  9. if (fd < 0 && dirlen && errno == ENOENT) {
  10. /*
  11. * Make sure the directory exists; note that the contents
  12. --- 1803,1809 ----
  13. strbuf_reset(tmp);
  14. strbuf_add(tmp, filename, dirlen);
  15. strbuf_addstr(tmp, "tmp_obj_XXXXXX");
  16. ! fd = git_mkstemp_mode(tmp->buf, 0644);
  17. if (fd < 0 && dirlen && errno == ENOENT) {
  18. /*
  19. * Make sure the directory exists; note that the contents
patch for git-2.26.0
作業用の一時ファイルとして実効ユーザが読み書きするのだから、普通は「mode=0644」で作成するのが当然だと思うのだが何故「mode=0444」なのであろうか。
3.4 性能が、、、
想像以上の重さだ。 SDcardでloopback(imageファイル)だ。性能は悪いだろうと思っていたが、比較的高速のSDXCでもここまでとは。全体的にもっさり。 2000ファイルもあるとlsですら10秒以上かかる。