Llinux系统增新磁盘实现在线扩容,增加lvm分区容量

本次在虚拟机上模拟新增硬盘,扩容lvm分区系统。

1、虚拟机加盘

给虚拟机加一块30GB的硬盘
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、不重启系统识别新增磁盘

     此次在不重启操作系统的情况下,扫描识别新加的磁盘,在实际生产当中发现有时候并没有效果,需要重启服务器才能识别到新加的磁盘。
1、fdisk -l查看当前磁盘信息

[root@localhost ~]# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0x9edfc7bb

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048  1050623  1048576  512M 83 Linux
/dev/sda2        1050624 30427135 29376512   14G 8e Linux LVM
/dev/sda3       30427136 47204351 16777216    8G 82 Linux swap / Solaris
/dev/sda4       47204352 62914559 15710208  7.5G 8e Linux LVM


Disk /dev/mapper/rhel-root: 17 GiB, 18253611008 bytes, 35651584 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 /dev/mapper/rhel-var: 2 GiB, 2147483648 bytes, 4194304 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 /dev/mapper/rhel-home: 2 GiB, 2147483648 bytes, 4194304 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

2、扫描磁盘,/sys/class/scsi_host下面有几个host就刷几次,我这边查到有33个host,全部刷新一遍

[root@localhost]# cd /sys/class/scsi_host
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host3/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host4/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host5/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host6/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host7/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host8/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host9/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host10/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host11/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host12/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host13/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host14/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host15/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host16/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host17/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host18/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host19/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host20/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host21/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host22/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host23/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host24/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host25/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host26/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host27/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host28/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host29/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host30/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host31/scan
[root@localhost scsi_host]# echo "- - -" > /sys/class/scsi_host/host32/scan

3、重新执行fdisk -l查看磁盘信息,可以看到多了一个30GB的/dev/sdb盘

[root@localhost ~]# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0x9edfc7bb

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048  1050623  1048576  512M 83 Linux
/dev/sda2        1050624 30427135 29376512   14G 8e Linux LVM
/dev/sda3       30427136 47204351 16777216    8G 82 Linux swap / Solaris
/dev/sda4       47204352 62914559 15710208  7.5G 8e Linux LVM


Disk /dev/mapper/rhel-root: 17 GiB, 18253611008 bytes, 35651584 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 /dev/mapper/rhel-var: 2 GiB, 2147483648 bytes, 4194304 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 /dev/mapper/rhel-home: 2 GiB, 2147483648 bytes, 4194304 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 /dev/sdb: 30 GiB, 32212254720 bytes, 62914560 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

3、LVM分区扩容

1、查看当前的vg信息,剩余容量等。

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  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               21.49 GiB
  PE Size               4.00 MiB
  Total PE              5502
  Alloc PE / Size       5376 / 21.00 GiB
  Free  PE / Size       126 / 504.00 MiB
  VG UUID               CWSc1N-T9PW-oPT9-YB9y-Rhsz-xNxV-uP8HoM
   
[root@localhost ~]# pvscan
  PV /dev/sda2   VG rhel            lvm2 [14.00 GiB / 0    free]
  PV /dev/sda4   VG rhel            lvm2 [<7.49 GiB / 504.00 MiB free]
  Total: 2 [21.49 GiB] / in use: 2 [21.49 GiB] / in no VG: 0 [0   ]

2、创建pv,将整个硬盘创建一个pv。

[root@localhost ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@localhost ~]# pvscan
  PV /dev/sda2   VG rhel            lvm2 [14.00 GiB / 0    free]
  PV /dev/sda4   VG rhel            lvm2 [<7.49 GiB / 504.00 MiB free]
  PV /dev/sdb                       lvm2 [30.00 GiB]
  Total: 3 [51.49 GiB] / in use: 2 [21.49 GiB] / in no VG: 1 [30.00 GiB]

3、扩容vg,可以看到多了30G剩于空间

[root@localhost ~]# vgextend rhel /dev/sdb
  Volume group "rhel" successfully extended
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <51.49 GiB
  PE Size               4.00 MiB
  Total PE              13181
  Alloc PE / Size       5376 / 21.00 GiB
  Free  PE / Size       7805 / <30.49 GiB
  VG UUID               CWSc1N-T9PW-oPT9-YB9y-Rhsz-xNxV-uP8HoM

4、扩容lv分区,这里选择对/分区进行扩容 。先查看/分区对应的lv name是/dev/rhel/root,当前大小为17G。执行lvextend加了20G空间。

[root@localhost scsi_host]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               894M     0  894M   0% /dev
tmpfs                  909M     0  909M   0% /dev/shm
tmpfs                  909M   18M  892M   2% /run
tmpfs                  909M     0  909M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  8.2G  8.9G  48% /
/dev/mapper/rhel-var   2.0G  238M  1.8G  12% /var
/dev/mapper/rhel-home  2.0G   47M  2.0G   3% /home
/dev/sda1              507M  159M  348M  32% /boot
tmpfs                  182M   28K  182M   1% /run/user/42
tmpfs                  182M  4.0K  182M   1% /run/user/0
[root@localhost ~]# vgdisplay -v rhel
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <51.49 GiB
  PE Size               4.00 MiB
  Total PE              13181
  Alloc PE / Size       5376 / 21.00 GiB
  Free  PE / Size       7805 / <30.49 GiB
  VG UUID               CWSc1N-T9PW-oPT9-YB9y-Rhsz-xNxV-uP8HoM
   
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                duRq2B-ki6Q-IoIE-m4ET-VVn1-ZHVx-NrpzPL
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                17.00 GiB
  Current LE             4352
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/rhel/var
  LV Name                var
  VG Name                rhel
  LV UUID                ksUp9S-Zta4-EQLr-0m5g-xdqz-lOWu-qAY0Uk
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rhel/home
  LV Name                home
  VG Name                rhel
  LV UUID                URbY36-FZa0-p5Li-8jgA-sdA4-YlH1-MdUyKs
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               mBZDl1-eqme-P0tU-amqc-Dmkj-Z6j0-SNvTi3
  PV Status             allocatable
  Total PE / Free PE    3585 / 0
   
  PV Name               /dev/sda4     
  PV UUID               lvQFY0-W5DR-OzWa-Uwml-O21E-cEIF-RPywK2
  PV Status             allocatable
  Total PE / Free PE    1917 / 126
   
  PV Name               /dev/sdb     
  PV UUID               lXQNKD-eYaQ-TwQM-NAdy-mwiC-QVk8-Y3butQ
  PV Status             allocatable
  Total PE / Free PE    7679 / 7679
   
