Linux逻辑卷管理(LVM)

Posted eddie1127

tags:

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

Linux逻辑卷管理(LVM)

LVM基本概念

PV (物理卷):

可以由硬盘或分区创建。(使用硬盘分区创建时PV时,若原分区已有文件系统,需将分区的id更改为8e,使用fdisk 命令中t选项修改)
VG(卷组):

由若干个PV组成,创建时候,需指定最小单元PE(物理扩展盘区)大小,并同时将指定的PV加入VG内作为成员。
PE(Physical Extent):
PE是PV中固定大小的物理存储单元,LVM技术就是通过在VG中交换PE来实现LV的空间容量扩容或缩减的
LV(逻辑卷):

在VG中划分,由若干个PE组成的分区,可以动态调整大小,其空间需小于或等于VG的最大空间。

LVM相关原理示意图如下:

技术图片

技术图片

LVM的操作管理

LVM操作相关命令

pv管理工具

显示pv信息

pvs:简要pv信息显示
pvdisplay:显示pv详细信息

创建pv
pvcreate /dev/DEVICE

删除pv
pvremove /dev/DEVICE

vg管理工具

显示卷组
vgs
vgdisplay

创建卷组
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]

管理卷组
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]

删除卷组
先做pvmove,再做vgremove

lv管理工具

显示逻辑卷
lvs
Lvdisplay

创建逻辑卷
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
lvcreate -l 60%VG -n mylv testvg
lvcreate -l 100%FREE -n yourlv testvg

删除逻辑卷
lvremove /dev/VG_NAME/LV_NAME

重设文件系统大小
fsadm [options] resize device [new_size[BKMGTEP]]
resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
xfs_growfs /mountpoint

扩展和缩减逻辑卷

扩展逻辑卷:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME
lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME

缩减逻辑卷:
umount /dev/VG_NAME/LV_NAME
e2fsck -f /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
mount

创建逻辑卷示例

创建物理卷
pvcreate /dev/sda3

为卷组分配物理卷
vgcreate vg0 /dev/sda3

从卷组创建逻辑卷
lvcreate -L 256M -n data vg0
mkfs.xfs /dev/vg0/data

挂载
mount /dev/vg0/data /mnt/data

跨主机迁移卷组

源计算机上
1 在旧系统中,umount所有卷组上的逻辑卷
2 禁用卷组
vgchange –a n vg0
lvdisplay
3 导出卷组
vgexport vg0
pvscan
vgdisplay
拆下旧硬盘

在目标计算机上
4 在新系统中安装旧硬盘,并导入卷组:vgimport vg0
5 vgchange –ay vg0 启用
6 mount所有卷组上的逻辑卷

逻辑卷管理器快照

使用LVM快照
为现有逻辑卷创建快照
lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data

挂载快照
mkdir -p /mnt/snap
mount -o ro /dev/vg0/data-snapshot /mnt/snap

恢复快照
umount /dev/vg0/data-snapshot
umount /dev/vg0/data
lvconvert --merge /dev/vg0/data-snapshot

删除快照
umount /mnt/databackup
lvremove /dev/vg0/databackup

实验:扩展逻辑卷

需求:原机器自带一块50G硬盘(设备名/dev/sda),使用LVM分区,先需将 / 分区扩展到100G。
步骤如下:

1、新增加一块100G硬盘/dev/sdb

[root@localhost ~]#fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b00a8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

2、对新增加硬盘创建pv

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

3、扩展原卷组

查看卷组信息

[root@localhost ~]#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               <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               I2rPlQ-7z6F-cbV4-vZZk-AK5v-u0FR-NmvCe2

扩展原卷组

[root@localhost ~]#vgextend centos /dev/sdb
  Volume group "centos" successfully extended

查看扩展后卷组信息

