kvm install

kvm install

  1. 查看CPU是否支持svm和vmx

    1
    [root@localhost app]# egrep "svm|vmx" /proc/cpuinfo
  2. 安装依赖软件包

    1
    2
    3
    [root@localhost app]# yum -y install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install
    [root@localhost app]# systemctl start libvirtd
    [root@localhost app]# systemctl enable libvirtd
  3. kvm检查

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //检查kvm模块是否安装
    [root@localhost app]# lsmod | grep kvm
    kvm_intel 53484 0
    kvm 316506 1 kvm_intel

    //检查kvm是否安装成功
    [root@localhost app]# virsh list --all

    //查看版本
    [root@localhost app]# kvm --version
    [root@localhost app]# virt-install --version
    [root@localhost app]# virsh --version
  4. kvm网络配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@localhost app]# vim /etc/sysconfig/network-scripts/ifcfg-em1
    BRIDGE=br0

    [root@localhost app]# vim /etc/sysconfig/network-scripts/ifcfg-br0
    DEVICE="br0"
    BOOTPROTO="dhcp"
    IPV6INIT="yes"
    IPV6_AUTOCONF="yes"
    ONBOOT="yes"
    TYPE="Bridge"
    DELAY="0"

    [root@localhost app]# echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf
    [root@localhost app]# sysctl -p

    [root@localhost app]# systemctl restart NetworkManager
  5. 创建vm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@localhost app]# virt-install \
    --network bridge:br0 \
    --name vm1 \
    --ram=1024 \
    --vcpus=1 \
    --disk path=/vm-images/vm1.img,size=10 \
    --graphics none \
    --location=http://my.server.com/pub/rhel7/install-x86_64/ \
    --extra-args="console=tty0 console=ttyS0,115200"
  6. 克隆vm

    1
    2
    3
    4
    5
    6
    7
    8
    [root@localhost app]# virsh suspend vm1
    [root@localhost app]# virt-clone \
    --connect qemu:///system \
    --original vm1 \
    --name vm1-clone \
    --file /vm-images/vm1-clone.img
    [root@localhost app]# virsh resume vm1
    [root@localhost app]# virsh start vm1-clone
  7. manage vm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@localhost app]# virsh list --all
    [root@localhost app]# virsh dominfo vm1
    [root@localhost app]# virt-top
    [root@localhost app]# virt-df vm1
    [root@localhost app]# virsh shutdown vm1
    [root@localhost app]# virsh start vm1
    [root@localhost app]# virsh autostart vm1
    [root@localhost app]# virsh autostart –disable vm1
    [root@localhost app]# virsh console vm1
  8. attaching storage device to a vm

    1
    2
    [root@localhost app]# virsh attach-disk vm1 /dev/sdb vdb --driver qemu --mode shareable
    [root@localhost app]# virsh detach-disk vm1 vdb
  9. set memory

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@localhost app]# virsh dominfo vm1 | grep memory
    [root@localhost app]# virsh setmem vm1 524288
    [root@localhost app]# virsh dominfo vm1 | grep memory

    or
    [root@localhost app]# virsh shutdown vm1
    [root@localhost app]# virsh edit vm1
    <memory>2097152</memory>
    [root@localhost app]# virsh create /etc/libvirt/qemu/vm1.xml
    [root@localhost app]# virsh dominfo vm1 | grep memory
    [root@localhost app]# virsh setmem vm1 2097152
    [root@localhost app]# virsh dominfo vm1 | grep memory
  10. set vcpu

    1
    2
    3
    4
    [root@localhost app]# virsh shutdown vm1
    [root@localhost app]# virsh edit vm1
    <vcpu>2</vcpu>
    [root@localhost app]# virsh create /etc/libvirt/qemu/vm1.xml
  11. Disk capacity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@localhost app]# dd if=/dev/zero of=/vm-images/vm1-add.img bs=1M count=10240
    [root@localhost app]# virsh shutdown vm1
    [root@localhost app]# virsh edit vm1
    <disk type='file' device='disk'>
    <driver name='qemu' type='raw' cache='none' io='threads'/>
    <source file='/vm-images/vm1.img'/>
    <target dev='vda' bus='virtio'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x04'
    function='0x0'/>
    </disk>
    <disk type='file' device='disk'>
    <driver name='qemu' type='raw' cache='none' io='threads'/>
    <source file='/vm-images/vm1-add.img'/>
    <target dev='vdb' bus='virtio'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x06'
    function='0x0'/>
    </disk>
    [root@localhost app]# virsh create /etc/libvirt/qemu/vm1.xml
  12. Deleting VMs

    1
    2
    3
    4
    5
    [root@localhost app]# virsh shutdown vm1-clone
    [root@localhost app]# virsh destroy vm1-clone

    [root@localhost app]# virsh undefine vm1-clone
    [root@localhost app]# rm /vm-images/vm1-clone.img