1. 硬盘扩容
qcow2格式磁盘,直接通过qemu-img 直接扩展qcow2磁盘, 新添加一块qcow2 格式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。
 
  
添加一块qcow2磁盘
[root@yangrz01 isos]# qemu-img create -f qcow2  test01_add.qcow2 2G
Formatting 'test01_add.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 

 

 
添加一块qcow2磁盘信息加入配置文件
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/home/work/isos/test2.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </disk>
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/home/work/isos/test01_add.qcow2'/>
      <target dev='vdb' bus='virtio'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>    <disk type='block' device='cdrom'>
启动虚拟机并配置磁盘
[root@localhost ~]# fdisk -l

 
Disk /dev/vda: 7516 MB, 7516192768 bytes
16 heads, 63 sectors/track, 14563 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: 0x0006d29d

 
   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       14564     6827008   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

 
Disk /dev/vdb: 2147 MB, 2147483648 bytes
16 heads, 63 sectors/track, 4161 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
发现新增的磁盘已经添加成功。
配置lvm扩展磁盘:
分区
[root@localhost ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4e991999.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4161, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-4161, default 4161): 
Using default value 4161

 
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

 
Command (m for help): w
The partition table has been altered!

 
Calling ioctl() to re-read partition table.
Syncing disks.
重读分区表
[root@localhost ~]# partx -a /dev/sdb
发现重读分区表可能会失败,查到的原因可能是因为虚拟机的缘故,条件允许可以重启虚拟机即可

 

 
扩展逻辑卷
[root@localhost ~]# pvcreate /dev/vdb1
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  5.8G  757M  4.7G  14% /
tmpfs                         246M     0  246M   0% /dev/shm
/dev/vda1                     485M   32M  428M   7% /boot
[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize PFree
  /dev/vda2  VolGroup lvm2 a--  6.51g    0 
  /dev/vdb1           lvm2 a--  2.00g 2.00g
[root@localhost ~]# vgextend VolGroup /dev/vdb1
  Volume group "VolGroup" successfully extended
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize VFree
  VolGroup   2   2   0 wz--n- 8.50g 2.00g
[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root 
  Extending logical volume lv_root to 7.80 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]# resize2fs  /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 2045952 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 2045952 blocks long.

 
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  7.7G  758M  6.6G  11% /
tmpfs                         246M     0  246M   0% /dev/shm
/dev/vda1                     485M   32M  428M   7% /boot

 
df -h结果发现,磁盘扩容成功。

 

 
内存和CPU扩容,都是扩容完成后重启虚拟机即可生效。
CPU扩容
编辑虚拟机配置文件
[root@yangrz01 ~]# virsh edit oeltest02
  <vcpu placement='static'>1</vcpu>   
也可以改为自动扩容:
current="1">4</vcpu>  #当前为1,自动扩容最大为4

 

 
内存扩容
[root@yangrz01 ~]# virsh edit oeltest02
<memory unit='KiB'>4194304</memory>  #修改最大支持内存扩容为4G
<currentMemory unit='KiB'>1048576</currentMemory> #当前KVM虚拟机内存为1G
需要修改内存时,直接修改这两处即可。