使用LVM方式扩容硬盘空间

Posted zhangyunfei-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用LVM方式扩容硬盘空间相关的知识,希望对你有一定的参考价值。

使用LVM方式扩容硬盘空间

目录

 [显示

环境说明

技术分享图片

案例环境说明:该解码引擎服务器已经采用LVM的方式进行了部署,原始的解码引擎遇到/data硬盘空间不足的问题,需要通过LVM方式再次对/data扩展空间。如图所示,在我们的解码服务器上新增了一块硬盘,空间大小为5G。

[[email protected] ~]# fdisk -l 

显示内容如图所示,新增硬盘为/dev/sdb。

技术分享图片

第一步:新建一个分区

[[email protected] ~]# 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): n                                            //添加新分区

Command action

e   extended (说明:扩展分区)

p   primary partition (1-4) (说明:主分区)

e                                                            //增加扩展分区

Partition number (1-4): 1

First cylinder (1-652, default 1)::<<ENTER

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): <<ENTER   //也可以指定大小+5G,如果直接输入回车则使用所有空间

Using default value 652

Command (m for help): p                                  //查看分区详情,可以看到新建的扩展分区

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 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: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         652     5237158+   5  Extended

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

l

First cylinder (1-652, default 1): <<ENTER

Using default value 1

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

Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 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: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1           5       40131    5  Extended

/dev/sdb5               1           5       40099+  83  Linux

Command (m for help): t             //修改分区格式,下面选的5是上面的sdb5,默认格式是Linux

Partition number (1-5): 5

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

(说明:8e为Linux LVM格式的编码)

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

Command (m for help): p                                  //查看分区情况

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 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: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1           5       40131    5  Extended

/dev/sdb5               1           5       40099+  8e  Linux LVM

Command (m for help): w                           //  保存分区

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

1、 创建物理卷pv。

[[email protected] ~]# pvcreate /dev/sdb5         //   创建pv    

Physical volume "/dev/sdb5" successfully created

[[email protected] ~]# pvdisplay /dev/sdb5        //   查看pv 

-- "/dev/sdb5" is a new physical volume of "5.00 GiB"

--- NEW Physical volume ---

PV Name               /dev/sdb5

VG Name               

PV Size               5.00 GiB

Allocatable           NO

PE Size               0   

Total PE              0

Free PE               0

Allocated PE          0

PV UUID               Hjoga4-EXmd-G53T-vAvQ-kDtA-60e2-vEHzBY

2、 查看已有的VG配置详情

[[email protected] ~]# vgdisplay 

--- Volume group ---

  VG Name               vg_ezsonar

System ID             

Format                lvm2

Metadata Areas        1

Metadata Sequence No  4

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                3

Open LV               3

Max PV                0

Cur PV                1

Act PV                1

VG Size               31.80 GiB

PE Size               4.00 MiB

Total PE              8141

Alloc PE / Size       8141 / 31.80 GiB

Free  PE / Size       0 / 0   

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

3、 增减逻辑卷

[[email protected] ~]# vgextend vg_ezsonar /dev/sdb5      // 扩展vg  将pv加入到vg_ezsonar 

Volume group "vg_ezsonar" successfully extended

[[email protected] ~]# vgdisplay                //查看VG情况

--- Volume group ---

VG Name               vg_ezsonar

System ID             

Format                lvm2

Metadata Areas        2

Metadata Sequence No  5

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                3

Open LV               3

Max PV                0

Cur PV                2

Act PV                2

VG Size               71.80 GiB

PE Size               4.00 MiB

Total PE              18380

Alloc PE / Size       8141 / 31.80 GiB

Free  PE / Size       1023 / 5.00 GiB                        //剩余分配空间

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

4、 确认需要扩容的分区,红色/data分区需要扩容

[[email protected] ~]# df -h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

14G  3.7G  9.2G  29% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

11G  2.2G  8.0G  22% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

5、 进行扩容

[[email protected] ~]# lvextend -L +5G /dev/mapper/vg_ezsonar-LogVol01      //扩展5G到lv:logVol01  

Extending logical volume LogVol01 to 15.18 GiB

Logical volume LogVol01 successfully resized

