lvm磁盘扩容

  Linux

说明

旧版:fdisk + + vgextend + lvextend + resize2fs/xfs_growfs

不修改磁盘大小,不修改pv大小,只是单纯的增加磁盘与pv的数量,然后扩容vg,然后lvm进行扩容


新版:parted + pvresize + lvmextend + resize2fs/xfs_growfs

通过parted修改磁盘大小,然后更新pv大小(同时也会更新vg大小),然后扩容lvm

仅在CentOS7测试过,不保证CentOS6可用


扩容步骤

复制命令时注意不要包括命令前面的提示符:“[root@localhost ~]# ” 和 “(parted) ”
!!!注意命令中的参数尤其是“磁盘分区号”和“磁盘结束位置”不要复制!!!

# 选择要扩容的磁盘
[root@localhost ~]# parted /dev/vda
# 注意!!!进入parted程序后命令前面都会有提示符“(parted) ”这个提示符后面才是命令!!!
# 查看磁盘分区列表
(parted) print
----------------------------------------------------
# 输出结果如下,这个例子比较极端,之前已经做过一次扩容,如果使用旧的方式是没法继续在同一个磁盘上面增加新分区然后扩容的,但是使用parted可以解决这个问题。
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 322GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1075MB  1074MB  primary   xfs          boot
 2      1075MB  42.9GB  41.9GB  primary                lvm
 4      42.9GB  102.9GB   60GB   extended
 5      43.0GB  102.9GB   60GB   logical                lvm
----------------------------------------------------
# 接在print后的操作
# 从“End”列可以看出4和5的结束大小为:102.9GB,这个指的是结束的磁盘块的位置不是分区大小,分区大小是“Size”列也就是:60GB
# 从“Type”列可以看出分区“4”是扩展分区,分区“5”是逻辑分区,按标准我们要先扩容**扩展分区然后再扩容逻辑分区
# 也就是先扩容分区“4” 再 扩容分区 “5”,虚拟机磁盘修改后的大小为322GB,那么我们将这两个分区的结束块调整到322GB
# 实际扩容时请以服务器实际的磁盘分区为准!!!
(parted) resizepart 4 322GB
(parted) resizepart 5 322GB
# 查看重新分区后大小
(parted) print
----------------------------------------------------
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 322GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1075MB  1074MB  primary   xfs          boot
 2      1075MB  42.9GB  41.9GB  primary                lvm
 4      42.9GB  322GB   279GB   extended
 5      43.0GB  322GB   279GB   logical                lvm
----------------------------------------------------
# 调整成功,退出parted命令行
(parted) quit
# 查看pv,使用 pvs 或 pvscan 命令皆可此时pv还没有扩容
[root@localhost ~]# pvscan 
  PV /dev/vda2   VG centos          lvm2 [<39.00 GiB / 0    free]
  PV /dev/vda5   VG centos          lvm2 [<60.00 GiB / 124.00 MiB free]
  Total: 2 [98.99 GiB] / in use: 2 [98.99 GiB] / in no VG: 0 [0   ]
# 调整pv大小
[root@localhost ~]# pvresize -v /dev/vda5 
    Archiving volume group "centos" metadata (seqno 6).
    Resizing volume "/dev/vda5" to 545018123 sectors.
    Resizing physical volume /dev/vda5 from 15359 to 66530 extents.
    Updating physical volume "/dev/vda5"
    Creating volume group backup "/etc/lvm/backup/centos" (seqno 7).
  Physical volume "/dev/vda5" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
# 调整成功,然后查看一下pv大小,扩容成功,有200.01GB的空闲空间
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree   
  /dev/vda2  centos lvm2 a--  <39.00g       0 
  /dev/vda5  centos lvm2 a--  259.88g <200.01g

# 为LV扩容
[root@localhost ~]# lvextend -L+200G /dev/centos/root 
  Size of logical volume centos/root changed from 90.00 GiB (23040 extents) to 290.00 GiB (74240 extents).
  Logical volume centos/root successfully resized.
# lv扩容成功后,扩容文件系统
[root@localhost ~]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=512    agcount=12, agsize=1973760 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=23592960, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=3855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 23592960 to 76021760
# 查看磁盘空间,已经扩容成功,剩余空间261GB
[root@localhost ~]#  df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     290G   30G  261G  11% /
devtmpfs                    7.8G     0  7.8G   0% /dev
tmpfs                       7.8G     0  7.8G   0% /dev/shm
tmpfs                       7.8G   11M  7.8G   1% /run
tmpfs                       7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1                  1014M  210M  805M  21% /boot
/dev/mapper/centos-var_log  5.0G  411M  4.6G   9% /var
tmpfs                       1.6G     0  1.6G   0% /run/user/1697
tmpfs                       1.6G     0  1.6G   0% /run/user/1694