LVM和磁盘配额

LVM

LVM概述

LVM是linux操作系统中对磁盘分区进行管理的一种逻辑机制,能够在保持现有数据不变的情况下动态调整磁盘容量,从而增强磁盘管理的灵活性

LVM管理

PV(物理卷)
物理卷是LVM机制的基本存储设备,通常对应为一个普通分区或整个硬盘
VG(卷组)
由一个或多个物理卷组成一个整体,即称为卷组,在卷组中可以动态地添加或移除物理卷
LV(逻辑卷)
逻辑卷建立在卷组之上,与物理卷没有直接关系。每一个卷组就是一个整体,从这个整体中“切出”一小块空间,作为用户创建文件系统的基础,这一小块空间就称为逻辑卷
PE(物理区域)
物理区域是物理卷中可用于分配的最小存储单元,默认4M

LVM建立步骤

1)①创建物理卷

[root@localhost ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

②查看是否创建成功

[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <79.00g  4.00m
  /dev/sdb          lvm2 ---   20.00g 20.00g
[root@localhost ~]# 

2)①创建卷组

[root@localhost ~]# vgcreate vg01 /dev/sdb
  Volume group "vg01" successfully created

②查看是否创建成功

[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <79.00g   4.00m
  vg01     1   0   0 wz--n- <20.00g <20.00g
[root@localhost ~]# 

3)①创建逻辑卷

[root@localhost ~]# lvcreate -n lg01 -L 5G vg01
  Logical volume "lg01" created.

②查看是否创建成功

[root@localhost ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <75.12g                                                    
  swap centos -wi-ao----  <3.88g                                                    
  lg01 vg01   -wi-a-----   5.00g 

4)格式化并挂载

[root@localhost ~]# mkfs.xfs /dev/vg01/lg01 
meta-data=/dev/vg01/lg01         isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mkdir /lv01
[root@localhost ~]# mount /dev/vg01/lg01 /lv01
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   76G  5.0G   71G    7% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G   13M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    378M     0  378M    0% /run/user/0
tmpfs                    378M   12K  378M    1% /run/user/42
/dev/mapper/vg01-lg01    5.0G   33M  5.0G    1% /lv01
[root@localhost ~]# 

LVM扩展操作

1)移除逻辑卷

[root@localhost ~]# lvremove vg01 /dev/vg01/lg01 
  Logical volume vg01/lg01 contains a filesystem in use.
[root@localhost ~]# umount /lv01
[root@localhost ~]# lvremove vg01 /dev/vg01/lg01 
Do you really want to remove active logical volume vg01/lg01? [y/n]: y
  Logical volume "lg01" successfully removed
[root@localhost ~]# 

2) 移除卷组

[root@localhost ~]# vgremove vg01
  Volume group "vg01" successfully removed
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <79.00g 4.00m
[root@localhost ~]# 

3)移除物理卷

[root@localhost ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <79.00g 4.00m
[root@localhost ~]#

4)扩展卷组

[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <79.00g   4.00m
  /dev/sdb1  vg01   lvm2 a--   <5.00g  <5.00g
  /dev/sdb2         lvm2 ---    5.00g   5.00g
  /dev/sdb3         lvm2 ---  <10.00g <10.00g
[root@localhost ~]# vgextend vg01 /dev/sdb2
  Volume group "vg01" successfully extended
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <79.00g   4.00m
  /dev/sdb1  vg01   lvm2 a--   <5.00g  <5.00g
  /dev/sdb2  vg01   lvm2 a--   <5.00g  <5.00g
  /dev/sdb3         lvm2 ---  <10.00g <10.00g
[root@localhost ~]# 


5)移除卷组中的pv

[root@localhost ~]# vgreduce vg01 /dev/sdb2
  Removed "/dev/sdb2" from volume group "vg01"
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <79.00g   4.00m
  /dev/sdb1  vg01   lvm2 a--   <5.00g  <5.00g
  /dev/sdb2         lvm2 ---    5.00g   5.00g
  /dev/sdb3         lvm2 ---  <10.00g <10.00g
