页首Html代码

返回顶部

记录linux LVM 扩容硬盘空间的记录

 

从2008年学linux的时候就知道有LVM这个东西,那个时候还是ext3文件系统,LVM逻辑卷管理,可以动态管理磁盘。

今天终于有这个需求了,硬盘空间200GB太小了,使用vmware vSphere 直接给linux的硬盘追加了100GB的磁盘,看看怎么把这100GB添加到系统中去;

首先确定下基本概念:

pv=phisycal volumn  物理卷

vg=volumn group      逻辑卷组
lv=logic volumn         逻辑卷

Physical volume (PV物理卷)、Volume group (VG卷组)、Logical volume(LV逻辑卷)

 

 

顺序是先定义 PV 物理卷;

然后定义一个 VG 分组,必须指定名称,比如 cl ; 一个分组可以包含多个PV物理卷;   

根据VG分组,进行LV逻辑卷的分区操作, VG下就像是硬盘分区一样, 比如 home, root swap 分别分配大小

相关信息查看命令为:

[root@h5_242 ~]# pvs
  PV         VG Fmt  Attr PSize    PFree
  /dev/sda2  cl lvm2 a--  <199.00g    0 
  /dev/sda3  cl lvm2 a--  <100.00g    0 
[root@h5_242 ~]# vgs
  VG #PV #LV #SN Attr   VSize   VFree
  cl   2   3   0 wz--n- 298.99g    0 
[root@h5_242 ~]# lvs
  LV   VG Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cl -wi-ao---- <241.12g                                                    
  root cl -wi-ao----   50.00g                                                    
  swap cl -wi-ao----   <7.88g                             
全局分区查看
#fdisk -l

磁盘 /dev/sda:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00046755

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   419430399   208665600   8e  Linux LVM
/dev/sda3       419430400   629145599   104857600   83  Linux

磁盘 /dev/mapper/cl-root:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/cl-swap:8455 MB, 8455716864 字节,16515072 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/cl-home:258.9 GB, 258897608704 字节,505659392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节  
#fdisk分区以后, 需要执行下面的命令 才能不重启linux 就立刻生效
partprobe /dev/sda3

新挂载的SCSI硬盘需要执行命令才能刷新(不必重启)
ls /dev/sd*

#强制内核再次检测PCI设备
ls /sys/class/scsi_host
echo '- - -' > /sys/class/scsi_host/host0/scan

ls /dev/sd*
tail -f /var/log/message

# 还可以尝试 scsi 设备刷新
cat /proc/scsi/scsi 

# re-scan  SCSI 总线
 echo "scsi add-single-device 0 0 1 0" > /proc/scsi/scsi



#刷新 新分区表信息
yum -y install parted
 partprobe

  



PV  VG LV  (物理卷  逻辑卷分组   逻辑卷) 相关命令为: 

[root@h5_242 ~]# pv
pv         pvchange   pvck       pvcreate   pvdisplay  pvmove     pvremove   pvresize   pvs        pvscan     
[root@h5_242 ~]# vg
vgcfgbackup    vgchange       vgconvert      vgdisplay      vgextend       vgimportclone  vgmknodes      vgremove       vgs            vgsplit        
vgcfgrestore   vgck           vgcreate       vgexport       vgimport       vgmerge        vgreduce       vgrename       vgscan         
[root@h5_242 ~]# lv
lvchange     lvcreate     lvextend     lvmconf      lvmdiskscan  lvmetad      lvmsadc      lvreduce     lvrename     lvs          
lvconvert    lvdisplay    lvm          lvmconfig    lvmdump      lvmpolld     lvmsar       lvremove     lvresize     lvscan       
[root@h5_242 ~]# 

  创建物理卷: 

pvcreate /dev/sda3
WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext3 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.


lvm> pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <199.00g 4.00m
/dev/sda3 lvm2 --- 100.00g 100.00g

  把物理卷 添加到 卷分组中

lvm> vgs
  VG #PV #LV #SN Attr   VSize    VFree
  cl   1   3   0 wz--n- <199.00g 4.00m

