shimada-kの日記

ソフトウェア・エンジニアのブログです

debianでUSBメモリを実行権付きでマウントするメモ

debianUSBメモリを扱う時のメモです。

私はプログラムの開発プロジェクトをUSBメモリに保存しています。そうすると、USBメモリ内の実行ファイルを実行したいわけです。しかし、私が常用しているdebianではUSBメモリのファイルは「読み込み+書き込み権限」でマウントされていて、ファイルを実行できません。

そこで私はコンパイルはUSBメモリ上で行い、生成された実行ファイルはホームディレクトリにコピーして実行権を付加して実行していました。この方法の問題点は

  • cp(1)とchmod(1)をする手間がかかる
  • 開発用のコンソールと実行用のコンソールの2つのコンソールを開かないといけない
  • cp(1)忘れなどで、実行ファイルのバージョンが古かったりする

などがあります。特に3番目の問題は深刻です。なので、USBメモリ上でファイルを実行できる用にすることにします。

環境:Linux debian 2.6.39-2-amd64 [debian wheezy]

通常USBメモリを挿すと自動的にマウントされます。このようなアイコンがデスクトップに表示されるはずです。USBメモリはWindowsでFAT32でフォーマットされたものとします。

f:id:shimada-k:20110623160947p:image

そして、マウントされたファイルシステム上で試しにファイルを作ってコンパイルしてみます。

hoge.c

#include <stdio.h>

int main(void)
{
	puts("hoge");
	return 0;
}

コンソールに「hoge」と表示するだけのプログラムです。これをコンパイルしてhogeというファイルを作成し、「ls -l」してみます。マウントポイントはdf(1)で確認したところ、使用したUSBメモリでは/media/C092-1447でした。

shimada@debian:~$ cd /media/C092-1447/
shimada@debian:/media/C092-1447$ gcc -o hoge hoge.c
shimada@debian:/media/C092-1447$ ls -l
合計 12
-rw-r--r-- 1 shimada shimada 6584 2011-06-23 14:04 hoge
-rw-r--r-- 1 shimada shimada 66 2011-06-23 14:00 hoge.c

実行権はついていません。chmod(1)をしてみます。

shimada@debian:/media/C092-1447$ chmod +x hoge
shimada@debian:/media/C092-1447$ ls -l
合計 12
-rw-r--r-- 1 shimada shimada 6584 2011-06-23 14:04 hoge
-rw-r--r-- 1 shimada shimada 66 2011-06-23 14:00 hoge.c
shimada@debian:/media/C092-1447$ chmod 777 hoge
shimada@debian:/media/C092-1447$ ls -l
合計 12
-rw-r--r-- 1 shimada shimada 6584 2011-06-23 14:04 hoge
-rw-r--r-- 1 shimada shimada 66 2011-06-23 14:00 hoge.c

chmod(1)は効きません。そこでusbmountというパッケージをaptでインストールします。

インストール後、USBメモリをマウントし直します。そうするとこのようなアイコンがデスクトップに表示されます。

f:id:shimada-k:20110623163029p:image

usbmountが動作するとアイコンが変化するようです。このままでは一般ユーザは書き込みができません。確認してみます。マウントポイントは/media/usb0です。

shimada@debian:~$ cd /media/usb0
shimada@debian:/media/usb0$ ls -l
合計 12
-rwxr-xr-x 1 root root 6584 2011-06-23 14:04 hoge
-rwxr-xr-x 1 root root 66 2011-06-23 14:00 hoge.c

オーナーIDとグループIDがrootになっています。さらにusbmount側の設定で実行が禁止されているのでパーミッションでは実行権がついていますが、実行できません。

ということでusbmountの設定を書き換えます。場所は/etc/usbmount/usbmount.confです。36行目から下を修正&追加します。

# Mount options: Options passed to the mount command with the -o flag.
# See the warning above regarding removing "sync" from the options.
MOUNTOPTIONS="sync,nodev,noatime,nodiratime" ←noexecを削除

# Filesystem type specific mount options: This variable contains a space
# separated list of strings, each which the form "-fstype=TYPE,OPTIONS".
#
# If a filesystem with a type listed here is mounted, the corresponding
# options are appended to those specificed in the MOUNTOPTIONS variable.
#
# For example, "-fstype=vfat,gid=floppy,dmask=0007,fmask=0117" would add
# the options "gid=floppy,dmask=0007,fmask=0117" when a vfat filesystem
# is mounted.
FS_MOUNTOPTIONS="-fstype=vfat,uid=shimada,gid=shimada" ←「""」内を追加

# If set to "yes", more information will be logged via the syslog
# facility.
VERBOSE=no

noexecを削除してuidとgidを使用するユーザIDにします。そしてマウントし直します。

確認です。

shimada@debian:~$ cd /media/usb0
shimada@debian:/media/usb0$ ls -l
合計 12
-rwxr-xr-x 1 shimada shimada 6584 2011-06-23 14:04 hoge
-rwxr-xr-x 1 shimada shimada 66 2011-06-23 14:00 hoge.c

いいですね。これで実行できるはずです。

shimada@debian:~$ cd /media/usb0
shimada@debian:/media/usb0$ ./hoge
hoge

「hoge」と表示できてこれほどうれしかったことは無いような気がします。

~~追記~~

FAT系だとシンボリックリンクを作ることができません。これだとLinux上で使用する際に不都合が生じます。
#FAT系は構造的にsymlink(2)を実装できないのだと思います・・・

そこで、試したところntfsだとシンボリックリンクを作成できたので、usbmout経由でntfsでフォーマットされたUSBメモリをマウントする方法を書いておきます。

/etc/usbmout/usbmout.confのFILESYSTEMSのオプションにntfsを追加します。

# Filesystem types: removable storage devices are only mounted if they
# contain a filesystem type which is in this list.
FILESYSTEMS="ntfs vfat ext2 ext3 ext4 hfsplus"

さらにFATの時と同様にFS_MOUNTOPTIONSでntfsを追加します。

# For example, "-fstype=vfat,gid=floppy,dmask=0007,fmask=0117" would add
# the options "gid=floppy,dmask=0007,fmask=0117" when a vfat filesystem
# is mounted.
FS_MOUNTOPTIONS="-fstype=ntfs,uid=shimada,gid=shimada"

これでマウントできるはずです。