码迷,mamicode.com
首页 > 其他好文 > 详细

LVM逻辑卷管理

时间:2019-02-24 15:02:00      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:groups   term   sed   ted   creat   port   dom   ...   ble   

LVM(Logical Volume Manager)逻辑卷管理器,是一种磁盘管理工具,在磁盘和分区之上建立一个逻辑层实现了灵活的磁盘管理。LVM可将多个物理分区整合在一起,可按需添加或删除其管理的物理分区,逻辑分区可跨磁盘,容量可动态调整。LVM不仅应用在操作系统上,也广泛应用在容器(如docker)存储、分布式文件系统等方面。

LVM术语

  • Physical Volume (PV) 物理卷,一个PV对应一个磁盘或一个磁盘分区
  • Volume Group (VG) 卷组,一个或多个PV的集合,可动态增减PV,LVM系统中可含有多个VG
  • Logical Volume(LV) 逻辑卷,建立在VG之上的虚拟block设备, 容量可动态增减
  • Physical Extent(PE) PV的最小存储单元,最小为128KB,默认为4MB,在创建VG时设定,一旦设定很难更改,只能重建VG

技术图片
下面是CentOS 7默认安装,使用LVM命令显示的一些LVM信息,可帮助理解LVM:

# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               1
  Allocated PE          9982
  PV UUID               fVpHC4-LcnW-IkvS-q1Iu-ZkIF-6dEo-BBuXFp
# 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               <39.00 GiB
  PE Size               4.00 MiB
  Total PE              9983
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               YXgJ03-dlJZ-rgu9-VNDB-pEuJ-7RZg-Dg9rEK
# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                ZRTNg0-Y44i-VbIb-cVxG-kCuq-U7Og-WI33wo
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-11-10 00:36:21 +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                gkAo2f-v8vS-GKZi-NfDz-F6qM-BhrD-IIXa12
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-11-10 00:36:22 +0800
  LV Status              available
  # open                 1
  LV Size                36.99 GiB
  Current LE             9470
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

查看磁盘分区信息:

# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x0003d1dd

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

Disk /dev/mapper/centos-root: 39.7 GB, 39720058880 bytes, 77578240 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

查看块设备信息:

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   39G  0 part 
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]

查看文件系统信息:

# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  6.3G   31G  17% /
/dev/sda1               xfs      1014M  169M  846M  17% /boot

LVM管理

CentOS/RHEL 7默认了安装了LVM工具:

# yum install lvm2

查看LVM文档和支持的命令:

# man lvm

LVM配置目录:/etc/lvm

LVM命令
lvmconfig 显示LVM配置信息
lvmdiskscan 查询LVM设备

# lvmdiskscan 
  /dev/centos/root [      36.99 GiB] 
  /dev/sda1        [       1.00 GiB] 
  /dev/centos/swap [       2.00 GiB] 
  /dev/sda2        [     <39.00 GiB] LVM physical volume
  /dev/sdb1        [      <1.91 GiB] LVM physical volume
  /dev/sdb2        [      <1.91 GiB] LVM physical volume
  /dev/sdb3        [       4.18 GiB] LVM physical volume
  2 disks
  1 partition
  0 LVM physical volume whole disks
  4 LVM physical volumes

lvmdump 为诊断目的创建 lvm信息转储

PV

PV命令
pvchange 修改PV属性
pvck 检查PV metadata
pvcreate 从磁盘或分区创建PV
pvdisplay 显示PV属性
pvmove 移动PV内容(Physical Extents)
pvremove 擦除PV标签,LVM不再将其识别为PV。活动LV使用的PV不能删除
pvresize 调整PV容量
pvs 查询PV信息

# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <39.00g 4.00m

pvscan 查找PV

# pvscan
  PV /dev/sda2   VG centos          lvm2 [<39.00 GiB / 4.00 MiB free]
  Total: 1 [<39.00 GiB] / in use: 1 [<39.00 GiB] / in no VG: 0 [0   ]

创建PV
在创建PV前,先挂载一块硬盘sdb:

lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   39G  0 part 
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    8G  0 disk
  1. 从磁盘创建PV
# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

查看新建PV状态:

# pvdisplay 
  "/dev/sdb" is a new physical volume of "8.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               8.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               SuVbLN-KNmT-DlpW-acPI-HqFQ-Sn9w-p3zZJd

删除PV:

# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
  1. 从分区创建PV

先用fdisk创建分区sdb1、sdb2、sdb3,分区类型必须是8e(Linux LVM):

