Linux LVM逻辑卷的创建、扩容、删除

时间:2021-2-20 作者:admin

目录

 

概要

创建

扩容

删除


概要

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

  • 物理卷(Physical Volume,PV)

指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

  • 卷组(Volume Group,VG)

类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。

  • 逻辑卷(Logical Volume,LV)

类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。

 

 

创建(实操)

先添加两块5G的新硬盘(如果为了练习,可以先添加三块5G的新硬盘,后面扩容方便点)

系统有两块对应的磁盘

按下面操作,对两块磁盘分区(/dev/sdb和/dev/sdc)

[root@localhost /]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xaefffe92.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

分区后 fdisk -l

创建物理卷(PV)

[root@localhost /]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@localhost /]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[root@localhost /]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               kEEGeU-FT5a-Bz4W-3HQ4-V2c1-zqyP-nNyB3W
   
  "/dev/sdb1" is a new physical volume of "<5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               g4plDW-thjy-Jtw7-LxBX-zLIJ-LqAI-2LZg7M
   
  "/dev/sdc1" is a new physical volume of "<5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               <5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               3prUPR-H5oT-OxAF-i2Hj-L9dS-Umks-lRg4mc
   
[root@localhost /]# 

创建卷组(VG)

创建了一个VG(myvg1)

[root@localhost /]# vgcreate myvg1 /dev/sdb1 /dev/sdc1
  Volume group "myvg1" successfully created
[root@localhost /]# vgdisplay
  --- Volume group ---
  VG Name               myvg1
  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               9.99 GiB
  PE Size               4.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               NucsdX-gjUo-XA7J-18ha-pK1E-YhlU-lHamqa
   
  --- 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               <49.00 GiB
  PE Size               4.00 MiB
  Total PE              12543
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               g4hpwo-ojVo-s7EC-GWfg-Mt0y-KVZv-QbaBPw
   

创建逻辑卷(LV)

创建了两个各2G的LV(mylv1和mylv2)

[root@localhost /]# lvcreate -L 2G -n mylv1 myvg1
  Logical volume "mylv1" created.
[root@localhost /]# lvcreate -L 2G -n mylv2 myvg1
  Logical volume "mylv2" created.
[root@localhost /]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/myvg1/mylv1
  LV Name                mylv1
  VG Name                myvg1
  LV UUID                QEcn9V-3FKG-gJad-ldQU-WtdN-xr59-Ognnwt
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-11-24 07:38:45 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/myvg1/mylv2
  LV Name                mylv2
  VG Name                myvg1
  LV UUID                OcR4go-Dacs-7C2d-2O5n-Me4u-HX51-F9lz1J
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-11-24 07:38:52 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                3ry9vu-dSCg-CU5z-lm39-28q5-BOYK-B3mtEG
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-09-23 23:06:03 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  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                zMe7qs-QyJF-odxN-ZJNk-wHw0-YKxv-3NrBRR
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-09-23 23:06:03 +0800
  LV Status              available
  # open                 1
  LV Size                <45.12 GiB
  Current LE             11550
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@localhost /]# 

格式化逻辑分区和挂载
 

其中vim /etc/fstab 中最下面添加两行(设置开机自动挂载)

/dev/myvg1/mylv1     /mytest1    xfs  defaults  0 0
/dev/myvg1/mylv2     /mytest2    xfs  defaults  0 0

[root@localhost /]# mkfs.xfs /dev/myvg1/mylv1 
meta-data=/dev/myvg1/mylv1       isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=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 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 /]# mkfs.xfs /dev/myvg1/mylv2
meta-data=/dev/myvg1/mylv2       isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=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 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 /]# cd /
[root@localhost /]# mkdir mytest1
[root@localhost /]# mkdir mytest2
[root@localhost /]# mount /dev/myvg1/mylv1 /mytest1
[root@localhost /]# mount /dev/myvg1/mylv2 /mytest2
[root@localhost /]# vim /etc/fstab 
[root@localhost /]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        49G   11G   38G  23% /
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G   14M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  188M  877M  18% /boot
tmpfs                   tmpfs     396M   33k  396M   1% /run/user/0
/dev/mapper/myvg1-mylv1 xfs       2.2G   34M  2.2G   2% /mytest1
/dev/mapper/myvg1-mylv2 xfs       2.2G   34M  2.2G   2% /mytest2
[root@localhost /]# 

