一、LVM简介



LVM是 Logical Volume Manager(逻辑卷管理)的简写。LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。



与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为应用与用户分配存储空间。在LVM管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。LVM也允许按用户组对存储卷进行管理,允许管理员用更直观的名称(如"sales'、 'development')代替物理磁盘名(如'sda'、'sdb')来标识存储卷。



二、 LVM基本术语



物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。



物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。



卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。



逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。



PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。



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



简单来说就是:



PV:是物理的磁盘分区



VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。



LV:也就是从VG中划分的逻辑分区



 



三、创建和管理LVM
1、显示硬盘及所属分区情况



[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44817895

   Device Boot      Start         End      Blocks   Id  System



2 、增加三个分区的操作(/dev/sdb1  /dev/sdb2 /dev/sdb3)

创建第一个主分区/dev/sdb1 大小为10G,/dev/sdb 为20G,/dev/sdb3 为10G



Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p ##分区类型
Partition number (1-4): 1 ##第一个主分区
First cylinder (1-6527, default 1):
Using default value 1     ##起始块
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G ##指定分区的大小



 



Command (m for help): n   ##开始分第二主分区
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-6527, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-6527, default 6527): +20G

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): n  ##开始分第三个主分区
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3919-6527, default 3919):
Using default value 3919
Last cylinder, +cylinders or +size{K,M,G} (3919-6527, default 6527): +10G

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44817895

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  8e  Linux LVM
/dev/sdb2            1307        3918    20980890   8e  Linux LVM
/dev/sdb3            3919        5224    10490445   8e  Linux LVM

Command (m for help): w                      ##最后记得保存,写入分区表
The partition table has been altered!



 

3、创建物理卷PV



[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3   
  Physical volume "/dev/sdb1" successfully created  #同时将三个分区创建成三个物理卷
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created



 

4、创建卷组VG

创建两个卷组vg_server1  vg_server2 将物理卷/dev/sdb1、/dev/sdb2加入卷组vg_server1 ;将物理卷/dev/sdb3加入卷组vg_server2



[root@localhost ~]# vgcreate vg_server1 /dev/sdb1 /dev/sdb2    
  Volume group "vg_server1" successfully created
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# vgcreate vg_server2 /dev/sdb3
  Volume group "vg_server2" successfully created
[root@localhost ~]#



5、创建逻辑卷lV

分在卷组vg_server1和vg_server2创建逻辑卷



[root@localhost ~]# lvcreate -L 15G -n vg_server1_lvm1 vg_server1    # -L指定卷大小,-n 指定逻辑卷名
  Logical volume "vg_server1_lvm1" created.                                        #指定跟卷组名
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 5G -n vg_server2_lvm2 vg_server2
  Logical volume "vg_server2_lvm2" created.



[root@localhost ~]# lvdisplay  #查看生成的逻辑卷
  --- Logical volume ---
  LV Path                /dev/vg_server2/vg_server2_lvm2
  LV Name                vg_server2_lvm2
  VG Name                vg_server2
  LV UUID                jP8fZf-CR1l-Tpat-Q92Y-H4bZ-unJQ-dTYDra
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-04-14 00:21:11 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/vg_server1/vg_server1_lvm1
  LV Name                vg_server1_lvm1
  VG Name                vg_server1
  LV UUID                j4UX2E-LuRf-WKgW-9KGH-dBb3-RvAK-PXcORB
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-04-14 00:20:54 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3840
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0



 

6、格式化



[root@localhost ~]# mk2fs -t ext3 /dev/vg_server1/vg_server1_lvm1
-bash: mk2fs: command not found
[root@localhost ~]# mke2fs -t ext3 /dev/vg_server1/vg_server1_lvm1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3932160 blocks
196608 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4026531840
120 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, 1605632, 2654208

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -t ext3 /dev/vg_server2/vg_server2_lvm2
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 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

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.



 

 

7 创建两个目录/mnt/lvm1 /mnt/lvm2,挂载生成两个逻辑卷



[root@localhost vg_server1]# mount /dev/vg_server2/vg_server2_lvm2 /mnt/lvm2
[root@localhost vg_server1]# mount /dev/vg_server1/vg_server1_lvm1 /mnt/lvm1



 

8、写入开机自动挂载



#
# /etc/fstab
# Created by anaconda on Mon Sep 14 21:58:13 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=5d9aa445-64de-4001-b45f-10db6bf51248 /                       ext4    defaults        1 1
UUID=c26e447c-8fde-4c82-b15f-5cd3a86a402b /boot                   ext4    defaults        1 2
UUID=ec4bd037-8b39-4148-9b07-9f15279caa92 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/mapper/vg_server1-lvm_1   /mnt/lvm1        ext3    defaults        0 0
/dev/mapper/vg_server2-lvm_2   /mnt/lvm2        ext3    defaults        0 0
~



9.LVM 扩容

LVM扩展,可以将另一个分区(没有数据的分区)或另一块新硬盘重新分区,然后将该分区创建物理卷,加入相应要扩容的逻辑卷的所在卷组。

在虚拟机添加一个块硬盘。



[root@localhost dir]# fdisk /dev/sdc                  #给新加硬分区                       

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-2610, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +5G

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe4571f5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  8e  Linux LVM  
/dev/sdc2            1307        1960     5253255   8e  Linux LVM    ##新建的分区/dev/sdc2,将用于LVM扩容



[root@localhost dir]# pvcreate /dev/sdc2                   #将分区/dev/sdc2创建为物理卷
  Physical volume "/dev/sdc2" successfully created   
[root@localhost dir]# vgextend vg_server1 /dev/sdc2 #将物理卷/dev/sdc2 加入卷组vg_server1
  Volume group "vg_server1" successfully extended
[root@localhost dir]# lvextend -L +5G /dev/vg_server1/vg_server1_lvm1 /dev/sdc2  #扩容/dev/vg_server1/vg_server1_lvm1逻辑卷,其中-L指定大小
                                                                                                                                       #+5G 容量增加5G ,如果没有”+“ 表示扩展到多少
  Size of logical volume vg_server1/vg_server1_lvm1 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
  Logical volume vg_server1_lvm1 successfully resized



[root@localhost lvm1]# /sbin/resize2fs /dev/vg_server1/vg_server1_lvm1                                   #最后记得用命令resize 扩展逻辑大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_server1/vg_server1_lvm1 is mounted on /mnt/lvm1; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_server1/vg_server1_lvm1 to 7340032 (4k) blocks.
The filesystem on /dev/vg_server1/vg_server1_lvm1 is now 7340032 blocks long.

[root@localhost lvm1]# df -h
Filesystem                              Size  Used Avail Use% Mounted on
/dev/sda2                                18G  3.7G   13G  22% /
tmpfs                                   504M     0  504M   0% /dev/shm
/dev/sda1                               194M   26M  158M  15% /boot
/dev/mapper/vg_server2-vg_server2_lvm2  5.0G  139M  4.6G   3% /mnt/lvm2
/dev/mapper/vg_server1-vg_server1_lvm1   25G  173M   26G   1% /mnt/lvm1
[root@localhost lvm1]#