lvm> vgcreate cl2 /dev/sda3
  Volume group "cl2" successfully created

####查看新建成功
lvm> vgs
  VG  #PV #LV #SN Attr   VSize    VFree   
  cl    1   3   0 wz--n- <199.00g    4.00m
  cl2   1   0   0 wz--n- <100.00g <100.00g

#### 发现两个不好,合并一下 , 因为直接 扩展不可以
lvm> vgextend cl2 cl
  Device cl not found.
lvm> vgextend cl /dev/sda3
  Physical volume '/dev/sda3' is already in volume group 'cl2'
  Unable to add physical volume '/dev/sda3' to volume group 'cl2'
  /dev/sda3: physical volume not initialized.

##### 合并两个 volum group
lvm> vgmerge  cl cl2     
  Volume group "cl2" successfully merged into "cl"
lvm> vgs
  VG #PV #LV #SN Attr   VSize   VFree  
  cl   2   3   0 wz--n- 298.99g 100.00g

  根据VG 逻辑分组 进行扩容处理

lvm> lvs
  LV   VG Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cl -wi-ao---- <141.12g                                                    
  root cl -wi-ao----   50.00g                                                    
  swap cl -wi-ao----   <7.88g                                                    
lvm> lvdisplay 
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                CjWBE7-OuOo-NIkk-O6t6-qUGK-w1P3-mUXHJQ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-07-10 10:51:18 +0800
  LV Status              available
  # open                 2
  LV Size                <7.88 GiB
  Current LE             2016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cl/home
  LV Name                home
  VG Name                cl
  LV UUID                O39uaO-cs4p-3xnX-TQre-97RW-UjQI-2zQHOv
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-07-10 10:51:19 +0800
  LV Status              available
  # open                 1
  LV Size                <141.12 GiB
  Current LE             36126
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                46CDUc-5p7Q-9hD5-22vp-YPUl-DSMN-7aQ5Dr
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-07-10 10:51:22 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0




######### 进行 Logic Volumn 的扩容处理

lvm> lvextend -L +100G /dev/cl/home
  Size of logical volume cl/home changed from <141.12 GiB (36126 extents) to <241.12 GiB (61726 extents).
  Logical volume cl/home successfully resized.

lvm> lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home cl -wi-ao---- <241.12g
root cl -wi-ao---- 50.00g
swap cl -wi-ao---- <7.88g



  发现虽然扩容成功,但是没有生效。 

使用 resizee2fs 无效, 因为磁盘分区不符合

[root@h5_242 ~]# resize2fs /dev/cl/home 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 当尝试打开 /dev/cl/home 时

  

使用 xfs_growfs  命令 成功更新系统