创建完成!

 

扩容(实操)

创建的时候创建了两个2G的逻辑卷,这里把mylv1这个逻辑卷增加到6G,也就是增加4G

[root@localhost /]# lvs
  LV    VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos -wi-ao---- <45.12g                                                    
  swap  centos -wi-ao----  <3.88g                                                    
  mylv1 myvg1  -wi-ao----   2.00g                                                    
  mylv2 myvg1  -wi-ao----   2.00g                                                    
[root@localhost /]# lvextend -L +4G /dev/myvg1/mylv1
  Size of logical volume myvg1/mylv1 changed from 2.00 GiB (512 extents) to 6.00 GiB (1536 extents).
  Logical volume myvg1/mylv1 successfully resized.
[root@localhost /]# lvs
  LV    VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos -wi-ao---- <45.12g                                                    
  swap  centos -wi-ao----  <3.88g                                                    
  mylv1 myvg1  -wi-ao----   6.00g                                                    
  mylv2 myvg1  -wi-ao----   2.00g                                                    
[root@localhost /]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        49G   11G   38G  23% /
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G   14M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  188M  877M  18% /boot
tmpfs                   tmpfs     396M   33k  396M   1% /run/user/0
/dev/mapper/myvg1-mylv1 xfs       2.2G   34M  2.2G   2% /mytest1
/dev/mapper/myvg1-mylv2 xfs       2.2G   34M  2.2G   2% /mytest2
[root@localhost /]# xfs_growfs /dev/myvg1/mylv1
meta-data=/dev/mapper/myvg1-mylv1 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 1572864
[root@localhost /]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        49G   11G   38G  23% /
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G   14M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  188M  877M  18% /boot
tmpfs                   tmpfs     396M   33k  396M   1% /run/user/0
/dev/mapper/myvg1-mylv1 xfs       6.5G   35M  6.4G   1% /mytest1
/dev/mapper/myvg1-mylv2 xfs       2.2G   34M  2.2G   2% /mytest2
[root@localhost /]# 

LV扩展是基于VG足够的情况

刚才我们一共创建了10G的VG,分了各2G的LV,再加扩展的4G,现在还剩下2G的VG

如果还需要把mylv2扩展3G,那么VG目前是不够的(会报错),需要进行VG扩容

