Centos7下LVM对文件系统进行在线扩容

@lianst  August 29, 2017

今天在测试一台vps,结果他家的模板系统分区太不好了(就不喷了),本来想让人家客服看看处理下,结果人家直接来了一句自己分。所以才有了这篇笔记,顺便也可以复习下LVM。

硬盘总空间是30G的SSD,根分区给了10G,剩下的20G空间挂载到了/home下。

[root@MyCloudServer ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos-root      8.5G  930M  7.6G  11% /    # 根分区给了10G
devtmpfs                     873M     0  873M   0% /dev
tmpfs                        893M     0  893M   0% /dev/shm
tmpfs                        893M  8.2M  885M   1% /run
tmpfs                        893M     0  893M   0% /sys/fs/cgroup
/dev/xvda1                   497M  161M  337M  33% /boot
/dev/mapper/Xvdbgroup-xvdb1   20G   45M   19G   1% /home  # 另一个分区20G挂载到了/home下

我的需求是要把所有空间都给根目录下,毕竟才30G空间,还分啥分。首先来查看一下当前的LVM划分状态:

[root@MyCloudServer ~]# pvscan 
  PV /dev/xvda2   VG centos      lvm2 [9.51 GiB / 40.00 MiB free]
  PV /dev/xvdb1   VG Xvdbgroup   lvm2 [20.00 GiB / 96.00 MiB free]
  Total: 2 [29.50 GiB] / in use: 2 [29.50 GiB] / in no VG: 0 [0   ]

[root@MyCloudServer ~]# vgscan 
  Reading all physical volumes.  This may take a while...
  Found volume group "centos" using metadata type lvm2
  Found volume group "Xvdbgroup" using metadata type lvm2
[root@MyCloudServer ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap # LV路径
  LV Name                swap # LV的名字
  VG Name                centos  # VG的名字
  LV UUID                Bs14H4-uzvw-o8NK-FwAH-6vlQ-wOR8-R7Yx0n  # LV的UUID
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-22 11:01:55 +0800
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                jH3frG-mRfk-hf0o-0KS6-rcth-aklH-BAciW4
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-22 11:01:55 +0800
  LV Status              available
  # open                 1
  LV Size                8.47 GiB
  Current LE             2168
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/Xvdbgroup/xvdb1
  LV Name                xvdb1
  VG Name                Xvdbgroup
  LV UUID                4nLoAG-0xFr-1agn-K3D0-lihf-erDn-PNUx8K
  LV Write Access        read/write
  LV Creation host, time MyCloudServer, 2017-08-29 17:10:30 +0800
  LV Status              available
  # open                 1
  LV Size                19.90 GiB
  Current LE             5095
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

从以上查看结果来看,挂了一块10G的硬盘,算是系统盘;又挂了一块20G的硬盘,应该算是数据盘了,一共分了连个PV、VG、LV。现在需要做的就是把20G哪一块给remove掉,并且把它添加到根分区所在的PV里面。

需要注意的是LVM删除的话是反向删除,也就是从LV开始删除,然后删除VG,再删除PV。

   
[root@MyCloudServer ~]# lvremove /dev/Xvdbgroup/xvdb1   # lvremove删除lv,提示卷在使用。原因是没有umount
  Logical volume Xvdbgroup/xvdb1 contains a filesystem in use.
[root@MyCloudServer ~]# umount /home/  # 卸载掉需要删除的lv
[root@MyCloudServer ~]# lvremove /dev/Xvdbgroup/xvdb1  # 删除lv
Do you really want to remove active logical volume xvdb1? [y/n]: y
  Logical volume "xvdb1" successfully removed
[root@MyCloudServer ~]# vgdisplay  # 查看vg
  --- Volume group ---
  VG Name               centos
  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               9.51 GiB
  PE Size               4.00 MiB
  Total PE              2434
  Alloc PE / Size       2424 / 9.47 GiB
  Free  PE / Size       10 / 40.00 MiB
  VG UUID               NnqUfE-nNgq-u0tY-rmV2-J7ku-nn5S-E3l26G
   
  --- Volume group ---
  VG Name               Xvdbgroup
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5119 / 20.00 GiB
  VG UUID               YYmS9Z-XmaO-gRi0-rIUR-e0sU-AenG-KwUSoq
   
[root@MyCloudServer ~]# vgremove Xvdbgroup # 删除vg
  Volume group "Xvdbgroup" successfully removed
[root@MyCloudServer ~]# pvdisplay # 查看pv
  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               centos
  PV Size               9.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2434
  Free PE               10
  Allocated PE          2424
  PV UUID               lFsKe4-CXvT-KHR1-qhrV-ct6O-rvMJ-aDGoRW
   
  "/dev/xvdb1" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/xvdb1
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9WiyQq-36PK-sG2m-GrUc-AuFb-0BK3-ivM11V
   
[root@MyCloudServer ~]# pvremove /dev/xvdb1  # 删除pv
  Labels on physical volume "/dev/xvdb1" successfully wiped

从lv删除到pv完成后,正式开始扩容操作。首先使用partprobe重读一下分区表。


[root@MyCloudServer ~]# partprobe

使用fdisk 对哪一块20G的盘进行分区并格式化。

 
[root@MyCloudServer ~]# fdisk /dev/xvdb # 分区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p # 打印当前分区

Disk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x25dec84b

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1            2048    41943039    20970496   83  Linux

Command (m for help): d  # 删除第一分区
Selected partition 1
Partition 1 is deleted

Command (m for help): n  # 新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p # 主分区
Partition number (1-4, default 1):   # 直接回车,默认是1
First sector (2048-41943039, default 2048):  #直接回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): t # 设置分区类型
Selected partition 1
Hex code (type L to list all codes): 8e # 8e就是LVM
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w  # 将更改写入硬盘
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@MyCloudServer ~]# mkfs.ext4 /dev/xvdb1  # 对刚刚创建的分区进行格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

格式化完成后,我们开始创建LVM。

 
[root@MyCloudServer ~]# pvcreate /dev/xvdb1     # 创建LVM和删除是反向的,即先创建PV
WARNING: ext4 signature detected on /dev/xvdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/xvdb1.
  Physical volume "/dev/xvdb1" successfully created
[root@MyCloudServer ~]# pvscan  # 查看一下,xvdb1已经创建好PV了,但是还没有加入到VG中。
  PV /dev/xvda2   VG centos   lvm2 [9.51 GiB / 40.00 MiB free]
  PV /dev/xvdb1               lvm2 [20.00 GiB]
  Total: 2 [29.51 GiB] / in use: 1 [9.51 GiB] / in no VG: 1 [20.00 GiB]
[root@MyCloudServer ~]# vgextend centos /dev/xvdb1 # 然后来扩展根分区的VG,VG名是centos
  Volume group "centos" successfully extended
[root@MyCloudServer ~]# pvscan  # 添加成功后查看,xvdb1已经添加到VG centos中了。
  PV /dev/xvda2   VG centos   lvm2 [9.51 GiB / 40.00 MiB free]
  PV /dev/xvdb1   VG centos   lvm2 [20.00 GiB / 20.00 GiB free]
  Total: 2 [29.50 GiB] / in use: 2 [29.50 GiB] / in no VG: 0 [0   ]
[root@MyCloudServer ~]# pvs # 查看pv
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/xvda2 centos lvm2 a--   9.51g 40.00m
  /dev/xvdb1 centos lvm2 a--  20.00g 20.00g
[root@MyCloudServer ~]# lvdisplay   # 查看lv
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Bs14H4-uzvw-o8NK-FwAH-6vlQ-wOR8-R7Yx0n
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-22 11:01:55 +0800
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                jH3frG-mRfk-hf0o-0KS6-rcth-aklH-BAciW4
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-22 11:01:55 +0800
  LV Status              available
  # open                 1
  LV Size                8.47 GiB
  Current LE             2168
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@MyCloudServer ~]# vgs # 查看vg,看到vg centos的大小为29.5g,空闲空间为20.04g
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   2   2   0 wz--n- 29.50g 20.04g
[root@MyCloudServer ~]# lvs # 查看lv
  LV   VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 8.47g                                                    
  swap centos -wi-ao---- 1.00g                                                    
