美文网首页
逻辑卷实现详解

逻辑卷实现详解

作者: 扎啤 | 来源:发表于2017-08-20 16:07 被阅读0次

在我们使用Linux的过程中难免会对现有的RAID磁盘阵列组的硬盘管理空间进行增加、减小操作等,如果当初安装系统时划分的硬盘空间没有考虑到今后的硬盘有可能会进行调整的话,我们就要重新规划并重装操作系统,以满足应用需求,这时Linux给我提供了一项硬盘设备管理技术LVM逻辑卷管理器(Logical Volume Manager)通过它,我们就可以实现对硬盘空间的动态划分和调整。

LVM逻辑卷管理器的技术结构

以下是LVM一些名词的解释,解释来自百度LVM:

PhysicalStorageMedia 物理存储设备
指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元。

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

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

LV(Logical Volume)逻辑卷
类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上,在逻辑卷LV之上可以建立文件系统。

PE(Physical Extent)物理块
每一个物理卷PV被划分为称为PE的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元,PE的大小是可以配置的,默认为4MB。所以物理卷(PV)大小等同的基本单元PE组成。

LE(Logical Extent)逻辑块
逻辑卷LV也被划分为可被寻址的基本单位,称为LE。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。


部署实践

1、为实验准备2块20G大小的硬盘


添加硬盘数量满足实验需求

如果不想在虚拟环境下重启识别新硬盘的话,可以执行下面的命令---
echo "- - -" > /sys/class/scsi_host/host0/scan

注意:三个- - -号之间有空隔。

2、将新添加的硬盘添加支持LVM逻辑卷管理中:

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

3、创建storage卷组,将新添加的两块硬盘设备加入到卷组中,查看卷组状态:

[root@CentOS7 ~]#vgcreate storage /dev/sdb /dev/sdc  ##添加入卷组
  Volume group "storage" successfully created
[root@CentOS7 ~]#vgdisplay 
  --- Volume group ---
  VG Name               storage    ##创建“storage”名称
  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               39.99 GiB
  PE Size               4.00 MiB
  Total PE              10238
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10238 / 39.99 GiB    ##硬盘卷组大小
  VG UUID               v8N8fM-s6x2-mTGN-AiCC-KGhK-uz9D-4w8LVc

4、划分出200M的逻辑设备:

在对逻辑卷划分中,如果我们使用-l参数来指定使用PE基本单元的个数,或者也可以使用常见以-L参数来以容量为单位划分。

[root@CentOS7 ~]#lvcreate -n lv -L 200 storage    ##200M大小的逻辑设备
  Logical volume "lv" created.
[root@CentOS7 ~]#lvdisplay     ##查看逻辑卷信息
  --- Logical volume ---
  LV Path                /dev/storage/lv
  LV Name                lv
  VG Name                storage
  LV UUID                ccH2lD-QUvS-IiUX-gc7K-kkE7-9MDD-UuMhmV
  LV Write Access        read/write
  LV Creation host, time CentOS7.laishaohua, 2017-08-20 20:27:40 +0800
  LV Status              available
  # open                 0
  LV Size                200.00 MiB    ##大小为200M大小
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

5、将逻辑卷格式为mkfs.xfs后挂载使用:

[root@CentOS7 ~]#mkfs.xfs /dev/storage/lv    ##格式化
meta-data=/dev/storage/lv        isize=512    agcount=4, agsize=12800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=51200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@CentOS7 ~]#mkdir /lvmstorage     ##创建挂载目录
[root@CentOS7 ~]#mount /dev/storage/lv /lvmstorage  ##挂载

6、查看挂载状态,并将挂载状态写入配置文件使其重启挂载信息不丢失

