天天看点

linux/unix mount 我的理解

以下是我个人对mount命令相关知识的整理,可能有不对地方。

一、 mount

命令:mount  umount  mountpoint mountstatus mount.type  umount.type

文件:/etc/fstab  /etc/mtab  /proc/mounts  函数: /usr/include/sys/mount.h

函数中的语法:

/* Mount a filesystem.  */

extern int mount (__const char *__special_file, __const char *__dir,

          __const char *__fstype, unsigned long int __rwflag,

          __const void *__data) __THROW;

/* Unmount a filesystem.  */

extern int umount (__const char *__special_file) __THROW;

/* Unmount a filesystem.  Force unmounting if FLAGS is set to MNT_FORCE.  */

extern int umount2 (__const char *__special_file, int __flags) __THROW;

1、/etc/fstab  /etc/mtab  /proc/mounts

 /etc/fstab   系统启动加载时自动挂载的系统目录,如/   /home等,系统准备装载的,通常只读、静态的。

/etc/mtab   系统当前系统挂载,记载的是现在系统已经装载的文件系统,动态,可以RW。

 /proc/mounts  来自系统内部动态挂载,其实是个link.

[root@heliy fukeyun]# ll /proc/mounts 

lrwxrwxrwx. 1 root root 11 11月 29 16:12 /proc/mounts -> self/mounts

[root@heliy fukeyun]# ls /proc/self/mount

mountinfo   mounts      mountstats 

 When  the  proc  filesystem is mounted (say at /proc), the files

 /etc/mtab and /proc/mounts have very similar contents. The  for-

mer  has  somewhat  more  information, such as the mount options

used, but is not  necessarily  up-to-date  (cf.  the  -n  option

 below).  It  is possible to replace /etc/mtab by a symbolic link

 to /proc/mounts, and especially when you have very large numbers

 of mounts things will be much faster with that symlink, but some

 information is lost that way, and in particular using the "user"

 option will fail

/proc/mounts 可能替代/etc/mtab.

在命令mount -a 参数时调用/etc/fstab   mount 没有参数时调用/etc/mtab

2 、各个命令

mount

格式:mount  参数  device  dir

参数:-a [-fFnrsvw] [-t vfstype]  [-o option[,option]...]

device: 我所知的有三种表示,常用的是块文件,如/dev/sda6。还有LABEL or UUID,NFS的IP:dir。

[fukeyun@heliy 桌面]$ ls /dev/disk/

by-id  by-label  by-path  by-uuid

等,例如uuid、LABEL

UUID=a6f7d427-347f-4b46-90d7-483d8ab556b6   /   ext4  defaults    1 1

LABEL=NEW\x20VOLUME    /mnt/usb   ext4    defaults    1 1

dir: 要注意此目录最好为空,如果有文件或目录,在mount后将被隐藏,在mount期间不可用,当umount后才可用。

参数:我常用的参数主要为 -a -f -t -o,其中-o内各个子参数要好好利用,功能较多。

-t: 支持的格式有 adfs,  affs,  autofs,  cifs,  coda,  coherent,  cramfs,debugfs, devpts, efs, ext, ext2, ext3, ext4, hfs, hfsplus, hpfs,iso9660, jfs, minix, msdos, ncpfs, nfs, nfs4, ntfs, proc,  qnx4, ramfs,  reiserfs,  romfs,  squashfs,  smbfs, sysv, tmpfs, ubifs, udf, ufs, umsdos, usbfs, vfat, xenix,  xfs,  xiafs. 

mount对挂载格式的调用是先-t 指定的类型,如果没有-t指定即默认为auto(auto根据 blkidor volume_id来判断 ),如果auto也无法判断,则mount查询/etc/filesystems,filessystems也没有,则查询/proc/filesystems.

-o: 子参数较多,有些启用,有些禁用被system kernel。/proc/mounts可查看些。

对所以格式都适用的常用子参数:

async  atime notime  auto noauto  dafault(rw/suid/dev/exec/auto/nouser/async) dev  nodev diratime  nodiratime  dirsync  exec noexec group _netdev  nofail  relatime norelatime suid nosuid owner remount ro rw sync user nouser users

对ntfs的有:iocharset utf8 uid gid umask ,对cifs、nfs见mount.cifs ,mount.nfs。

 -V 打印版本信息

-h 打印帮助信息

-l 列出所有挂载的系统,包括挂载卷标

-t<类型> 指定要挂载或者列出文件系统类型

-v 打印程序详细信息

-a 挂载fstab中提到的所有文件系统

-F 和“-a” 联用,以并行顺序为每个设备挂载

-f 完成除了实际系统调用之外所有的挂载工作,但是并不实际挂载设备

-i 不调用/sbin/mount<文件系统>的帮助,即使文件存在

-n 在挂载过程中不向/etc/mtab 中写入

-p<数字> 在加密挂载的情况下,从数字指定的文件描述符中读取口令

-s 容忍在挂载过程中的一些错误

-r 以只读的模式挂载设备,与“-o or”相同

-L<标签> 只挂在符合指定标签的分区

-U 只挂载符合指定标签的分区

-t<类型> 指定要挂载的文件类型

-O 和“-a” 连用,指定不希望挂载的设备

-o 指定一些操作

--bind 重新挂载设备,在两个挂载点下都可以使用

--move 把一个挂载树移到其他地方

 在fstab加一行使普通用户权限能对cdrom挂载

/dev/cdrom  /cd  iso9660  ro,user,noauto,unhide

any  user  can  mount  the iso9660 filesystem found on his CDROM using the command。

多个挂载点 --bind或-R   --rbind或-R ,这两个参数只是增加挂载点,不会改变初始挂载点,当初始挂载点被umount时,这些增加的还能用。效果有点像hardware link一样。如果用命令,则重启后没有,必须在fstab里指定。

  mount --bind olddir newdir

  mount -B olddir newdir

  在fstab里:  /olddir /newdir none bind

 mount --rbind olddir newdir

  mount -R olddir newdir

更改初始挂载点: mount -o remount  newdir

或                       mount --move olddir newdir

                          mount -M olddir newdir

--make-shared 这个即后面的几个参数,我现在还没理解透,以后补充。

mount返回码含义:

       mount has the following return codes (the bits can be ORed):

       0      success

       1      incorrect invocation or permissions

       2      system error (out of memory, cannot fork, no more loop devices)

       4      internal mount bug

       8      user interrupt

       16     problems writing or locking /etc/mtab

       32     mount failure

       64     some mount succeeded

这是网上找的几个例子:

挂载 windows 共享目录

root@snail-hnlinux:~# mount -t cifs -o username=administrator,password=123456 192.168.1.20:Download /media/share

挂载 NFS 共享目录

mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 192.168.1.12:/home/uptech/ /media/nfs/

挂载其它设备

root@snail-hnlinux:~# mount -o loop /home/rootfs.cramfs /media/cm

mountpoint  判断一个目录是不是mount点。

二、 automount

 服务:/etc/init.d/autofs,默认开机启动

命令:automount

文件:/etc/auto.master  auto.misc auto.net  auto.smb等

命令automount是临时配置,重启OS后失效,所以要配置必须在文件里配置并重启autofs服务。

下面的例子加深大家理解:

<a href="http://down.51cto.com/data/2353318" target="_blank">附件:http://down.51cto.com/data/2353318</a>

      本文转自fuhaixiong 51CTO博客,原文链接:http://blog.51cto.com/heliy/174532,如需转载请自行联系原作者

继续阅读