[[email protected]r ~]#
[root@EZSonar ~]# resize2fs /dev/mapper/vg_ezsonar-LogVol01   //执行该重设大小 
[[email protected] ~]# mount             //查看挂载情况,原有分区采用什么文件格式 

/dev/mapper/vg_ezsonar-LogVol02 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/mapper/vg_ezsonar-LogVol01 on /data type xfs (rw)

/dev/mapper/vg_ezsonar-LogVol00 on /var type xfs (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

[[email protected] ~]# xfs_growfs /dev/mapper/vg_ezsonar-LogVol01      //xfs文件格式采用xfs_growfs 

meta-data=/dev/mapper/vg_ezsonar-LogVol01 isize=256    agcount=4, agsize=666880 blks

=                       sectsz=512   attr=2, projid32bit=0

data     =                       bsize=4096   blocks=2667520, imaxpct=25

=                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0

log      =internal               bsize=4096   blocks=2560, version=2

=                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

data blocks changed from 2667520 to 3978240

5、查看扩容结果

[[email protected] ~]# df –h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

14G  3.7G  9.2G  29% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

                       16G  2.2G   13G  15% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

附录,对根分区进行扩容

首先确认VG有剩余空间可用

[[email protected] ~]# vgdisplay  

--- Volume group ---

VG Name               vg_ezsonar

System ID             

Format                lvm2

Metadata Areas        2

Metadata Sequence No  6

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                3

Open LV               3

Max PV                0

Cur PV                2

Act PV                2

VG Size               41.79 GiB

PE Size               4.00 MiB

Total PE              10699

Alloc PE / Size       9421 / 36.80 GiB

 Free  PE / Size       1278 / 4.99 GiB //有5G剩余空间可用

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

[[email protected] ~]# mount             //查看挂载情况,原有分区采用什么文件格式 

/dev/mapper/vg_ezsonar-LogVol02 on / type ext4 (rw)

根分区采用ext4格式

[[email protected] ~]# lvextend -L +4.5G /dev/mapper/vg_ezsonar-LogVol02 /扩展4.5G到lv:logVol02  

Extending logical volume LogVol02 to 18.12 GiB

Logical volume LogVol02 successfully resized

[[email protected] ~]# resize2fs /dev/mapper/vg_ezsonar-LogVol02              

//ext4 文件格式采用resize2f

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/mapper/vg_ezsonar-LogVol02 is mounted on /; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 3

Performing an on-line resize of /dev/mapper/vg_ezsonar-LogVol02 to 8814592 (4k) blocks.

The filesystem on /dev/mapper/vg_ezsonar-LogVol02 is now 8814592 blocks long.

[[email protected] ~]# df -h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

                       18G  3.7G   14G  22% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

16G  2.2G   13G  15% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

附录:LVM技术优势说明

优点

比起正常的硬盘分区管理,LVM更富于弹性:

§ 使用卷组(VG),使众多硬盘空间看起来像一个大硬盘。

§ 使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区。

§ 可以创建小的逻辑卷(LV),在空间不足时再动态调整它的大小。

§ 在调整逻辑卷(LV)大小时可以不用考虑逻辑卷在硬盘上的位置,不用担心没有可用的连续空间。It does not depend on the position of the LV within VG, there is no need to ensure surrounding available space.

§ 可以在线(online)对逻辑卷(LV)和卷组(VG)进行创建、删除、调整大小等操作。LVM上的文件系统也需要重新调整大小,某些文件系统也支持这样的在线操作。

§ 无需重新启动服务,就可以将服务中用到的逻辑卷(LV)在线(online)/动态(live)迁移至别的硬盘上。

§ 允许创建快照,可以保存文件系统的备份,同时使服务的下线时间(downtime)降低到最小。

这些优点使得LVM对服务器的管理非常有用,对于桌面系统管理的帮助则没有那么显著,你需要根据实际情况进行取舍。

以上是关于使用LVM方式扩容硬盘空间的主要内容,如果未能解决你的问题,请参考以下文章

给linux虚拟机硬盘扩容(LVM分区)

CentOS 6.9之LVM创建,扩容

CentOS 6.9之LVM创建,扩容等常用操作

Centos7下,LVM根分区扩容

linux CentOS 7 中LVM讲解配置实例及磁盘扩容等

linux 根分区LVM扩容