C7-磁盘高级管理--LVM逻辑卷管理

1.LVM逻辑卷概念

LVM(Logical Volume Manager)逻辑卷管理,它是Linux环境下对磁盘和分区进行管理的一种机制。
普通的磁盘分区管理方式在分区划分好之后一般无法改变其大小(有风险),当一个分区存放不下文件时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。

2.LVM的组成部分

物理卷(PV,Physical Volume):由磁盘或分区转化而成
卷组(VG,Volume Group):将多个物理卷组合在一起组成了卷组,组成同一个卷组的可以是同一个硬盘的不同分区,也可以是不同硬盘上的不同分区,也可以是整块磁盘,我们通常把卷组理解为一块硬盘。
逻辑卷(LV,Logical Volume):把卷组理解为硬盘的话,那么我们的逻辑卷则是硬盘上的分区,逻辑卷可以进行格式化,存储数据。
物理扩展(PE,Physical Extend):PE卷组的最小存储单元,PE所在的位置是VG卷组,即硬盘上,那么我们可以把PE理解为硬盘上的扇区,默认是4MB,可自由配置。

3.创建LVM

将磁盘分区,或整块磁盘不分区(不做任何操作)
	将磁盘上的分区或整块盘建立成物理卷(PV)
	将新增的物理卷组合成卷组(VG),并且可以通过添加或者删除一块物理卷,来实现卷组大小的调整。
	最后将卷组划分成逻辑卷(LV),逻辑卷也是可以随意调整大小的,逻辑卷相当于真正的分区,那么要使用必须进行格式化和挂载。

3.1物理卷管理
1)使用gdisk 来进行分区,分区的系统ID需要改为8e00

