逻辑卷LVM

Posted dinghc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了逻辑卷LVM相关的知识,希望对你有一定的参考价值。

当我们创建好raid组并分区以后,分区的大小是固定不变的,那么我们怎么可以更改分区的大小呢,这就需要使用到逻辑卷LVM了
使用LVM可以在不关机的情况下,简单的改变各分区的大小
技术图片
1.创建物理卷PV(多个物理卷可以组成一个卷组,也叫资源池)

[root@dinghuachong-98 ~]# yum install lvm2 -y
[root@dinghuachong-98 ~]# pvcreate /dev/md1 #将磁盘(raid组)转换成pv物理卷
#将上面添加的3个raid组都转变成物理卷
[root@dinghuachong-98 ~]# pvcreate /dev/md/raid0
WARNING: xfs signature detected on /dev/md/raid0 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/md/raid0.
  Physical volume "/dev/md/raid0" successfully created.
     
[root@dinghuachong-98 ~]# pvcreate /dev/md1
WARNING: xfs signature detected on /dev/md1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/md1.
  Physical volume "/dev/md1" successfully created.
{1}
[root@dinghuachong-98 ~]# pvcreate /dev/md5
WARNING: xfs signature detected on /dev/md5 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/md5.
  Physical volume "/dev/md5" successfully created.

查看物理卷

[root@dinghuachong-98 ~]# pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/md1      lvm2 ---   19.98g  19.98g
  /dev/md127    lvm2 ---  <39.97g <39.97g
  /dev/md5      lvm2 ---  <39.97g <39.97g

生成卷组lvm_raid

[root@dinghuachong-98 ~]# vgcreate lvm_raid /dev/md127 /dev/md1 /dev/md5
  Volume group "lvm_raid" successfully created
[root@dinghuachong-98 ~]# vgs    #查看卷组信息
  VG       #PV #LV #SN Attr   VSize  VFree 
  lvm_raid   3   0   0 wz--n- 99.91g 99.91g
#######################如果需要将卷组加大
 
[root@dinghuachong-98 ~]vgextend /dev/sdk #增加新物理卷到卷组

进行逻辑卷分区

##选项 -L 指定大小 -n指定名称,要以lv开头
[root@dinghuachong-98 ~]# lvcreate -L 5G lvm_raid 
  Logical volume "lvol0" created.
[root@dinghuachong-98 ~]# lvcreate -L 5G lvm_raid 
  Logical volume "lvol1" created.
[root@dinghuachong-98 ~]# lvcreate -L 5G -n lv1 lvm_raid 
  Logical volume "lv1" created.

可以对逻辑卷进行格式化,并且挂载

[root@dinghuachong-98 ~]# mkfs.xfs /dev/lvm_raid/lv1
 
[root@dinghuachong-98 ~]# mkfs.xfs /dev/lvm_raid/lvol0
meta-data=/dev/lvm_raid/lvol0    isize=512    agcount=8, agsize=163712 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
 
[root@dinghuachong-98 ~]# mkdir /lvm{1..5} 
[root@dinghuachong-98 ~]# mount /dev/lvm_raid/lv1 /lvm1
[root@dinghuachong-98 ~]# mount /dev/lvm_raid/lvol0 /lvm2
[root@dinghuachong-98 ~]# df -h   #查询成功,挂载成功,lvm逻辑卷创建成功
Filesystem                  Size  Used Avail Use% Mounted on
/dev/sda2                    48G  1.5G   47G   4% /
devtmpfs                    476M     0  476M   0% /dev
tmpfs                       487M     0  487M   0% /dev/shm
tmpfs                       487M  7.6M  479M   2% /run
tmpfs                       487M     0  487M   0% /sys/fs/cgroup
/dev/sda1                  1014M  127M  888M  13% /boot
tmpfs                        98M     0   98M   0% /run/user/0
/dev/mapper/lvm_raid-lv1    5.0G   33M  5.0G   1% /lvm1
/dev/mapper/lvm_raid-lvol0  5.0G   33M  5.0G   1% /lvm2

对逻辑卷进行扩大

[root@dinghuachong-98 ~]# lvextend -L +5G /dev/lvm_raid/lv1 #增加空间
  Size of logical volume lvm_raid/lv1 changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
  Logical volume lvm_raid/lv1 successfully resized.
[root@dinghuachong-98 ~]# df -h  #发现逻辑卷没有增加容量
Filesystem                  Size  Used Avail Use% Mounted on
/dev/sda2                    48G  1.5G   47G   4% /
devtmpfs                    476M     0  476M   0% /dev
tmpfs                       487M     0  487M   0% /dev/shm
tmpfs                       487M  7.6M  479M   2% /run
tmpfs                       487M     0  487M   0% /sys/fs/cgroup
/dev/sda1                  1014M  127M  888M  13% /boot
tmpfs                        98M     0   98M   0% /run/user/0
/dev/mapper/lvm_raid-lv1    5.0G   33M  5.0G   1% /lvm1
/dev/mapper/lvm_raid-lvol0  5.0G   33M  5.0G   1% /lvm2
  
 
[root@dinghuachong-98 ~]# xfs_growfs /dev/lvm_raid/lv1  #对逻辑卷激活
meta-data=/dev/mapper/lvm_raid-lv1 isize=512    agcount=8, agsize=163712 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1309696 to 2621440
[root@dinghuachong-98 ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/sda2                    48G  1.5G   47G   4% /
devtmpfs                    476M     0  476M   0% /dev
tmpfs                       487M     0  487M   0% /dev/shm
tmpfs                       487M  7.6M  479M   2% /run
tmpfs                       487M     0  487M   0% /sys/fs/cgroup
/dev/sda1                  1014M  127M  888M  13% /boot
tmpfs                        98M     0   98M   0% /run/user/0
/dev/mapper/lvm_raid-lv1     10G   33M   10G   1% /lvm1
/dev/mapper/lvm_raid-lvol0  5.0G   33M  5.0G   1% /lvm2

逻辑卷扩大成功,至于缩小逻辑卷,在工作场景中,很危险,轻易不要操作!

以上是关于逻辑卷LVM的主要内容,如果未能解决你的问题,请参考以下文章

lvm逻辑卷技术

lvm逻辑卷操作

LVM逻辑卷:创建LVM分区实例

LVM逻辑卷管理--在线扩容逻辑卷与卷组容量缩减逻辑卷快照

逻辑卷管理(LVM)

LVM 逻辑卷管理