如何给KVM虚拟机添加disk, cpu, 内存, 网卡设备

2 minute read

背景

本文主要描述一下如何给虚拟机添加硬盘, CPU, 网卡.

例子

添加硬盘

创建硬盘文件

# qemu-img create -f qcow2 -o encryption=off,cluster_size=2M,preallocation=full /data03/kvmdisk/disk02.img 10G  
Formatting '/data03/kvmdisk/disk02.img', fmt=qcow2 size=10737418240 encryption=off cluster_size=2097152 preallocation='full'   
  
[root@db-172-16-3-150 schemas]# virsh  
Welcome to virsh, the virtualization interactive terminal.  
  
Type:  'help' for help with commands  
       'quit' to quit  

编辑虚拟机配置

virsh # edit centos6_6_x64  

在第一块硬盘配置下面添加, 注意文件名, 硬盘名称, 注意slot不要和其他所有项冲突

缓存自由配置. type可以选择raw或qcow2. 数据库建议使用raw格式. 测试时为了性能,你可以使用writeback模式(但是fsync不能保证)。

    <disk type='file' device='disk'>  
      <driver name='qemu' type='qcow2' cache='writethrough'/>  
      <source file='/data03/kvmdisk/disk02.img'/>  
      <target dev='vdb' bus='virtio'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>  
    </disk>  

xml全部如下 :

virsh # dumpxml centos6_6_x64  
<domain type='kvm' id='6'>  
  <name>centos6_6_x64</name>  
  <uuid>4c613d4e-716b-f2cb-4df3-09bc7779f7df</uuid>  
  <memory unit='KiB'>4194304</memory>  
  <currentMemory unit='KiB'>4194304</currentMemory>  
  <vcpu placement='static'>4</vcpu>  
  <os>  
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>  
    <boot dev='hd'/>  
  </os>  
  <features>  
    <acpi/>  
    <apic/>  
  </features>  
  <cpu mode='host-model'>  
    <model fallback='allow'/>  
  </cpu>  
  <clock offset='utc'/>  
  <on_poweroff>destroy</on_poweroff>  
  <on_reboot>restart</on_reboot>  
  <on_crash>restart</on_crash>  
  <devices>  
    <emulator>/usr/libexec/qemu-kvm</emulator>  
    <disk type='file' device='disk'>  
      <driver name='qemu' type='raw' cache='writethrough'/>  
      <source file='/data03/kvmdisk/disk01.img'/>  
      <target dev='vda' bus='virtio'/>  
      <alias name='virtio-disk0'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>  
    </disk>  
    <disk type='file' device='disk'>  
      <driver name='qemu' type='qcow2' cache='writethrough'/>  
      <source file='/data03/kvmdisk/disk02.img'/>  
      <target dev='vdb' bus='virtio'/>  
      <alias name='virtio-disk1'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>  
    </disk>  
    <controller type='usb' index='0'>  
      <alias name='usb0'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>  
    </controller>  
    <interface type='bridge'>  
      <mac address='52:54:00:76:ac:2b'/>  
      <source bridge='virbr0'/>  
      <target dev='vnet0'/>  
      <model type='virtio'/>  
      <alias name='net0'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>  
    </interface>  
    <serial type='pty'>  
      <source path='/dev/pts/3'/>  
      <target port='0'/>  
      <alias name='serial0'/>  
    </serial>  
    <console type='pty' tty='/dev/pts/3'>  
      <source path='/dev/pts/3'/>  
      <target type='serial' port='0'/>  
      <alias name='serial0'/>  
    </console>  
    <memballoon model='virtio'>  
      <alias name='balloon0'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>  
    </memballoon>  
  </devices>  
</domain>  

重启虚拟机 :

virsh # shutdown centos6_6_x64  
Domain centos6_6_x64 is being shutdown  
  
virsh # start centos6_6_x64  
Domain centos6_6_x64 started  

连接到console进行验证是否已经添加硬盘 :

virsh # console centos6_6_x64  
  
CentOS release 6.6 (Final)  
Kernel 2.6.32-504.el6.x86_64 on an x86_64  
  
digoal.sky-mobi.com login: root  
Password:   
Last login: Thu Apr  2 02:01:13 on ttyS0  
[root@digoal ~]# fdisk -l  
  
Disk /dev/vda: 34.4 GB, 34370224128 bytes  
255 heads, 63 sectors/track, 4178 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: 0x0001bf14  
  
   Device Boot      Start         End      Blocks   Id  System  
/dev/vda1   *           1        3698    29696000   83  Linux  
/dev/vda2            3698        3959     2097152   82  Linux swap / Solaris  
  
Disk /dev/vdb: 10.7 GB, 10747904000 bytes  
16 heads, 63 sectors/track, 20825 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: 0x00000000  

修改CPU

直接修改xml并重启,

  <vcpu placement='static'>4</vcpu>  

略.

添加网卡

直接修改xml并重启, 注意mac地址, slot不要冲突.

略 :

    <interface type='bridge'>  
      <mac address='52:54:00:76:ac:2c'/>  
      <source bridge='virbr0'/>  
      <model type='virtio'/>  
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>  
    </interface>  

检验 :

[root@digoal ~]# ethtool eth0  
Settings for eth0:  
        Link detected: yes  
[root@digoal ~]# ethtool eth1  
Settings for eth1:  
        Link detected: no  

Flag Counter

digoal’s 大量PostgreSQL文章入口