KVM学习笔记

1.查看主机是否支持KVM,egrep -c '(vmx|svm)' /proc/cpuinfoubuntu

2.若是单台虚机的vCPU数量大于物理vCPU的数量,会带来明显性能损失。windows

3.VirtualBox建立的虚拟机目前不支持Inter VT-x/EPT等虚拟化,因此直接使用vmvare建立虚拟机,而后安装kvm便可。使用qemu建立虚拟机时,必定要启用-enable-kvm,会带来明显的性能提高。api

4.运行一台虚拟机:bash

qemu-system-x86_64 -m 1024 -smp 1 -enable-kvm -boot order=cd -hda ubuntu-server.qcow2 -cdrom ubuntu-16.04-server-amd64.iso -monitor stdio -net nic -net user,hostfwd=::5022-:22,hostfwd=::5080-:80 -net dump -vnc :2 -device piix3-usb-uhci -device usb-tablet

windows虚机能够加 -balloon virtio -device virtio-serial-pci 用于虚拟virtio相关的设备,-net nic,model=virtio网络

-m 内存MBdom

-smp vCPU数量tcp

-boot order=cd 即启动顺序为先是硬件磁盘,再是cdrom光盘。(c:hda,d:cdrom)ide

-hda file 表示使用文件做为系统的第0块IDE磁盘,hdb,hdc,hdd分别表示第1-3块磁盘。使用咱们制做好的磁盘文件。性能

-cdrom file 使用文件做为光盘镜像。测试

-monitor stdio 在标准输入输出上开启qemu monitor

-net nic 分配一张网卡

-net tap 表示启用桥接模式的网络,qemu会检测当前系统的ip route找到brX网卡接口

没有任何-net参数时,qemu默认使用-net nic 。桥接网络须要配置桥接网卡如br0.

-net user表示使用用户模式的网络。其本身实现了TCP/IP协议栈。hostfwd表示端口转发。将虚机的端口转发到宿主机的端口。

-net dump 至关于对虚机执行tcpdump

-vnc :2 表示开启VNC界面,不使用SDL显示虚机的图形界面。VNC server的端口为5900+2,使用vncviwer访问这个端口便可。

-device piix3-usb-uhci -device usb-tablet 解决VNC中的鼠标偏移问题

5.建立一个空镜像文件:qemu-img create -f qcow2 xxxx.qcow2 20

最后一个参数表示镜像文件大小。QCOW2格式的镜像为动态增加大小。初始大小为0

6.查看QEMU支持的全部cpu模型:qemu-system-x86_64 -cpu ?

7.可用的物理内存空间和交换空间的大小之和应该等于或大于配置给全部客户机的内存总和,不然,在各个客户机内存使用同时达到较高比率时可能会有客户机因内存不足被强制关闭。

8.磁盘挂载:ls -al /dev/disk/by-uuid  查看磁盘的UUID

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=aca02c5d-6cd5-4d44-99c3-01254abc3cda /               ext4    errors=remount-ro 0       1


<1> fdisk -l 查看可用磁盘,假如可用磁盘是/dev/sda

<2> fdisk /dev/sda编辑分区信息,p查看分区表,w保存分区表,n新建分区

<3> mount /dev/sda4 /mnt 将分区挂载到挂载点

<4>编辑 /etc/fstab 将挂载命令开机执行

<5> mount -a 验证fstab文件是否正确。

9.换光盘问题,在qemu monitor界面下,输入

info block

获得光盘的设备名如ide1-cd0:

ide0-hd0 (#block160): win10.qcow2 (qcow2)
    Cache mode:       writeback

ide1-cd0 (#block379): /dev/cdrom (raw, read-only)
    Removable device: not locked, tray closed
    Cache mode:       writeback

<1>弹出光驱:

eject ide1-cd0

<2>插入新光盘:change ide1-cd0 newCdrom.iso或者/dev/cdrom,/dev/sr0

10.虚机迁移:

<1> 原主机根据镜像文件建立增量镜像文件:qemu-img create -f qcow2 -o backing_file=ubuntu-server.qcow2,size=20G ubuntu-instance1.qcow2   注意backing_file即为基础的镜像文件,ubuntu-instance2.qcow2为增量镜像文件。

<2>目的主机:一样建立同一个backing_file的镜像文件,

qemu-img create -f qcow2 -o backing_file=ubuntu-server.qcow2,size=20G ubuntu-instance2.qcow2

 

<3>运行目的主机打开其监听状态:

qemu-system-x86_64 -m 1024 -smp 1,maxcpus=8 -enable-kvm -boot order=cd -drive file=ubuntu-instance2.qcow2,if=virtio -cdrom /dev/cdrom -monitor stdio -net nic,model=virtio -net user  -net dump -vnc :3 -balloon virtio -device virtio-serial-pci -incoming tcp:0:6666

  注意-incoming tcp:0:6666 表示监听0.0.0.0:6666端口。

<4>在源主机的qemu monitor中执行migrate -i tcp:127.0.0.1:6666,等待增量执行完成。源主机会被suspend

11.启动虚机时添加-cpu host参数能够支持KVM嵌套虚拟化。-cpu host表示将宿主机的cpu暴露给客户机使用,可是存在动态迁移的问题。所以建议使用-cpu qemu64,+vmx来激活vmx特性。其余cpu特性同理。

12.在宿主机中,开启KSM(Kennel SamePage Merging)内核同页合并以减小物理内存的使用,该服务扫描QEMU进程中相同的内存而后合并,若是运行的虚拟机操做系统相似可使用该服务减小内存。开启: echo 1>/sys/kernel/mm/ksm/run

13.libvirt xml示例:

命令行:

qemu-system-x86_64 -m 1024 -smp 1,maxcpus=8 -enable-kvm -boot order=cd -drive file=win10.qcow2,if=virtio -cdrom /dev/cdrom -monitor stdio -net nic,model=virtio -net user -vnc :1 -device piix3-usb-uhci -device usb-tablet -balloon virtio -device virtio-serial-pci
<domain type="kvm" xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
    <name>win2008</name>
    <uuid>825a2ede-0aca-47b6-b0c8-aebe70f5180e</uuid>
    <os>
        <type>hvm</type>
        <boot dev="hd"/>
        <boot dev="cdrom"/>
    </os>
    <vcpu placement='static'>1</vcpu>
    <memory unit='KiB'>1048576</memory>
    <currentMemory unit='KiB'>1048576</currentMemory>

    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>restart</on_crash>
    <on_lockfailure>poweroff</on_lockfailure>

    <features>
        <pae/>
        <acpi/>
        <apic/>
    </features>

    <clock offset='utc'/>

    <devices>
          <emulator>/usr/bin/kvm-spice</emulator>
          <disk type='file' device='disk'>
            <driver name='qemu' type='qcow2'/>
               <source file='/home/denglei/qemu/win10.qcow2'/>
            <target dev='vda' bus='virtio'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
          </disk>
          
          <controller type='usb' index='0'>
              <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
          </controller>
          <controller type='ide' index='0'>
              <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
          </controller>

        <interface type='network'>
          <mac address='52:54:00:bd:f9:55'/>
          <source network='default'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
        </interface>
        <serial type="pty">
            <target port='0'/>
        </serial>
        <console type="pty">
            <target port='0'/>
        </console>
        <input type="tablet" bus="usb"/>
        <input type="mouse" bus="ps2"/>
        <graphics type="vnc" port="5904" listen="0.0.0.0"/>

         <sound model='ich6'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
        </sound>
        <video>
          <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
        </video>

        <memballoon model='virtio'>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
        </memballoon>

    </devices>
</domain>


虚拟机磁盘读写测试。

本地宿主机测试的磁盘读写

14.新的命令的书写(网络部分),同时建立多台虚拟机会出现mac地址相同的状况,所以给虚机网卡指定ip:

#!/bin/sh
no=$2
qemu-system-x86_64 -m 1024 -smp 1,maxcpus=8 -enable-kvm -boot order=cd -drive file=$1,if=virtio -cdrom /dev/cdrom -monitor stdio\
 -netdev tap,id=net$2\
 -device e1000,netdev=net$2,mac=`echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"'`\
 -vnc :$2 -balloon virtio\
 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5\
相关文章
相关标签/搜索