1 查看原虚拟机磁盘空间大小

执行命令:qemu-img info KVM02.qcow2

示例如下:

(base) [root@Centos7 ImagesB]# qemu-img info KVM02.qcow2 
image: KVM02.qcow2
file format: qcow2
virtual size: 50G (53687091200 bytes)
disk size: 48G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

2 给磁盘扩容

执行命令:qemu-img resize KVM02.qcow2 +10G

示例如下:

(base) [root@Centos7 ImagesB]# qemu-img resize KVM02.qcow2 +10G
Image resized.
(base) [root@Centos7 ImagesB]# qemu-img info KVM02.qcow2 
image: KVM02.qcow2
file format: qcow2
virtual size: 60G (64424509440 bytes)
disk size: 48G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

3 重启虚拟机

键入命令:virsh start Lanke02

示例如下:

(base) [root@WolHawk ImagesB]# virsh start Lanke02
Domain Lanke02 started

4 查看磁盘空间

键入命令:df  -h

示例如下:

[root@Lanke02 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.4G     0  1.4G    0% /dev
tmpfs                    1.4G     0  1.4G    0% /dev/shm
tmpfs                    1.4G  9.3M  1.4G    1% /run
tmpfs                    1.4G     0  1.4G    0% /sys/fs/cgroup
/dev/mapper/centos-root   47G   25G   23G   53% /
/dev/vda1               1014M  187M  828M   19% /boot
tmpfs                    285M  8.0K  285M    1% /run/user/42
tmpfs                    285M     0  285M    0% /run/user/0

5 查看磁盘分区

键入命令:fdisk -l

示例如下:

[root@Lanke02 ~]# fdisk -l

磁盘 /dev/vda64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ac5b7

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM

磁盘 /dev/mapper/centos-root:50.5 GB, 50457477120 字节,98549760 个扇区
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 字节

6 磁盘分区

(1)创建新分区

键入命令:fdisk /dev/vda

Step1:创建分区

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

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


命令(输入 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 获取帮助):p

磁盘 /dev/vda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ac5b7

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM

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

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。

Step2:查看分区

[root@Lanke02 ~]# fdisk -l

磁盘 /dev/vda64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ac5b7

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM
/dev/vda3       104857600   125829119    10485760   8e  Linux LVM

磁盘 /dev/mapper/centos-root:50.5 GB, 50457477120 字节,98549760 个扇区
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 字节

(2)更新分区表

更新分区表命令:partprobe

查看分区表命令:cat  /proc/partitions

示例如下:

[root@Lanke02 ~]# cat /proc/partitions
major minor  #blocks  name

 252        0   62914560 vda
 252        1    1048576 vda1
 252        2   51379200 vda2
  11        0    1048575 sr0
 253        0   49274880 dm-0
 253        1    2097152 dm-1
[root@Lanke02 ~]# partprobe
[root@Lanke02 ~]# cat /proc/partitions
major minor  #blocks  name

 252        0   62914560 vda
 252        1    1048576 vda1
 252        2   51379200 vda2
 252        3   10485760 vda3
  11        0    1048575 sr0
 253        0   49274880 dm-0
 253        1    2097152 dm-1

7 创建物理卷、加入卷组、扩展逻辑卷

(1)创建物理卷

查看已有物理卷命令:pvs

创建新的物理卷命令:pvcreate /dev/vda3

示例如下:

[root@Lanke02 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/vda2  centos lvm2 a--  <49.00g 4.00m
[root@Lanke02 ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.
[root@Lanke02 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/vda2  centos lvm2 a--  <49.00g  4.00m
  /dev/vda3         lvm2 ---   10.00g 10.00g

(2)加入卷组

查看已有卷组命令:vgs

加入已有卷组命令:vgextend centos /dev/vda3

示例如下:

[root@Lanke02 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <49.00g 4.00m
[root@Lanke02 ~]# vgextend centos /dev/vda3
  Volume group "centos" successfully extended
[root@Lanke02 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   2   2   0 wz--n- 58.99g 10.00g

(3)扩展逻辑卷

键入命令1:lvextend -l +100%FREE /dev/centos/root

键入命令2:resize2fs /dev/centos/root

当命令2报错时,须使用“df -Th”查看“/dev/mapper/centos-root”的文件类型,若为“xfs”,则使用命令3

键入命令3(当命令报错时,查看):xfs_growfs /dev/centos/root

示例如下:

[root@Lanke02 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
  Logical volume centos/root successfully resized.
[root@Lanke02 ~]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 当尝试打开 /dev/centos/root 时
找不到有效的文件系统超级块.
[root@Lanke02 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3079680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12318720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6015, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12318720 to 14940160

8 查看硬盘扩容情况

键入命令:df -Th

示例如下:

[root@Lanke02 ~]# df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  1.4G     0  1.4G    0% /dev
tmpfs                   tmpfs     1.4G     0  1.4G    0% /dev/shm
tmpfs                   tmpfs     1.4G  9.4M  1.4G    1% /run
tmpfs                   tmpfs     1.4G     0  1.4G    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        57G   25G   33G   44% /
/dev/vda1               xfs      1014M  187M  828M   19% /boot
tmpfs                   tmpfs     285M   12K  285M    1% /run/user/42
tmpfs                   tmpfs     285M     0  285M    0% /run/user/0

从上面结果可知,磁盘空间扩容成功。

9 参考:

参考1:https://www.jianshu.com/p/27f0e25663c5

posted on 2021-11-15 16:45  LankeHome  阅读(957)  评论(0)    收藏  举报