思路
只能挂载在根分区,根由谁来挂载?
mount命令详解
分区, 创建、调整、查看文件系统,挂载文件系统、显示挂载信息、验证挂载
写时复制和原处读写
同步IO和异步IO的区别
在创建文件系统之后,如果要使用此文件系统,应该将设备/分区关联至根设备的某个目录之下
根由内核管理,是一切的起始
根的由来

1)刚开机时,post加电自检
2)自检后,装载bootloader
3)bootloader装载用户 所选择的操作系统内核(系统启动装载的第一个程序)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
CentOS 7 : /etc/grub2 .cfg 中定义了
{
load_video
linux16 /boot/vmlinuz-3 .10.0-514.26.2.el7.x86_64 root=UUID=07151862-c2b9-45dc-bf7a-af8d2a6fa6c1 rocrashkernel=auto rhgb quiet net.ifnames=0 LANG=en_US.UTF-8
root=UUID=07151862-c2b9-45dc-bf7a-af8d2a6fa6c1
initrd16 /boot/initramfs-3 .10.0-514.26.2.el7.x86_64.img
}
CentOS 6 : /boot/grub/grub .conf中定义了
title CentOS 6 (2.6.32-696.el6.i686)
root (hd0,0)
kernel /vmlinuz-2 .6.32-696.el6.i686 ro root= /dev/mapper/myvg-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=myvg /lv_swap KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet rd_NO_DM rd_LVM_LV=myvg /lv_root rhgb quiet
initrd /initramfs-2 .6.32-696.el6.i686.img
根的位置:人为的通过grub配置文件,传递给内核,内核才能加载根
|
4)内核(通用目的的管理程序,不会完成特定目的的操作)初始化后,取得系统的控制权
/内核初始化后---> 装载根 --> 启动根文件系统上的Init程序(完成特定目的操作)
为了完成特定目的的操作,应该启动应用程序,存储在外部存储设备上的应用程序
1、装载某个磁盘分区,根文件系统所在的分区,满足FHS标准的分区
1)根分区:启动时用到的分区
2)启动后,通过根分区某个路径下的mount命令关联其他分区至根,保证任何文件都处于根下
3) 根必须提供启动所依赖的程序和库文件,且只能在根文件系统中(bin,sbin,lib,lib64)
2、启动用户空间的第一个程序(/sbin/init)
init程序代替内核,启动程序:
1、所有程序都是init的子进程,都是init仿照自己的样子捏出来的, init 程序fork自身而来
2、任何子进程也能有子进程
3、init的类型:
pstree查看:CentOS5:init, CentOS6: initstart, CentOS7: systemd
如何通过mount命令关联其他操作系统
挂载

1) 关联的目录非空时,挂载后,此目录中的文件暂时隐藏
2) 挂载后,内核会将挂载信息记录在/proc/mounts文件中
3)将两个非根文件系统关联需要一个中间层 或者 为了保证一切文件都属于根,则必须先将一个文件系统关联至根,然后才能将另一个文件系统关联至此文件系统,已经关联至根的文件系统.
卸载