[root@CentOS7 ~]#df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda2                50G  1.2G   49G   3% /
devtmpfs                479M     0  479M   0% /dev
tmpfs                   489M     0  489M   0% /dev/shm
tmpfs                   489M  6.7M  482M   2% /run
tmpfs                   489M     0  489M   0% /sys/fs/cgroup
/dev/sda3                40G   33M   40G   1% /app
/dev/sr0                7.8G  7.8G     0 100% /mnt/cdrom
/dev/sda1              1014M  116M  899M  12% /boot
tmpfs                    98M     0   98M   0% /run/user/0
/dev/mapper/storage-lv  197M   11M  187M   6% /lvmstorage    ##卷状态
[root@CentOS7 ~]#echo "/dev/storage/lv /lvmstorage xfs defaults 0 0" >> /etc/fstab     ##echo 写入配置文件

7、缩小逻辑卷

注意:对逻辑卷缩小操作不当将会有丢失数据的危险,所以在生产环境中不得已需要缩小逻辑卷的话,也记得将重要的数据提前备份,以防数据丢失。另外对LVM逻辑卷缩小操作前需要检查文件系统的完整性,确保数据安全。
此外由于为mkfs.xfs格式系统,xfs默认只能扩大不能缩小,所以我们需要利用xfsdump / xfsrestore 来实现缩小,缩小后需要重新格式化才能挂载。(适用系统刚安装好,逻辑分区内没有什么数据或数据不多且不重要的情况下)

[root@CentOS7 ~]#rpm -qa xfsdump    ##查看系统有无xfsdump
xfsdump-3.1.4-1.el7.x86_64
[root@CentOS7 ~]#yum -y install xfsdump    ##安装xfsdump套件
[root@CentOS7 ~]#umount /lvmstorage/    ##卸载设备和挂载点的关联

检查文件系统的完整性:

[root@CentOS7 ~]#e2fsck -f /dev/storage/lv    ##e2fsck -f 强制检查
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
e2fsck: Bad magic number in super-block while trying to open /dev/storage/lv

The superblock could not be read or does not describe a correct ext2
filesystem.  If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193 <device>

Linux e2fsck命令用于检查使用 Linux ext2 档案系统的 partition 是否正常工作。

将LV逻辑卷的容量减小到150M:

[root@CentOS7 ~]#lvreduce -L 150M /dev/storage/lv 
  Rounding size to boundary between physical extents: 152.00 MiB.
  WARNING: Reducing active logical volume to 152.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce storage/lv? [y/n]: y
  Size of logical volume storage/lv changed from 200.00 MiB (50 extents) to 152.00 MiB (38 extents).
  Logical volume storage/lv successfully resized.

减小后将逻辑分区重新通过mkfs.xfs命令重新格式化才能挂载上

[root@CentOS7 ~]#mkfs.xfs -f /dev/storage/lv    ##格式化

将文件系统重新挂载并查看硬盘状态:

[root@CentOS7 ~]#mount -a 
[root@CentOS7 ~]#df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda2                50G  1.2G   49G   3% /
devtmpfs                479M     0  479M   0% /dev
tmpfs                   489M     0  489M   0% /dev/shm
tmpfs                   489M  6.7M  482M   2% /run
tmpfs                   489M     0  489M   0% /sys/fs/cgroup
/dev/sda3                40G   33M   40G   1% /app
/dev/sr0                7.8G  7.8G     0 100% /mnt/cdrom
/dev/sda1              1014M  116M  899M  12% /boot
tmpfs                    98M     0   98M   0% /run/user/0
/dev/mapper/storage-lv  149M  7.8M  141M   6% /lvmstorage    ##缩小至150M

8、扩容逻辑卷,只要卷组中的空间足够大就可以一直为逻辑卷扩容。

将逻辑卷lv扩展至300M:

[root@CentOS7 ~]#lvextend -L 300M /dev/storage/lv    ##扩展至300M
  Size of logical volume storage/lv changed from 152.00 MiB (38 extents) to 300.00 MiB (75 extents).
  Logical volume storage/lv successfully resized.