[root@h5_242 ~]# xfs_growfs  /dev/mapper/cl-home 
meta-data=/dev/mapper/cl-home    isize=512    agcount=4, agsize=9248256 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=36993024, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=18063, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 36993024 to 63207424
[root@h5_242 ~]# 
[root@h5_242 ~]# 
[root@h5_242 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   50G  6.0G   45G   12% /
devtmpfs             2.9G     0  2.9G    0% /dev
tmpfs                2.9G     0  2.9G    0% /dev/shm
tmpfs                2.9G   26M  2.9G    1% /run
tmpfs                2.9G     0  2.9G    0% /sys/fs/cgroup
/dev/sda1           1014M  219M  796M   22% /boot
/dev/mapper/cl-home  242G  131G  111G   55% /home
tmpfs                581M     0  581M    0% /run/user/0
tmpfs                581M     0  581M    0% /run/user/1001
tmpfs                581M     0  581M    0% /run/user/1028
tmpfs                581M     0  581M    0% /run/user/1016
tmpfs                581M     0  581M    0% /run/user/1021

  

 

 

 

 

 

 

 

 

 

------------------------------------------

参考链接:  https://www.linuxidc.com/Linux/2015-10/123960.htm

Centos 7.5 利用LVM实现动态扩容大小
操作系统:Centos 7.5 64位

说明:当有在操作系统时发现磁盘空间不足时,可以通过增加磁盘空间大小来满足,但是以哪种方式来增加可能有不同方法,现在针对操作系统在安装采用LVM方式来动态调整磁盘空间大小,

知识点解释:

 LVM是逻辑盘卷管理(Logical VolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配

Linux的LVM非常强大,可以在生产运行系统上面直接在线扩展硬盘分区,可以把分区umount以后收缩分区大小,还可以在系统运行过程中把一个分区从一块硬盘搬到另一块硬盘上面去等等,简直就像变魔术,而且这一切都可以在一个繁忙运行的系统上面直接操作,不会对你的系统运行产生任何影响,很安全。

LVM使用有局限性。虽然能很方便的扩容和缩容磁盘的空间(扩容磁盘大小,文件不丢失),但是一旦出现问题,数据丢失,想要恢复数据就有点困难!

三步:

①创建一个物理分区-搞成物理卷②制作成一个卷组③划分成逻辑卷

 

 

1.如果在安装系统时选择是以LVM方式安装的话,可以通过已经安装好后系统磁盘查询
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   70G  9.4G   61G   14% /
devtmpfs                 4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

说明:centos-root 和centos-home 是通过LVM方式来配置的

2、使用df  -T -h命令查看操作系统的文件类型
[root@localhost ~]#  df  -T -h
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        70G  9.4G   61G   14% /
devtmpfs                devtmpfs  4.8G     0  4.8G    0% /dev
tmpfs                   tmpfs     4.9G     0  4.9G    0% /dev/shm
tmpfs                   tmpfs     4.9G  9.5M  4.8G    1% /run
tmpfs                   tmpfs     4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  171M  844M   17% /boot
/dev/mapper/centos-home xfs        27G   37M   27G    1% /home
tmpfs                   tmpfs     984M   12K  984M    1% /run/user/42
tmpfs                   tmpfs     984M     0  984M    0% /run/user/0

说明:通过查询发现操作系统的文件格式是:xfs

3.查看卷组名称及卷组使用情况  vgdisplay
[root@localhost ~]#  vgdisplay
  --- Volume group ---
  VG Name               centos --VG名称
  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               98.99 GiB --总共的空间大小
  PE Size               4.00 MiB
  Total PE              25342
  Alloc PE / Size       25341 / <98.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               oCJsUm-wF9P-a0Pc-rLvl-aCa1-0Inr-psWPzn

 

4.查看当前逻辑卷的空间状态,如下内容:/ 70G、/home 26G、swap 2G
[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                 /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                0F9zrD-AfsY-teOk-OLqB-ZKa7-6fcZ-aVQ0VM
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:41 +0800
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                v64Mg2-gDRG-UGoe-InX6-qqsG-GuiD-eRrcs8
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:42 +0800
  LV Status              available
  # open                 1
  LV Size                <26.99 GiB
  Current LE             6909
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                0le8V4-P7XQ-jtC7-7xtY-wKUv-dcNe-mJrB4T
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:43 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

5.现在感觉/ 70G不够用,想扩容增加300G,新插入一块300G硬盘;用‘fdisk -l’查看
[root@localhost ~]# fdisk -l

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0004c640

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   209707007   103803904   8e  Linux LVM

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-root:75.2 GB, 75161927680 字节,146800640 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-home:29.0 GB, 28978446336 字节,56598528 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   70G  9.4G   61G   14% /
devtmpfs                 4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

6、格式化/dev/sdb磁盘

[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xbe0b32e5 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System

 

命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-629145599,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-629145599,默认为 629145599):
将使用默认值 629145599
分区 1 已设置为 Linux 类型,大小设为 300 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

7、将 /dev/sdb配置为LVM格式
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):l

 
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

8、查询配置为LVM格式后结果如下:

[root@localhost ~]# fdisk -l

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0004c640

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   209707007   103803904   8e  Linux LVM

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

磁盘 /dev/mapper/centos-root:75.2 GB, 75161927680 字节,146800640 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-home:29.0 GB, 28978446336 字节,56598528 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

9.将新加的分区 /dev/sdb1 创建为物理卷
[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

10.给卷组‘centos ’扩容,将物理卷 /dev/sdb1 扩展至‘centos ’卷组

[root@localhost ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

 

10.此时卷组‘centos ’有300G空余空间,及 /dev/sdb1,将其全部扩展至 /home
root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-home
  Size of logical volume centos/home changed from <26.99 GiB (6909 extents) to <326.99 GiB (83709 extents).
  Logical volume centos/home successfully resized.

11.发现扩展错分区了,把第10步的增加空间给缩小回去到原来值,26G

[root@localhost ~]# lvreduce -L -300G /dev/mapper/centos-home
  WARNING: Reducing active and open logical volume to <26.99 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: yes
  Size of logical volume centos/home changed from <326.99 GiB (83709 extents) to <26.99 GiB (6909 extents).
  Logical volume centos/home successfully resized.

12.重新把PV中的300G多余空间分配给LV为root
[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 70.00 GiB (17920 extents) to 370.00 GiB (94720 extents).
  Logical volume centos/root successfully resized.

 

13.此时300G磁盘虽已扩展至 /root,但并没写入文件系统;进入 /etc/fstab 确认 /root 文件系统--->xfs文件系统
[root@localhost ~]# more /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Sep 14 17:08:44 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                              xfs     defaults        0 0
UUID=28137713-1148-49bc-b74d-7ac9bdf0bc1b /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

14、用xfs_growfs 命令把文件写入系统中,使扩容生效
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=4587520 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=18350080, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=8960, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 18350080 to 96993280

说明:若使用ext4文件格式的,是使用resize2fs命令来生效

 

15.查看扩容后的硬盘空间小,如下内容则说明,已实现扩容。大功告成!
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  370G  9.4G  361G    3% /
devtmpfs                4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

 

本次扩容指令汇总:
1.创建分区                                                #fdisk /dev/sdb

2.创建物理卷                                             #pvcreat /dev/sdb1

3.查看卷组名称及使用情况                         #vgdisplay

4.将物理卷扩展到卷组                               #vgextend cl /dev/sdb1   (此处‘cl’是卷组名称)

5.将卷组中空闲空间扩展到 /home           #lvextend -l +100%FREE /dev/mapper/cl-home

6.刷新文件系统是扩容生效 xfs_growfs /dev/mapper/centos-root

7.若是ext4文件格式使用扩容生效                     #resize2fs /dev/mapper/centos-home

相关LVM知识点如下:
这里总结下扩展与缩小LV的实施顺序:

扩展LV

1、卸载LV

命令:umount  “挂载目录”

2、扩展LV

命令:lvextend -L +500m  /dev/lv00/lv01 或者 lvresize -L 5G  /dev/lv00/lv01

lvextend 表示增加500M,lvresize表示增加到5G,是调整后的容量。

3、检查文件系统

命令:fsck -f  /dev/vg00/lv01

4、重建文件系统

  命令:resize2fs  /dev/lv00/lv01

这一步很关键,重建文件系统不用担心LV的数据会丢失,当然,以防万一,也可以先备份下LV里的数据。

5、挂载LV

命令:mount  /dev/lv001/lv01  /opt

缩小LV

  缩小LV需要事先知道LV中的数据容量大小,缩小容量值不应超过剩余空间容量,而且要知道减少LV容量后的新大小。可以通过lvscan来查看LV容量,通过vgdisplay来查看剩余容量。

1、卸载LV

命令:umount  “挂载目录”

2、检查文件系统

命令:fsck -f  /dev/lv00/lv01

3、调整LV大小并重建文件系统

命令:resize2fs  /dev/vg00/lv01  2G

4、减小LV容量

命令:lvreduce -L -3G  /dev/vg00/lv01

5、挂载LV

  命令:mount  /dev/lv001/lv01  /opt
————————————————
版权声明:本文为CSDN博主「贾维斯博客」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010735147/java/article/details/84254926

 

posted @ 2020-04-02 22:13  ayanmw  阅读(2135)  评论(0编辑  收藏  举报

页脚Html代码