# 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.

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-16777215, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +2048MB
Partition 1 of type Linux and of size 1.9 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): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (4001792-16777215, default 4001792): 
Using default value 4001792
Last sector, +sectors or +size{K,M,G} (4001792-16777215, default 16777215): +2048MB
Partition 2 of type Linux and of size 1.9 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

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

Command (m for help): t
Partition number (1-3, default 3): 
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.

加载分区:

# partprobe 
# fdisk -l
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4001791     1999872   8e  Linux LVM
/dev/sdb2         4001792     8001535     1999872   8e  Linux LVM
/dev/sdb3         8001536    16777215     4387840   8e  Linux LVM
...

创建PV:

# pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

VG

VG命令
vgcfgbackup 备份VG配置
vgcfgrestore 恢复VG配置
vgchange 修改VG属性
vgck 检查VG元数据
vgconvert 转换VG元数据格式
vgcreate 创建VG
vgdisplay 显示VG属性
vgexport 从系统中注销VG
vgextend 向VG中增加PV
vgimport 注册已注销的VG
vgimportclone 注册和重命名克隆的VG

# vgimportclone --basevgname vg00_snap /dev/sdc /dev/sdd

vgmerge 将两个VG合并
vgmknodes Create the special files for volume group devices in /dev
vgreduce 删除VG中的一个或多个PV
vgremove 删除VG
vgrename 重命名VG
vgs 查询VG信息

# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <39.00g 4.00m

vgscan 查找VG

# vgscan
  Reading volume groups from cache.
  Found volume group "centos" using metadata type lvm2

vgsplit 将VG拆分为两个, 通过移动整个PV将任何LV从一个VG移动到另一个VG
创建VG
从PV /dev/sdb1、/dev/sdb2创建VG,VG名称为testvg,指定PE为16M:

# vgcreate -s 16M testvg /dev/sdb{1,2}
  Volume group "testvg" successfully created

查看新建VG的状态:

# vgdisplay
--- Volume group ---
  VG Name               testvg
  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               3.81 GiB
  PE Size               16.00 MiB
  Total PE              244
  Alloc PE / Size       0 / 0   
  Free  PE / Size       244 / 3.81 GiB
  VG UUID               dnUeP6-AktH-EUlJ-ok1Z-CvhS-L5Op-zgecve
# pvscan 
  PV /dev/sdb1   VG testvg          lvm2 [1.98 GiB / 1.98 GiB free]
  PV /dev/sdb2   VG testvg          lvm2 [5.98 GiB / 5.98 GiB free]

增加VG容量
可从PV创建VG,也可直接从磁盘和分区创建,这样会自动创建PV,如下将/dev/sdb3添加到VG中:

# vgextend testvg /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.
  Volume group "testvg" successfully extended

删除VG

# vgremove testvg
  Volume group "testvg" successfully removed

LV

LV命令
lvchange 修改LV属性
lvconvert Convert a Logical Volume from linear to mirror or snapshot.
lvcreate 创建LV
lvdisplay 显示LV属性
lvextend 扩展LV容量
lvreduce 减少LV容量
lvremove 删除LV
lvrename 重命名LV
lvresize 调整LV容量
lvs 查询LV信息

# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 36.99g                                                    
  swap centos -wi-ao----  2.00g

lvscan 查询LV

# lvscan
  ACTIVE            ‘/dev/centos/swap‘ [2.00 GiB] inherit
  ACTIVE            ‘/dev/centos/root‘ [36.99 GiB] inherit

创建LV
从testvg创建名为lv1,容量为4G的LV

# lvcreate -L 4G -n lv1 testvg
  Logical volume "lv1" created.

查看新建LV状态:

# lvdisplay
--- Logical volume ---
  LV Path                /dev/testvg/lv1
  LV Name                lv1
  VG Name                testvg
  LV UUID                Dt6e9v-1NGf-HIZp-tUYh-2Rbl-SAEk-W8YOpL
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-02-24 13:10:28 +0800
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

格式化和mount:
LV必须使用全名。

# mkfs -t xfs /dev/testvg/lv1
# mkdir /mnt/lvm
# mount /dev/testvg/lv1 /mnt/lvm

查看LV:

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/testvg-lv1   4.0G   33M  4.0G   1% /mnt/lvm

LVM逻辑卷管理

标签:groups   term   sed   ted   creat   port   dom   ...   ble   

原文地址:https://blog.51cto.com/7308310/2354083

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!