KVM虚拟机扩容磁盘

[root@wulj ~]# virsh domblklist master-192.168.100.160         #查看虚拟机路径
目标     源
------------------------------------------------
vda        /home/clsn.raw
hda        -

[root@wulj ~]# virsh blockresize --path /home/clsn.raw  --size 50G master-192.168.100.160   给虚拟机扩容
重新定义 '/home/clsn.raw' 块设备大小

 

[root@k8s-master ~]# fdisk /dev/vda          #进入虚拟机
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/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000d081d

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM
Command (m for help): n
Command (m for help): p
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000d081d

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM
/dev/vda3        20971520   104857599    41943040   83  Linux

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@k8s-master ~]# lsblk                      #查看扩容信息
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0   50G  0 disk
|-vda1          252:1    0    1G  0 part /boot
`-vda2          252:2    0    9G  0 part
  |-centos-root 253:0    0    8G  0 lvm  /
  `-centos-swap 253:1    0    1G  0 lvm  [SWAP]
[root@k8s-master ~]# reboot                      #重启虚拟机使新增的分区生效
Connection to k8s-master closed by remote host.
Connection to k8s-master closed.
[root@wulj ~]# ssh k8s-master                     #登录虚拟机
Last login: Fri Mar 11 16:48:43 2022 from 192.168.100.166
[root@k8s-master ~]# lsblk                       #查看分区信息
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0   50G  0 disk
|-vda1          252:1    0    1G  0 part /boot
|-vda2          252:2    0    9G  0 part
| |-centos-root 253:0    0    8G  0 lvm  /
| `-centos-swap 253:1    0    1G  0 lvm  [SWAP]
`-vda3          252:3    0   40G  0 part
[root@k8s-master ~]# pvcreate /dev/vda3         #创建PV 
  Physical volume "/dev/vda3" successfully created.    #显示successfully表示成功
[root@k8s-master ~]# vgextend centos  /dev/vda3    #创建卷组
  Volume group "centos" successfully extended
[root@k8s-master ~]# vgdisplay            #查看卷组信息
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               48.99 GiB
  PE Size               4.00 MiB
  Total PE              12542
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       10239 / <40.00 GiB
  VG UUID               6dDo3E-2PWn-TGIW-6nri-ouuQ-7pKg-Px142m

[root@k8s-master ~]# lvextend -L +40G /dev/mapper/centos-root   #提示是需要10240个扇区,实际扇区只有10239个,只能扩容39.99以下空间。
  Insufficient free space: 10240 extents needed, but only 10239 available
[root@k8s-master ~]# lvextend -L +39G /dev/mapper/centos-root    #使用39G扩容,且扩容成功
  Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <47.00 GiB (12031 extents).
  Logical volume centos/root successfully resized.
[root@k8s-master ~]# resize2fs /dev/centos/root      #resize2fs 只支持调整ext2\ext3\ext4文件系统的大小
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@k8s-master ~]# df -hv                          #查看信息没有成功
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.7M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root  8.0G  2.1G  6.0G  26% /
/dev/vda1               1014M  137M  878M  14% /boot
tmpfs                    100M     0  100M   0% /run/user/0
[root@k8s-master ~]# xfs_growfs /dev/centos/root        #使用xfs文件信息成功扩容 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 2096128 to 12319744
[root@k8s-master ~]# df -hv                           #扩容信息
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.7M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  2.1G   45G   5% /
/dev/vda1               1014M  137M  878M  14% /boot
tmpfs                    100M     0  100M   0% /run/user/0
[root@k8s-master ~]#

 

posted @ 2022-03-11 17:23  一杯枸杞养生茶  阅读(306)  评论(0)    收藏  举报