KVM虚拟机扩容硬盘

虚拟机磁盘创建后大小固定,后期新增磁盘空间可通过扩容当前分区或新增磁盘实现。对于KVM虚拟机,介绍了当前硬盘扩容方法,包括查看、调整镜像磁盘空间,分区,加入LVM及最后扩容;还说明了新增硬盘的步骤,如创建磁盘、修改配置文件、格式化并挂载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虚拟机磁盘在创建时就固定了大小,后期要新增磁盘空间的话,只能扩容当前分区空间或者新增磁盘。

一、当前硬盘扩容

kvm虚拟机磁盘格式分为raw和qcow2两种,扩容方式命令相同,区别是后缀名

  • 查看当前硬盘空间并扩容镜像磁盘空间
# qemu-img info vm5.qcow2 
image: vm5.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

# qemu-img resize /data/kvm/vm5.qcow2 +10G  //镜像空间加10G
Image resized.

# qemu-img info vm5.qcow2 
image: vm5.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
  • 重启虚拟机,查看磁盘空间已经调整,挂载空间还需要调整
# virsh shutdown vm5
# virsh start vm5
# virsh console vm5
# df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       19G  697M   17G   4% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/vda1             477M   34M  418M   8% /boot

# fdisk -l
Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
/dev/vda2            1018       41611    20458496   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
  • 新增的空间进行分区
# fdisk /dev/vda 

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Command (m for help): n    //新增分区
Command action
   e   extended
   p   primary partition (1-4)
Select (default p): p      //主分区,分一个区的话,后面可以全部选择默认
Partition number (1-4): 3
First cylinder (1-62415, default 1): 41611
Last cylinder, +cylinders or +size{K,M,G} (41611-62415, default 62415): 
Using default value 62415

Command (m for help): p    //打印分区情况

Disk /dev/vda: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00070304

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41611       62415    10485640   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.
  • 把 /dev/vda3 加入到 lvm 里面去,如 /dev/vda3 设备不存在则重启虚拟机即可
# pvcreate /dev/vda3     //创建物理卷
  Physical volume "/dev/vda3" successfully created
# pvs                    //查看卷
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/vda2  VolGroup lvm2 a--u 19.51g     0 
  /dev/vda3           lvm2 ---- 10.00g 10.00g
# vgextend VolGroup /dev/vda3   //VolGroup虚拟卷扩展,vda3加入到VolGroup(这个名字从上面pvs中VG字段获取)
  Volume group "VolGroup" successfully extended
# pvs
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/vda2  VolGroup lvm2 a--u 19.51g     0 
  /dev/vda3  VolGroup lvm2 a--u 10.00g 10.00g
# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  VolGroup   2   2   0 wz--n- 29.50g 10.00g
# lvs					   //查看逻辑卷
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao---- 17.51g                                                    
  lv_swap VolGroup -wi-ao----  2.00g                                                    
# df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  696M   16G   5% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/vda1             477M   40M  413M   9% /boot
# lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root   //扩展卷,卷名从 df -h 获取
  Size of logical volume VolGroup/lv_root changed from 17.51 GiB (4482 extents) to 27.50 GiB (7041 extents).
  Logical volume lv_root successfully resized.
# df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  696M   16G   5% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/vda1             477M   40M  413M   9% /boot
  • 使用 resize2fs 或 xfs_growfs 命令进行最后扩容
# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 7209984 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 7209984 blocks long.

# xfs (CentOS 7)文件系统格式使用 xfs_growfs 命令扩容
# xfs_growfs /dev/mapper/VolGroup-lv_root

# df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       27G  696M   25G   3% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/vda1             477M   40M  413M   9% /boot
二、新增硬盘
  • 创建磁盘
# vm5_2.qcow2 是硬盘名称,千万不要跟现有的冲突,否则就把现有主机的给down了

# qemu-img create -f qcow2  vm5_2.qcow2 5G
Formatting 'vm5_2.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off 
  • 关闭虚拟机并修改配置文件,添加如下配置
# virsh destroy vm5
Domain vm5 destroyed
# virsh edit vm5
......
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/data/kvm/vm5_2.qcow2'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>
......
  • 开机虚拟机,格式化硬盘并挂载
# virsh start vm5
# virsh console vm5
# mkfs.ext4 /dev/vdb
# mount /dev/vdb /data
对于在Linux环境下使用virsh和qemu-img命令对KVM虚拟机进行硬盘扩容后的文件系统分区扩展,推荐的资料《Linux virsh KVM虚机硬盘扩容教程:步骤详解》将为你提供详尽的操作步骤和实例。虚拟机硬盘容量的增加只是完成了存储空间的物理扩展,而要让操作系统识别并使用这部分额外空间,还必须在虚拟机内部进行文件系统分区的扩展。以下是详细步骤: 参考资源链接:[Linux virsh KVM虚机硬盘扩容教程:步骤详解](https://wenku.csdn.net/doc/2fen83fkrf?spm=1055.2569.3001.10343) 1. **启动虚拟机**:完成硬盘扩容后,启动虚拟机。 2. **检查新的磁盘空间**:首先使用`df -h`命令查看当前的磁盘空间使用情况,确认新的硬盘空间是否已经出现。 3. **识别新硬盘**:使用`lsblk`或`fdisk -l`命令识别新添加的硬盘分区。 4. **分区扩展**:使用`parted`或`fdisk`等工具对新硬盘进行分区操作。例如,使用`fdisk /dev/sdb`(假设新硬盘为/dev/sdb),按照提示创建新分区。 5. **文件系统格式化**:对新创建的分区进行文件系统格式化,例如使用`mkfs.ext4 /dev/sdb1`命令格式化为ext4文件系统。 6. **挂载新分区**:创建挂载点并挂载新分区,如`mkdir /mnt/newdata`和`mount /dev/sdb1 /mnt/newdata`。 7. **扩展现有文件系统**:如果需要将新分区合并到现有的文件系统中,可以使用`resize2fs`工具。例如,如果现有文件系统为`/dev/sda1`,则可以使用`resize2fs /dev/sda1`命令将文件系统扩展到新分区。 8. **更新`/etc/fstab`**:为了避免系统重启后丢失挂载配置,应更新`/etc/fstab`文件,添加新分区的挂载信息。 9. **检查和测试**:重启虚拟机以确保配置正确,并再次使用`df -h`命令确认文件系统已经正确扩展。 通过上述步骤,你可以在虚拟机内部成功扩展文件系统分区,从而使用新增的硬盘空间。详细的操作和示例可以在提供的资料《Linux virsh KVM虚机硬盘扩容教程:步骤详解》中找到。该资料不仅涵盖了硬盘扩容的过程,还包括了文件系统分区扩展的详细操作,对于解决Linux环境下KVM虚拟机硬盘扩容问题具有极大的帮助。 参考资源链接:[Linux virsh KVM虚机硬盘扩容教程:步骤详解](https://wenku.csdn.net/doc/2fen83fkrf?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王格子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值