聊聊Linux的LVM(Logical Volume Manager)(上)

 

 

在Linux系统规划管理中,存储管理是一个非常重要的方面。日常运维过程中,两个方面的现实要求经常围绕这一个过程:动态空间管理和零散的存储硬件资源。

 

和Windows不同,Linux存储目录结构和磁盘结构是分离的。各个磁盘设备是被“装载mount”到指定的mount point上,我们经常可以看到一个“目录”的空间是多大。但是,存储是动态变化的过程。一个目录现在留足1G空间就够了,但是日后难保不会有2G的需要。如果使用传统的一些方法,变化过程就是比较复杂的。

 

另一方面,多年的信息化给企业信息中心遗留下存储设备通常是零散、大小不一的。往往需要一个目录空间很大,但是只有一些零散的小存储盘。如何整合这些零散存储资源,对操作系统管理而言是一个问题。

 

在Linux环境中,一种存储池“Storage Pool”机制的技术,就可以帮助我们进行存储资源的管理和调整。LVM(Logical Volume Manager)逻辑卷管理器是Linux提供的逻辑存储池。

 

1、LVM概述

 

在LVM中,有几个概念需要了解熟悉。

 

PV(Physical Volume)物理卷就是实际对应的物理存储设备。我们最常见的就是磁盘分区等存储设备。传统的存储管理方式是将物理设备格式化后,就直接mount到一个目录上。在LVM中,PV是一个基础物理单位,也是存储池的基本组成部分。存储分区要成为PV,需要调用LVM命令方法,在分区中记录相应的元数据信息。

 

LVG(Logical Volume Group)逻辑卷组是LVM的重要组成部分。从结构上看,LVG位于PV和LV(Logical Volume)的中间,也是Linux存储池的核心。若干个PV可以组成一个LVG。在分配LV(Logical Volume)的时候,也是在一个特定的LVG上进行分配,和PV的对应关系是通过LVM进行自动的管理对应。

 

LV(Logical Volume)逻辑卷是建立在LVG基础上的分配空间。LV是从LVG的基础上进行分配的,本质上是通过软件方法进行管理的空间组。正是由于通过LVM控制,我们可以实现动态的LV大小调整,从存储池中进行空间补充、回收。注意:LV适合LVG对应的,一个LV对操作系统和我们而言,是一个独立的存储空间。但是实质上,一个LV可能是一个PV的部分空间,也可能是跨多个PV。

 