[root@localhost ~]# 

6)给现有挂载点扩容
现在挂载点容量为2G

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   76G  5.0G   71G    7% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G   13M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    378M   12K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
/dev/mapper/vg01-lv01    2.0G   33M  2.0G    2% /lv01   //当前挂载为2G

扩容后还显示2G(需要执行扩展命令)

[root@localhost ~]# lvextend -L +2G /dev/vg01/lv01    //扩容+2G
  Size of logical volume vg01/lv01 changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume vg01/lv01 successfully resized.
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   76G  5.0G   71G    7% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G   13M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    378M   12K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
/dev/mapper/vg01-lv01    2.0G   33M  2.0G    2% /lv01   //显示2G

扩展生效

[root@localhost ~]# xfs_growfs /dev/vg01/lv01   //ext4文件系统的命令为 resize2fs
meta-data=/dev/mapper/vg01-lv01  isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524288 to 1048576
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   76G  5.0G   71G    7% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G   13M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    378M   12K  378M    1% /run/user/42
tmpfs                    378M     0  378M    0% /run/user/0
/dev/mapper/vg01-lv01    4.0G   33M  4.0G    1% /lv01

磁盘配额

作用

对用户在指定文件系统(分区)中使用的磁盘空间、文件数量进行限制,以防止个别用户恶意或无意间占用大量磁盘空间,从而保持系统存储空间的稳定性和持续可用性

配置

1)将磁盘以支持配额功能的方式重新挂载

[root@localhost ~]# mount -o remount,usrquota,grpquota /dev/vg01/lv01 
[root@localhost ~]# 

2)查看功能是否启用

[root@localhost ~]# quotaon -p /dev/vg01/lv01 
group quota on /lv01 (/dev/mapper/vg01-lv01) is off
user quota on /lv01 (/dev/mapper/vg01-lv01) is off
[root@localhost ~]# 

3)创建用户和组的磁盘配额的配置文件

[root@localhost lv01]# quotacheck -cugv /dev/vg01/lv01 
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/mapper/vg01-lv01 [/lv01] done
quotacheck: Cannot stat old user quota file /lv01/aquota.user: 没有那个文件或目录. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /lv01/aquota.group: 没有那个文件或目录. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /lv01/aquota.user: 没有那个文件或目录. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /lv01/aquota.group: 没有那个文件或目录. Usage will not be subtracted.
quotacheck: Checked 3 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
[root@localhost lv01]# ls
aquota.group  aquota.user  lost+found
[root@localhost lv01]# 

4)对zhangsan进行限制
LVM和磁盘配额_第1张图片
5)开启磁盘配额功能

[root@localhost lv01]# quotaon -u /dev/vg01/lv01 
[root@localhost lv01]# quotaon -p /dev/vg01/lv01 
group quota on /lv01 (/dev/mapper/vg01-lv01) is off
user quota on /lv01 (/dev/mapper/vg01-lv01) is on
[root@localhost lv01]# quotaon -g /dev/vg01/lv01 
[root@localhost lv01]# quotaon -p /dev/vg01/lv01 
group quota on /lv01 (/dev/mapper/vg01-lv01) is on
user quota on /lv01 (/dev/mapper/vg01-lv01) is on
[root@localhost lv01]# 

6)测试

[zhangsan@localhost lv01]$ touch 1 2 3
dm-2: warning, user file quota exceeded.
[zhangsan@localhost lv01]$ ls
1  2  3  aquota.group  aquota.user  lost+found
[zhangsan@localhost lv01]$ touch 4
dm-2: write failed, user file limit reached.
touch: 无法创建"4": 超出磁盘限额
[zhangsan@localhost lv01]$ ls
1  2  3  aquota.group  aquota.user  lost+found
[zhangsan@localhost lv01]$ 

7)查看zhangsan配额使用成功

[root@localhost /]# quota -u zhangsan
Disk quotas for user zhangsan (uid 1001): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
/dev/mapper/vg01-lv01
                      0    2048    3072               3*      2       3   6days
[root@localhost /]# 

你可能感兴趣的:(linux)