linux下的存储管理之二

本文详细介绍了Linux系统下EXT3和EXT4文件系统的特性,如文件大小限制、无限子目录、延时分配等,并探讨了超级块的查看和调整。同时,讲解了文件链接的原理,包括符号链接和硬链接的使用及限制。文章还深入讨论了挂载操作,包括挂载选项如rw、ro、noexec、sync、async等,并给出了挂载ISO文件和使用自动挂载(Autofs)的方法。最后,文章提到了磁盘配额的启用和管理,以及如何针对用户和组设置配额限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二、存储管理(2)

1、ext3/4文件系统

认识EXT3/4文件系统
========================================================
Ext3: 文件系统最大16TB,单个文件最大2TB
Ext4: 文件系统最大1EB,单个文件最大16TB

无限数量的子目录/ Extents/ 多块分配/ 延迟分配/ 快速 fsck/ 日志校验/ “无日志”/ 在线碎片整理/ inode 相关特性/ 默认启用 barrier

Ext3/Ext4磁盘布局:

file://C:\Users\zjyh\AppData\Local\Temp\ct_tmp/1.png

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/”: 不允许将硬链接指向目录

file://C:\Users\zjyh\AppData\Local\Temp\ct_tmp/1.png

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/

file://C:\Users\zjyh\AppData\Local\Temp\ct_tmp/1.png

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
========================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值