[root@localhost ~]# lvextend -L +20G /dev/rhel/root
  Size of logical volume rhel/root changed from 17.00 GiB (4352 extents) to 37.00 GiB (9472 extents).
  Logical volume rhel/root successfully resized.
[root@localhost ~]# vgdisplay -v rhel
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <51.49 GiB
  PE Size               4.00 MiB
  Total PE              13181
  Alloc PE / Size       10496 / 41.00 GiB
  Free  PE / Size       2685 / <10.49 GiB
  VG UUID               CWSc1N-T9PW-oPT9-YB9y-Rhsz-xNxV-uP8HoM
   
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                duRq2B-ki6Q-IoIE-m4ET-VVn1-ZHVx-NrpzPL
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                37.00 GiB
  Current LE             9472
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/rhel/var
  LV Name                var
  VG Name                rhel
  LV UUID                ksUp9S-Zta4-EQLr-0m5g-xdqz-lOWu-qAY0Uk
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rhel/home
  LV Name                home
  VG Name                rhel
  LV UUID                URbY36-FZa0-p5Li-8jgA-sdA4-YlH1-MdUyKs
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-29 12:30:27 -0500
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               mBZDl1-eqme-P0tU-amqc-Dmkj-Z6j0-SNvTi3
  PV Status             allocatable
  Total PE / Free PE    3585 / 0
   
  PV Name               /dev/sda4     
  PV UUID               lvQFY0-W5DR-OzWa-Uwml-O21E-cEIF-RPywK2
  PV Status             allocatable
  Total PE / Free PE    1917 / 0
   
  PV Name               /dev/sdb     
  PV UUID               lXQNKD-eYaQ-TwQM-NAdy-mwiC-QVk8-Y3butQ
  PV Status             allocatable
  Total PE / Free PE    7679 / 2685

5、扩容文件系统,上述可以看到lv已经扩容成功,但是df查看文件系统还是没有变化,我们需要再进行文件系统的扩容,这里要区分系统类型,如果是ext4以前的系统格式,需要使用resize2fs命令进行扩容文件系统,xfs格式,需要使用xfs_growfs命令进行扩容。

Filesystem            Type      Size  Used Avail Use% Mounted on
devtmpfs              devtmpfs  894M     0  894M   0% /dev
tmpfs                 tmpfs     909M     0  909M   0% /dev/shm
tmpfs                 tmpfs     909M   18M  892M   2% /run
tmpfs                 tmpfs     909M     0  909M   0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs        17G  8.2G  8.9G  48% /
/dev/mapper/rhel-var  xfs       2.0G  238M  1.8G  12% /var
/dev/mapper/rhel-home xfs       2.0G   47M  2.0G   3% /home
/dev/sda1             xfs       507M  159M  348M  32% /boot
tmpfs                 tmpfs     182M   28K  182M   1% /run/user/42
tmpfs                 tmpfs     182M  4.0K  182M   1% /run/user/0
[root@localhost scsi_host]# xfs_growfs /dev/rhel/root
xfs_growfs: /dev/rhel/root is not a mounted XFS filesystem
[root@localhost scsi_host]# xfs_growfs /dev/mapper/rhel-root
xfs_growfs: /dev/mapper/rhel-root is not a mounted XFS filesystem
[root@localhost scsi_host]# xfs_growfs /
meta-data=/dev/mapper/rhel-root  isize=512    agcount=7, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=4456448, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           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 4456448 to 9699328
[root@localhost scsi_host]# df -hT
Filesystem            Type      Size  Used Avail Use% Mounted on
devtmpfs              devtmpfs  894M     0  894M   0% /dev
tmpfs                 tmpfs     909M     0  909M   0% /dev/shm
tmpfs                 tmpfs     909M   18M  892M   2% /run
tmpfs                 tmpfs     909M     0  909M   0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs        37G  8.3G   29G  23% /
/dev/mapper/rhel-var  xfs       2.0G  238M  1.8G  12% /var
/dev/mapper/rhel-home xfs       2.0G   47M  2.0G   3% /home
/dev/sda1             xfs       507M  159M  348M  32% /boot
tmpfs                 tmpfs     182M   28K  182M   1% /run/user/42
tmpfs                 tmpfs     182M  4.0K  182M   1% /run/user/0
[root@localhost scsi_host]# 

6、可以看到/分区已在线扩容完成,但是在执行的过程中使用LV Path和Filesystem扩容的时候都报错xfs_growfs: is not a mounted XFS ,直接使用挂载点成功,这个没有想明白是什么原因。

备注:xfs文件系统只能在线扩容,不能在线缩小,ext4可以在线扩容也能在线缩小。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