下图摘自Red Hat的网站资料(http://www.redhat.com/magazine/009jul05/features/lvm2/),详细说明了LVM几个核心概念的关系。

 

17203031_201310092150381.jpg

 

下面介绍一下PE和LE。PE(Physical Extents)是PV的基本组成。每一个PV都是通过多个固定大小尺寸的PE组成。而LE(Logical Extent)与LV的关系类似,一个LV由多个固定大小的LE组成。目前LVM版本下,PE和LE的大小是相同的(注意!),默认都是4M。

 

PE和LE的机制就解决了LV横跨多个PV的对应问题。LVG解决的就是LE和PE对应问题。

 

在Linux中管理LVM主要通过LVM管理工具和Linux内核支持。目前我们常见的Linux版本中,逻辑卷功能已经非常成熟。下面我们通过实验介绍一下最基本的LVM使用。

 

2、环境准备和配置

 

我们选择Linux 2.6内核版本进行试验,也就是redhat 5.3。

 

 

[root@SimpleLinux ~]# uname -r

2.6.18-128.el5

 

 

使用LVM,要求内核中支持LVM,并且安装了LVM管理相关的rpm包。我们可以使用rpm –qa进行验证。

 

 

[root@SimpleLinux ~]# rpm -qa | grep lvm

system-config-lvm-1.1.5-1.0.el5

lvm2-2.02.40-6.el5

 

 

此时系统中,存储环境如下:

 

 

[root@SimpleLinux ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              48G   11G   35G  23% /

tmpfs                 449M     0  449M   0% /dev/shm

[root@SimpleLinux ~]# fdisk -l

 

Disk /dev/sda: 54.3 GB, 54390718464 bytes

255 heads, 63 sectors/track, 6612 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1        6420    51568618+  83  Linux

/dev/sda2            6421        6611     1534207+  82  Linux swap / Solaris

 

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1045     8388607+  8e  Linux LVM

 

Disk /dev/sdc: 11.3 GB, 11331395584 bytes

255 heads, 63 sectors/track, 1377 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

Disk /dev/sdc doesn't contain a valid partition table

 

 

当前系统中包括三个盘,sda-c。其中sda已经被使用,我们在sdb和sdc上进行试验。为了便于样式,我们将sdc进行进一步格式化,分割为五个分区。

 

 

[root@SimpleLinux ~]# fdisk /dev/sdc

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

 

 

The number of cylinders for this disk is set to 1377.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 

 

Command (m for help): n  

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-1377, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-1377, default 1377): +2000M

(篇幅原因,省略部分……)

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

[root@SimpleLinux ~]# fdisk -l

 

Disk /dev/sda: 54.3 GB, 54390718464 bytes

255 heads, 63 sectors/track, 6612 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1        6420    51568618+  83  Linux

/dev/sda2            6421        6611     1534207+  82  Linux swap / Solaris

 

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1045     8388607+  8e  Linux LVM

 

Disk /dev/sdc: 11.3 GB, 11331395584 bytes

255 heads, 63 sectors/track, 1377 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdc1               1         244     1959898+  83  Linux

/dev/sdc2             245         488     1959930   83  Linux

/dev/sdc3             489         732     1959930   83  Linux

/dev/sdc4             733        1377     5180962+   5  Extended

/dev/sdc5             733         976     1959898+  83  Linux

/dev/sdc6             977        1377     3221001   83  Linux

 

 

下面就可以进行逻辑卷实验。

 

3、LVM基础配置试验

 

LVM配置主要是通过LVM命令台和工具命令来实现的。通过lvm的帮助功能,我们能看到对lv、vg和pv的管理命令。

 

 

[root@SimpleLinux ~]# lvm

lvm> help

  Available lvm commands:

  Use 'lvm help ' for more information

  

  dumpconfig      Dump active configuration

  formats         List available metadata formats

  help            Display help for commands

  lvchange        Change the attributes of logical volume(s)

  lvconvert       Change logical volume layout

  lvcreate        Create a logical volume

  lvdisplay       Display information about a logical volume

  lvextend        Add space to a logical volume

  lvmchange       With the device mapper, this is obsolete and does nothing.

  lvmdiskscan     List devices that may be used as physical volumes

  lvmsadc         Collect activity data

  lvmsar          Create activity report

  lvreduce        Reduce the size of a logical volume

  lvremove        Remove logical volume(s) from the system

  lvrename        Rename a logical volume

  lvresize        Resize a logical volume

  lvs             Display information about logical volumes

  lvscan          List all logical volumes in all volume groups

  pvchange        Change attributes of physical volume(s)

  pvresize        Resize physical volume(s)

  pvck            Check the consistency of physical volume(s)

  pvcreate        Initialize physical volume(s) for use by LVM

  pvdata          Display the on-disk metadata for physical volume(s)

  pvdisplay       Display various attributes of physical volume(s)

  pvmove          Move extents from one physical volume to another

  pvremove        Remove LVM label(s) from physical volume(s)

  pvs             Display information about physical volumes

  pvscan          List all physical volumes

  segtypes        List available segment types

  vgcfgbackup     Backup volume group configuration(s)

  vgcfgrestore    Restore volume group configuration

  vgchange        Change volume group attributes

  vgck            Check the consistency of volume group(s)

  vgconvert       Change volume group metadata format

  vgcreate        Create a volume group

  vgdisplay       Display volume group information

  vgexport        Unregister volume group(s) from the system

  vgextend        Add physical volumes to a volume group

  vgimport        Register exported volume group with system

  vgmerge         Merge volume groups

  vgmknodes       Create the special files for volume group devices in /dev

  vgreduce        Remove physical volume(s) from a volume group

  vgremove        Remove volume group(s)

  vgrename        Rename a volume group

  vgs             Display information about volume groups

  vgscan          Search for all volume groups

  vgsplit         Move physical volumes into a new or existing volume group

  version         Display software and driver version information

lvm>

 

 

注意:lvm虽然提供了控制台接口,但是直接敲命令Linux也是支持的。

 

 

[root@SimpleLinux /]# pvscan

  No matching physical volumes found

 

 

虽然命令行很多,但是基本上lvm的命令还有很强的规律性。lv开头的都是对logical volume的操作,vg是对logical volume group,而pv是针对phyiscal volume的操作。

 

创建使用LVM,我们需要从最底层的PV进行构建管理。首先,我们需要创建最底层的Phsical Volume。

 

--pvcreate命令创建PV

[root@SimpleLinux /]# pvcreate /dev/sdb1

  Physical volume "/dev/sdb1" successfully created

 

--显示信息

[root@SimpleLinux /]# pvscan

  PV /dev/sdb1                      lvm2 [8.00 GB]

  Total: 1 [8.00 GB] / in use: 0 [0   ] / in no VG: 1 [8.00 GB]

[root@SimpleLinux /]# pvcreate /dev/sdc1

  Physical volume "/dev/sdc1" successfully created

[root@SimpleLinux /]# pvcreate /dev/sdc2

  Physical volume "/dev/sdc2" successfully created

[root@SimpleLinux /]# pvcreate /dev/sdc3

  Physical volume "/dev/sdc3" successfully created

[root@SimpleLinux /]# pvcreate /dev/sdc5

  Physical volume "/dev/sdc5" successfully created

[root@SimpleLinux /]# pvcreate /dev/sdc6

  Physical volume "/dev/sdc6" successfully created

[root@SimpleLinux /]# pvscan

  PV /dev/sdb1                      lvm2 [8.00 GB]

  PV /dev/sdc1                      lvm2 [1.87 GB]

  PV /dev/sdc2                      lvm2 [1.87 GB]

  PV /dev/sdc3                      lvm2 [1.87 GB]

  PV /dev/sdc5                      lvm2 [1.87 GB]

  PV /dev/sdc6                      lvm2 [3.07 GB]

  Total: 6 [18.55 GB] / in use: 0 [0   ] / in no VG: 6 [18.55 GB]

[root@SimpleLinux /]#

 

 

使用pvs可以显示出当前可用的Physical Volume情况。

 

 

[root@SimpleLinux /]# pvs

  PV         VG   Fmt  Attr PSize PFree

  /dev/sdb1       lvm2 --   8.00G 8.00G

  /dev/sdc1       lvm2 --   1.87G 1.87G

  /dev/sdc2       lvm2 --   1.87G 1.87G

  /dev/sdc3       lvm2 --   1.87G 1.87G

  /dev/sdc5       lvm2 --   1.87G 1.87G

  /dev/sdc6       lvm2 --   3.07G 3.07G

 

 

底层PV创建完成之后,就可以创建Logical Volume Group。

 

 

[root@SimpleLinux /]# vgcreate VolGrp01 /dev/sdb1 /dev/sdc1

  Volume group "VolGrp01" successfully created

[root@SimpleLinux /]# vgs

  VG       #PV #LV #SN Attr   VSize VFree

  VolGrp01   2   0   0 wz--n- 9.86G 9.86G

 

 

[root@SimpleLinux /]# vgcreate VolGrp02 /dev/sdc2 /dev/sdc3

  Volume group "VolGrp02" successfully created

[root@SimpleLinux /]# vgs

  VG       #PV #LV #SN Attr   VSize VFree

  VolGrp01   2   0   0 wz--n- 9.86G 9.86G

  VolGrp02   2   0   0 wz--n- 3.73G 3.73G

[root@SimpleLinux /]# vgdisplay

  --- Volume group ---

  VG Name               VolGrp02

  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               3.73 GB

  PE Size               4.00 MB

  Total PE              956

  Alloc PE / Size       0 / 0  

  Free  PE / Size       956 / 3.73 GB

  VG UUID               gfB8C3-80Ym-eEO5-L12p-8vdS-GWxy-t1QgWx

  

  --- Volume group ---

  VG Name               VolGrp01

  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               9.86 GB

  PE Size               4.00 MB

  Total PE              2525

  Alloc PE / Size       0 / 0  

  Free  PE / Size       2525 / 9.86 GB

  VG UUID               SDx5QB-7t14-RFrk-FUkK-1M43-4sNe-LFljgm

  

 

 

创建了两个LVG对象,每个LVG中包括两个分区PV。VolGrp01和VolGrp02为我们创建出的存储池。在其中,我们可以创建控制大小的Logical Volume。

 

 

[root@SimpleLinux /]# lvcreate -n lv1 -L +100M VolGrp01

  Logical volume "lv1" created

[root@SimpleLinux /]# lvs

  LV   VG       Attr   LSize   Origin Snap%  Move Log Copy%  Convert

  lv1  VolGrp01 -wi-a- 100.00M   

 

 

注意下lvcreate参数,-n表示创建lv的名称,-L表示补充大小。最后一个参数表示在哪个LVG中进行分配。这个分配动作是动态过程。创建了lv1之后,就可以将其作为一个存储进行单独使用。

 

最后,我们可以格式化lv1,进行分配。

 

 

[root@SimpleLinux ~]# mkfs -t ext3 /dev/VolGrp01/lv1

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

25688 inodes, 102400 blocks

5120 blocks (5.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=67371008

13 block groups

8192 blocks per group, 8192 fragments per group

1976 inodes per group

Superblock backups stored on blocks:

        8193, 24577, 40961, 57345, 73729

 

Writing inode tables: done                           

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 21 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

 

最后,我们可以加载mount到一个目录上。

 

 

[root@SimpleLinux /]# mkdir /voltest01

[root@SimpleLinux /]# ls -l | grep vol

drwxr-xr-x  2 root   root      4096 Oct  9 12:30 voltest01

 

[root@SimpleLinux VolGrp01]# mount /dev/VolGrp01/lv1 /voltest01

[root@SimpleLinux VolGrp01]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              48G   11G   35G  23% /

tmpfs                 449M     0  449M   0% /dev/shm

/dev/mapper/VolGrp01-lv1

                       97M  1.6M   91M   2% /voltest01

 

 

使用dd命令,测试/voltest01目录的可用性。

 

 

[root@SimpleLinux VolGrp01]# cd /voltest01/

[root@SimpleLinux voltest01]# ls -l

total 12

drwx------ 2 root root 12288 Oct  9 12:34 lost+found

[root@SimpleLinux voltest01]# dd if=/dev/zero f=/voltest01/res bs=1024 count=1000

1000+0 records in

1000+0 records out

1024000 bytes (1.0 MB) copied, 0.00376333 seconds, 272 MB/s

[root@SimpleLinux voltest01]# ls -l

total 1017

drwx------ 2 root root   12288 Oct  9 12:34 lost+found

-rw-r--r-- 1 root root 1024000 Oct  9 12:36 res

 

 

添加目录和加载成功。最后一个问题是,我们加载mount是手工过程,需要修改/etc/fstab,来实现自动添加。在下次reboot系统时候,就可以实现自动加载Logical Volume了。

 

 

[root@SimpleLinux ~]# blkid

/dev/sda2: TYPE="swap" LABEL="SWAP-sda2"

/dev/sda1: LABEL="/" UUID="36e661fb-e9ef-4583-8fc9-422b00009016" TYPE="ext3" SEC_TYPE="ext2"

/dev/cdrom: LABEL="RHEL/5.3 i386 DVD" TYPE="iso9660"

/dev/VolGrp01/lv1: UUID="5e007a89-326d-4a71-9744-8cd7d36a90a7" SEC_TYPE="ext2" TYPE="ext3"

/dev/mapper/VolGrp01-lv1: UUID="5e007a89-326d-4a71-9744-8cd7d36a90a7" SEC_TYPE="ext2" TYPE="ext3"

 

 

[root@SimpleLinux ~]# cat /etc/fstab

LABEL=/                 /                       ext3    defaults        1 1

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

UUID="5e007a89-326d-4a71-9744-8cd7d36a90a7"       /voltest01              ext3    defaults        1 1

LABEL=SWAP-sda2         swap                    swap    defaults        0 0

 

 

重新reboot之后,依然加载成功。

 

 

[root@SimpleLinux ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              48G   11G   35G  23% /

tmpfs                 449M     0  449M   0% /dev/shm

/dev/mapper/VolGrp01-lv1

                       97M  5.6M   87M   7% /voltest01

 

 

下面我们测试一下LVM的调整功能。

fj.pnguntitled.jpg

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17203031/viewspace-774018/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/17203031/viewspace-774018/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值