LVM实践全纪录

要求创建一个5G的逻辑卷

思路:
1. 物理的设备
2. 将物理设备做成物理卷
3. 创建卷组并将物理卷加入其中
4. 创建逻辑卷
5. 格式化逻辑卷
6. 挂载使用

1.准备好物理设备,sdb、sdc硬盘

[root@uos01 ~]# lsblk /dev/sd[b,c]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb    8:16   0  10G  0 disk 
sdc    8:32   0  10G  0 disk 

2.创建物理卷

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

查看物理卷

[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree 
  /dev/sda3  uos lvm2 a--  <23.50g     0 
  /dev/sdb       lvm2 ---   10.00g 10.00g
  /dev/sdc       lvm2 ---   10.00g 10.00g

查看详细信息

[root@uos01 ~]# pvdisplay /dev/sdb
  "/dev/sdb" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ujZ7N1-pZ2p-e6UK-k9vL-XspG-Gw0H-kA7Tmp
[root@uos01 ~]# pvdisplay /dev/sdc
  "/dev/sdc" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               RJridM-pc1m-OCG2-c5Vu-a2RH-NfdU-vQCI8k

3.创建卷组myvg01,加入物理卷PV

[root@uos01 ~]# vgcreate myvg01 /dev/sdb /dev/sdc
  Volume group "myvg01" successfully created

查看卷组信息

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   0   0 wz--n- 19.99g 19.99g
[root@uos01 ~]# vgdisplay myvg01 
  --- Volume group ---
  VG Name               myvg01
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               S4cFUe-cvEZ-RQZa-cSCS-PzA9-3ARB-gXZwoG
 

4.创建逻辑卷

参数解释

-n:指定逻辑卷的名字
-L:指定逻辑卷的大小
-l:指定逻辑卷的大小
举例:
-l 100            100个PE,每个PE大小默认4M,故逻辑卷大小为400M
-l 50%free        卷组剩余空间的50%
-L 5G             创建大小为5GB的逻辑卷

创建逻辑卷mylv01

[root@uos01 ~]# lvcreate -n mylv01 -L 5G myvg01 
  Logical volume "mylv01" created.

查看逻辑卷信息

[root@uos01 ~]# lvs /dev/myvg01/mylv01 
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-a----- 5.00g    
[root@uos01 ~]# lvdisplay /dev/myvg01/mylv01 
  --- Logical volume ---
  LV Path                /dev/myvg01/mylv01
  LV Name                mylv01
  VG Name                myvg01
  LV UUID                8923dl-0uCR-Fox1-aJKY-c3IQ-d62K-EKjR7J
  LV Write Access        read/write
  LV Creation host, time uos01, 2024-08-28 16:09:14 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   

可以在系统中的两个地方,找到lv分区信息

[root@uos01 ~]# ll /dev/mapper/myvg01-mylv01 
lrwxrwxrwx 1 root root 7  8月 28 16:09 /dev/mapper/myvg01-mylv01 -> ../dm-3
[root@uos01 ~]# ll /dev/myvg01/mylv01 
lrwxrwxrwx 1 root root 7  8月 28 16:09 /dev/myvg01/mylv01 -> ../dm-3

查看卷组还剩余多少空间

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   1   0 wz--n- 19.99g 14.99g

再创建一个200M的逻辑卷mylv02,每个LE默认4M,用-l 100 参数指定100个LE,因此是400M

[root@uos01 ~]# lvcreate -n mylv02 -l 100 myvg01
  Logical volume "mylv02" created.

查看mylv02大小

[root@uos01 ~]# lvs /dev/myvg01/mylv02
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv02 myvg01 -wi-a----- 400.00m     

查看myvg01剩余大小

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   2   0 wz--n- 19.99g 14.60g

创建卷组剩余空间的50%,逻辑卷mylv03

[root@uos01 ~]# lvcreate -n mylv03 -l 50%free myvg01
  Logical volume "mylv03" created.

查看mylv03

[root@uos01 ~]# lvs /dev/myvg01/mylv03
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv03 myvg01 -wi-a----- 7.30g 

查看此时vg剩余

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 7.30g

5.格式化逻辑卷mylv01

[root@uos01 ~]# mkfs.xfs /dev/myvg01/mylv01
meta-data=/dev/myvg01/mylv01     isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
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
Discarding blocks...Done.

6.挂载逻辑卷使用

[root@uos01 ~]# mkdir /funlyp-lv01
[root@uos01 ~]# mount /dev/myvg01/mylv01 /funlyp-lv01/
[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01
[root@uos01 ~]# 

写入数据

[root@uos01 ~]# touch /funlyp-lv01/lvm实践操作.log
[root@uos01 ~]# ls /funlyp-lv01/
lvm实践操作.log


lvm动态扩容

任务:你现在觉得这个分区太小了,不够用,如何扩容?

思路:
1.查看该挂载点,对应的逻辑卷是哪一个/dev/mapper/vg01-lv01
2.查看该逻辑卷所在的卷组,是否还有剩余空间。
3.如果vg01空间不够,就得先扩容卷组,再扩容逻辑卷
4.如果vg01空间足够,直接扩容逻辑卷

直接逻辑卷扩容

1.查看挂载点,属于哪个逻辑卷

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01

2.查看逻辑卷大小

[root@uos01 ~]# lvs /dev/mapper/myvg01-mylv01
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 5.00g   

3.查看卷组空间

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 7.30g

4.卷组空间还剩7.3G,直接扩容逻辑卷

直接扩容加5G

[root@uos01 ~]# lvextend -L +5G /dev/myvg01/mylv01
  Size of logical volume myvg01/mylv01 changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
  Logical volume myvg01/mylv01 successfully resized.

查看扩容结果

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01

发现容量未更新
同步文件系统,使其容量更新,只针对xfs

[root@uos01 ~]# xfs_growfs /dev/myvg01/mylv01
meta-data=/dev/mapper/myvg01-mylv01 isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
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
data blocks changed from 1310720 to 2621440

再次查看扩容结果

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs        10G  104M  9.9G    2% /funlyp-lv01

xfs文件系统只支持增大分区空间的情况,不支持减小的情况(切记!!!!!)
硬要减小的话,只能在减小后将逻辑分区重新通过mkfs.xfs命令重新格式化才能挂载上,这样的话这个逻辑分区上原来的数据就丢失了。
可以选择用ext4文件系统,支持扩缩容


还是觉得逻辑卷容量太小了,想要扩容到20G

先扩容卷组,再扩容逻辑卷

1.查看逻辑卷大小和卷组空间

[root@uos01 ~]# lvs /dev/mapper/myvg01-mylv01
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 10.00g   
[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 2.30g


2.卷组空间已经不够支持逻辑卷扩容到20G了,得先扩容卷组

添加新硬盘sde

[root@uos01 ~]# lsblk /dev/sde
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sde    8:64   0  10G  0 disk 

再把sde制作成物理卷pv

[root@uos01 ~]# pvcreate /dev/sde
WARNING: dos signature detected on /dev/sde at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sde.
  Physical volume "/dev/sde" successfully created.
[root@uos01 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda3  uos    lvm2 a--  <23.50g     0 
  /dev/sdb   myvg01 lvm2 a--  <10.00g     0 
  /dev/sdc   myvg01 lvm2 a--  <10.00g  2.30g
  /dev/sde          lvm2 ---   10.00g 10.00g

添加物理卷到卷组(卷组扩容)

[root@uos01 ~]# vgextend myvg01 /dev/sde
  Volume group "myvg01" successfully extended
[root@uos01 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda3  uos    lvm2 a--  <23.50g      0 
  /dev/sdb   myvg01 lvm2 a--  <10.00g      0 
  /dev/sdc   myvg01 lvm2 a--  <10.00g   2.30g
  /dev/sde   myvg01 lvm2 a--  <10.00g <10.00g
注意:
正常情况下,应该先将/dev/sde物理设备创建为物理卷再加入到卷组中;如果直接加入卷组,系统会自动帮你将其做成物理卷。

查看扩容后的卷组

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize   VFree  
  myvg01   3   3   0 wz--n- <29.99g <12.30g

3.扩容逻辑卷mylv01到20G

[root@uos01 ~]# lvextend -L +10G /dev/myvg01/mylv01
  Size of logical volume myvg01/mylv01 changed from 10.00 GiB (2560 extents) to 20.00 GiB (5120 extents).
  Logical volume myvg01/mylv01 successfully resized.

查看扩容后的逻辑卷

[root@uos01 ~]# lvs /dev/myvg01/mylv01
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 20.00g  

查看挂载目录的容量,发现还未更新

[root@uos01 ~]# df -h | grep funlyp
/dev/mapper/myvg01-mylv01   10G  104M  9.9G    2% /funlyp-lv01

手动更新

[root@uos01 ~]# xfs_growfs /dev/myvg01/mylv01
meta-data=/dev/mapper/myvg01-mylv01 isize=512    agcount=8, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=2621440, 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
data blocks changed from 2621440 to 5242880

再次查看已更新

[root@uos01 ~]# 
[root@uos01 ~]# df -h | grep funlyp
/dev/mapper/myvg01-mylv01   20G  176M   20G    1% /funlyp-lv01



删除lvm

当你需要删除逻辑卷,怎么操作

思路:
需要提前把数据备份好,然后依次删除
* 逻辑卷
* 卷组
* 物理卷

1.取消挂载

root@uos01 ~]# umount /funlyp-lv01 

2.删除逻辑卷mylv01

[root@uos01 ~]# lvremove /dev/myvg01/mylv0
/dev/myvg01/mylv01  /dev/myvg01/mylv02  /dev/myvg01/mylv03  
[root@uos01 ~]# lvremove /dev/myvg01/mylv01
Do you really want to remove active logical volume myvg01/mylv01? [y/n]: y
  Logical volume "mylv01" successfully removed.
[root@uos01 ~]# 
[root@uos01 ~]# lvs /dev/myvg01
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv02 myvg01 -wi-a----- 400.00m                                                    
  mylv03 myvg01 -wi-a-----   7.30g   

已成功删除逻辑卷mylv01,此时查看还有卷组myvg01上还存在两个逻辑卷。

3.删除卷组myvg01

直接删除卷组,会提示先删除卷组下的逻辑卷,然后再删除卷组。

[root@uos01 ~]# vgremove myvg01 
Do you really want to remove volume group "myvg01" containing 2 logical volumes? [y/n]: y
Do you really want to remove active logical volume myvg01/mylv02? [y/n]: y
  Logical volume "mylv02" successfully removed.
Do you really want to remove active logical volume myvg01/mylv03? [y/n]: y
  Logical volume "mylv03" successfully removed.
  Volume group "myvg01" successfully removed
[root@uos01 ~]# 

4.删除物理卷

查看本机上的物理卷pv

[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree 
  /dev/sda3  uos lvm2 a--  <23.50g     0 
  /dev/sdb       lvm2 ---   10.00g 10.00g
  /dev/sdc       lvm2 ---   10.00g 10.00g
  /dev/sde       lvm2 ---   10.00g 10.00g

删除物理卷/dev/sdb /dev/sdc /dev/sde

[root@uos01 ~]# pvremove /dev/sdb /dev/sdc /dev/sde
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
  Labels on physical volume "/dev/sde" successfully wiped.

查看lvm相关信息,是否还有残留

[root@uos01 ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root uos -wi-ao---- <17.50g                                                    
  swap uos -wi-ao----   4.00g                                                    
  var  uos -wi-ao----   2.00g                                                    
[root@uos01 ~]# 
[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree
  /dev/sda3  uos lvm2 a--  <23.50g    0 
[root@uos01 ~]# 
[root@uos01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize   VFree
  uos   1   3   0 wz--n- <23.50g    0 
[root@uos01 ~]# 

posted @ 2024-08-29 10:57  先ping一下网关  阅读(35)  评论(0编辑  收藏  举报