[root@lianxi ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:       
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +15G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 728B3E2E-4FF8-45F8-A517-79134C8BF361
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 10485693 sectors (5.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        31459327   15.0 GiB    8E00  Linux LVM

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
  1. 创建物理卷—pvcreate
[root@lianxi ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
  1. 查看物理卷—pvscan(简化pvs) & pvdisplay
[root@lianxi ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb1         lvm2 ---   15.00g 15.00g
[root@lianxi ~]# pvdisplay    #查看每一个物理卷的详细信息
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               M4P3J9-VTUh-wH9h-1CrC-XeiU-c2OK-EwlVNL
   
  "/dev/sdb1" is a new physical volume of "15.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               15.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               crWrZd-h4rz-m0hS-vg9v-tZVv-q8jH-ERNzHV
  1. 删除物理卷—pvremove
[root@lianxi ~]# pvcreate /dev/sdc   #先添加一个
  Physical volume "/dev/sdc" successfully created.
[root@lianxi ~]# pvs                 #查看
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb1         lvm2 ---   15.00g 15.00g
  /dev/sdc          lvm2 ---   20.00g 20.00g
[root@lianxi ~]# pvremove /dev/sdc   #删除
  Labels on physical volume "/dev/sdc" successfully wiped.
[root@lianxi ~]# pvs                #查看
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb1         lvm2 ---   15.00g 15.00g

3.2卷组管理

  1. 创建卷组—vgcreate
    格式:vgcreate 选项 卷组名 物理卷(写多个时用空格分开)
    -s 大小:指定VG的PE大小
[root@lianxi ~]# vgcreate -s 3M vg1 /dev/sdb1 /dev/sdc
  Volume group "vg1" successfully created
  1. 查看卷组—vgscan(vgs) & vgdisplay
[root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      2   0   0 wz--n- <35.00g <35.00g
[root@lianxi ~]#  vgdisplay
  --- Volume group ---
  VG Name               centos
  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.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               E9PMme-ueMm-GUxW-wbcf-DB2K-ULEv-cfZuHt
   
  --- Volume group ---
  VG Name               vg1
  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               <35.00 GiB
  PE Size               3.00 MiB
  Total PE              11945
  Alloc PE / Size       0 / 0   
  Free  PE / Size       11945 / <35.00 GiB
  VG UUID               V4heyX-bBBa-7i2g-c6b4-yzl8-99TU-wKEa4b
  1. 减小卷组容量–vgreduce
    格式:vgreduce 卷组名 物理卷名
[root@lianxi ~]# vgreduce vg1 /dev/sdc
  Removed "/dev/sdc" from volume group "vg1"
[root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      1   0   0 wz--n- <15.00g <15.00g

拓展:vgreduce -a 卷组 #删除指定卷组内所有空的物理卷
4) 增加卷组容量–vgextend
格式:vgrextend 卷组名 物理卷名

[root@lianxi ~]# vgextend vg1 /dev/sdc
  Volume group "vg1" successfully extended
[root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      2   0   0 wz--n- <35.00g <35.00g
  1. 删除卷组—vgremove(若已存数据则谨慎操作)
[root@lianxi ~]# vgremove vg1
  Volume group "vg1" successfully removed
[root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0 
  [root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0 
[root@lianxi ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb1         lvm2 ---   15.00g 15.00g   #已经没有了
  /dev/sdc          lvm2 ---   20.00g 20.00g

3.3逻辑卷管理

  1. 创建逻辑卷–lvcreate
    格式:lvcreate 选项 -n 逻辑卷名 卷组名
    -L 容量:指定创建的逻辑卷大小,单位:MB,GB,TB等
    -l(小) PE个数:按照PE个数来指定逻辑卷大小
    -n 逻辑卷名:指定逻辑卷的名字
[root@lianxi ~]# lvcreate -L 20G -n lv1 vg1
  Rounding up size to full physical extent 20.00 GiB
  Logical volume "lv1" created
  1. 查看逻辑卷—lvscan (lvs)& lvdisplay
[root@lianxi ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
  lv1  vg1    -wi-a-----  20.00g      #创建成功                                                 
[root@lianxi ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  centos   1   2   0 wz--n- <19.00g     0 
  vg1      2   1   0 wz--n- <35.00g 14.99g    #剩余14.99g可用
[root@lianxi ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb1  vg1    lvm2 a--  <15.00g 14.99g   #剩余14.99g可用
  /dev/sdc   vg1    lvm2 a--  <20.00g     0    #用完

[root@lianxi ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                A2F3RR-o5PM-hEwI-1FpN-JZe3-Wvgd-LpPjIp
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-04-15 06:01:34 -0400
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                juSqQV-iQ2J-NM90-Cuxh-QLvf-kG03-fx9h0k
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-04-15 06:01:35 -0400
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                5WhMjS-LCKW-nrmI-ygIt-L9gX-lfBU-AhsjIU
  LV Write Access        read/write
  LV Creation host, time lianxi, 2020-04-23 01:14:51 -0400
  LV Status              available
  # open                 0
  LV Size                20.00 GiB
  Current LE             6827
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

注:因为逻辑卷相当于是分区,所以需要进行格式化和挂载才能使用,并设置自启动
3)格式化、挂载

[root@lianxi ~]# mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=1310784 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5243136, 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@lianxi ~]# mkdir /lv1
[root@lianxi ~]# mount /dev/vg1/lv1 /lv1
[root@lianxi ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.7G   16G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/vg1-lv1       20G   33M   20G   1% /lv1
  1. 增加逻辑卷容量–lvextend
    lvextend -L +10G /dev/vg1/lv1 #先增加lv的大小
    xfs_growfs /dev/vg1/lv1 #再修改文件系统的大小
[root@lianxi ~]#  lvextend -L +10G /dev/vg1/lv1
  Rounding size to boundary between physical extents: 10.00 GiB.
  Size of logical volume vg1/lv1 changed from 20.00 GiB (6827 extents) to 30.00 GiB (10241 extents).
  Logical volume vg1/lv1 successfully resized.
  [root@lianxi ~]# mount /dev/vg1/lv1 /lv1   #重新挂载
mount: /dev/mapper/vg1-lv1 is already mounted or /lv1 busy
       /dev/mapper/vg1-lv1 is already mounted on /lv1
[root@lianxi ~]# df -h     
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.7G   16G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/vg1-lv1       20G   33M   20G   1% /lv1    #显示没有加载成功
[root@lianxi ~]# xfs_growfs /dev/vg1/lv1   #所以我们要更新一下
meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=1310784 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5243136, 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 5243136 to 7865088
[root@lianxi ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.7G   16G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/vg1-lv1       30G   33M   30G   1% /lv1    #成功了
  1. 减小逻辑卷容量—lvreduce
    注意:xfs文件系统默认只支持扩容,不支持缩小,so,无法真正做到无损
[root@lianxi ~]# mkdir /word
[root@lianxi ~]# cp -a /dev/vg1/lv1 /word   #备份
[root@lianxi ~]# umount /dev/vg1/lv1         #卸载
[root@lianxi ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.7G   16G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
[root@lianxi ~]# lvreduce -L 20G /dev/vg1/lv1    #调整大小,默认大小,原来30G,变成20G,
  Rounding size to boundary between physical extents: 20.00 GiB.
  WARNING: Reducing active logical volume to 20.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
  Size of logical volume vg1/lv1 changed from 30.00 GiB (10241 extents) to 20.00 GiB (6827 extents).
  Logical volume vg1/lv1 successfully resized.
[root@lianxi ~]# mkfs.xfs -f /dev/vg1/lv1    #强制更新
meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=1310784 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5243136, 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@lianxi ~]#  mount /dev/vg1/lv1 /lv1    #重新挂载
[root@lianxi ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.7G   16G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/vg1-lv1       20G   33M   20G   1% /lv1
[root@lianxi ~]#   数据恢复就可以了

你可能感兴趣的:(Linux基础)