[root@CentOS7 ~]#xfs_growfs /dev/storage/lv 
xfs_growfs: /dev/storage/lv is not a mounted XFS filesystem
[root@CentOS7 ~]#xfs_growfs /dev/storage/lv    ##目标XFS文件系统来扩展
meta-data=/dev/mapper/storage-lv isize=512    agcount=4, agsize=9728 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=38912, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 38912 to 76800

df -Th查看分区空间

[root@CentOS7 ~]#df -Th
Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs        50G  1.2G   49G   3% /
devtmpfs               devtmpfs  479M     0  479M   0% /dev
tmpfs                  tmpfs     489M     0  489M   0% /dev/shm
tmpfs                  tmpfs     489M  6.7M  482M   2% /run
tmpfs                  tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/sda3              xfs        40G   33M   40G   1% /app
/dev/sr0               iso9660   7.8G  7.8G     0 100% /mnt/cdrom
/dev/sda1              xfs      1014M  116M  899M  12% /boot
tmpfs                  tmpfs      98M     0   98M   0% /run/user/0
/dev/mapper/storage-lv xfs       297M  7.9M  289M   3% /lvmstorage    ##扩展至300M

9、逻辑卷快照建立

在对逻辑卷管理中我们还可以建立快照卷功能,这项功能类似于VMware中的快照功能,提供数据纠错还原功能。

往逻辑卷组设备所挂载的目录用dd写入一个文件20M的文件:

[root@CentOS7 ~]#dd if=/dev/zero of=/lvmstorage/readme bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0348104 s, 602 MB/s
[root@CentOS7 /lvmstorage]#ll
total 20480
-rw-r--r--. 1 root root 20971520 Aug 20 15:18 readme

使用-s参数生成一个快照卷,-L参数指定划分的大小:

[root@CentOS7 ~]#lvcreate -L 150M -s -n SNAP  /dev/storage/lv
  Using default stripesize 64.00 KiB.
  Rounding up size to full physical extent 152.00 MiB
  Logical volume "SNAP" created.
[root@CentOS7 ~]#lvdisplay 
 --- Logical volume ---
  LV Path                /dev/storage/SNAP
  LV Name                SNAP
  VG Name                storage
  LV UUID                3Ftw8X-dqir-EEfA-ZAD9-KQjD-lQAS-np4Yn9
  LV Write Access        read/write
  LV Creation host, time CentOS7.laishaohua, 2017-08-20 15:23:10 +0800
  LV snapshot status     active destination for lv
  LV Status              available
  # open                 0
  LV Size                300.00 MiB
  Current LE             75
  COW-table size         152.00 MiB
  COW-table LE           38
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

在LV设备卷所挂载的目录中创建一个100M的二进制文件,再来查看逻辑卷状态:

