一、LVM概述
1.1 Logical Volume Manager,逻辑卷管理
- 动态调整磁盘容量,从而提高磁盘管理的灵活性
- /boot分区用于存放引导文件,不能基于LVM创建
- 图形界面管理工具:system-config-lvm
1.2 物理卷
物理卷是LVM机制的基本存储设备,通常对应为一个普通分区或整个硬盘。创建物理卷时,会在分区或硬盘的头部创建一个保留区块,用于记录LVM的属性,并把存储空间分割成默认大小为4MB的基本单元(Physical Extent,PE),从而构成物理卷
1.3卷组
VG(卷组)
卷组:由一个或多个物理卷组成一个整体即称为卷组在卷组中可以动态的添加或移除物理卷。如图所示,许多个物理卷可以分别组成不同的卷组,卷组的名称由用户自定义
1.4逻辑卷
-
逻辑卷建立在卷组之上,与物理卷没有直接关系
-
对于逻辑卷来说,每一个卷组就是一个整体,从这个整体中“切出”一小块空间,作为用户创建文件系统的基础,这一小块空间就称为逻辑卷
-
使用mkfs等工具在逻辑卷上创建文件系统以后,就可以挂载到Linux系统中的目录下使用
二、LVM的管理命令
功能 | scan扫描 | create建立 | display显示 | remove移除 | extend扩展 | reduce减少 |
---|---|---|---|---|---|---|
PV管理命令 | pvscan | pvcreate | pvdisplay | pvremove | ||
VG管理命令 | vgscan | vgcreate | vgdisplay | vgremove | vgextend | vgreduce |
LV管理命令 | lvscan | lvcreate | lvdisplay | lvremove | lvextend | lvreduce |
常用命令
pvcreate 设备名(磁盘) [设备名2 ... ...]
vgcreate 卷组名 物理卷名1 物理卷名2
lvcreate -L 容量大小 -n 逻辑卷名 卷组名
lvextend -l +大小 /dev/卷组名/逻辑卷名
三、LVM管理实验
先在虚拟机中加两块 块硬盘,硬盘每块1G容量 添加完毕后重启虚拟机
fdisk -l ###查看下三块硬盘
[root@localhost ~]# yum -y install lvm2 ###Centos7.6 最小化安装后没有此命令,需要安装
[root@localhost 桌面]# pvcreate /dev/sdb /dev/sdc ### 硬盘做下物理卷
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
[root@localhost 桌面]# pvdisplay ###查看下
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID AkJVxo-rWfy-WxyD-rlTM-cn26-0UgS-c9vP0M
"/dev/sdb" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xefVFX-IoEh-7iP5-y5UA-dPQ3-PkSH-mOQHmE
"/dev/sdc" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 1fhrpD-tocY-peKe-mXFB-pzkC-m2Tc-2jolEW
[root@localhost 桌面]# vgcreate vg01 /dev/sdb /dev/sdc ###将2块硬盘 创建VG01
Volume group "vg01" successfully created
[root@localhost 桌面]# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID SJ74ft-AtFu-nrc2-3RRI-q4hw-ZR3O-kCdgSs
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID B3v06X-8bbp-pgwU-3dBw-Manr-cFT3-MFVPo6
[root@localhost 桌面]# vgscan ###看下vg情况
Reading all physical volumes. This may take a while...
Found volume group "vg01" using metadata type lvm2
Found volume group "VolGroup" using metadata type lvm2
[root@localhost 桌面]# lvcreate -n lv01 -L 500M vg01 ####创建逻辑卷lv01,大小为500M
Logical volume "lv01" created
创建完成后进行格式化操作,这里格式化的格式是ext4
[root@localhost 桌面]# mkfs.ext4 /dev/vg01/lv01
开机启动 加入自动挂载 (永久挂载)
vi /etc/fstab
/dev/vg01/lv01 /mnt ext4 defaults 0 0
mount -a 检查
[root@localhost 桌面]# mount -a #########一般没问题,不会报出相关提示
[root@localhost 桌面]# df -hT ###查看挂载情况
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 18G 3.3G 14G 21% /
tmpfs tmpfs 491M 224K 491M 1% /dev/shm
/dev/sda1 ext4 485M 35M 426M 8% /boot
/dev/mapper/vg01-lv01 ext4 485M 11M 449M 3% /mnt
扩容,假如空间不够用时使用
[root@localhost 桌面]# lvextend -L +500M /dev/vg01/lv01 ####加500M空间
Extending logical volume lv01 to 1000.00 MiB
Logical volume lv01 successfully resized
[root@localhost 桌面]# df -h ###检查空间情况
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 21% /
tmpfs 491M 224K 491M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/mapper/vg01-lv01 485M 11M 449M 3% /mnt
[root@localhost 桌面]# resize2fs /dev/vg01/lv01 ####初始化
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lv01 is mounted on /mnt; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/vg01/lv01 to 1024000 (1k) blocks.
The filesystem on /dev/vg01/lv01 is now 1024000 blocks long.
################### 减小空间 ,一般会出问题,生产环境不用 ############
[root@localhost 桌面]# lvreduce -L -500M /dev/vg01/lv01
WARNING: Reducing active and open logical volume to 500.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
Reducing logical volume lv01 to 500.00 MiB
Logical volume lv01 successfully resized
[root@localhost 桌面]# resize2fs /dev/vg01/lv01
resize2fs 1.41.12 (17-May-2010)
resize2fs: New size smaller than minimum (524048)
[root@localhost 桌面]# df -h ######可以看到不好用
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 21% /
tmpfs 491M 224K 491M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/mapper/vg01-lv01 969M 11M 909M 2% /mnt
[root@localhost 桌面]# mount -a
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg01-lv01,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
间彻底废了,不能卸载也不能重启一看空间减少不成功
现在演示还原 删除 LV
[root@localhost 桌面]# lvremove /dev/vg01/lv01 #####删除LV01
Do you really want to remove active logical volume lv01? [y/n]: y
Logical volume "lv01" successfully removed
[root@localhost 桌面]# lvcreate -n lv01 -L 500 vg01 ######重新创建LV01
Logical volume "lv01" created
[root@localhost 桌面]# mkfs.ext4 /dev/vg01/lv01 #####格式化
[root@localhost 桌面]# mount /dev/vg01/lv01 /mnt ###重新挂进去
######vg扩容,加物理卷#######
pvcreate /dev/sdd
[root@localhost 桌面]# vgextend vg01 /dev/sdd
四、磁盘配额
4.1概述
为了避免在服务器中出现类似磁盘空间不足的问题,可以使用磁盘配额功能,对用户I在指定文件系统(分区)中使用的磁盘空间、文件类型进行限制,以防止个别用户恶意,或者无意间占用大量空间,从而保持系统在存储空间的稳定性和持续可用性,即让用户在系统中能使用多少空间。
4.2常用操作
开启磁盘配额选项
mount -o remount,usrquota,grpquota /dev/vg01/lv01
####看末尾:usrquota,grpquota 必须有这个才能开启磁盘配额功能####
[root@localhost 桌面]# mount
/dev/mapper/VolGroup-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,rootcontext="system_u:object_r:tmpfs_t:s0")
/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)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/mapper/vg01-lv01 on /mnt type ext4 (rw,usrquota,grpquota)
setenforce 0 #####临时关闭核心防护
setenforce ? #####检查核心防护状态
vi /etc/selinux/config ###永久关闭核心防护
SELINUX=disabled
[root@localhost 桌面]# lvcreate -n lv02 -L 500M vg01
Logical volume "lv02" created
[root@localhost 桌面]# mkfs.ext4 /dev/vg01/lv02
[root@localhost 桌面]# mount -o usrquota,grpquota /dev/vg01/lv02 /opt
[root@localhost 桌面]# mount
/dev/mapper/VolGroup-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,rootcontext="system_u:object_r:tmpfs_t:s0")
/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)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/mapper/vg01-lv01 on /mnt type ext4 (rw,usrquota,grpquota)
/dev/mapper/vg01-lv02 on /opt type ext4 (rw,usrquota,grpquota)
[root@localhost opt]# yum -y install quota ###安装quota
quotacheck -cvug /opt
[root@localhost opt]# quotaon -p /opt ##查看磁盘配额有没有开启
[root@localhost opt]#quotaon /opt ##开启磁盘配额功能
[root@localhost /]# quotaon -p /opt ####已经开启
group quota on /opt (/dev/mapper/vg01-lv02) is on
user quota on /opt (/dev/mapper/vg01-lv02) is on
[root@localhost opt]# useradd tom
[root@localhost opt]# passwd tom
[root@localhost opt]# edquota -u tom
Disk quotas for user tom (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/vg01-lv02 0 10000 10240 0 5 6
表头 | 含义 |
---|---|
文件系统(filesystem) | 说明该限制值是针对哪个文件系统(或分区); |
磁盘容量(blocks) | 此列的数值是 quota 自己算出来的,单位为 Kbytes,不要手动修改; |
磁盘容量的软限制(soft) | 当用户使用的磁盘空间超过此限制值,则用户在登陆时会收到警告信息,告知用户磁盘已满,单位为 KB; |
磁盘容量的硬限制(hard) | 要求用户使用的磁盘空间最大不能超过此限制值,单位为 KB; |
文件数量(inodes) | 同 blocks 一样,此项也是 quota自己计算出来的,无需手动修改; |
文件数量的软限制(soft) | 当用户拥有的文件数量超过此值,系统会发出警告信息; |
文件数量的硬限制(hard) | 用户拥有的文件数量不能超过此值。 |
tom 用户在opt 目录软件最大是10M 最大文件个数5
[root@localhost opt]# chmod 777 /opt #########这里是测试,权限777
切换tom
[tom@localhost opt]$ dd if=/dev/zero of=/opt/tom1 bs=1k count=10000
dm-3: warning, user block quota exceeded.
记录了10000+0 的读入
记录了10000+0 的写出
10240000字节(10 MB)已复制,0.0468673 秒,218 MB/秒
[tom@localhost opt]$ dd if=/dev/zero of=/opt/tom2 bs=1k count=240
dm-3: write failed, user block limit reached.
dd: 正在写入"/opt/tom2": 超出磁盘限额
记录了239+0 的读入
记录了238+0 的写出
243712字节(244 kB)已复制,0.00153139 秒,159 MB/秒
[tom@localhost opt]$ quota
quota: Cannot open quotafile /mnt/aquota.user: 没有那个文件或目录
Disk quotas for user tom (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/mapper/vg01-lv02
10240* 10000 10240 7days 2 5 6
* 号已经满了
[tom@localhost opt]$ dd if=/dev/zero of=/opt/tom3 bs=1k count=240
dd: 正在打开"/opt/tom3": 超出磁盘限额
查看所有用户磁盘配额情况
[root@localhost opt]# repquota -a
repquota: Cannot open quotafile /mnt/aquota.user: 没有那个文件或目录
*** Report for user quotas on device /dev/mapper/vg01-lv02
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 13 0 0 2 0 0
tom +- 10240 10000 10240 6days 2 5 6