Chinaunix首页 | 论坛 | 博客
  • 博客访问: 184250
  • 博文数量: 20
  • 博客积分: 543
  • 博客等级: 下士
  • 技术积分: 411
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-06 20:38
文章存档

2012年(12)

2009年(8)

分类: LINUX

2012-09-21 20:50:15

用mdev可以实现U盘的热插拔管理:在U盘插入时自动建立一个文件夹并把U盘挂载在该文件夹,当U盘拔掉时自动卸载并把该文件夹删掉。

现在出现一个这样的问题:
当不运行某进程时,插拔U盘一切正常;
在某进程运行期间插拔U盘,一切正常;
先插U盘,再运行某进程,在某进程运行期间拔掉U盘, 这时文件夹不能删除,删除文件夹返回EBUSY错误。
经查,该进程没有任何读写U盘文件的操作。

问题出在哪里呢?原来是该进程调用 unshare 系统调用导致的。
从unshare的手册页中可以看到:
unshare()  allows a process to disassociate parts of its execution context that are currently being shared with other processes.  Part of the execution  context, such as the mount namespace, is shared implicitly when a new process is created using fork(2) or vfork(2), while other parts, such as virtual  memory, may be shared by explicit request when creating a process using clone(2).

CLONE_FS
Reverse  the  effect of the clone(2) CLONE_FS flag.  Unshare file system attributes, so that the calling process no longer shares its root directory, current directory, or umask attributes with any other process.  chroot(2), chdir(2), or umask(2)

问题明了了: 在fork后,该进程继承了系统的所有挂载点,包括U盘的挂载目录;在用CLONE_FS选项调用unshare后,所有挂载点与系统脱离,成为一个独立的namespace,系统的所有挂载卸载操作不能影响到本进程的挂载点。 此时把U盘拔掉,系统已经把U盘卸载,而在该进程里面,仍然是挂载的, U盘的挂载目录仍然在使用中, 因此不能删除。 可以从  cat /proc//mounts 中读取挂载点信息来验证这个结论。

阅读(6125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~