• 描述:LVM是redhat研发的一个组件,全称:Logical Volume Mangager,逻辑卷管理器。是内核dm(device mapper,磁盘映射)模块提供的功能,现在流行的版本是LVM2。

  • 功能:LVM2能够通过PV、VG、LV来根据业务对磁盘空间的使用需求来动态,不中断业务的情况下通过扩展、缩减工具来灵活调整分区的大小。并提供快照功能,提供了比较可靠的数据备份功能。

    注意:linux系统上如果把"/boot"这个单独划分出来时,"/boot"分区不能放在LVM上,因为"/boot"分区是和系统相关的,grup程序是无法识别LVM的,也就是说grup没有驱动LVM的程序。

  • 涉及的概念:

PV:physical volume,物理卷

VG:volume group,卷组

LV:logical volume,逻辑卷

PE:physical extents,物理盘区,在VG创建时PE就被指定其大小,是在lvm上数据存储的最小单位

LE:logical extents,逻辑盘区,在创建LV时被指定

snapshot:快照,主要用在数据备份

  • 图解LVM的组成

    linux之LVM管理_第1张图片

  • 实验操作

    实验环境:

    [root@zcj ~]# cat /etc/redhat-release
    CentOS release 5.10 (Final)
    [root@zcj ~]# uname -m
    x86_64

    事先准备了三个分区作为来作为PV,每个分区都是1GB大小,用fdisk工具已把这三个分区的ID已修改成8e(lvm的文件系统)


    [root@zcj ~]# fdisk -l
    Disk /dev/sda: 32.2 GB, 32212254720 bytes
    255 heads, 63 sectors/track, 3916 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1        1275    10241406   83  Linux
    /dev/sda2            1276        1530     2048287+  83  Linux
    /dev/sda3            1531        1661     1052257+  82  Linux swap / Solaris
    /dev/sda4            1662        3916    18113287+   5  Extended
    /dev/sda5            1662        1725      514048+  83  Linux
    /dev/sda6            1726        1848      987966   8e  Linux LVM
    /dev/sda7            1849        1971      987966   8e  Linux LVM
    /dev/sda8            1972        2094      987966   8e  Linux LVM

    创建PV、VG、LV,

    [root@zcj ~]# pvcreate /dev/sda6 /dev/sda7
      Writing physical volume data to disk "/dev/sda6"
      Physical volume "/dev/sda6" successfully created
      Writing physical volume data to disk "/dev/sda7"
      Physical volume "/dev/sda7" successfully created
    root@zcj ~]# pvs  \\可用pvdisplay命令查看详细信息
      PV         VG   Fmt  Attr PSize   PFree
      /dev/sda6       lvm2 a--  964.81M 964.81M
      /dev/sda7       lvm2 a--  964.81M 964.81M
    [root@zcj ~]# vgcreate myvg0 /dev/sda6 /dev/sda7
      Volume group "myvg0" successfully created
    [root@zcj ~]# vgs
      VG    #PV #LV #SN Attr   VSize VFree
      myvg0   2   0   0 wz--n- 1.88G 1.88G
    [root@zcj ~]# lvcreate -L 500M myvg0  \\可用“-n lvname”来指定名称
      Logical volume "lvol0" created
    [root@zcj ~]# lvs
      LV    VG    Attr   LSize   Origin Snap%  Move Log Copy%  Convert
      lvol0 myvg0 -wi-a- 500.00M

    至此,已创建好了LV了,接下来把创建好的LV设备格式化后,挂载就可使用了,但我们创建好的LV的设备是哪个呢?我们在格式化LV设备时可不能像引用VG一样,用VG的名称就可以“[root@zcj ~]# lvcreate -L 500M myvg0”我们得先用lvdisplay命令先得到LV的设备文件。


    [root@zcj ~]# lvdisplay
      --- Logical volume ---
      LV Name                /dev/myvg0/lvol0
      VG Name                myvg0
      LV UUID                hfjQyu-Fszp-ikQs-C4YK-FK9I-NxsV-H3OsFX
      LV Write Access        read/write
      LV Status              available
      # open                 0
      LV Size                500.00 MB
      Current LE             125 \\默认LE的大小
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0
    [root@zcj ~]# ll /dev/myvg0/lvol0 \\是一个链接文件
    lrwxrwxrwx 1 root root 23 Nov 13 13:17 /dev/myvg0/lvol0 -> /dev/mapper/myvg0-lvol0
    [root@zcj ~]# mkfs -t ext3 /dev/myvg0/lvol0
    [root@zcj ~]# mount /dev/myvg0/lvol0 /mnt/lvm/
    [root@zcj ~]# ls /mnt/lvm/
    lost+found
    [root@zcj ~]# cp /etc /mnt/lvm/
    cp: omitting directory `/etc'
    [root@zcj ~]# cp -R /etc /mnt/lvm/
    [root@zcj ~]# ls /mnt/lvm/
    etc  lost+found

    扩展、缩减LV

    扩充先要扩充LV的边界,再用resize2fs命令通知文件系统。并且不需要卸载文件系统。

    [root@zcj ~]# lvextend -L 1G /dev/myvg0/lvol0
      Extending logical volume lvol0 to 1.00 GB
      Logical volume lvol0 successfully resized
    [root@zcj ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1             9.5G  6.5G  2.6G  72% /
    /dev/sda5             487M   11M  451M   3% /home
    /dev/sda2             1.9G  222M  1.6G  13% /var
    tmpfs                 249M     0  249M   0% /dev/shm
    /dev/mapper/myvg0-lvol0
                          485M   69M  391M  15% /mnt/lvm \\与下边resize2fs后的比较
    [root@zcj ~]# resize2fs /dev/myvg0/lvol0
    resize2fs 1.39 (29-May-2006)
    Filesystem at /dev/myvg0/lvol0 is mounted on /mnt/lvm; on-line resizing required
    Performing an on-line resize of /dev/myvg0/lvol0 to 1048576 (1k) blocks.
    The filesystem on /dev/myvg0/lvol0 is now 1048576 blocks long.
    [root@zcj ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1             9.5G  6.5G  2.6G  72% /
    /dev/sda5             487M   11M  451M   3% /home
    /dev/sda2             1.9G  222M  1.6G  13% /var
    tmpfs                 249M     0  249M   0% /dev/shm
    /dev/mapper/myvg0-lvol0
                          992M   70M  872M   8% /mnt/lvm

    缩减时刚好与扩充时的顺序相反,先缩减文件系统边界,再缩减LV边界,并且先要卸载文件系统,进行文件系统的检测,比扩充的风险大。缩减操作完后再检测文件系统,最后挂载使用。

    [root@zcj ~]# umount /dev/myvg0/lvol0
    [root@zcj ~]# fsck /dev/myvg0/lvol0
    fsck 1.39 (29-May-2006)
    e2fsck 1.39 (29-May-2006)
    /dev/myvg0/lvol0: clean, 1804/260096 files, 103831/1048576 blocks
    [root@zcj ~]# resize2fs /dev/myvg0/lvol0 300M
    resize2fs 1.39 (29-May-2006)
    Please run 'e2fsck -f /dev/myvg0/lvol0' first.\\用fsck检测还不行
    [root@zcj ~]# e2fsck -f /dev/myvg0/lvol0
    e2fsck 1.39 (29-May-2006)
    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/myvg0/lvol0: 1804/260096 files (0.5% non-contiguous), 103831/1048576 blocks
    [root@zcj ~]# resize2fs /dev/myvg0/lvol0 300M
    resize2fs 1.39 (29-May-2006)
    Resizing the filesystem on /dev/myvg0/lvol0 to 307200 (1k) blocks.
    The filesystem on /dev/myvg0/lvol0 is now 307200 blocks long.
    [root@zcj ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1             9.5G  6.5G  2.6G  72% /
    /dev/sda5             487M   11M  451M   3% /home
    /dev/sda2             1.9G  222M  1.6G  13% /var
    tmpfs                 249M     0  249M   0% /dev/shm
    [root@zcj ~]# lvs
      LV    VG    Attr   LSize Origin Snap%  Move Log Copy%  Convert
      lvol0 myvg0 -wi-a- 1.00G
    [root@zcj ~]# lvreduce -L 300M /dev/myvg0/lvol0
      WARNING: Reducing active logical volume to 300.00 MB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce lvol0? [y/n]: y
      Reducing logical volume lvol0 to 300.00 MB
      Logical volume lvol0 successfully resized
    [root@zcj ~]# lvs
      LV    VG    Attr   LSize   Origin Snap%  Move Log Copy%  Convert
      lvol0 myvg0 -wi-a- 300.00M
    [root@zcj ~]# e2fsck /dev/myvg0/lvol0
    e2fsck 1.39 (29-May-2006)
    /dev/myvg0/lvol0: clean, 1804/77216 files, 80014/307200 blocks
    [root@zcj ~]# mount /dev/myvg0/lvol0 /mnt/lvm/
    [root@zcj ~]# ls /mnt/lvm/
    etc  lost+found

    扩展VG、缩减VG、移除PV

    要想移除PV时,先要保证此PV上的数据已转移到了其他的PV上,用pvmove即可实现数据的转移,再从VG中把此PV移除出来,最后用pvremove把块设备从PV中移除。

    [root@zcj ~]# pvs
      PV         VG    Fmt  Attr PSize   PFree
      /dev/sda6  myvg0 lvm2 a--  964.00M 664.00M
      /dev/sda7  myvg0 lvm2 a--  964.00M 964.00M
    [root@zcj ~]# vgextend myvg0 /dev/sda8
      No physical volume label read from /dev/sda8
      Writing physical volume data to disk "/dev/sda8"
      Physical volume "/dev/sda8" successfully created
      Volume group "myvg0" successfully extended
    [root@zcj ~]# pvs
      PV         VG    Fmt  Attr PSize   PFree
      /dev/sda6  myvg0 lvm2 a--  964.00M 664.00M
      /dev/sda7  myvg0 lvm2 a--  964.00M 964.00M
      /dev/sda8  myvg0 lvm2 a--  964.00M 964.00M
    [root@zcj ~]# vgs
      VG    #PV #LV #SN Attr   VSize VFree
      myvg0   3   1   0 wz--n- 2.82G 2.53G
    ###############MOVE PV###############
    [root@zcj ~]# pvmove /dev/sda6
      /dev/sda6: Moved: 1.3%
      /dev/sda6: Moved: 100.0%
    [root@zcj ~]# vgreduce myvg0 /dev/sda6
      Removed "/dev/sda6" from volume group "myvg0"
    [root@zcj ~]# pvremove /dev/sda6
      Labels on physical volume "/dev/sda6" successfully wiped
    [root@zcj ~]# pvs
      PV         VG    Fmt  Attr PSize   PFree
      /dev/sda7  myvg0 lvm2 a--  964.00M 664.00M
      /dev/sda8  myvg0 lvm2 a--  964.00M 964.00M

    快照卷的创建、移除

    快照卷的创建与LV的创建类似

    [root@zcj ~]# lvcreate -L 50M -s -p r -n lv010-snap /dev/myvg0/lvol0
      Rounding up size to full physical extent 52.00 MB
      Logical volume "lv010-snap" created
    [root@zcj ~]# ls /dev/myvg0/
    lv010-snap  lvol0
    [root@zcj ~]# mount -o ro /dev/myvg0/lv010-snap /media/ \\只读方式挂载
    [root@zcj ~]# ls /media/
    etc  lost+found
    ########这里就可以进行备份操作了########################
    [root@zcj ~]# lvremove /dev/myvg0/lv010-snap
    Do you really want to remove active logical volume lv010-snap? [y/n]: y
      Logical volume "lv010-snap" successfully removed

    快照卷创建后起初里面没有数据,它只是一个原卷的一个入口,访问快照卷,其实是指向了原卷。当快照卷创建后,原卷中的数据被修改时,原卷会把修改前的数据copy一份到快照卷的空闲空间里,然后再对原卷的数据进行修改,这个过程叫做copy on write(cow,写时复制),这样对快照卷的备份始终是创建快照时前的数据,如果cow很频繁,当写入的数据量超过我们建立的快照卷的大小时,快照卷立刻被释放,所以在快照卷的生命周期里请做完你的备份工作,在创建快照时也要估算好cow的数据量,如果快照卷的大小创建成与原卷一样大,那快照卷就是“与天同寿”了。