[root@MyCloudServer ~]# lvextend -L +20G /dev/centos/root   #-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节;+20G就是增加20G空间
  Size of logical volume centos/root changed from 8.47 GiB (2168 extents) to 28.47 GiB (7288 extents).
  Logical volume root successfully resized
[root@MyCloudServer ~]# lvs # 查看lv,已经看到20G已经加上了
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 28.47g                                                    
  swap centos -wi-ao----  1.00g  

到这里,根分区的LVM已经扩容完成了。接下来我们要测试一下我们的折腾结果了。

查看此时的磁盘空间,没有发现增加的20G空间。

                                       
[root@MyCloudServer ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.5G  930M  7.6G  11% /
devtmpfs                 873M     0  873M   0% /dev
tmpfs                    893M     0  893M   0% /dev/shm
tmpfs                    893M  8.2M  885M   1% /run
tmpfs                    893M     0  893M   0% /sys/fs/cgroup
/dev/xvda1               497M  161M  337M  33% /boot

因为目前系统还不知道你已经扩容好了,要刷新一下磁盘分区信息。需要注意的是extX分区格式和xfs分区格式的刷新命令不一样。

 
[root@MyCloudServer ~]# resize2fs /dev/centos/root  # 适用于extX分区格式
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.

[root@MyCloudServer ~]# xfs_growfs /dev/centos/root  # 适用于xfs分区格式
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=555008 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2220032, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=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 2220032 to 7462912

再来查看一下硬盘信息,就可以看到根分区的空间已经添加成功了。

 
[root@MyCloudServer ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   29G  930M   28G   4% /
devtmpfs                 873M     0  873M   0% /dev
tmpfs                    893M     0  893M   0% /dev/shm
tmpfs                    893M  8.2M  885M   1% /run
tmpfs                    893M     0  893M   0% /sys/fs/cgroup
/dev/xvda1               497M  161M  337M  33% /boot

添加新评论