在挂载某个分区前需要先建立一个挂载点,这个挂载点是以目录的形式出现的。一旦把某个分区挂载到这个目录下,那么再往这个目录里写数据时,则都会写到该分区中。在挂载该分区前,挂载点下最好是个空目录,不为空也不影响该分区的使用,但是会把以前的东西遮挡住看不到了,只有再卸载了才能看到。
命令:mount
常用选项:-a , -t , -o
如果不加任何选项,直接运行mount命令。会查看当前系统已经挂载的所有分区,以及文件系统的类型,挂载点和一些选项等信息。
[[email protected] ~]# mount
/dev/sda3 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)
建立一个空目录,以及一个空白文档。
[[email protected] ~]# mkdir /newdir
[[email protected] ~]# touch /newdir/newfile.txt
[[email protected] ~]# ls !$
ls /newdir/newfile.txt
/newdir/newfile.txt
然后挂载/dev/sdb5
[[email protected] ~]# mount /dev/sdb5 /newdir/
mount: wrong fs type, bad option, bad superblock on /dev/sdb5,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
不能完成挂载,根据提示可以查看一下错误信息:
[[email protected] ~]# dmesg |tail
ip6_tables: (C) 2000-2006 Netfilter Core Team
nf_conntrack version 0.5.0 (7909 buckets, 31636 max)
ip_tables: (C) 2000-2006 Netfilter Core Team
e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
type=1305 audit(1463181232.556:3): audit_pid=1201 old=0 auid=4294967295 ses=4294967295 res=1
eth0: no IPv6 routers present
sd 0:0:1:0: [sdb] Cache data unavailable
sd 0:0:1:0: [sdb] Assuming drive cache: write through
sdb: sdb1 < sdb5 sdb6 >
EXT4-fs (sdb5): bad block size 8192
可以看到,/dev/sdb5指定的块值8192不合法,所以只能重新格式化磁盘。
[[email protected] ~]# mke2fs -t ext4 -L TEST /dev/sdb5
继续挂载磁盘:
[[email protected] ~]# mount /dev/sdb5 /newdir/
[[email protected] ~]# ls /newdir/
lost+found //原来的newfile.txt被覆盖了
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot
/dev/sdb5 988M 1.3M 935M 1% /newdir
我们用umount命令卸载磁盘分区,然后用LABEL的方式挂载分区:
[[email protected] ~]# umount /newdir/
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot
[[email protected] ~]# mount LABEL=TEST /newdir
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot
/dev/sdb5 988M 1.3M 935M 1% /newdir
使用UUID进行挂载,通过blkid命令获取各分区的UUID
[[email protected] ~]# blkid
/dev/sda1: UUID="82d89195-8a63-4d3f-944f-03b308893c3f" TYPE="ext4"
/dev/sda2: UUID="38631717-1603-4987-a781-9e378950bc48" TYPE="swap"
/dev/sda3: UUID="f77b4c47-ca03-43f9-b420-cc0be633446a" TYPE="ext4"
/dev/sdb5: LABEL="TEST" UUID="9b16b8cb-b854-4964-a126-9f9d31955c22" TYPE="ext4"
也可以指定哪个分区使用
[[email protected] ~]# blkid /dev/sdb5
/dev/sdb5: LABEL="TEST" UUID="9b16b8cb-b854-4964-a126-9f9d31955c22" TYPE="ext4"
挂载:
[[email protected] ~]# umount /newdir
[[email protected] ~]# mount UUID="9b16b8cb-b854-4964-a126-9f9d31955c22" /newdir
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot
/dev/sdb5 988M 1.3M 935M 1% /newdir
如果想让某个分区开机后就自动挂载,有两个办法可以实现:在/etc/fstab中添加一行,如上例中那行;把挂载命令写到/etc/rc.d/rc.local文件中去,系统启动完后会执行这个文件中的命令。
[[email protected] ~]# cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
挂载磁盘时最好使用UUID或LABEL这两种方法。
命令:umount
这个命令后面可以跟挂载点也可以跟分区名,但是不能跟LABEL和UUID
[[email protected] ~]# umount /dev/sdb5
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot
[[email protected] ~]# mount UUID="9b16b8cb-b854-4964-a126-9f9d31955c22" /newdir
[[email protected] ~]# umount /newdir
[[email protected] ~]# mount UUID="9b16b8cb-b854-4964-a126-9f9d31955c22" /newdir
umount命令有一个非常有用的选项 -l ,有时会遇到不能卸载的情况:
[[email protected] newdir]# umount /newdir
umount: /newdir: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
这是因为当前目录在要卸载的分区上。解决方法两种:一是到其他目录,二是使用 -l 选项:
[[email protected] newdir]# umount -l /newdir
[[email protected] newdir]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.6G 15G 10% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 154M 15% /boot