2016年6月2日 星期四

[Ubuntu] KVM+QEMU 使用筆記 (Ubuntu 14.04)

  • 安裝指令
    #apt-get install qemu-kvm qemu-system libvirt-bin bridge-utils

  • 建立Image檔
    qemu-img

    check[-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] filename

    create
    [-q] [-f fmt] [-o options] filename [size]

    commit [-q] [-f fmt] [-t cache] filename

    compare [-f fmt] [-F fmt] [-p] [-q] [-s] filename1 filename2

    convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename

    info [-f fmt] [--output=ofmt] [--backing-chain] filename

    map [-f fmt] [--output=ofmt] filename

    snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename

    rebase [-q] [-f fmt] [-t cache] [-p] [-u] -b backing_file [-F backing_fmt] filename

    resize [-q] filename [+ | -]size

    amend [-q] [-f fmt] -o options filename

    [-f fmt] : raw|qcow2|qcow|vmk|cloop

    Example:
    qemu-img create -f qcow2 linux.img 5G

  • Create Virtual Machine
    qemu-system-{cpu-architecute} [--enable-kvm]  [-smp options] [-boot dev] [-m size] [-cdrom iso] [-soundhw sdev] [-net nic] [-vnc conn] [-k keyboard] [-netdev options] [disk options] [-monitor options] [--enable-kvm]

    [--enable-kvm] : 啟動KVM能力,該功能CPU必須支援
        1. #egrep 'vmx|svm' /proc/cpuinfo

    [--smp options] :
        1. -smp [current allocation] [,maxcpus=n] [,sockets=n] [,cores=n] [,threads=n]     ( Example : -smp 2,maxcpus=4,sockets=4,cores=2,threads=2 )

    [-boot dev] : a, b (floppy 1 and 2), c (first hard disk), d (first CD-ROM), n-p (Etherboot from network adapter 1-4)

    [-m size] : memory size , default M

    [-cdrom iso] : 加入 cdrom

    [-soundhw sdev] : 加入 sound card

    [-vnc conn] :
        1. conn = host:port,password
        2. conn = host:port,tls,x509=/path
        3. conn = host:port,tls,x509verify=/path

    [-k keyboard]  keyboard layout

    [-netdev options]    
        1. -netdev nic [,vlan=n] [,macaddr=mac] [,model=type] [,name=name] [,addr=addr]
        2. -netdev tap [,vlan=n][,name=name][,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]
        3. -netdev bridge ,id=id[,br=bridge][,helper=helper] -net bridge[,vlan=n][,name=name][,br=bridge][,helper=helper]
        4. -netdev vhost-user,chardev=id[,vhostforce=on|off][,queues=n]
        5. -netdev none
     
    [nic] : 建立一個網路卡,並且連接至VLAN n ( n預設為0 )
    [tap] : 連接到host TAP的網路介面
    [bridge] : Bridge 模式,
    [vhost-user]
    [type] : [virtio|i82551|i82557b|i82559er|ne2k_pci|ne2k_isa|pcnet|rtl8139|e1000| smc91c111|lance|mcf_fec]

    [disk options] :
        1. -drive file=[path] ,if=none [,id=id_name] [,format=qow2] -device virtio-blk-pci,drive-virtio-disk0,id=virtio-disk0 [,bootindex=1]
        ( Example : -drive file=/images/ubuntu-14.04.img,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,drive=drive-virtio-disk0,id=virtio-disk-0,bootindex=1 )


    [-monitor options] :
        1. -monitor stdio
        2. -monitor telnet:[bind_ip]:[port] [,server] [,nowait] [,nodelay]
        ( Example : -monitor telnet::7000, server, nowait, nodelay )
       
    Example 1 : (安裝 Guest OS )
    qemu-system-x86_64 --enable-kvm -smp 2,maxcpus=4,sockets=4,cores=2,threads=2 linux.img -cdrom ubuntu-14.04-server-amd64.iso -boot d -vnc :0,password -k en-us -netdev nic -monitor stdio


  • Virtual Machine相關指令
    #telnet  host  port

    // 暫停/恢復/重置
    (qemu) stop  #暫停VM
    (qemu) cont #恢復VM
    (qemu) system_reset #重新啟動 VM
    (qemu) system_poweddown  #送ACPI Shutdown request
    (qemu) quit #關閉

    // Memory 相關
    (qemu) memsave    #儲存 Virutal memory 資料
    (qemu) pmemsave    #儲存 Physical memory 資料

    // Snapshot 相關
    (qemu) savevm name    #建立Snapshot
    (qemu) loadvm name    #恢復Snapshot
    (qemu) delvm    #刪除Snapshot
    (qemu) info snapshot    #顯示可用的Snapshot

  • NAT網路設定
    //-------Host OS 設定-------
    //安裝 tap tool

    #apt-get install uml-utilities

    //KVM Command
    #qemu-system-x86_64 linux.img --enable-kvm -vnc :0,password -k en-us -netdev nic -monitor stdio

    // 建立通道
    #tunctl -t tap0

    // 設定iptables
    #echo 1 > /proc/sys/net/ipv4/ip_forward
    #iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    #iptables -I FORWARD 1 -i tap0 -j ACCEPT
    #iptables -I FORWARD 1 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT

    //-------Guest OS 設定-------
    ip address : 192.168.100.2
    gateway : 192.168.100.1
    dns : 192.168.100.1

  • Bridge網路設定
    //-------Host OS 設定-------
    //安裝Bridge
    #apt-get install bridge-utils

    //建立Bridge
    #brctl addbr br0
    #brctl addif br0 eth0

    //KVM Command
    #qemu-system-x86_64 linux.img --enable-kvm -netdev tap,id=hostnet0,ifname=vnet0,script=/etc/qemu-ifup -device virtio-net-pci,netdev=hostnet0,id=net0 -vnc :0,password -k en-us -monitor stdio

  • 設定VLAN(802.1Q)
    VLAN示意圖
    要對某VM送出或進來的封包執行802.1Q,方法很簡單,只需要將VLAN和VNet0以起綁定至br0.2即可,如VLAN示意圖所示。

    //安裝vlan tools
    #apt-get install vlan

    //設定VLAN,假設VLAN為2
    #vconfig add eth0 2

    //建立新的Bridge,並且綁定eth0.2
    #brctl addbr br0.2
    #brctl addif br0.2 eth0.2

    //修改 /etc/qemu-ifup
    switch=br0.2
    for br in $switch; do
      if [ -d /sys/class/net/$br/bridge/. ];then
        brctl addif $br vnet0
      fi
    done

    //KVM Command
    #qemu-system-x86_64 linux.img --enable-kvm -netdev tap,id=hostnet0,ifname=vnet0,script=/etc/qemu-ifup -device virtio-net-pci,netdev=hostnet0,id=net0 -vnc :0,password -k en-us -monitor stdio


Reference:
[1] 架設Linux KVM虛擬化主機 (Set up Linux KVM virtualization host)
[2] QEMU Emulator User Documentation
[3] Chapter 14. Administrating Virtual Machines with QEMU Monitor
[4] 使用 monitor command 监控 QEMU 运行状态 
[5] Setting up QEMU with a NAT
[6] CentOS + KVM 建立 VLAN 的方式
[7]  KVM虚拟化技术之使用Qemu-kvm创建和管理虚拟机

沒有留言:

張貼留言