Centos 6.5 x64 lvm磁盘扩容

 

系统环境:

Centos 6.5 x64最小化安装

我是用EXSI做测试的。

 

LVM简介

LVM是逻辑盘卷管理(LogicalVolume Manager)的简称,它是一种磁盘管理工具,最主要的功能就是可以随时在线调整分区的大小,解决了安装Linux系统时需要评估分区大小的烦恼。

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)

PEphysical extent

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

LElogical extent

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

 

如图所示LVM模型:

spacer.gif

如下图所示PVVGLV三者关系

spacer.gif

查看分区情况

[root@localhost ~]# df -hT

Filesystem                   Type   Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_rootext4    19G  5.0G  13G  29% /

tmpfs                        tmpfs 246M     0  246M  0% /dev/shm

/dev/sda1                    ext4   485M  33M  427M   8% /boot

 

默认已经划分好了lvm分区。

 

下面介绍如何扩容:

增加一块硬盘,这里我是增加一块10G的硬盘

spacer.gif

需要系统重启一遍,才能识别到新的硬盘。

查看硬盘,蓝色部分是新的硬盘,大小是10G

[root@localhost ~]# fdisk –l

Disk /dev/sda: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x0006f76c

 

  Device Boot      Start         End      Blocks  Id  System

/dev/sda1  *           1          64      512000  83  Linux

Partition 1 does not end on cylinderboundary.

/dev/sda2              64        2611   20458496   8e  Linux LVM

 

Disk /dev/sdb: 10.7GB, 10737418240 bytes

255 heads, 63sectors/track, 1305 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x00000000

 

 

Disk /dev/mapper/VolGroup-lv_root: 19.9 GB,19906166784 bytes

255 heads, 63 sectors/track, 2420 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

 

 

Disk /dev/mapper/VolGroup-lv_swap: 1040 MB,1040187392 bytes

255 heads, 63 sectors/track, 126 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

 

需要先对新硬盘分区

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

 

Command (m for help): n

Command action

  e   extended

  p   primary partition (1-4)

p

Partition number (1-4): 1

下面几个直接回车就可以了

First cylinder (1-1305, default 1):

Using default value 1

Last cylinder, +cylinders or +size{K,M,G}(1-1305, default 1305):

Using default value 1305

设置分区类型

Command (m for help): t

Selected partition 1

一定要指定分区的格式为8e,这是LVM的分区格式

Hex code (type L to list codes): 8e

Changed system type of partition 1 to 8e(Linux LVM)

保存分区

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

查看磁盘,发现有一个分区了

[root@localhost ~]# fdisk -l

 

Disk /dev/sdb: 10.7GB, 10737418240 bytes

255 heads, 63sectors/track, 1305 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x00aa5f36

 

   Device Boot      Start         End      Blocks  Id  System

/dev/sdb1               1        1305   10482381   8e  Linux LVM

 

格式化分区

[root@localhost ~]# mkfs.ext4 /dev/sdb1

 

创建PV

[root@localhost ~]# pvcreate /dev/sdb1

 

查看当前已经存在的VG信息

[root@localhost ~]# vgdisplay

提示

  Physical volume "/dev/sdb1" successfully created

如果没有就需要自己安装下LVM这个组件了

安装lvm

[root@localhost ~]# yum install -y lvm2

再次查看,居然有了

[root@localhost ~]# vgdisplay

 

--- Volume group ---

  VGName               VolGroup

  System ID

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  3

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               2

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               19.51 GiB

  PE Size               4.00 MiB

  Total PE              4994

  Alloc PE / Size       4994 / 19.51 GiB

  Free PE / Size       0 / 0

  VG UUID              llC3XA-5InX-nVZ2-os5P-hg3n-21l5-TU5p63

 

注意上面,默认的VG名为VolGroup

/dev/sdb1加入到VolGroup

[root@localhost ~]# vgextend VolGroup /dev/sdb1

  Volume group "VolGroup"successfully extended

 

增加10G空间

 [root@localhost~]# lvextend -L +10G /dev/mapper/VolGroup-lv_root

  Insufficient free space: 2560 extents needed,but only 2558 available

提示错误,没有那么多的剩余空间

增加9.99G空间试试,发现成功了。

[root@localhost ~]# lvextend -L +9.99G /dev/mapper/VolGroup-lv_root

  Rounding size to boundary between physicalextents: 9.99 GiB

  Size of logical volume VolGroup/lv_rootchanged from 18.54 GiB (4746 extents)                                                                                                                     to 28.53 GiB (7304 extents).

  Logical volume lv_root successfully resized

执行该重设大小,使用如下命令:

resize2fs/dev/mapper/VolGroup-lv_root

查看磁盘分区,发现磁盘增加了9G空间

[root@localhost ~]# df -hT

Filesystem                   Type   Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root ext4    29G 5.0G   22G  19% /

tmpfs                        tmpfs 246M     0  246M  0% /dev/shm

/dev/sda1                    ext4   485M  33M  427M   8% /boot