1) 卸载一个被非根设备关联的文件系统时,应该先卸载关联的所有设备,才能解除与根的关联关系
2) 卸载时,如果将要被卸载的设备,正在被某个进程访问,是不能解除关联关系.(例如cd进程)
分区,创建、查看、调整文件系统 ,挂载,显示挂载情况,挂载鸡,配置开机自动挂载,卸载
挂载
mount命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
[root@localhost ~]
mount is /bin/mount
[root@localhost ~]
mount [OPTIONS...] device_partition MOUNT_POINT
DEVICE_PARTITION:
1)设备文件, /dev/sda1
2)全局唯一标示,-U UUID。推荐:UUID:设备文件名改变时,UUID唯一不变
3) 卷标, -L LABEL
4) 内核支持的伪文件系统,proc,sysfs,devtmpfs,configfs
5) 目录,--bind DIR
MOUNT_POINT:
DIR:建议为空目录,非空目录时,挂载后原目录中的文件暂时会隐藏
临时挂载点:mnt,media
OPTIONS:
-t vfstype
-r readonly ,
-w read and write,
-a auto,
- v ,verbose
-n
--bind
-L LABEL
-U UUID
-o OPTIONS:
async
sync
atime
noatime
diratime
nodiratime
auto
noauto
exec /noexec
dev /nodev
suid /nosuid
remount
ro /rw
user /nouser
acl
CentOS 5,6:默认不启用acl
CentOS 7 : 默认启用acl
default
CentOS 默认挂载选项:
rw, suid, dev, exec , auto, nouser, async, and relatime.
|
##将设备关联至目录,目录作为设备的访问入口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
1、查看所有分区
[root@localhost ~]
2、修改分区
[root@localhost ~]
1) 分区新分区
Command (m for help): n
2) 分配空间
Last cylinder, +cylinders or +size{K,M,G} (1576-2610, default 2610): +100M
3)查看分区
Command (m for help): p
4)保存并退出
Command (m for help): w
3、查看内核识别的分区表
[root@localhost ~]
[root@localhost ~]
4、让内核重读分区表
[root@localhost ~]
[root@localhost ~]
5、格式化、创建新的文件系统
[root@localhost ~]
块大小=4096 (log=2)
131122 blocks (5.00%) reserved for the super user
Superblock backups stored on blocks
6、将设备 /dev/sdb8 关联至 /mnt 目录
[root@localhost ~]
|
##查看挂载信息
1
2
3
4
5
6
7
8
|
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
|
1
2
3
4
5
6
7
8
9
|
+++++++++++++++++++++++++++++++++
设备 挂载点 文件系统类型 访问权限(rw)
实现资源分割与管理
proc --> /proc
sysfs --> /sys
devpts --> /dev/pts
tmpfs --> /dev/shm
+++++++++++++++++++++++++++++++++
|
##挂载验证
1
2
3
|
[root@localhost ~]
lost+found
|
##解除关联关系
1
2
3
4
|
1、目录
[root@localhost ~]
2、设备
[root@localhost ~]
|
##卸载时,如果出现device busy时,有进程正在访问此设备
1
2
3
4
5
6
7
|
fuser - v MOUNT_POINT
lsof - v MOUNT_POINT
bash : bash 内建命令在访问
fuser -km MOUNT_POINT (root)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost mnt]
[root@localhost mnt]
用户 进程号 权限 命令
/mnt : root 2142 ..c.. bash
[root@localhost mnt]
/mnt : 2142c
Connection closed by foreign host.
Disconnected from remote host(192.168.1.103:22) at 10:46:17.
Type `help' to learn how to use Xshell prompt.
[c:\~]$
|
使用选项注释
PARTITIONS:
1、-U UUID
1
2
3
4
5
6
7
8
9
10
11
12
|
1 、获取UUID
[root@localhost ~]# blkid /dev/sda3
/dev/sda3: UUID= "8e02ad4b-818b-405e-96b1-2f8aedf9136b" TYPE= "ext4"
2 、以UUID挂载:
[root@localhost ~]# mount -U 8e02ad4b-818b-405e-96b1-2f8aedf9136b /mnt
3 、验证挂载
[root@localhost ~]# ls /mnt
lost+found
[root@localhost ~]# mount
/dev/sda3 on /mnt type ext4 (rw)
|
2、-L LABEL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
1、修改卷标为MYDATA
[root@localhost ~]
tune2fs 1.41.12 (17-May-2010)
2、查看LABEL
[root@localhost ~]
/dev/sda3 : UUID= "8e02ad4b-818b-405e-96b1-2f8aedf9136b" TYPE= "ext4" LABEL= "MYDATA"
3、以LABEL挂载
[root@localhost ~]
4、验证挂载
[root@localhost ~]
lost+found
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw)
|
3、伪文件系统挂载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
1、查看伪文件系统
[root@localhost ~]
proc on /proc type proc (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
sysfs on /sys type sysfs (rw)
2、查看伪文件系统生成的文件
[root@localhost ~]
block bus class dev devices firmware fs hypervisor kernel module power
3、卸载伪文件系统
[root@localhost ~]
[root@localhost ~]
4、查看
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
0
5、挂载伪文件系统
[root@localhost ~]
[root@localhost ~]
2
[root@localhost ~]
6、查看
[root@localhost ~]
ls /sys
block bus class dev devices firmware fs hypervisor kernel module power
|
4、目录绑定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
mount --bind olddir newdir
1、显示 /mnt 下的文件
[root@localhost ~]
lost+found
2、显示 /sys 下的文件
[root@localhost ~]
block bus class dev devices firmware fs hypervisor kernel module power
3、绑定目录
[root@localhost ~]
4、显示Newdir
[root@localhost ~]
block bus class dev devices firmware fs hypervisor kernel module power
5、查看挂载信息
[root@localhost ~]
/sys on /mnt type none (rw,bind)
|
OPTIONS:
-r 只读挂载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1、测试挂载
[root@localhost ~]
[root@localhost ~]
lost+found
2、查看挂载信息
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw)
3、卸载后,重新挂载
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro)
|
挂载时,内核将信息记录于/proc/mounts文件中,然后同步至/etc/mtab文件中
-n 此次挂载不同步信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@localhost ~]
[root@localhost ~]
/dev/mapper/myvg-lv_root on / type ext4 (rw)
proc on /proc type proc (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/mapper/myvg-lv_home on /home type ext4 (rw)
/dev/mapper/myvg-lv_usr on /usr type ext4 (rw)
/dev/mapper/myvg-lv_var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sysfs on /sys type sysfs (rw)
[root@localhost ~]
/dev/sda3 /mnt ext4 rw,relatime,barrier=1,data=ordered 0 0
|
-a 挂载所有定义在fstab文件中且设备挂载选项支持自动挂载的设备
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
1 、定义fstab
/dev/sdb1 /mydata ext4 defaults,acl 2 3
2 、创建MOUNT_POINT
install -d /mydata
2 、自动挂载
[root@localhost ~]# mount | fgrep /mydata
[root@localhost ~]# echo $?
1
[root@localhost ~]# mount -a -v
mount: UUID=5009dc18-28f5-4b32-8c7c-0ea1328ea224 already mounted on /boot
mount: /dev/mapper/myvg-lv_home already mounted on /home
mount: /dev/mapper/myvg-lv_usr already mounted on /usr
mount: /dev/mapper/myvg-lv_ var already mounted on / var
mount: tmpfs already mounted on /dev/shm
mount: devpts already mounted on /dev/pts
mount: sysfs already mounted on /sys
mount: proc already mounted on /proc
/dev/sdb1 on /mydata type ext4 (rw,acl)
3 、查看:
[root@localhost ~]# mount | fgrep /mydata
/dev/sdb1 on /mydata type ext4 (rw,acl)
|
-v 挂载信息
1
2
3
4
5
6
7
|
[root@localhost mnt]
/dev/sda3 on /mnt type ext4 (ro)
[root@localhost /]
[root@localhost /]
mount : you didn't specify a filesystem type for /dev/sda3
I will try type ext4
/dev/sda3 on /mnt type ext4 (rw)
|
-o options:
defaults CentOS 默认挂载选项: rw, suid, dev, exec, auto, nouser, async, and relatime.
man mount中查看
1
2
3
4
5
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw)
********其他默认的属性信息不会显示********
|
ext4系列的文件系统,默认async,nodiratime
CentOS7,默认acl
sync 同步IO
1
2
3
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw, sync )
|
nosuid 挂载的设备上的文件有suid权限时,不能运行为一个进程
1
2
3
4
5
|
[root@localhost ~]
umount /mnt
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw,nosuid)
|
remount 重新挂载,不卸载的情况下,完成设备重新挂载,用于修改挂载属性
1
2
3
4
5
6
7
8
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro)
[root@localhost /]
[root@localhost /]
|
nodev 挂载的文件系统上,有设备文件,不能使用
1
2
3
4
|
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (rw,nosuid,nodev)
|
noexec 挂载的文件系统上,有程序文件,不能运行为一个进程
1
2
3
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro,noexec)
|
noauto 挂载的文件系统,不支持通过 fstab文件自动挂载
1
2
3
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro,noexec)
|
默认属性,默认只显示rw,修改为Noauto也不会显示
user 挂载的设备能被普通用户挂载
1
2
3
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro,noexec,nosuid,nodev)
|
当调整为普通用户可挂载时,自动进行,调整为其内部的设备不可用。且用户可挂载此设备属性不会显示
1
2
3
|
[root@localhost ~]
[root@localhost ~]
/dev/sda3 on /mnt type ext4 (ro,noexec,nosuid)
|
noatime 挂载的文件系统,不更新文件访问时间戳
atime: 每次访问文件时,更新磁盘中的文件的时间戳。写磁盘产生,IO产生
##测试默认属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@localhost ~]
[root@localhost dir ]
/dev/sda3 : UUID= "8e02ad4b-818b-405e-96b1-2f8aedf9136b" TYPE= "ext4" LABEL= "MYDATA"
[root@localhost ~]
[root@localhost ~]
[root@localhost dir ]
Access: 2017-08-09 05:42:49.662993539 +0800
Modify: 2017-08-09 05:42:49.662993539 +0800
Change: 2017-08-09 05:42:49.766999113 +0800
[root@localhost dir ]
[root@localhost dir ]
2017年 08月 09日 星期三 05:44:05 CST
[root@localhost dir ]
Access: 2017-08-09 05:44:02.933994163 +0800
Modify: 2017-08-09 05:42:49.662993539 +0800
Change: 2017-08-09 05:42:49.766999113 +0800
|
##关闭atime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@localhost /]
[root@localhost /]
/dev/sda3 on /mnt type ext4 (rw,noatime)
[root@localhost /]
访问前
[root@localhost dir ]
Access: 2017-08-09 05:44:02.933994163 +0800
Modify: 2017-08-09 05:42:49.662993539 +0800
Change: 2017-08-09 05:42:49.766999113 +0800
[root@localhost dir ]
2017年 08月 09日 星期三 06:09:22 CST
访问
[root@localhost dir ]
访问后的时间
[root@localhost dir ]
Access: 2017-08-09 05:44:02.933994163 +0800
Modify: 2017-08-09 05:42:49.662993539 +0800
|
diratime/nodiratime 目录访问时间戳
ext默认不启用目录访问时间戳
##测试默认属性
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost dir ]
[root@localhost mnt]
Access: 2017-08-09 05:43:50.596000162 +0800
Modify: 2017-08-09 05:42:49.790996746 +0800
Change: 2017-08-09 05:42:49.790996746 +0800
[root@localhost mnt]
[root@localhost dir ]
[root@localhost dir ]
[root@localhost mnt]
stat dir
Access: 2017-08-09 05:43:50.596000162 +0800
Modify: 2017-08-09 05:42:49.790996746 +0800
Change: 2017-08-09 05:42:49.790996746 +0800
|
##启用diratime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@localhost mnt]
[root@localhost mnt]
File: "dir"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: 803h /2051d Inode: 518145 Links: 2
Access: (0755 /drwxr-xr-x ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-08-09 05:43:50.596000162 +0800
Modify: 2017-08-09 05:42:49.790996746 +0800
Change: 2017-08-09 05:42:49.790996746 +0800
[root@localhost mnt]
[root@localhost dir ]
[root@localhost mnt]
File: "dir"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: 803h /2051d Inode: 518145 Links: 2
Access: (0755 /drwxr-xr-x ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-08-09 05:43:50.596000162 +0800
Modify: 2017-08-09 05:42:49.790996746 +0800
Change: 2017-08-09 05:42:49.790996746 +0800
|
mount命令挂载特性总结:
auto和user,无论启用或关闭均不会显示
目录diratime默认不启用。文件atime默认启用
默认为async
写时复制和原地读写
shell程序 (特殊字符的转换,词法/语法分析) ---> 提交给内核 -->内核加载把程序从磁盘加载至内存,分配CPU时针周期,内存资源的分配-->轮到程序在CPU上运行时--> CPU加载指令--> 运行指令 -->指令运行需要数据时,从内存中读取数据

同步IO和异步IO的区别
程序运行完成了,就会将内存中的数据同步至磁盘.同步的方式有"同步IO" "异步IO"