[root@localhost ~]#vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               148.99 GiB
  PE Size               4.00 MiB
  Total PE              38142
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       25600 / 100.00 GiB
  VG UUID               I2rPlQ-7z6F-cbV4-vZZk-AK5v-u0FR-NmvCe2

4、扩展 / 分区所在的LV

查看当前:LV状态

[root@localhost ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                0fF8gI-m0lE-kYtz-ML43-5uaS-ze05-jJXBch
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-17 20:50:23 +0800
  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                oM5Bcw-tgJ1-6k06-GCdk-sFx3-cy6E-xU3kJs
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-17 20:50:23 +0800
  LV Status              available
  # open                 1
  LV Size                46.99 GiB
  Current LE             12030
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

由以上信息可知 / 分区所属的LV为/dev/centos/root

扩展/dev/centos/root 的空间到100G

[root@localhost ~]#lvextend -r -L +50G /dev/centos/root 
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 96.99 GiB (24830 extents).
  Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3079680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12318720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6015, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12318720 to 25425920

至此,/ 分区 逻辑卷空间扩展完成。

逻辑卷的缩减

注意:LVM的缩减,只能针对ext系列的文件系统,如果centos7系统默认使用xfs文件系统,LVM只能扩容,不能缩减。

逻辑卷缩减操作步骤:
```
1、先备份数据(建议备份,不是必须,缩减lv大小有一定风险)

2、umount取消挂载(需要离线缩减)
umount /dev/VG_NAME/LV_NAME

3、需要对文件系统做检查,否则会因警告提示
fsck -f umount /dev/VG_NAME/LV_NAME

4、缩减文件系统到指定大小
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]

5、缩减lv到指定大小
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME

6、重新挂载lv到对应的挂载点
mount /dev/VG_NAME/LV_NAME /mountpoint
```

### 逻辑卷的快照管理

对现有逻辑卷(/dev/centos/root)创建快照

[root@localhost ~]#lvcreate -s -n root_snapshot -L 10G /dev/centos/root
  Logical volume "root_snapshot" created.
[root@localhost ~]#

查看快照结果

[root@localhost ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                0fF8gI-m0lE-kYtz-ML43-5uaS-ze05-jJXBch
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-17 20:50:23 +0800
  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/data
  LV Name                root
  VG Name                centos
  LV UUID                oM5Bcw-tgJ1-6k06-GCdk-sFx3-cy6E-xU3kJs
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-17 20:50:23 +0800
  LV snapshot status     source of
                         root_snapshot [active]
  LV Status              available
  # open                 1
  LV Size                96.99 GiB
  Current LE             24830
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/data_snapshot
  LV Name                root_snapshot
  VG Name                centos
  LV UUID                CLDfvM-9paq-ea6H-Hz1C-BYPr-eiGF-7hmqFT
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-08-18 12:34:31 +0800
  LV snapshot status     active destination for root
  LV Status              available
  # open                 0
  LV Size                96.99 GiB
  Current LE             24830
  COW-table size         10.00 GiB
  COW-table LE           2560
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4
   

新生成的 /dev/centos/data_snapshot 即是原来/dev/centos/data的快照备份

利用LVM快照恢复数据

注意:通过LVM快照方式恢复数据,必须先将原lv和快照lv先umont取消挂载,离线方式进行。

1、将原lv与快照取消挂载
umount /dev/centos/data_snapshot
umount /dev/centos/data

2、使用快照恢复原lv数据
[root@localhost ~]#lvconvert --merge /dev/centos/data_snapshot 
  Delaying merge since origin is open.
  Merging of snapshot centos/root_snapshot will occur on next activation of centos/data.

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

逻辑卷管理(LVM)

LInux磁盘管理:LVM逻辑卷

Linux磁盘管理,使用LVM逻辑卷来管理我们的磁盘

Linux —— 1.5万字详解LVM逻辑卷管理

转载Linux磁盘管理:LVM逻辑卷管理

Linux 服务器配置LVM(逻辑卷管理)