二、存储管理(2)
1、ext3/4文件系统
认识EXT3/4文件系统
========================================================
Ext3: 文件系统最大16TB,单个文件最大2TB
Ext4: 文件系统最大1EB,单个文件最大16TB
无限数量的子目录/ Extents/ 多块分配/ 延迟分配/ 快速 fsck/ 日志校验/ “无日志”/ 在线碎片整理/ inode 相关特性/ 默认启用 barrier
Ext3/Ext4磁盘布局:
inode table: 存储文件的元数据(文件权限,时间戳,指向block的指针等信息)
data block: 存储是文件的实际数据
========================================================
查看superblock(文件系统的信息)
dumpe2fs
[root@yangs ~]# dumpe2fs /dev/sda2 |less
tune2fs
[root@yangs ~]# tune2fs -l /dev/sda3
tune2fs 1.39 (29-May-2006)
Filesystem volume name: yang
Last mounted on: <not available>
Filesystem UUID: 28459f88-87dc-4624-94a7-07b0f3eb2420
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 48960
Block count: 195312
Reserved block count: 9765
Free blocks: 183410
Free inodes: 48946
First block: 1
Block size: 1024
First inode: 11
Inode size: 128
Journal inode: 8
更改文件系统的信息
[root@yangs ~]# tune2fs -L yyy /dev/sda5
[root@yangs ~]# tune2fs -o acl /dev/sda5
[root@yangs ~]# tune2fs -o ^acl /dev/sda5
修复文件系统(修复超块)
强烈建议,先卸载文件系统
[root@yangs ~]# umount /dev/sda5
[root@yangs ~]# fsck /dev/sda5
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
yyy: clean, 13/122112 files, 8262/244140 blocks
[root@yangs ~]# e2fsck /dev/sda5
e2fsck 1.39 (29-May-2006)
yyy: clean, 13/122112 files, 8262/244140 blocks
2、文件链接原理
文件链接
========================================================
一、符号链接 symbolic link
[root@yangs ~]# echo 222 > /file222.txt
[root@sxl1 ~]# ln -s /file222.txt /boot/s-file222.txt
[root@sxl1 ~]# ll -i /file222.txt /boot/s-file222.txt
41 lrwxrwxrwx 1 root root 12 Jan 10 16:19 /boot/s-file222.txt -> /file222.txt
18 -rw-r--r-- 1 root root 4 Jan 10 16:18 /file222.txt
二、硬链接
[root@yangs ~]# echo 111 > /file111.txt
[root@yangs ~]# ln /file111.txt /etc
[root@yangs ~]# ln /file111.txt /etc/h-111.txt
[root@yangs ~]# ll -i /file111.txt /etc/file111.txt /etc/h-111.txt
17 -rw-r--r-- 3 root root 4 Jan 10 16:16 /etc/file111.txt
17 -rw-r--r-- 3 root root 4 Jan 10 16:16 /etc/h-111.txt
17 -rw-r--r-- 3 root root 4 Jan 10 16:16 /file111.txt
注:硬链接
1. 不能跨分区
2. 不支持目录做硬链接
[root@yangs home]# ln /home/ /mnt
ln: “/home/”: 不允许将硬链接指向目录
3、Mount挂载详解
Mount挂载详解
========================================================
挂载选项 -t 文件系统类型 -o 选项1,选项2...
示例:
mount -t ext4 -o acl /dev/sdb1 /mnt/disk1
文件系统类型:ext4,nfs,cifs
例1:exec/noexec
[root@yangs ~]# mount /dev/vg01/lv01 /mnt/lv01
[root@yangs ~]# mount -o noexec /dev/vg01/lv02 /mnt/lv02
[root@yangs ~]# cp -rf /bin/date /mnt/lv01
[root@yangs ~]# cp -rf /bin/date /mnt/lv02
[root@yangs ~]# /mnt/lv01/date
Fri Jan 10 10:40:21 CST 2014
[root@yangs ~]# /mnt/lv02/date
-bash: /mnt/lv02/date: Permission denied
[root@yangs ~]# mount
/dev/mapper/vg01-lv01 on /mnt/lv01 type ext4 (rw)
/dev/mapper/vg01-lv02 on /mnt/lv02 type ext4 (rw,noexec)
没有指定任何选项,则为默认选项 man mount
defaults
Use default options: rw, suid, dev, exec, auto,
nouser, async, and relatime.
常见的挂载选项
rw 读写
ro 只读
suid 支持suid
dev 支持设备文件
nodev 不支持设备文件
noexec 不允许执行二进制文件
exec 允许执行二进制文件
auto mount -a 开机自动挂载
noauto mount -a 开机不自动挂载
async 异步写入
sync 同步同入
noatime 不更新访问时间atime
usrquota 支持用户级磁盘配额功能
grpquota 支持组级磁盘配额功能
acl 支持acl功能
remount 在线重新挂载
========================================================
扩展知识:Relatime 驱动器访问优化
POSIX 标准要求操作系统维护记录每个文件最后一次被访问的文件系统元数据。这个时间戳被称为atime,
维护它需要一个重复的对存储的写入操作。这些写入操作让存储是设备及其连接保持忙碌和通电状态。因
为很少应用程序会使用 atime 数据,所以这个存储设备活动是在浪费电力。特别是即使没有从存储中读取
该文件也会发生写入存储的事件,但是从缓冲中写入。有时,Linux 内核还支持 mount 的 noatime选项,并
不在使用此选项挂载的文件系统中写入 atime。但是只是关闭这个特性是有问题的,因为有些应用程序会依
赖 atime 数据,并在此数据不可用时失败。
红帽企业版 Linux 6 使用的内核之后此另一个可替换选项 -relatime。Relatime 维护 atime数据,但不是
每次访问该文件时都更改。启用这个选项,则只在上次更新 atime(mtime)后修改该文件时,或者最后一次
访问该文件是在相当长一段时间前(默认为一天)时才会将 atime 数据写入磁盘。
默认情况下,所有现在挂载的文件系统都启用 relatime。要在整个系统中限制这个特性,请使用 boot 参
数 default_relatime=0。如果默认在某个系统中启用 relatime,您可以通过使用选项 norelatime
挂载某个系统来限制它在某个具体文件系统中的使用。最后,要使系统更新文件的 atime 数据的默认周期
有所不同,请使用 relatime_interval= 引导参数,以秒为单位指定周期。默认值为 86400。
========================================================
例2:async,sync
[root@yangs ~]# mount /dev/vg01/lv01 /mnt/lv01
[root@yangs ~]# mount -o sync /dev/vg01/lv02 /mnt/lv02
[root@yangs ~]# mount
/dev/mapper/vg01-lv01 on /mnt/lv01 type ext4 (rw)
/dev/mapper/vg01-lv02 on /mnt/lv02 type ext4 (rw,sync)
[root@yangs ~]# time cp -rf /etc /mnt/lv01
[root@yangs ~]# time cp -rf /etc /mnt/lv02
例3:acl
[root@yangs ~]# vim /etc/fstab
/dev/mapper/vg01-lv01 /mnt/lv01 ext4 defaults,acl 0 0
[root@yangs ~]# mount -o remount /mnt/lv01
[root@yangs ~]# mount
/dev/mapper/vg01-lv01 on /mnt/lv01 type ext4 (rw,acl)
挂载时可以使用(针对基本分区)
设备名 /dev/sda3
卷标 LABEL yang
UUID UUID="353a34b8-9233-465c-b7cf-92c8d308c05b"
UDEV 可以给设备建立一个别名
例4:使用卷标挂载
[root@yangs ~]# tune2fs -L yang /dev/sda3 //设置卷标LABEL
tune2fs 1.39 (29-May-2006)
[root@yangs ~]# vim /etc/fstab
LABEL=yang /mnt/disk1 ext4 defaults 0 0
例5:使用UUID挂载
[root@yangs ~]# blkid /dev/sda5
/dev/sda5: UUID="353a34b8-9233-465c-b7cf-92c8d308c05b" SEC_TYPE="ext2" TYPE="ext3"
[root@yangs ~]# vim /etc/fstab
UUID="353a34b8-9233-465c-b7cf-92c8d308c05b"/mnt/disk1 ext4 defaults 0 0
挂载ISO文件
1. 将光盘制作成iso
[root@yangs ~]# dd if=/dev/cdrom of=/rhel6.iso
[root@yangs ~]# dd </dev/cdrom >/rhel6.iso
2. 将文件制作成iso,例如将/etc制作成etc.iso
[root@yangs ~]# genisoimage -o /tmp/etc.iso -r /etc
[root@yangs ~]# file /tmp/etc.iso
/var/etc.iso: ISO 9660 CD-ROM filesystem data 'CDROM '
3. 使用iso,挂载
[root@yangs ~]# mount -t iso9660-o loop /tmp/etc.iso /mnt/iso/
4、Autofs自动挂载
Automount (Autofs)自动挂载
挂载是由访问产生的卸载是由超时产生的
========================================================
一、手动挂载(临时)
mount -t 文件系统类型 -o 选项1,选项2... 设备文件 挂载点
===本地设备文件
/dev/cdrom
/dev/sr0
/dev/sda5
/dev/vg01/lv01
/dev/md0
/dev/md1
/var/dvd.iso
[root@yangs ~]# mount -t ext4 -o ro /dev/sda5 /mnt/sda5
[root@yangs ~]# mount -t iso9660 -o loop /dvd.iso /mnt/dvd
[root@yangs ~]# mount -t iso9660 /dev/cdrom /mn/cd
===网络设备文件
NFS: 192.168.10.240:/home/dir1
CIFS: //192.168.10.240/dir2
[root@yangs ~]# mount -t nfs 192.168.10.240:/home/dir1 /mnt/dir1
[root@yangs ~]# mount -t cifs //192.168.0.240/dir2 /mnt/dir2
二、实现自动挂
第一种解决方案: /etc/fstab
/dev/sda5 /mnt/sda5 ext4 defaults 0 0
192.168.10.240:/home/dir1 /mnt/dir1 nfs defaults 0 0
//192.168.10.240/dir2 /mnt/dir2 cifs defaults 0 0
优点:可以实现开机自动挂载
缺点:由于网络连接的不稳定性,会导致网络设备挂载失败
结论:适合挂载本地设备和持续使用的设备
第二种解决方案: automount按需挂载
[root@yangs ~]# rpm -q autofs
autofs-5.0.5-54.el6.x86_64
[root@yangs ~]# grep '^TIMEOUT' /etc/sysconfig/autofs
TIMEOUT=300
案例1:使用automount将光盘/dev/cdrom自动挂载到/mnt/disk/cdrom
父挂载点(监控目录): /mnt/disk
子挂载点(关键字): cdrom
[root@yangs ~]# vim /etc/auto.master
/mnt/disk /etc/auto.yangsheng
[root@yangs ~]# vim /etc/auto.yangsheng
cdrom -fstype=iso9660,ro,nosuid,nodev:/dev/sr0
[root@yangs ~]# service autofs restart
案例2:使用automount将192.168.10.240:/home/dir1 /mnt/nfs/dir1
父挂载点:/mnt/nfs
子挂载点:dir1
[root@yangs ~]# vim /etc/auto.master
/mnt/nfs /etc/auto.nfs
[root@yangs ~]# vim /etc/auto.nfs
dir1 -ro 192.168.10.240:/home/dir1
[root@yangs ~]# service autofs restart
[root@yangs ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
192.168.10.240:/home/dir1
1032192 44544 935424 5% /mnt/nfs/dir1
========================================================
扩展知识:存储端NFS服务器配置
[root@yangs ~]# mkdir /home/dir1
[root@yangs ~]# chmod 777 /home/dir1
[root@yangs ~]# touch /home/dir1/test.txt
[root@yangs ~]# vim /etc/exports
/home/dir1 *(rw,sync) //*表示所有主机
[root@yangs ~]# service nfs restart
[root@yangs ~]# chkconfig nfs on //将其设置为开机自动启动
========================================================
5、磁盘配额
磁盘配额 quota
作用: 限制用户或组对磁盘空间的使用,例如文件服务器,邮件服务器...
========================================================
一、 启用磁盘限额
1. 让文件系统支持配额
[root@yangs ~]# vim /etc/fstab
/dev/vg01/lv_home /home ext4 defaults,usrquota,grpquota 1 2
[root@yangs ~]# mount -o remount /home/
[root@yangs ~]# mount
/dev/mapper/vg01-lv_home on /home type ext3 (rw,usrquota,grpquota)
2. 创建磁盘配额的数据库文件
注意: 建议停用SELinux
[root@yangs ~]# quotacheck -acug
[root@yangs ~]# ll /home/aquota.*
-rw------- 1 root root 12288 09-09 23:06 /home/aquota.group
-rw------- 1 root root 11264 09-09 23:06 /home/aquota.user
//-a 所有分区(已支持配额)
//-c 创建
//-u 用户
//-g 组
3.启动磁盘配额
[root@yangs ~]# quotaon -a //启动所有分区的磁盘配额
二、日常管理
+++设置配额+++
方法一:edquota
[root@yangs ~]# edquota -u alice
Disk quotas for user alice (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/vg01-lv_home 16 8192 10240 4 0 8
blocks: 从磁盘的使用量限制,比如限制用户使用10M,单位是k
inodes: 从创建文件数限制,比如只能建立100个文件
soft: 软限制 block 8M
hard: 硬限制 block 10M
grace time: 宽限期,默认7天 # edquota -t
[root@yangs ~]# su - alice
[alice@yangs ~]$ pwd
/home/alice
[alice@yangs ~]$ dd if=/dev/zero of=file99 bs=1M count=6
6+0 records in
6+0 records out
6291456 bytes (6.3 MB) copied, 0.00549407 seconds, 1.1 GB/s
[alice@yangs ~]$ dd if=/dev/zero of=file99 bs=1M count=9
dm-2: warning, user block quota exceeded.
9+0 records in
9+0 records out
9437184 bytes (9.4 MB) copied, 0.0113783 seconds, 829 MB/s
[alice@yangs ~]$ dd if=/dev/zero of=file99 bs=1M count=11
dm-2: warning, user block quota exceeded.
dm-2: write failed, user block limit reached.
dd: 写入 “file99”: 超出磁盘限额
10+0 records in
9+0 records out
10440704 bytes (10 MB) copied, 0.0101768 seconds, 1.0 GB/s
[alice@yangs ~]$ quota //查看自己的配额情况
Disk quotas for user alice (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/mapper/vg01-lv_home
10240* 8192 10240 6days 8* 0 8
方法二: setquota
# setquota -u username block软限制 block硬限制 inode软限制 inode硬限制 分区
# setquota -u jack 80000 100000 15 20 /dev/sda2
# quota jack
方法三:复制
# edquota -p alice tom robin user1 user2 user3
将alice的配额方案复制给后面所有用户
+++查看配额+++
查看单个用户的配额: # quota jack
查看所有用户的配额: # repquota -a
# repquota -ag
普通用户查看自己配额: $ quota
========================================================
扩展知识:针对组设置配额
例1:限制hr组的成员能在/home/hr目录中:100M 50文件
[root@yangs ~]# groupadd hr
[root@yangs ~]# useradd hr01 -G hr
[root@yangs ~]# useradd hr02 -G hr
[root@yangs ~]# mkdir /home/hr
[root@yangs ~]# chgrp hr /home/hr
[root@yangs ~]# chmod 2770 /home/hr
[root@yangs ~]# ll -d /home/hr
drwxrws--- 2 root hr 4096 09-12 17:07 /home/hr
[root@yangs ~]# edquota -g hr
Disk quotas for group hr (gid 507):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/vg01-lv_home 4 0 102400 1 0 50
[root@yangs ~]# repquota -ag
========================================================