Linux下LVM

LVM(Logic Volume Management,逻辑卷管理

        由多个块设备(pv,卷)组成一个逻辑卷组(vg),接着在逻辑组上创建逻辑卷(lv),实现在线缩减逻辑卷与逻辑卷组。

lvm-pic.bmp

实验:

    1、VM虚拟机添加硬盘:

        2G(/dev/sdb)

        3G(/dev/sdc)

        5G(/dev/sdd)

    2、查看硬盘信息:

[root@neolinux ~]# fdisk -l | grep -e "^Disk /dev/sd[b-d]"
Disk /dev/sdb: 2147 MB, 2147483648 bytes
Disk /dev/sdc: 3221 MB, 3221225472 bytes
Disk /dev/sdd: 5368 MB, 5368709120 bytes

    3、将磁盘设定为PV,并且查看PV的信息:

[root@neolinux ~]# pvcreate /dev/sd[b-d]                               #将磁盘设置为PV
  Physical volume "/dev/sdb" successfully created
  Physical volume "/dev/sdc" successfully created
  Physical volume "/dev/sdd" successfully created
[root@neolinux ~]# pvs                                                 #显示当前PV信息
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda3  VG   lvm2 a--  17.80g    0 
  /dev/sdb        lvm2 ---   2.00g 2.00g
  /dev/sdc        lvm2 ---   3.00g 3.00g
  /dev/sdd        lvm2 ---   5.00g 5.00g
[root@neolinux ~]# pvdisplay /dev/sdb                                  #显示PV详细信息
  "/dev/sdb" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0                                              #PE Size是在创建VG时指定
  Total PE              0                                              #该磁盘一共有多少个PE,取决于创建VG时,指定PE的大小
  Free PE               0                                              #该磁盘剩余可用PE数量
  Allocated PE          0
  PV UUID               cu50Yp-PcWC-o76e-CTyK-CBgt-BjaJ-2z98lt         #每一个PV的UUID都不一样
[root@neolinux ~]#

    4、创建VG,并且查看VG的信息:

[root@neolinux ~]# vgcreate -s 16M myvg /dev/sd[b-d]                   #创建名字为myvg的逻辑卷组,添加/dev/sdb,/dev/sdc,/dev/sdd到此卷组中,指定PE大小为16M,默认为4M
  Volume group "myvg" successfully created
[root@neolinux ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  VG     1   2   0 wz--n- 17.80g    0 
  myvg   3   0   0 wz--n-  9.95g 9.95g                                 #此VG 一共有多少容量可使用
[root@neolinux ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               9.95 GiB
  PE Size               16.00 MiB                                        #VG中PE大小
  Total PE              637
  Alloc PE / Size       0 / 0   
  Free  PE / Size       637 / 9.95 GiB
  VG UUID               DLTJNf-he5S-I8rw-jUNZ-8CK1-Cb3w-TqCMCD
[root@neolinux ~]# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               myvg                                              #显示磁盘加入到myvg卷组中
  PV Size               2.00 GiB / not usable 16.00 MiB
  Allocatable           yes 
  PE Size               16.00 MiB
  Total PE              127
  Free PE               127
  Allocated PE          0
  PV UUID               cu50Yp-PcWC-o76e-CTyK-CBgt-BjaJ-2z98lt

    5、创建逻辑卷lv,并查看lv信息

[root@neolinux ~]# lvcreate  -n mylv -L 6G -p rw myvg                     #创建lv 
  Logical volume "mylv" created.
#-n:指定lv名称
#-L:指定lv大小
#-p:指定读写权限
[root@neolinux ~]# lvs 
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV01 VG   -wi-ao---- 9.77g                                                    
  LV02 VG   -wi-ao---- 8.04g                                                    
  mylv myvg -wi-a----- 6.00g 
[root@neolinux ~]# lvdisplay /dev/myvg/mylv
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                Ob0WO9-CMn0-pVLu-xd5a-fy4g-ABDM-JGB8et
  LV Write Access        read/write
  LV Creation host, time neolinux, 2016-06-04 02:29:09 +0800
  LV Status              available
  # open                 0
  LV Size                6.00 GiB
  Current LE             384
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
[root@neolinux ~]#

    6、格式化文件系统,挂载使用

[root@neolinux ~]# mke2fs -t ext4 /dev/myvg/mylv                         #格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@neolinux ~]# mount /dev/myvg/mylv /mnt                                #挂载使用
[root@neolinux ~]# mount | grep /mnt
/dev/mapper/myvg-mylv on /mnt type ext4 (rw)
[root@neolinux ~]# df -Th /mnt/
Filesystem           Type  Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                     ext4  5.8G   12M  5.5G   1% /mnt
[root@neolinux ~]# cp /etc/rc.d/init.d/functions /mnt
[root@neolinux ~]# ll /mnt/
total 44
-rw-r--r-- 1 root root 25419 Jun  4 02:55 functions
drwx------ 2 root root 16384 Jun  4 02:51 lost+found
[root@neolinux ~]#

    7、在线扩展lv容量

[root@neolinux ~]# lvextend -L +1G /dev/myvg/mylv   #+号表示在原有容量的基础上新增多少,不写+号表示容量扩容到多少,在扩展容量之前,应该提前检查该LV所在的VG上可用容量是否满足需求,在去扩展LV的逻辑边界
  Size of logical volume myvg/mylv changed from 6.00 GiB (384 extents) to 7.00 GiB (448 extents).  
  Logical volume mylv successfully resized
[root@neolinux ~]# df -Th /mntFilesystem          #查看挂载/mnt目录容量大小,没有发生变化,是因为没有扩展LV的逻辑边界         
Type  Size  Used Avail Use% Mounted on/dev/mapper/myvg-mylv                     
ext4  5.8G   13M  5.5G   1% /mnt
[root@neolinux ~]# resize2fs /dev/myvg/mylv     #扩展LV的逻辑边界,这里没有指定大小,表示与LV的物理边界一样大
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv to 1835008 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 1835008 blocks long.
[root@neolinux ~]# df -Th /mnt                #扩展之后的容量显示
Filesystem           Type  Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                     ext4  6.8G   14M  6.5G   1% /mnt
[root@neolinux ~]# ll /mnt/                   #/mnt目录下文件一样存在
total 44
-rw-r--r-- 1 root root 25419 Jun  4 02:55 functions
drwx------ 2 root root 16384 Jun  4 02:51 lost+found
在线扩容步骤总结:
    1、检查VG的容量是否满足扩容需求,不满足,请先扩容VG
    2、执行扩容LV物理边界命令
    3、确认LV物理边界是否扩容成功
    4、执行扩容LV逻辑边界
    5、检查挂载目录容量,确认扩容完成

    8、缩减LV容量

[root@neolinux ~]# umount /mnt    #取消挂载
[root@neolinux ~]# e2fsck -f /dev/myvg/mylv     #强制对磁盘进行检查
e2fsck 1.41.12 (17-May-2010)
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/myvg/mylv: 12/458752 files (0.0% non-contiguous), 65030/1835008 blocks
[root@neolinux ~]# resize2fs /dev/myvg/mylv 3G    #缩减LV的逻辑边界到3G容量大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 786432 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 786432 blocks long.
[root@neolinux ~]# lvreduce /dev/myvg/mylv -L 3G    #缩减LV物理边界到3G容量大小
  WARNING: Reducing active logical volume to 3.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y        #告警,确认
  Size of logical volume myvg/mylv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume mylv successfully resized
[root@neolinux ~]# lvs        #检查LV容量缩减是否完成
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV01 VG   -wi-ao---- 9.77g                                                    
  LV02 VG   -wi-ao---- 8.04g                                                    
  mylv myvg -wi-a----- 3.00g 
[root@neolinux ~]# mount /dev/myvg/mylv /mnt/    #重新挂载该LV
[root@neolinux ~]# ll /mnt/        #检查数据依然存在
total 44
-rw-r--r-- 1 root root 25419 Jun  4 02:55 functions
drwx------ 2 root root 16384 Jun  4 02:51 lost+found

    9、移除PV

[root@neolinux ~]# vgreduce myvg /dev/sdb        #在当LV挂载时,选择移除PV,在选择移除PV的时候,要考虑到该移除PV的总容量要小于当前该VG的剩余可用容量
  Removed "/dev/sdb" from volume group "myvg"
[root@neolinux ~]# pvs                        #确认PV已经移除VG
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda3  VG   lvm2 a--  17.80g    0 
  /dev/sdb        lvm2 ---   2.00g 2.00g
  /dev/sdc   myvg lvm2 a--   2.98g 2.98g
  /dev/sdd   myvg lvm2 a--   4.98g 1.98g
[root@neolinux ~]# ll /mnt/            #LV挂载数据依然存在
total 44
-rw-r--r-- 1 root root 25419 Jun  4 02:55 functions
drwx------ 2 root root 16384 Jun  4 02:51 lost+found
[root@neolinux ~]# vgs                
  VG   #PV #LV #SN Attr   VSize  VFree
  VG     1   2   0 wz--n- 17.80g    0 
  myvg   2   1   0 wz--n-  7.97g 4.97g
#我们在此进行LV缩减至2G
[root@neolinux ~]# df -Th /mnt
Filesystem           Type  Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                     ext4  2.0G  3.1M  1.9G   1% /mnt
[root@neolinux ~]# pvs        #可以清楚的看到LV上的数据保存在/dev/sdc这个PV中,
  PV         VG   Fmt  Attr PSize  PFree   
  /dev/sda3  VG   lvm2 a--  17.80g       0 
  /dev/sdb        lvm2 ---   2.00g    2.00g
  /dev/sdc   myvg lvm2 a--   2.98g 1008.00m
  /dev/sdd   myvg lvm2 a--   4.98g    4.98g
[root@neolinux ~]# 
#数据在/dev/sdc这个PV中,我们需要把这个PV移除VG
[root@neolinux ~]# pvmove /dev/sdc /dev/sdd    #移动/dev/sdc上的PE到/dev/sdd
  /dev/sdc: Moved: 0.0%
  /dev/sdc: Moved: 100.0%
[root@neolinux ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda3  VG   lvm2 a--  17.80g    0 
  /dev/sdb        lvm2 ---   2.00g 2.00g
  /dev/sdc   myvg lvm2 a--   2.98g 2.98g        #可以看到/sdc上没有存在数据
  /dev/sdd   myvg lvm2 a--   4.98g 2.98g        #实际存储的数据转移到/dev/sdd上
[root@neolinux ~]# 
#接下来我们可以安心把/dev/sdc从VG上卸载
[root@neolinux ~]# vgreduce myvg /dev/sdc 
  Removed "/dev/sdc" from volume group "myvg"
[root@neolinux ~]# ll /mnt/        #此时LV不受任何影响
total 44
-rw-r--r-- 1 root root 25419 Jun  4 03:35 functions
drwx------ 2 root root 16384 Jun  4 03:34 lost+found
[root@neolinux ~]#

    10、PV移除

[root@neolinux ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sda3  VG   lvm2 a--  17.80g    0 
  /dev/sdb        lvm2 ---   2.00g 2.00g
  /dev/sdc        lvm2 ---   3.00g 3.00g
  /dev/sdd   myvg lvm2 a--   4.98g 2.98g
[root@neolinux ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped

    11、快照snopshot功能

[root@neolinux ~]# df -Th /mnt        #检查mylv,大小为2G,使用3.1M
Filesystem           Type  Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                     ext4  2.0G  3.1M  1.9G   1% /mnt
[root@neolinux ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV01 VG   -wi-ao---- 9.77g                                                    
  LV02 VG   -wi-ao---- 8.04g                                                    
  mylv myvg -wi-ao---- 2.00g                                                    
[root@neolinux ~]# vgs            #查看mylv所在的vg还有6G的容量,足够我们创建快照卷
  VG   #PV #LV #SN Attr   VSize  VFree
  VG     1   2   0 wz--n- 17.80g    0 
  myvg   2   1   0 wz--n-  7.97g 5.97g
[root@neolinux ~]# lvcreate -L 3G -s -n mylv-snapshot /dev/myvg/mylv          #创建快照卷,大小为3G,快照卷选择的大小,应该是需要快照的卷容量加上需要备份时间内此卷的变化容量
  Reducing COW size 3.00 GiB down to maximum usable size 2.02 GiB.            #比如说快照备份时间需要30分钟,在30分钟原卷数据变化量1G,那么我们应该选择快照卷的大小为大于2G+1G 
  Logical volume "mylv-snapshot" created.

    12、对快照卷备份管理

[root@neolinux ~]# mount /dev/myvg/mylv-snapshot /opt/                    #快照卷是需要格式化就可以挂载,查看
[root@neolinux ~]# ls /opt/
functions  lost+found
[root@neolinux ~]# touch /mnt/test            #在原卷创建test文件
[root@neolinux ~]# ll /mnt/
total 44
-rw-r--r-- 1 root root 25419 Jun  4 03:35 functions
drwx------ 2 root root 16384 Jun  4 03:34 lost+found
-rw-r--r-- 1 root root     0 Jun  5 03:32 test
[root@neolinux ~]# 
[root@neolinux ~]# tar JcPf /backup/mylv-`date +%F`.tar.xz /opt/*        #对快照卷文件系统级别备份
[root@neolinux ~]# dd if=/dev/myvg/mylv-snapshot of=/backup/mylv-`date +%F`.dd        #对快照卷做块级别的备份
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 45.3875 s, 47.3 MB/s
[root@neolinux ~]# umount /opt/            #对快照卷取消挂载        
[root@neolinux ~]# lvremove /dev/myvg/mylv-snapshot         #删除快照卷
Do you really want to remove active logical volume mylv-snapshot? [y/n]: y
  Logical volume "mylv-snapshot" successfully removed
[root@neolinux ~]#

      13、模拟对原卷破坏

[root@neolinux ~]# dd if=/dev/zero of=/dev/myvg/mylv bs=512 count=1000        #模拟对原卷的破坏
1000+0 records in
1000+0 records out
512000 bytes (512 kB) copied, 0.0212266 s, 24.1 MB/s
[root@neolinux ~]# ll /mnt/            #检查数据,其实这个时候原卷已经损坏
total 0
[root@neolinux ~]# mount
/dev/mapper/VG-LV01 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/VG-LV02 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg-mylv on /mnt type ext4 (rw)
[root@neolinux ~]# umount /mnt/            #取消挂载原卷
[root@neolinux ~]# mount /dev/myvg/mylv /mnt/        #重新挂载原卷,已经挂载不上去了
mount: you must specify the filesystem type

    14、使用对数据块的备份恢复原卷数据

[root@neolinux ~]# dd if=/backup/mylv-2016-06-05.dd of=/dev/myvg/mylv         #对快照卷进行恢复
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 53.3457 s, 40.3 MB/s
[root@neolinux ~]# mount /dev/myvg/mylv /mnt        #重新挂载
[root@neolinux ~]# ll /mnt/        #检查数据为快照卷备份那时刻的数据
total 44
-rw-r--r-- 1 root root 25419 Jun  4 03:35 functions
drwx------ 2 root root 16384 Jun  4 03:34 lost+found
[root@neolinux ~]#

    15、编写一个小脚本对lvm进行定时备份

#!/bin/bash                            #/root/scripts/backuplvm.sh
#
/sbin/lvcreate -L 3G -s -n mylv-snapshot /dev/myvg/mylv
/bin/mount /dev/myvg/mylv-snapshot /opt/
/bin/tar JcPf /backup/mylv-`date +%F` /opt/*
/bin/umount /opt/
/sbin/lvremove /dev/myvg/mylv-snapshot
#
[root@neolinux scripts]# chmod +x /root/scripts/backuplvm.sh
[root@neolinux scripts]# crontab -l        #每天晚上23点59分59秒开始备份
0 0 * * * /bin/bash /root/scripts/backuplvm.sh
[root@neolinux scripts]#

原创文章,作者:nice_neo_linux,如若转载,请注明出处:http://www.178linux.com/17666

(0)
nice_neo_linuxnice_neo_linux
上一篇 2016-06-09
下一篇 2016-06-10

相关推荐

  • 第四周作业

    1. 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限 [root@localhost ~]# chmod -R g=,o=  /home/tuser1 [root@localhost home]# ll drwx——  3 root&…

    Linux干货 2016-12-26
  • 马哥教育21期网络班—第8周课程+练习—-成长进行时–不退步

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:一种网路装置,负责网路桥接,将网络的多个网段在数据链路层连接起来。 集线器:将多条以太网线或光纤集合连接在同一段物理介质下的装置。工作在物理层。 二层交换机:工作在数据链路层,交换机内部的CPU会在每个端口成功连接时,通过将MAC地址和端口对应,形成一张MAC表。在今后的通…

    Linux干货 2016-08-24
  • 马哥教育网络班21期第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥也叫桥接器,是连接两个局域网的一种存储/转发设备,用来连接不同网段。 集线器称为“Hub”,主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上。 二层交换机工作于OSI模型的第2层(数据链路层),可识别数据包中的MAC地址信…

    Linux干货 2016-09-19
  • gzip压缩算法

    gzip,zlib,以及图形格式png,使用的是同一个压缩算法deflate。我们通过对gzip源码的分析来对deflate压缩算法做一个详细的说明: 第一,gzip压缩算法基本原理的说明。 第二,gzip压缩算法实现方法的说明。 第三,gzip实现源码级的说明。 1. Gzip压缩算法的原理      &n…

    Linux干货 2015-07-30
  • Linux磁盘与文件系统管理的一些命令

    fdisk fdisk命令用于观察硬盘实体使用情况,也可对硬盘分区。它采用传统的问答式界面,而非类似DOS fdisk的cfdisk互动式操作界面,因此在使用上较为不便,但功能却丝毫不打折扣。 输入m列出可以执行的命令 p:显示磁盘分区表 n:new,新建分区 d:delete,删除分区 t:更改系统类型 l:列出已知分区类型 w:保存并退出 q:不保存退出…

    Linux干货 2017-04-23
  • bash工作特性之命令执行状态返回值与命令行展开概述

    bash工作特性之命令执行状态返回值与命令行展开概述 bash是什么? shell是一种用户接口程序,提供了用户与内核进行交互的接口。shell可以分为两类:一类为GUI,即用户图形界面;另一类为CLI,即命令行接口,将用户的命令送入到内核去执行,大多数发行版中最常用的就是bash。 bash特性 命令执行状态返回值 bash通过状态返回值来输出结果: 命令…

    Linux干货 2017-07-09