lvm讲解

Posted

tags:

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

lvm

lvm (Logical Volume Manager)逻辑分卷管理器;可用于管理磁盘驱动器或其他类似的大容量存储设备.

通过LVM,可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),可以更简便地扩大和缩小分区,可以增删分区时无需担心某个硬盘上没有足够的连续空间,LVM是用来方便管理的,不会提供额外的安全保证。

lvm 基本组成

物理卷Physical volume (PV):可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的header,其余部分被切割为一块块物理区域(physical extents)。
卷组Volume group (VG):将一组物理卷收集为一个管理单元。
逻辑卷Logical volume (LV):虚拟分区,由物理区域(physical extents)组成。
物理区域Physical extent (PE):硬盘可供指派给逻辑卷的最小单位(通常为4MB)

示例:

两块物理硬盘
                
  硬盘1 (/dev/sda):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
    |分区1 50GB (物理卷)          |分区2 50GB (物理卷)        |   
    |/dev/sda1                    |/dev/sda2                |    
    |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _|_ _ _ _ _ _ _ _ _ _ _ _ _|
                                  
  硬盘2 (/dev/sdb):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    |分区1 120GB (物理卷)                |
    |/dev/sdb1                          |       
    | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
LVM方式

  卷组VG1 (/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
    |逻辑卷1 15GB                  |逻辑卷2 35GB                        |逻辑卷3 170GB                 |
    |/dev/MyStorage/rootvol        |/dev/MyStorage/homevol             |/dev/MyStorage/mediavol       |
    |_ _ _ _ _ _ _ _ _ _ _ _ _ _ __|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |

创建LVM

实例:
1、创建分区:

fdisk /dev/sdb

2、t 更改ID

Command (m for help): t
Partition number (1-3,5, default 5): 1
Hex code (type L to list all codes): L  

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  ‘W95 Ext‘d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

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

3、准备物理卷
pvcreate命令用于将物理硬盘分区初始化为物理卷,以便LVM使用

pvcreate  /dev/sdb1 /dev/sdb2     

在使用pvcreate命令时,如果提示没有这个命令,使用yum安装,在使用yum安装时,可以使用yum provides pvcreate来确认命令的依赖软件包。

[[email protected] ~]# pvcreate /dev/sdb1 /dev/sdb2
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
WARNING: xfs signature detected on /dev/sdb2 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb2.
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

验证物理卷:

[[email protected] ~]# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb1     lvm2 ---  10.0g 10.0g
  /dev/sdb2     lvm2 ---   5.0g  5.0g   
  
 详细显示PV信息   
 
 [[email protected] ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               UeQKm2-m02J-wUx-AVs7-B7Oj-31I-gbuua
   
  "/dev/sdb2" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               yt1V3N-SPNr-vylp-ZpDz-Drkb-lkdY-41y1r

4、创建卷组

vgcreate命令用于创建LVM卷组。卷组(Volume Group)将多个物理卷组织成一个整体,屏蔽了底层物理卷细节。在卷组上创建逻辑卷时不用考虑具体的物理卷信息。  

用法: 

vgcreate 卷组名  对应物理卷
创建:
[[email protected] ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
  Volume group "vg1" successfully created   
查看:
[[email protected] ~]# vgs
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg1   2   0   0 wz--n- 14.99g 14.99g

5、创建逻辑卷

lvcreate 命令用于创建LVM的逻辑卷。逻辑卷是创建在卷组之上的。逻辑卷对应的设备文件保存在卷组目录下,例如:在卷组vg100上创建一个逻辑卷lvol0,则此逻辑卷对应的设备文件为/dev/vg100/lvol0。  

常用参数:   
-L 指定大小
# vg1上创建一个3G的lv
[[email protected] ~]# lvcreate -L 3G -n lv01 vg1
  Logical volume "lv01" created.   

# vg1上创建占vg1容量50%的lv02 
[[email protected] ~]# lvcreate -l 50%VG -n lv02 vg1
  Logical volume "lv02" created.    

# 剩余空间全部分配给lv03  
[[email protected] ~]# lvcreate -l 100%FREE -n lv03 vg1
  Logical volume "lv03" created.  
 
# 检查lv状态 
[[email protected] ~]# lvscan
  ACTIVE            ‘/dev/vg1/lv01‘ [3.00 GiB] inherit
  ACTIVE            ‘/dev/vg1/lv02‘ [7.50 GiB] inherit
  ACTIVE            ‘/dev/vg1/lv03‘ [4.50 GiB] inherit

6、格式化

[[email protected] ~]# mke2fs -t ext4 /dev/vg1/lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe  blocks
196608 inodes, 786432 blocks
39321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

7、挂载

[[email protected] ~]# mount /dev/vg1/lv01 /date1
[[email protected] ~]# mount /dev/vg1/lv02 /date2
[[email protected] ~]# mount /dev/vg1/lv03 /date3
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G  1.9G   14G  12% /
devtmpfs              1.4G     0  1.4G   0% /dev
tmpfs                 1.4G     0  1.4G   0% /dev/shm
tmpfs                 1.4G  8.6M  1.4G   1% /run
tmpfs                 1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M  56% /boot
tmpfs                 280M     0  280M   0% /run/user/0
/dev/mapper/vg1-lv01  2.9G  9.0M  2.8G   1% /date1
/dev/mapper/vg1-lv02  7.3G   34M  6.9G   1% /date2
/dev/mapper/vg1-lv03  4.4G   18M  4.1G   1% /date3

8、写开机启动挂载

编辑 /etc/fstab 文件

扩容逻辑卷

lvresize命令用于调整LVM逻辑卷的空间大小,可以增大空间和缩小空间。使用lvresize命令调整逻辑卷空间大小和缩小空间时需要谨慎,因为它有可能导致数据丢失。

添加物理卷扩大卷组:

vgextend命令用于动态扩展LVM卷组,它通过向卷组中添加物理卷来增加卷组的容量。LVM卷组中的物理卷可以在使用vgcreate命令创建卷组时添加,也可以使用vgextend命令动态的添加
[[email protected] ~]# vgextend vg1 /dev/sdb5
WARNING: ext4 signature detected on /dev/sdb5 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb5.
  Physical volume "/dev/sdb5" successfully created.
  Volume group "vg1" successfully extended

如果原有vg卷上还有多余空间,则不需要前面的这一步,可以直接用以下步骤扩容:

# 指定容量扩展空间

[[email protected] ~]# lvextend -L +500M /dev/vg1/lv01
  Size of logical volume vg1/lv01 changed from 3.00 GiB (768 extents) to 3.49 GiB (893 extents).
  Logical volume vg1/lv01 successfully resized.
  
# 扩展剩余所有空间  

[[email protected] ~]# lvextend -l +100%FREE /dev/vg1/lv03
  Size of logical volume vg1/lv03 changed from 4.50 GiB (1151 extents) to 9.00 GiB (2305 extents).
  Logical volume vg1/lv03 successfully resized.
  
# 查看状态  

[[email protected] ~]# lvscan
  ACTIVE            ‘/dev/vg1/lv01‘ [3.49 GiB] inherit
  ACTIVE            ‘/dev/vg1/lv02‘ [7.50 GiB] inherit
  ACTIVE            ‘/dev/vg1/lv03‘ [9.00 GiB] inherit

检查磁盘错误:

在做此操作前,确认磁盘不是挂载状态!!!xfs不需要
[[email protected] ~]# e2fsck -f /dev/vg1/lv01
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv01: 14/196608 files (0.0% non-contiguous), 31038/786432 blocks

更新逻辑卷信息:

如果不更新卷信息,重新挂载后无法识别扩容后的大小!!
[[email protected] ~]# resize2fs /dev/vg1/lv01
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv01 to 914432 (4k) blocks.
The filesystem on /dev/vg1/lv01 is now 914432 blocks long.

xfs文件系统需要执行:

xfs_growfs /dev/vg1/lv01

扩容前大小:

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G  1.9G   14G  12% /
devtmpfs              1.4G     0  1.4G   0% /dev
tmpfs                 1.4G     0  1.4G   0% /dev/shm
tmpfs                 1.4G  8.6M  1.4G   1% /run
tmpfs                 1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M  56% /boot
tmpfs                 280M     0  280M   0% /run/user/0
/dev/mapper/vg1-lv01  2.9G  9.0M  2.8G   1% /date1
/dev/mapper/vg1-lv02  7.3G   34M  6.9G   1% /date2
/dev/mapper/vg1-lv03  4.4G   18M  4.1G   1% /date3

扩容后大小:

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G  1.9G   14G  12% /
devtmpfs              1.4G     0  1.4G   0% /dev
tmpfs                 1.4G     0  1.4G   0% /dev/shm
tmpfs                 1.4G  8.6M  1.4G   1% /run
tmpfs                 1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M  56% /boot
tmpfs                 280M     0  280M   0% /run/user/0
/dev/mapper/vg1-lv03  8.8G   21M  8.3G   1% /date3
/dev/mapper/vg1-lv02  7.3G   34M  6.9G   1% /date2
/dev/mapper/vg1-lv01  3.4G   12M  3.2G   1% /date1

缩减逻辑卷(xfs文件系统不支持)

1、先取消挂载
2、检查磁盘错误:

[[email protected] ~]# e2fsck -f /dev/vg1/lv01
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv01: 14/196608 files (0.0% non-contiguous), 31038/786432 blocks

3、更新逻辑卷信息:

[[email protected] ~]# resize2fs /dev/vg1/lv01 3G
** 后面的这个大小是缩减后的大小**
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv01 to 914432 (4k) blocks.
The filesystem on /dev/vg1/lv01 is now 914432 blocks long.

4、重设大小:

[[email protected] ~]#  lvresize -L 3G /dev/vg1/lv01      
  WARNING: Reducing active logical volume to 3.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv01? [y/n]: y
  Size of logical volume vg1/lv01 changed from 3.49 GiB (893 extents) to 3.00 GiB (768 extents).
  Logical volume vg1/lv01 successfully resized.
[[email protected] ~]# lvextend -L 3G /dev/vg1/lv02
  New size given (768 extents) not larger than existing size (1919 extents)
[[email protected] ~]# lvs
  LV   VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg1 -wi-ao---- 3.00g                                                    
  lv02 vg1 -wi-a----- 7.50g                                                    
  lv03 vg1 -wi-ao---- 9.00g                                                    
[[email protected] ~]# mount /dev/vg1/lv02 /date2
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              16G  1.9G   14G  12% /
devtmpfs              1.4G     0  1.4G   0% /dev
tmpfs                 1.4G     0  1.4G   0% /dev/shm
tmpfs                 1.4G  8.6M  1.4G   1% /run
tmpfs                 1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M  56% /boot
tmpfs                 280M     0  280M   0% /run/user/0
/dev/mapper/vg1-lv01  2.9G  9.1M  2.8G   1% /date1
/dev/mapper/vg1-lv03  3.9G   18M  3.6G   1% /date3
/dev/mapper/vg1-lv02  2.9G   23M  2.7G   1% /date2

lvresize和lvextend 是一样的效果,不过后者要在磁盘挂载后才能看到磁盘大小的变化!!

以上是关于lvm讲解的主要内容,如果未能解决你的问题,请参考以下文章

lvm讲解/磁盘故障小案例

4.10/4.11/4.12 lvm讲解 4.13 磁盘故障小案例

lvm讲解

LVM讲解

LVM简单讲解

4.10/4.11/4.12 lvm讲解 4.13 磁盘故障小案例