CentOS 5.9 LVM的简单使用

当初学习鸟哥的时候,就想过lvm这东西有用吗?现在磁盘那么大,怎么会需要动态调整?直到有一天,你发现原来并不是所有的服务器都是你规划的,所有的磁盘并没有想象的那么大,于是你默默的重新拿出鸟哥,敲敲打打,把硬盘变大。

一:使用LVM,系统自带,无需安装

1 需求:系统一直用/dev/sdc 300G进行挂载,作为数据盘,另有两块闲置的 300G /dev/sdb 和 300G /dev/sdd磁盘;最近/dev/sdc这块盘突然用完了,就决定把/dev/sdb 和/dev/sdd 用lvm拓展成一个大盘了先用着了,操作如下:

a 使用fdisk对/dev/sdb 和 /dev/sdd进行分区,修改systemID为8e,然后partprobe通知系统,操作后如下图所示

Disk /dev/sdb: 298.9 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       36351   291989376   8e  Linux LVM

Disk /dev/sdd: 298.9 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1       36351   291989376   8e  Linux LVM

b 创建PV
pvcreate /dev/sdb1 /dev/sdd1

c 创建VG
vgcreate -s 16M mpkvg /dev/sdb1 /dev/sdd1

-s: 指定PE的大小,默认4M,鸟哥上指定的是16M,这里一时手快,就业指定成16M了
mpkvg是vg的名称

d 创建LV
lvcreate -l 35642 -n mpklv mpkvg

-l 后面接的是PE的个数
-n 后面接的是LV的名称

e 格式化挂载,使用
mkfs -t ext3 /dev/mpkvg/mpklv
mkdir -p /data/mumayi/mpk
mount /dev/mpkvg/mpklv /data/mumayi/mpk

如下图是我们机器挂载信息:
[root@budong ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G  4.8G   14G  27% /
/dev/sda6             9.5G  151M  8.9G   2% /tmp
/dev/sda3              29G  586M   27G   3% /var
/dev/sda2              29G   14G   13G  52% /usr
tmpfs                  16G     0   16G   0% /dev/shm
/dev/sda7             170G  1.1G  160G   1% /data/logs
/dev/mapper/mpkvg-mpklv
                      549G  261G  261G  51% /data/mumayi/mpk

2 增大 LVM ,刚才还有一块 /dev/sdc 没有使用上,现在把他也加入新创建的LV中

a fdisk对/dev/sdc进行分区,修改systemID为8e,然后partprobe通知系统,结果如下图所示

Disk /dev/sdc: 298.9 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1       36351   291989376   8e  Linux LVM

b 利用 pvcreate 建置 PV
pvcreate /dev/sdc1

c 利用 vgextend 将 PV 加入我们的 mpkvg
vgextend mpkvg /dev/sdc1

d 利用 lvresize 将新加入的 PV 內的 PE 加入 mpklv 中
lvresize -l +17821 /dev/mpkvg/mpklv

这里增加LV的大小时,可以先用vgdisplay看下有多少Free  PE,再根据需要增加,如下是我的vgdisplay显示
[root@budong ~]# vgdisplay 
  --- Volume group ---
  VG Name               mpkvg
  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               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               835.36 GB
  PE Size               16.00 MB
  Total PE              53463
  Alloc PE / Size       35642 / 556.91 GB
  Free  PE / Size       17821 / 278.45 GB
  VG UUID               7hL3Ul-2crx-iRN6-Kfss-pk3l-iSaj-Nb683C

执行 lvresize -l +17821 /dev/mpkvg/mpklv 后
[root@budong ~]# vgdisplay 
  --- Volume group ---
  VG Name               mpkvg
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                
  VG Size               835.36 GB
  PE Size               16.00 MB
  Total PE              53463
  Alloc PE / Size       53463 / 835.36 GB
  Free  PE / Size       0 / 0   
  VG UUID               7hL3Ul-2crx-iRN6-Kfss-pk3l-iSaj-Nb683C

e 通过 resize2fs 将档案系统的容量增加,这一步执行时间有些长,可以开个screen把任务放到后台执行
[root@budong ~]#resize2fs /dev/mpkvg/mpklv 
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mpkvg/mpklv is mounted on /data/mumayi/mpk; on-line resizing required
Performing an on-line resize of /dev/mpkvg/mpklv to 218984448 (4k) blocks.
The filesystem on /dev/mpkvg/mpklv is now 218984448 blocks long.

[root@budong ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G  4.8G   14G  27% /
/dev/sda6             9.5G  151M  8.9G   2% /tmp
/dev/sda3              29G  586M   27G   3% /var
/dev/sda2              29G   14G   13G  52% /usr
tmpfs                  16G     0   16G   0% /dev/shm
/dev/sda7             170G  1.1G  160G   1% /data/logs
/dev/mapper/mpkvg-mpklv
                      823G  261G  521G  34% /data/mumayi/mpk
3 这是个坑,网上说一个VG最多只能有65534个PE,这个跟自己LVM版本和系统内核版本都有关系,部分说明如下
man vgcreate
部分结果如下,自己看下官方说明:
 -s, --physicalextentsize PhysicalExtentSize[bBsSkKmMgGtTpPeE]
              Sets  the  physical  extent size on physical volumes of this volume group.  A size suffix (k for
              kilobytes up to t for terabytes) is optional, megabytes is the default if no suffix is  present.
              The default is 4 MB and it must be at least 1 KB and a power of 2.

              Once  this  value has been set, it is difficult to change it without recreating the volume group
              which would involve backing up and restoring data  on  any  logical  volumes.   However,  if  no
              extents need moving for the new value to apply, it can be altered using vgchange -s.

              If  the  volume  group  metadata uses lvm1 format, extents can vary in size from 8KB to 16GB and
              there is a limit of 65534 extents in each logical volume.  The default of 4 MB leads to a  maxi-
              mum logical volume size of around 256GB.

              If  the  volume  group  metadata  uses lvm2 format those restrictions do not apply, but having a
              large number of extents will slow down the tools but have no impact on I/O  performance  to  the
              logical volume.  The smallest PE is 1KB.

              The 2.4 kernel has a limitation of 2TB per block device.


同时贴一个我们线上正在使用LVM的案例:
[root@budong ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_mmy
  System ID             
  Format                lvm2
  Metadata Areas        7
  Metadata Sequence No  14
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                7
  Act PV                7
  VG Size               7.18 TB
  PE Size               4.00 MB
  Total PE              1881830
  Alloc PE / Size       1881830 / 7.18 TB
  Free  PE / Size       0 / 0   
  VG UUID               YnfBAI-vcKc-GxNc-HcX3-J6j2-zg9a-CrcZ4a


可以看到这个已经使用LVM拓展到7.18 T了,远大于网上说的一个LV最大256G的限制,做技术的一定要多思考,不能被一些过时的资料蒙蔽了眼睛。

二:是时候结束了

自言自语:

如果要求数据的可靠性还是raid 1或5吧,LVM可以简单的理解成raid 0(说的不准确,raid 0不能动态调节大小),就是多了动态调节大小的功能;另外LVM还被很多人来做数据库Mysql的快照备份,当然这是后话,有时间再说了。

参考资料:

鸟哥:http://linux.vbird.org/linux_basic/0420quota.php

羽飞博客:http://www.opsers.org/base/one-day-a-little-learning-linux-logical-volume-manager-lvm-on-the-rhel6.html