[root@CentOS7 ~]#dd if=/dev/zero of=/lvmstorage/files count=1 bs=100M
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 3.13618 s, 33.4 MB/s
[root@CentOS7 ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/storage/SNAP
  LV Name                SNAP
  VG Name                storage
  LV UUID                3Ftw8X-dqir-EEfA-ZAD9-KQjD-lQAS-np4Yn9
  LV Write Access        read/write
  LV Creation host, time CentOS7.laishaohua, 2017-08-20 15:23:10 +0800
  LV snapshot status     active destination for lv
  LV Status              available
  # open                 0
  LV Size                300.00 MiB
  Current LE             75
  COW-table size         152.00 MiB
  COW-table LE           38
  Allocated to snapshot  48.64%    ##逻辑卷上升
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

检验SNAP快照效果,对逻辑卷进行快照合并还原,(先卸载逻辑设备目录的挂载)

[root@CentOS7 ~]#lvconvert --merge /dev/storage/SNAP 
  Merging of volume storage/SNAP started.
  lv: Merged: 39.23%
  lv: Merged: 100.00%
[root@CentOS7 ~]#mount -a
[root@CentOS7 ~]#ls /lvmstorage/    ##逻辑卷设备被快照后创建的100M文件也被还原了
readme

10、删除逻辑卷

当不再需要使用LVM逻辑卷管理器时,我们可以依次安装顺序删除逻辑卷
取消逻辑卷与目录的挂载关联,并删除 /etc/fstab配置文件中的设备删除:

[root@CentOS7 ~]#umount /lvmstorage/
[root@CentOS7 ~]#sed -i '$d' /etc/fstab  ##因为设备文件在最后一行,我们直接用sed -i执行删除

将LV逻辑卷设备删除,需要属y确认操作:

[root@CentOS7 ~]#lvremove /dev/storage/lv 
Do you really want to remove active logical volume storage/lv? [y/n]: y
  Logical volume "lv" successfully removed

将VG卷组删除:

[root@CentOS7 ~]#vgremove storage 
  Volume group "storage" successfully removed

最后将PV物理卷设备移除:

[root@CentOS7 ~]#pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.

确认上述操作执行成功,无提示信息则为操作正确完成逻辑卷设备移除:
[root@CentOS7 ~]#lvdisplay ;vgdisplay ;pvdisplay


总结:

  • 在对逻辑卷大小调整时,针对xfs和ext4不同的文件系统中,所使用的命令都不相同
    resize2fs命令 针对的是ext2、ext3、ext4文件系统
    xfs_growfs命令 针对的是xfs文件系统
  • xfs文件系统只支持增大分区空间的情况,不支持缩小,要减小的话,只能在减小后将逻辑分区重新通过mkfs.xfs命令重新格式化才能挂载,这样就会将原来存在的数据清除,所以在对xfs文件系统格式操作时需切记。
  • 在对逻辑卷快照操作能仅有一次有效,一旦被还原后则会被自动删除
  • 移除逻辑卷要依次删除LV逻辑卷、VG卷组后在移除PV物理卷设备,顺序不可颠倒。
  • 当然对硬盘的一切修改操作都需要建立在拥有备份的前提下才可进行。总之谨记住硬盘有价,数据无价,请谨慎操作!

相关文章

  • 逻辑卷实现详解

    在我们使用Linux的过程中难免会对现有的RAID磁盘阵列组的硬盘管理空间进行增加、减小操作等,如果当初安装系统时...

  • LVM逻辑卷配置过程

    LVM逻辑卷配置过程详解 LVM原理 要想理解好LVM的原理,我们必须首先要掌握4个基本的逻辑卷概念。 ①PE(P...

  • TCP/IP详解卷1 - 思维导图(1)

    TCP/IP详解卷1 - 系列文TCP/IP详解卷1 - 思维导图(1)TCP/IP详解卷1 - wireshar...

  • TCP/IP详解卷1 - wireshark抓包分析

    TCP/IP详解卷1 - 系列文TCP/IP详解卷1 - 思维导图(1)TCP/IP详解卷1 - wireshar...

  • TCP/IP详解卷1 - 思维导图(2)

    TCP/IP详解卷1 - 系列文TCP/IP详解卷1 - 思维导图(1)TCP/IP详解卷1 - wireshar...

  • 异步社区本周预售新书

    《算法详解(卷1)——算法基础》 Tim Roughgarden著 算法详解系列图书共有4卷,本书是第一卷——基础...

  • 2-4KVC

    KVC和KVO详解[https://www.jianshu.com/p/b9f020a8b4c9] 实现逻辑

  • LiveData

    LiveDataBus实现原理#用法详解#LiveData扩展 LiveDataBus实现原理#用法详解#Live...

  • 从源码角度剖析tcp/ip---ip协议(1)

    庖丁解牛,从源码角度来深入tcp/ip。----《TCP/IP详解 卷2:实现》。 一、简介和介绍 ip协议的是t...

  • 磁盘扩容

    添加一块新硬盘(查看硬盘信息) 创建分区 创建逻辑卷 查看逻辑卷 添加逻辑卷到逻辑组 lv增加大小

网友评论

      本文标题:逻辑卷实现详解

      本文链接:https://www.haomeiwen.com/subject/vlsxdxtx.html