ページ更新: 2009-02-04 (水) (5228日前)

(2001-06-15作成)

「mount -o bind」でディレクトリをディレクトリにマウントする

mount (8) によると、kernel 2.4.* 系列以後では、ディレクトリに対してディレクトリをマウントできるそうです。

(補足:2.6.26以後ではread-only mountも出来るようになったそうです)

これができると、

  • ftpサーバの各ユーザのディレクトリに共通のものをくくり出すことができます。
  • ユーザのcgiをchroot環境で動作させるときに、/binやperlをユーザーごとに用意しなくても済むようになります。

Debian GNU/Linux woody (testing) で kernel 2.4.5 のパッケージが使えるようになったので、早速試してみました。

目次

[編集]

情報源 #

  • Linuxのオンラインマニュアル man 8 mount
[編集]

Read-only bind mounts #

  • メモ: Debian Etch は 2.6.18 / 2.6.24-etchnhalf / 2.6.26-bpo (etch-backports)、 Ubuntu 8.04 Server は 2.6.24-23、 Ubuntu 8.10 は 2.6.27-9-generic、 Debian Lenny は 2.6.26
  • メモ: Ubuntu 9.10 は 2.6.26-2
[編集]

確認 #

[編集]

300個のディレクトリをmountできるか? #

まずは、300個のディレクトリをmountできるかどうか試してみました。 (もともと、1台のWWWサーバに300人ほどユーザがいて、binとlibを共有したかったので)

以下のperlスクリプトを実行します。(注意:他に、後始末をするスクリプトも用意しないと大変です)

#!/usr/bin/perl

use strict;

my $olddir= './old/';
my $newdir= './new/';

for( my $i= 0; $i < 300; $i++ ) {
    mkdir "$olddir$i";
    mkdir "$newdir$i";
    `mount --bind $olddir$i $newdir$i`;
}

ちゃんと、300個のディレクトリをマウントできました。

なお、もっと沢山のディレクトリで試すなら、kernelのソースコードで制限を調べたほうがよいかと。

# df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda3              2893660   1006924   1739744  37% /
/dev/hda1              2585304    508764   1945212  21% /home
/home/example/old/0    2585304    508764   1945212  21% /home/example/new/0
/home/example/old/1    2585304    508764   1945212  21% /home/example/new/1
                          : (略)
/home/example/old/298  2585304    508764   1945212  21% /home/example/new/298
/home/example/old/299  2585304    508764   1945212  21% /home/example/new/299
[編集]

1つのユーザでマウントしてftpから確認 #

では次に、ユーザー example のホームディレクトリに /bin と /etc をマウントし、ftp経由でアクセスできるかどうか試してみます。

# pwd
/home/example

# mkdir bin etc
# mount --bind /bin bin
# mount --bind /etc etc

# df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda3              2893660   1006976   1739692  37% /
/dev/hda1              2585304    508772   1945204  21% /home
/bin                   2893660   1006976   1739692  37% /home/example/bin
/lib                   2893660   1006976   1739692  37% /home/example/etc

ftpサーバには proftpd 1.2.0.pre10-2.1 を用い、proftpd.conf は以下の設定を変更してあります。 DefaultRoot が chroot環境を使う設定です。

DefaultRoot        ~ !root
TimesGMT           off
LsDefaultOptions   "-l"

Windows のftpでアクセスしたところ、特に問題なくアクセスできます。(以下には書いてありませんが、get もできました。putは試していません)

ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for file list.
drwxr-xr-x   2 root     root         4096 Jun 13 22:56 bin
drwxr-xr-x   4 root     root         4096 Jun 13 22:54 etc
226-Transfer complete.
226 Quotas off

ftp> ls etc
200 PORT command successful.
150 Opening ASCII mode data connection for file list.
-rw-r--r--   1 root     root         4170 May 24 07:16 Muttrc
drwxr-xr-x   2 root     root         4096 Apr 29 00:03 Net
-rw-r--r--   1 root     root        46419 Nov 26  2000 TextConfig
-rw-r--r--   1 root     root        45973 Oct 14  2000 TextConfig.dpkg-old
            : (略)
-rw-r--r--   1 root     root          297 Oct 23  2000 warnquota.conf
-rw-r--r--   1 root     root        13684 May 20 22:19 webalizer.conf
-rw-r--r--   1 root     root         3936 Feb 26 04:57 wgetrc
226-Transfer complete.
226 Quotas off
ftp>
[編集]

read-only bind mount (2009-03-11) #

Debian Lennyにて確認:

$ uname -r
2.6.26-1-686

$ mkdir t
$ sudo mount --rbind /usr/local/bin ./t
$ mount
 :
 :
/usr/local/bin on /home/USERNAME/t type none (rw,bind)

$ sudo umount t
[編集]

設定 #

[編集]

fstabに設定 (2005-09-03) #

  • 2005-09-03 書き忘れてたので追加。なお、man 8 mountによると、bind以外に'rbind'や'move'(こちらはkernel 2.5.1以降)も 使えるとあるが、試していない。

/etc/fstabに2行追加。

# /etc/fstab: static file system information.
#
# <file system> <mount point>           <type>  <options> <dump> <pass>
(略)
/bin            /home/example/bin        none    bind    0       0
/lib            /home/example/lib        none    bind    0       0

要するに:

  • typeフィールドはnoneにする
  • optionフィールドはbindにする。(mount -o XXXXX の XXXXX 部分を指定する)

マウントしてみる:

# /etc/init.d/mountall.sh
Mounting local filesystems...
/bin on /home/example/bin type none (rw,bind)
/lib on /home/example/lib type none (rw,bind)

確認。OK。

# mount
(無関係なエントリは省略)
/bin on /home/example/bin type none (rw,bind)
/lib on /home/example/lib type none (rw,bind)
[編集]

余談 #

[編集]

kernel 2.2以前では… #

なお、kernel 2.2 以前のものでは、bind -o mount は使えません。 同様のことをするには、次の方法があります。

  1. nfsサーバにして、localでマウントする
    nfsを経由すると、余分にリソースを使ってしまうなあ。
    # mount localhost:/bin /home/example/bin
    あるいは
    # mount `uname -n`:/bin /home/example/bin
  2. loopbackデバイスを使う
    loopbackデバイスは、/dev/loop0〜7 の8個しかない。これを増やすにはデバイスファイルだけでなく、カーネルの修正が必要かも。sysctl (8) には見あたらなかったし。
    # dd if=/dev/zero of=wwwbin bs=1k count=20480     (20MBの領域を作る)
    # mkfs -t ext2 wwwbin                                (フォーマット)
    # mount -o loop -t ext2 wwwbin /home/example/bin  (マウント)