zoukankan      html  css  js  c++  java
  • Redhat 平台下 LVM 管理说明

    Redhat 平台下  LVM 管理说明

    LVM 是 Logical Volume Manager(逻辑卷管理器)的简写,它为主机提供了更高层次的磁盘存储管理能力。LVM 可以帮助系统管理员为应用与用户方便地分配 存储空间。在 LVM 管理下的逻辑卷可以按需改变大小或添加移除。另外,LVM 可 以为所管理的逻辑卷提供定制的命名标识。因此,使用 LVM 主要是方便了对存储 系统的管理,增加了系统的扩展性。

    Linux,AIX,HP-UNIX 平台下的 LVM  命令不同。下面以 Redhat  下的命令来做一些说明。

    一.    LVM   理论知识

    1.  LVM 创建的流程:  PV-->VG-->LV

    PV(Physical Volume) :物理卷,可以是单独磁盘,也可以是硬盘分区。

    VG(Volume  Group)  :卷组,是 PV 的组合,可以看成单独的逻辑磁盘。 LV(Logical   Volume) :逻辑分区,相当于物理分区的/dev/hdaX,只有逻辑卷才 可以写数据。

    PE(Physical Extent)  :物理范围。VG 单元,类似于 RAID 的条带大小。      当 多个 PV 组成一个 VG 时,LVM 会在所有 PV 上做类似格式化的动作,将每个 PV 切 成一块块的空间,这一块块的空间就称为 PE,  通常是 4MB。

    LE(Logical Extent)  :逻辑范围。LV 的组成单位。大小为 PE 的倍数(通常为 1:1)。

    2. 工作原理

     

    1. LVM  在每个物理卷头部都维护了一个  metadata,每个 metadata  中都包含了整个  VG  的信息,包括每个  VG  的布局配置、PV  的编号、LV  的编号,以及每 个  PE 到  LE  的映射关系。同一个  VG  中的每个  PV  头部的信息是相同的,这样有利于故障时进行数据恢复。
    2. LVM  对上层文件系统提供  LV  层,隐藏了操作细节。对于文件系统,对  LV的操作与原先对  Partition 的操作没有差别。当对  LV  进行写入操作时,LVM定位相应的  LE,通过   PV 头部的映射表,将数据写入到相应的  PE 上。
    3. LVM  实现的关键在于在  PE 和  LE  间建立映射关系,不同的映射规则决定了不同的  LVM  存储模型。LVM  支持多个  PV  的  Stripe 和  Mirror       ,这点和软Raid  的实现十分相似。

    使用  LVM  的优势:

    •文件系统可以跨多个磁盘,因此大小不会受物理磁盘的限制。

    •可以在系统运行状态下动态地扩展文件系统大小。

    •可以增加新磁盘到  LVM  的存储池中。

    •可以以镜像的方式冗余重要数据到多个物理磁盘上。

    •可以很方便地导出整个卷组,并导入到另外一台机器上。

    使用  LVM  的限制:

    •在从卷组中移除一个磁盘时必须使用  reducevg,否则会出问题。

    •当卷组中的一个磁盘损坏时,整个卷组都会受影响。

    •不能减小文件系统大小(受文件系统类型限制)。

    •因为加入了额外的操作,存储性能会受影响(使用  Stripe 的情况另当别论)。

    二.  创建相应的磁盘分区

    1. 修改磁盘,创建相应的分区

    [root@hejianlai]# fdisk -l

    Disk  /dev/sda: 42.9  GB, 42949672960  bytes
    255  heads, 63  sectors/track, 5221  cylinders
    Units = cylinders of  16065  *  512  = 8225280  bytes
    Device  Boot          Start                  End            Blocks      Id     System
    /dev/sda1       *                     1                  255          2048256      83    Linux
    /dev/sda2                           256                  382          1020127+    82    Linux  swap / Solaris
    /dev/sda3                           383                5221        38869267+      5    Extended
    /dev/sda5                           383                  395            104391      83    Linux
    /dev/sda6                           396                  408            104391      83    Linux
    /dev/sda7                           409                  421            104391      83    Linux
    /dev/sda8                           422                  434            104391      83    Linux
    

    这里创建了 4 个扩展分区,每个 100M。  用 fdisk /dev/sda 创建的。 分区没有 格式化。

    2. 使磁盘分区生效

    #partprobe

    三.  创建 PV

    1.相关命令

    pvcreate   创建 PV

    pvscan        扫描 PV

    pvdisplay    显示 PV

    pvremove   删除 PV

    partprobe

    2.创建物理卷

    [root@hejianlai]#  pvcreate /dev/sda5 /dev/sda6

    Physical volume  "/dev/sda5" successfully created

    Physical volume  "/dev/sda6" successfully created

    [root@hejianlai]#  pvscan

    PV  /dev/sda5       lvm2 [101.94 MB]

    PV  /dev/sda6       lvm2 [101.94 MB]

    Total: 2 [203.89 MB]  / in use: 0  [0    ] / in no  VG: 2  [203.89 MB]

    [root@hejianlai]#  pvdisplay

    "/dev/sda5" is  a  new physical volume  of  "101.94 MB"

    --- NEW   Physical volume  ---

    PV  Name                             /dev/sda5

    VG  Name

    PV  Size                            101.94  MB Allocatable      NO

    PE Size   (KByte)           0

    Total PE                            0

    Free  PE                           0

    Allocated PE                    0

    PV  UUID                               hDtAhK-adlx-5Ex0-ogc1-Alvm-H274-a8u7c2

    "/dev/sda6" is  a  new physical volume  of  "101.94 MB"

    --- NEW   Physical volume  ---

    PV  Name                             /dev/sda6

    VG  Name

    PV  Size                            101.94  MB Allocatable      NO

    PE Size   (KByte)           0

    Total PE                            0

    Free  PE                           0

    Allocated PE                    0

    PV  UUID                               Sy1wPG-XnjQ-eYD5-91vj-Ug5b-EGgU-i30lCS

    四.  创建 VG

    1.相关命令

    vgcreate       创建 VG

    vgscan          扫描 VG

    vgdispaly

    vgextend

    vgreduce

    vgchange

    vgremove

    2.创建针对逻辑卷(LV)的卷组(VG)

    [root@hejianlai]#  vgcreate vg0  /dev/sda5 /dev/sda6

    Volume group  "vg0"  successfully created

    [root@hejianlai]#  vgscan
    Reading  all  physical volumes.     
    This  may take a  while... Found volume  group  "vg0"  using metadata type   lvm2
    Device  '/dev/sda6' has  been  left open. Device  '/dev/sda5' has  been  left open.
    [root@hejianlai]# vgdisplay
    --- Volume group  ---
    VG  Name                               vg0
    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                             200.00  MB
    PE Size                              4.00  MB /*分配的块的大小默认为 4M*/ Total PE 50
    Alloc   PE / Size          0  / 0
    Free    PE / Size             50  / 200.00  MB
    VG  UUID                              iE7uoy-boR3-N6Cv-91Gf-G5vE-ftD6-gSGVlB
    

    3.删除与添加卷组中的逻辑卷

    [root@hejianlai]# vgreduce vg0  /dev/sda5

    Removed "/dev/sda5" from  volume  group  "vg0"

    [root@hejianlai]# vgextend vg0  /dev/sda5

    Volume group  "vg0"  successfully  extended

    4.  PE 和 LV  大小的关系

    In  order to limit the   Linux  kernel memory usage, there is a  limit of

    65,536  physical extents (PE)  per  logical  volume  (LV).  Hence,  the   LVM PE size will directly determine the   maximum  size of  a  logical volume (LV)!  For  example, 4MB  PE size (the default PE size) will limit singl e  logical volume  (LV)  to 256GB, 16MB  PE size will limit single LV  to grow beyond  1TB, etc.

    Beside   the   PE size, the   maximum  size of  single LV  is also limited by

    CPU  architecture and  Linux  kernel version:

    Linux  kernel version 2.4.x limit  the   maximum   LV  size to 2TB.

    Some older Linux  kernel prior to 2.4.x,  the   maximum   LV  size is limite d  to 1TB (caused by  the   integer  signedness problems   in the   block   laye r).

    The combination of  32-bit  CPU  and  Linux  kernel version 2.6.x,  the   lim it of  logical volume  size is maximized  at 16TB.

    For  Linux  kernel 2.6.x  running on  64-bit  CPU, the   maximum   LV  size is

    8EB (extremely terrible big  storage for this time  being!)

    这段问题里有如下 2 个重点信息:

    1. PE 的大小决定 LV  的容量,默认 4M  的 PE 最大支持的 256G 的 LV。  16M 的

    PE 最大支持 1TB 的 LV。

    2. 影响 LV  容量的还有 CPU  架构。  对于 32bit 下,Linux  内核为 2.6.X  的支 持 16TB,64bit  下,Linux  内核为 2.6.x 的支持 8EB。

    所以如果要想支持大量的 LV。  那么对在创建 LV  的时候,还需要指定 PE 的大

    小。  这个我们在 vgcreate 命令中加一个-s  参数即可:

    [root@hejianlai]# vgremove  vg0

    Volume group  "vg0"  successfully removed

    [root@hejianlai]#  vgcreate -s 16MB  vg0  /dev/sda5  /dev/sda6

    Volume group  "vg0"  successfully created

    [root@hejianlai]#  vgscan

    Reading  all  physical volumes. This  may take a  while... Found volume  group  "vg0"  using metadata type   lvm2

    Device  '/dev/sda6' has  been  left open. Device  '/dev/sda5' has  been  left open.

    [root@hejianlai]# vgdisplay

    --- Volume group  ---

    VG  Name                               vg0

    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                             192.00  MB

    PE Size                              16.00  MB - 这里我们看到 PE 改成 16MB 了。

    Total PE                            12

    Alloc   PE / Size          0  / 0

    Free    PE / Size             12  / 192.00  MB

    VG  UUID                              A9w4ho-3Fdo-ELYl-VRqY-yhaZ-61rt-68sXsi

    五、创建逻辑卷 LV

    1.相关命令

    lvcreate

    lvscan

    lvdisplay

    lvextend

    lvreduce

    lvremove

    lvresize

    2.创建逻辑卷 LV

    [root@hejianlai]# lvcreate -L  184M  -n  oralv vg0

    Rounding  up  size to full  physical extent 192.00  MB Logical volume  "  oralv" created

    [root@hejianlai]#  lvscan

    ACTIVE                         '/dev/vg0/ oralv'  [192.00 MB]  inherit

    [root@hejianlai]#  lvdisplay

    ---  Logical volume  ---

    LV  Name                                 /dev/vg0/oralv

    VG  Name                                 vg0

    LV  UUID                                PAyJph-B9K2-P15u-Zkl3-pBxs-e0TG-DCuRzq

    LV  Write  Access            read/write

    LV  Status                          available

    #  open                                 0

    LV  Size                              192.00  MB

    Current LE                          12    -- 注  12*16MB=192MB,和之前的 PE 是 1:1

    Segments                              2

    Allocation                       inherit

    Read ahead  sectors       auto

    - currently set to         256

    Block  device                    253:0

    六、挂载逻辑卷 LV

    1. lv 的格式化

    [root@hejianlai vg0]#  mkfs.ext3 /dev/vg0/oralv

    mke2fs  1.40.8 (13-Mar-2008)

    Filesystem label= OS  type:  Linux

    Block  size=1024 (log=0) Fragment  size=1024 (log=0)

    49152  inodes, 196608  blocks

    9830  blocks (5.00%)  reserved for the   super user

    First  data block=1

    Maximum  filesystem  blocks=67371008

    24  block   groups

    8192  blocks per  group, 8192  fragments per  group

    2048  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   23  mounts  or

    180  days,  whichever comes first.      Use tune2fs -c or  -i to override. [root@hejianlai vg0]#  mkdir  -p  /u01/backup

    [root@hejianlai vg0]#  mount  /dev/vg0/oralv /u01/backup

    [root@hejianlai vg0]#  df  -T

    Filesystem        Type      1K-blocks            Used Available Use%  Mounted on

    /dev/sda1           ext3          1984016        651076      1230528    35% 

    tmpfs                tmpfs            349016                  0        349016      0%  /dev/shm

    /dev/mapper/vg0-oralv     ext3            190403            5664        174909      4%  /u01/backup

    挂载成功之后,就可以使用了。

    七、LVM 的容量调整

    LVM 的容量调整可以在多个环节进行调整,比如:可以在物理卷上,VG 上,

    以及 LV 上,都可以进行容量的扩展,这也是 LVM 它的一个优势所在。

    1.添加物理卷(PV)

    首先应卸载在使用过程中的 LV,然后必须保证该磁盘的类型是 lvm 类型,才

    能添加进来。

    [root@hejianlai vg0]#  umount /dev/mapper/vg0-oralv

    [root@hejianlai vg0]#  pvcreate /dev/sda7

    Physical volume  "/dev/sda7" successfully created

    [root@hejianlai vg0]#  pvscan

    PV

    /dev/sda5

    VG

    vg0

    lvm2

    [96.00 MB   / 0

    free]

    PV

    /dev/sda6

    VG

    vg0

    lvm2

    [96.00 MB   / 0

    free]

    PV

    /dev/sda7

     

     

    lvm2

    [101.94 MB]

     

    Total: 3  [293.94 MB]  / in use: 2  [192.00 MB]  / in no  VG: 1  [101.94MB]

    2.添加 VG 的容量

    把上面新添加的 LVM 磁盘加入到 vg0 卷组中。

    [root@hejianlai vg0]#  vgextend vg0  /dev/sda7

    Volume group  "vg0"  successfully  extended

    [root@hejianlai vg0]#  vgdisplay

    --- Volume group  ---

    VG  Name                               vg0

    System  ID

    Format                                lvm2

    Metadata   Areas             3

    Metadata   Sequence  No    3

    VG  Access                          read/write VG  Status resizable MAX  LV                                 0

    Cur  LV                              1

    Open LV                              0

    Max  PV                               0

    Cur  PV                              3

    Act  PV                              3

    VG  Size                             288.00  MB PE Size     16.00  MB Total PE                       18

    Alloc   PE / Size          12  / 192.00  MB Free       PE / Size             6  / 96.00  MB

    VG  UUID                              A9w4ho-3Fdo-ELYl-VRqY-yhaZ-61rt-68sXsi

    3.添加 LV 的容量

    把新加入 LVM 磁盘的容量加入 LV 中。

    [root@hejianlai vg0]#  lvextend -L  +90M  /dev/vg0/oralv

    Rounding  up  size to full  physical extent 96.00  MB Extending logical  volume  oralv to 288.00  MB Logical volume  oralv successfully resized

    这里要注意,如果是 lvextend -L  90MB  /dev/vg0/oralv,  则表示增加到 90m。

    如果有加号,就表示增加 90M。

    [root@hejianlai vg0]#  lvscan

    ACTIVE                         '/dev/vg0/oralv'  [288.00 MB]  inherit

    [root@hejianlai vg0]#  resize2fs -f /dev/vg0/oralv

    resize2fs 1.40.8 (13-Mar-2008)

    Resizing the   filesystem on  /dev/vg0/oralv to 294912  (1k) blocks. The filesystem on  /dev/vg0/oralv is  now 294912  blocks long.

    做这一步是由于新扩展的 90M 空间未格式化,不能使用。需在保证原有数据不被破坏情况下在线格式化。

    如果不做这一步,  LV 的容量没有真正的加入进 LV 卷中,因为相关信息写 入到了磁盘超级块中。

    4.挂载使用

    [root@hejianlai  vg0]#  mount /dev/vg0/oralv  /u01/backup

    [root@hejianlai vg0]#  df  -T

    Filesystem        Type      1K-blocks            Used Available Use%  Mounted on

    /dev/sda1           ext3          1984016        651128      1230476    35%  /

    tmpfs                tmpfs            349016                  0        349016      0%  /dev/shm

    /dev/mapper/vg0-oralv  ext3            285599            6168        267635      3%  /u01/backup

    八、LVM 的卸载方法

    LVM 卸载的方法与分区的删除方法类似,最后创建的最先删除。顺序如下:

    先删除 LV

    再删除 VG

    最后 PV

    以前的 LVM 的分区应用 fdisk 转换成其它类型的文件系统,当普通分区使用。

    九、LVM 的卸载过程

    1.umount 取消挂载

    [root@hejianlai vg0]#  umount /dev/vg0/oralv

    [root@hejianlai vg0]#  df  -T

    Filesystem        Type      1K-blocks            Used Available Use%  Mounted on

    /dev/sda1           ext3          1984016        651148      1230456    35%  /

    tmpfs                tmpfs            349016                  0        349016      0%  /dev/shm

    2.删除 LV 逻辑卷

    [root@hejianlai vg0]#  lvremove  /dev/vg0/oralv

    Do  you  really want  to remove  active logical volume  "oralv"? [y/n]:  y

    Logical volume  "oralv" successfully removed

    3.删除 VG 卷组

    [root@hejianlai vg0]#  vgchange  -a n  vg0

    0  logical  volume(s) in volume  group  "vg0"  now active

    [root@hejianlai vg0]#  vgremove  vg0

    Volume group  "vg0"  successfully removed 对 vgchange  命令的说明,先看帮助:

    [root@hejianlai vg0]#  vgchange  --help

    vgchange: Change volume  group  attributes vgchange

    [-A|--autobackup  {y|n}]

    [--alloc AllocationPolicy] [-P|--partial]

    [-d|--debug] [-h|--help]

    [--ignorelockingfailure] [--ignoremonitoring]

    [--monitor  {y|n}] [-t|--test]

    [-u|--uuid]

    [-v|--verbose] [--version]

    {-a|--available  [e|l]{y|n}  |    - 这个是我们使用的参数,表示状态可用不可用,有两个值:y 和 n。

    -c|--clustered  {y|n} |

    -x|--resizeable  {y|n} |

    -l|--logicalvolume  MaxLogicalVolumes  |

    -p|--maxphysicalvolumes  MaxPhysicalVolumes  |

    -s|--physicalextentsize  PhysicalExtentSize[kKmMgGtTpPeE] |

    --addtag  Tag |

    --deltag Tag} [VolumeGroupName...]

    4.删除 PV

    [root@hejianlai vg0]#  pvscan

    PV  /dev/sda5                                     lvm2 [101.94 MB]

    PV  /dev/sda6                                     lvm2 [101.94 MB]

    PV  /dev/sda7                                     lvm2 [101.94 MB]

    Total: 3  [305.83 MB]  / in use: 0  [0      ] / in no  VG: 3  [305.83 MB]

    [root@hejianlai vg0]#  pvremove  /dev/sda5 /dev/sda6 /dev/sda7

    Labels on  physical volume  "/dev/sda5" successfully wiped

    Labels on  physical volume  "/dev/sda6" successfully wiped

    Labels on  physical volume  "/dev/sda7" successfully wiped

    十.  命令说明

    pvcreate (创建物理卷)

    pvdisplay (显示物理卷信息)

    pvscan  (扫描物理卷)

    pvmove (转移物理卷资料)

    pvmove /dev/hda1 /dev/hda2 (转移/dev/hda1 资料到/dev/hda2)

    pvmove /dev/hda1 (转到/dev/hda1 资料到别的物理卷)

    pvremove  (删除物理卷)

    vgcreate (创建卷组)

    vgdisplay (显示卷组信息)

    vgscan  (扫描卷组)

    vgextend (扩展卷组)      

    vgextend vg0  /dev/hda2    (把物理卷/dev/hda2 加到vg0 卷组中)

    vgreduce (删除卷组中的物理卷) 

    vgreduce vg0  /dev/hda2 (把物理卷/dev/h da2 从卷组 vg0 中删除)

    vgchange  (激活卷组) 

    vgchange  -a y  vg0  (激活卷组 vg0)

    vgchange  -a n  vg0  (相反)

    vgremove  (删除卷组)  

    vgremove  vg0  (删除卷组 vg0)

    lvcreate (创建逻辑卷)

    lvdisplay (显示逻辑卷信息)

    lvscan (扫描逻辑卷)

    lvextend (扩展逻辑卷) 

    lvextend -L  +5G /dev/vg0/data  (扩展逻辑卷/dev/vg0/data 5 个 G)

    mke2fs(查看 LV 的 block  数),如:mke2fs  -n  /dev/vg0/oralv.     注意,这里一定要加-n。  这里只是查看 block  数,如果不加,就把分区格式化了。 从这个命 令可以得出 Block  size 和 blocks,这两个数的乘积就是分区大小。

    a)resize2fs(调整文件系统大小) resize2fs -f  /dev/vg0/oralv 剩余的 block 数

    b)lvreduce (调整 LV 大小)     lvreduce -L-50M  /dev/vg0/oralv,调整 lv 分区 大小,减小 50M

    lvremove  (删除逻辑卷)

    如:  lvremove /dev/vg_data/lv_data1

    十一.  LVM 修复方法

    现在很多人对逻辑卷不是很细心,在做逻辑卷时可能会导致系统崩溃。在此我向各位提供自己修复逻辑卷的一点心得。 首先说下因为逻辑卷而造成系统瘫痪的的原因。在做逻辑卷的时候造成文件系统和逻辑卷的大小不统一而造成的系统瘫痪。 其实修复很简单,但你的系统瘫痪后会进入 Linux 的修复系统。由于进入修复模式后所有的文件和目录都为只读,需要重新挂载成可读可写。

    #mount -o  remount,rw /

    进入/etc/fstab 这个文件将需要修复的逻辑卷注释掉。进入系统进行修改。 或者直接进行修改:

    #lvm  lvexdend  -L  200M  /dev/vgname/lvname

    #resize2fs  /dev/vgname/lvname  200M

    在增大或缩小 LVM 时要注意顺序,这样可以避免在对逻辑卷进行修复。增大时 先增大 LV,后增大文件系统,缩小时先缩小文件系统,后缩小 LV 大小。

  • 相关阅读:
    Flask的jinja2模板中自定义过滤器的使用
    Django的CBV方式讲解
    【Flask】 Jinja2模板语言
    如何把excel数据导入数据库
    在flask中使用jsonify和json.dumps的区别
    flask request 获取json内容2种方式
    flask装饰器route实现路由功能理解
    ASP.NET—016:ASP.NET中保存文件对话框
    子查询(章节摘要)
    今年股票注定有一波行情
  • 原文地址:https://www.cnblogs.com/Dev0ps/p/9381244.html
Copyright © 2011-2022 走看看