LVM磁盘在线扩容

  • 控制台添加新硬盘或编辑修改增加磁盘容量,这里将100GB 修改为120GB
  1. 查看分区情况,这里发现磁盘/dev/sda容量已经增加,但分区容量依然是100GB
[root@Fantasy ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors   ## 磁盘/dev/sda
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: 0x000ca99f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux    ## 两个分区 sda1
/dev/sda2         2099200   209715199   103808000   8e  Linux LVM  ## sda2

Disk /dev/mapper/centos-root: 106.3 GB, 106296246272 bytes, 207609856 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


[root@Fantasy ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.9M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root ext4       98G  3.2G   90G   4% /
/dev/sda1               xfs      1014M  151M  864M  15% /boot
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0

  • 开始扩容

步骤:1.创建新的PV—>2.扩容VG(将新的PV加入到当前VG)—>3.扩容现有LV---->4.再扩容文件系统----5.验证

  1. 创建新的PV
    • 新建分区
    [root@Fantasy ~]# fdisk /dev/sda
    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): n                                          ## 新建分区
    Partition type:
        p   primary (2 primary, 0 extended, 2 free)
        e   extended
    Select (default p): p                                                  ## 选择主分区
    Partition number (3,4, default 3): 3                              ## 选择分区编号
    First sector (209715200-251658239, default 209715200): 
    Using default value 209715200
    Last sector, +sectors or +size{K,M,G} (209715200-251658239, default 251658239): +20GB   ## 新建分区大小
    Partition 3 of type Linux and of size 18.6 GiB is set
    
    Command (m for help): p                                       ##  查看分区
    
    Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000ca99f
    
      Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   209715199   103808000   8e  Linux LVM
    /dev/sda3       209715200   248776703    19530752   83  Linux            ## 新增分区 /dev/sda3
    
    Command (m for help): t                        ## 修改分区类型                       
    Partition number (1-3, default 3): 3           ## 选择上面新增的分区编号  3
    Hex code (type L to list all codes): 8e       ## 8e  改变分区类型为LVM
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): p                  ## 查看分区, /dev/sda3类型已修改为LVM
    
    Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000ca99f
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   209715199   103808000   8e  Linux LVM
    /dev/sda3       209715200   248776703    19530752   8e  Linux LVM      ##  分区类型为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)
    Syncing disks.
    [root@Fantasy ~]# part
    parted     partprobe  partx      
    [root@Fantasy ~]# partprobe     ## 更新内核分区表
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been    opened read-only.
    [root@Fantasy ~]# 
    [root@Fantasy ~]# fdisk -l   ## 再次检查分区信息
    
    Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000ca99f
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   209715199   103808000   8e  Linux LVM
    /dev/sda3       209715200   248776703    19530752   8e  Linux LVM
    
    Disk /dev/mapper/centos-root: 106.3 GB, 106296246272 bytes, 207609856 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
    
  • 新建PV(物理卷)
[root@Fantasy ~]# pvcreate /dev/sda3     
  Physical volume "/dev/sda3" successfully created.
[root@Fantasy ~]# pvdisplay      ## 查看物理卷
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <99.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              25343
  Free PE               0
  Allocated PE          25343
  PV UUID               BbVJ8u-CU3d-fraz-QenF-v2uS-GaqO-vDv6dp
   
  "/dev/sda3" is a new physical volume of "<18.63 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               <18.63 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               38Ea0B-JMVX-wM5T-FOpp-7xbZ-0UCw-zgyMt5
  1. 扩容VG(卷组)
  • 查看当前VG信息
[root@Fantasy ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <99.00 GiB
  PE Size               4.00 MiB
  Total PE              25343
  Alloc PE / Size       25343 / <99.00 GiB
  Free  PE / Size       0 / 0                       ## 可用空闲大小为0
  VG UUID               bbKHNi-oDas-eWzu-Kt52-csjW-NbVo-lKdW0n
  • 扩容
## 将新增的pv:/dev/sda3 添加至名称为 centos的vg
[root@Fantasy ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
## 检查VG
[root@Fantasy ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               117.62 GiB
  PE Size               4.00 MiB
  Total PE              30111
  Alloc PE / Size       25343 / <99.00 GiB
  Free  PE / Size       4768 / 18.62 GiB     ## 可用空闲空间已增加
  VG UUID               bbKHNi-oDas-eWzu-Kt52-csjW-NbVo-lKdW0n
  1. 扩容LV(逻辑卷)
  • 查看LV信息
[root@Fantasy ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                qZQHS6-UiTA-wGXY-RgHq-Occy-d56Y-Za429d
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-25 12:20:34 +0800
  LV Status              available
  # open                 1
  LV Size                <99.00 GiB       ## 大小为99.00GB
  Current LE             25343 
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  • 扩容LV
[root@Fantasy ~]# lvextend -L +18.62GB  /dev/centos/root         ## 将新增的VG 18.62GB 分配给指定LV
  Rounding size to boundary between physical extents: 18.62 GiB.
  Size of logical volume centos/root changed from <99.00 GiB (25343 extents) to <117.62 GiB (30110 extents).
  Logical volume centos/root successfully resized.
  • 查看LV信息
[root@Fantasy ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                qZQHS6-UiTA-wGXY-RgHq-Occy-d56Y-Za429d
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-07-25 12:20:34 +0800
  LV Status              available
  # open                 1
  LV Size                <117.62 GiB        ## 扩容后大小为117.62GB
  Current LE             30110
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  1. 扩容文件系统
  • 查看卷组信息
[root@Fantasy ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   120G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
├─sda2            8:2    0    99G  0 part 
│ └─centos-root 253:0    0 117.6G  0 lvm  /
└─sda3            8:3    0  18.6G  0 part 
  └─centos-root 253:0    0 117.6G  0 lvm  /
sr0              11:0    1     1G  0 rom  
  • 查看文件系统磁盘空间
[root@Fantasy ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.9M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root ext4       98G  3.2G   90G   4% /               ## 没有变化
/dev/sda1               xfs      1014M  151M  864M  15% /boot
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0
  • 扩容文件系统
[root@Fantasy ~]# resize2fs /dev/centos/root      ## ext4 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos/root is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 15
The filesystem on /dev/centos/root is now 30832640 blocks long.

## xfs文件系统扩容使用如下命令
xfs_growfs /dev/centos/root
  1. 验证
[root@Fantasy ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.9M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root ext4      116G  3.2G  107G   3% /          ## 容量已增加
/dev/sda1               xfs      1014M  151M  864M  15% /boot
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0

大吉大利

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值