[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <49.00g 4.00m
  myvg1    2   2   0 wz--n-   9.99g 1.99g
[root@localhost ~]# lvextend -L +3G /dev/myvg1/mylv2
  Insufficient free space: 768 extents needed, but only 510 available

先关机给虚机添加一块新磁盘(如果最前面添加了三块,就不需要操作嘞)

先分区

[root@localhost ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xf30bfa41.

Command (m for help): 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

扩展VG

[root@localhost ~]# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created.
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <49.00g 4.00m
  myvg1    2   2   0 wz--n-   9.99g 1.99g
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <49.00g  4.00m
  /dev/sdb1  myvg1  lvm2 a--   <5.00g     0 
  /dev/sdc1  myvg1  lvm2 a--   <5.00g  1.99g
  /dev/sdd1         lvm2 ---   <5.00g <5.00g
[root@localhost ~]# vgextend myvg1 /dev/sdd1
  Volume group "myvg1" successfully extended
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  centos   1   2   0 wz--n- <49.00g  4.00m
  myvg1    3   2   0 wz--n- <14.99g <6.99g
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <49.00g  4.00m
  /dev/sdb1  myvg1  lvm2 a--   <5.00g     0 
  /dev/sdc1  myvg1  lvm2 a--   <5.00g  1.99g
  /dev/sdd1  myvg1  lvm2 a--   <5.00g <5.00g
[root@localhost ~]# 

然后按之前的方式扩展LV

[root@localhost ~]# lvextend -L +3G /dev/myvg1/mylv2
  Size of logical volume myvg1/mylv2 changed from 2.00 GiB (512 extents) to 5.00 GiB (1280 extents).
  Logical volume myvg1/mylv2 successfully resized.
[root@localhost ~]# lvs
  LV    VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos -wi-ao---- <45.12g                                                    
  swap  centos -wi-ao----  <3.88g                                                    
  mylv1 myvg1  -wi-ao----   6.00g                                                    
  mylv2 myvg1  -wi-ao----   5.00g                                                    
[root@localhost ~]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        49G   11G   38G  23% /
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G   14M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  188M  877M  18% /boot
/dev/mapper/myvg1-mylv2 xfs       2.2G   34M  2.2G   2% /mytest2
/dev/mapper/myvg1-mylv1 xfs       6.5G   35M  6.4G   1% /mytest1
tmpfs                   tmpfs     396M  4.1k  396M   1% /run/user/42
tmpfs                   tmpfs     396M   25k  396M   1% /run/user/0
[root@localhost ~]# xfs_growfs /dev/m
mapper/ mcelog  mem     midi    mqueue/ myvg1/  
[root@localhost ~]# xfs_growfs /dev/myvg1/mylv2
meta-data=/dev/mapper/myvg1-mylv2 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 1310720
[root@localhost ~]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        49G   11G   38G  23% /
devtmpfs                devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     2.0G   14M  2.0G   1% /run
tmpfs                   tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  188M  877M  18% /boot
/dev/mapper/myvg1-mylv2 xfs       5.4G   35M  5.4G   1% /mytest2
/dev/mapper/myvg1-mylv1 xfs       6.5G   35M  6.4G   1% /mytest1
tmpfs                   tmpfs     396M  4.1k  396M   1% /run/user/42
tmpfs                   tmpfs     396M   25k  396M   1% /run/user/0
[root@localhost ~]# 

扩容完成!

 

删除(实操)

删除LV之前需要先卸载挂载点和在fstab中去掉加的两行挂载信息

[root@localhost ~]# umount /mytest1
[root@localhost ~]# umount /mytest2
[root@localhost ~]# vim /etc/fstab 

删除LV、VG、PV

[root@localhost ~]# lvs
  LV    VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos -wi-ao---- <45.12g                                                    
  swap  centos -wi-ao----  <3.88g                                                    
  mylv1 myvg1  -wi-a-----   6.00g                                                    
  mylv2 myvg1  -wi-a-----   5.00g                                                    
[root@localhost ~]# lvremove /dev/myvg1/mylv1
Do you really want to remove active logical volume myvg1/mylv1? [y/n]: y
  Logical volume "mylv1" successfully removed
[root@localhost ~]# lvremove /dev/myvg1/mylv2
Do you really want to remove active logical volume myvg1/mylv2? [y/n]: y
  Logical volume "mylv2" successfully removed
[root@localhost ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <45.12g                                                    
  swap centos -wi-ao----  <3.88g                                                    
[root@localhost ~]# 
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <49.00g   4.00m
  myvg1    3   0   0 wz--n- <14.99g <14.99g
[root@localhost ~]# vgremove myvg1
  Volume group "myvg1" successfully removed
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <49.00g 4.00m
[root@localhost ~]# 
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <49.00g  4.00m
  /dev/sdb1         lvm2 ---   <5.00g <5.00g
  /dev/sdc1         lvm2 ---   <5.00g <5.00g
  /dev/sdd1         lvm2 ---   <5.00g <5.00g
[root@localhost ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped.
[root@localhost ~]# pvremove /dev/sdc1
  Labels on physical volume "/dev/sdc1" successfully wiped.
[root@localhost ~]# pvremove /dev/sdd1
  Labels on physical volume "/dev/sdd1" successfully wiped.
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <49.00g 4.00m
[root@localhost ~]# 

删除完成!

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。