KVM虚拟机扩容硬盘

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

一、当前硬盘扩容

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)
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
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小王格子

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值