2-挂载、卸载文件系统


2016.6.14

mount和umount

  • mount命令:挂载文件系统、ISO镜像

    • mount [-t类型] 存储设备 挂载点目录
    • mount -o loop ISO镜像文件 挂载点目录
  • umount命令:卸载已挂载的文件系统

    • umount 存储设备位置
    • umount 挂载点目录
  • 示例

    • 建立挂载点目录:/mailbox
    • 挂载分区设备:/dev/sdb1
    • 访问分区设备:
    • 挂载点目录/mailbox中创建文件进行测试
    • 查看/mailbox目录中的内容
    • 查看磁盘挂载情况(mount)
    • 卸载分区设备:/dev/sdb1

创建目录:

[root@test1 jason]# mkdir /mailbox
[root@test1 jason]# ls
learn
[root@test1 jason]# cd /
[root@test1 /]# ls
bin   cloud  etc      home       lib    lost+found  media  mnt  opt   root  selinux  sys  usr
boot  dev    ftphome  lalala.oo  lib64  mailbox     misc   net  proc  sbin  srv      tmp  var
[root@test1 /]#
```bash

挂载前的查看:
```bash
[root@test1 /]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
[root@test1 /]#

执行挂载,并查看挂载后的状态:

[root@test1 /]# mount /dev/sdb1 /mailbox/
[root@test1 /]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sdb1 on /mailbox type ext4 (rw)   //新挂载的
[root@test1 /]#

第一种卸载方式:umount /dev/sdb1

[root@test1 /]# umount /dev/sdb1
[root@test1 /]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
[root@test1 /]#

重新挂载,用第二种方式来卸载:

[root@test1 /]# mount /dev/sdb1 /mailbox/
[root@test1 /]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sdb1 on /mailbox type ext4 (rw)
[root@test1 /]# umount /mailbox/
[root@test1 /]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
[root@test1 /]#
[root@test1 /]# cd mailbox/
[root@test1 mailbox]# ls

lost+found

解释:独立地分区都会有lost+found这个文件夹

before mount:

[root@test1 mailbox]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sdb1 on /mailbox type ext4 (rw)

mount:

[root@test1 mailbox]# mount /dev/cdrom /mnt
mount: block device /dev/sr0 is write-protected, mounting read-only

after mount:
```bash
[root@test1 mailbox]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sdb1 on /mailbox type ext4 (rw)
/dev/sr0 on /mnt type iso9660 (ro)
[root@test1 mailbox]#
[root@test1 mailbox]# cd
[root@test1 ~]# ls -l /dev/sr0 /dev/cdrom /dev/dvd
lrwxrwxrwx 1 root root      3 Jun 14 09:13 /dev/cdrom -> sr0
lrwxrwxrwx 1 root root      3 Jun 14 09:13 /dev/dvd -> sr0
brw-rw---- 1 root cdrom 11, 0 Jun 14 09:13 /dev/sr0
[root@test1 ~]#

这就是为什么每次挂载光驱的时候提示的都是/dev/sr0,因为cdrom是一个链接

卸载及查看:

[root@test1 ~]# umount /dev/sr0
[root@test1 ~]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sdb1 on /mailbox type ext4 (rw)
[root@test1 ~]#

设置文件系统的自动挂载

  • /etc/fstab配置文件
    • 包含需要开机后自动挂载的文件系统记录
    • 第一字段:设备名或设备卷标名
    • 第二字段:文件系统的挂载点目录的位置
    • 第三字段:文件系统类型,如EXT4,Swap等。
    • 第四字段:挂载参数,及mount命令“-o”选项后可使用的参数。例如,defaults、rw、ro、noexec分别表示默认参数、可写、只读、禁用执行程序。
    • 第五字段:表示文件系统是否需要dump备份(dump是一个备份工具)。一般设为1时表示需要,设为0时被dump所忽略。
    • 第六字段:该数字用于决定在系统启动时进行磁盘检查的顺序,0表示不进行检查,1表示优先检查,2表示其次检查。对于根分区设为1,其他分区设为2.

截图:

  • 实例:每次重新开机后,能够自动完成挂载
    • 将/dev/sdb1分区挂载到/mailbox目录

修改/etc/fstab配置文件:

[root@test1 ~]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue May 24 00:02:03 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_test-lv_root /                       ext4    defaults        1 1
UUID=a844ed1c-d5cf-4898-90f7-e9f040f4426e /boot                   ext4    defaults        1 2
/dev/mapper/vg_test-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0        /mnt    iso9660 0 0
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/fstab" 16L, 803C

测试:

[root@test1 ~]# reboot

Broadcast message from jason@test1
    (/dev/pts/0) at 10:35 ...

The system is going down for reboot NOW!
[root@test1 ~]#

失败:

[root@test1 jason]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
[root@test1 jason]# cd /mnt
[root@test1 mnt]# ls
benets2  hgfs  inittab  test
[root@test1 mnt]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue May 24 00:02:03 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_test-lv_root /                       ext4    defaults        1 1
UUID=a844ed1c-d5cf-4898-90f7-e9f040f4426e /boot                   ext4    defaults        1 2
/dev/mapper/vg_test-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0        /mnt    iso9660 defaults 0 0
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq

再次重启(略) 再测试:

[root@test1 jason]# mount
/dev/mapper/vg_test-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sr0 on /mnt type iso9660 (ro)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
[root@test1 jason]# cd /mnt
[root@test1 mnt]# ls
CentOS_BuildTag  images                    repodata                       RPM-GPG-KEY-CentOS-Testing-6
EFI              isolinux                  RPM-GPG-KEY-CentOS-6           TRANS.TBL
EULA             Packages                  RPM-GPG-KEY-CentOS-Debug-6
GPL              RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Security-6
[root@test1 mnt]#

成功

总结:原因是上面少写了了一个defaults


results matching ""

    No results matching ""