zoukankan      html  css  js  c++  java
  • kvm日常管理

    创建虚拟机

    快速启动虚拟机

    [root@localhost ~]# yum install kvm libvirt python-virtinst qemu-kvm virt-viewer bridge-utils virt-install -y
    [root@localhost ~]# systemctl start libvirtd
    [root@localhost ~]# systemctl enable libvirtd
    [root@localhost ~]# vim /etc/libvirt/qemu.conf 
    vnc_listen = "0.0.0.0"
    [root@localhost ~]# qemu-img create -f qcow2 /data/system-ubuntu.img 20G
    
    virt-install --name ubuntu14.4 --boot hd,cdrom 
    --virt-type kvm --ram 1024 --vcpus 2 --network=default 
    --cdrom=/data/ubuntu-14.04.3-desktop-amd64.iso 
    --disk path=/data/system-ubuntu.img  
    --graphics vnc,password=root,port=5910 --noautoconsole 
    

      

    桥接网络

    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 #设置eth0为桥接模式
    BOOTPROTO=none
    DEVICE="eth0"
    NM_CONTROLLED=no
    ONBOOT="yes"
    BRIDGE=br0
    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 #新建网桥配置文件
    BOOTPROTO=static
    TYPE=Bridge
    DEVICE=br0
    NM_CONTROLLED=no
    IPADDR=192.168.100.129
    NETMASK=255.255.255.0
    GATEWAY=192.168.100.2
    DNS1=10.0.0.51
    [root@localhost ~]# service network restart
    
    
    
    virt-install --name ubuntu14.4 
    --virt-type kvm --ram 1024 --vcpus 2 --network bridge:br0  
    --cdrom=/data/ubuntu-14.04.3-desktop-amd64.iso 
    --disk path=/data/system-ubuntu.img  
    --graphics vnc,password=root,port=5910 --noautoconsole
    

      

    快照

    [root@localhost ~]# virsh snapshot-create-as ubuntu14.4 16-03  #为ubuntu14.4创建名为16-03的快照 
    [root@localhost ~]# virsh snapshot-list ubuntu14.4  #查看快照列表
     Name                 Creation Time             State
    ------------------------------------------------------------
     16-03                2017-04-14 16:03:59 +0800 running
    [root@localhost ~]# qemu-img info /data/system-ubuntu.img 
    image: /data/system-ubuntu.img
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 1.9G
    cluster_size: 65536
    Snapshot list:
    ID        TAG                 VM SIZE                DATE       VM CLOCK
    1         16-03                  956M 2017-04-14 16:03:59   00:45:32.590
    [root@localhost ~]# virsh snapshot-revert ubuntu14.4 16-03 #还原快照
    [root@localhost ~]# virsh snapshot-delete ubuntu14.4 16-03 #删除快照
    

      

    新建nat网络

    [root@localhost ~]# vim /usr/share/libvirt/networks/net1.xml
    <network>
      <name>net1</name>
      <bridge name="virbr2"/>
      <forward/>
      <ip address="192.168.10.254" netmask="255.255.255.0">
        <dhcp>
          <range start="192.168.10.100" end="192.168.10.200"/>
        </dhcp>
      </ip>
    </network>
    
    [root@localhost ~]# virsh net-define /usr/share/libvirt/networks/net1.xml
    [root@localhost ~]# virsh net-start net1
    [root@localhost ~]# virsh net-autostart net1
    [root@localhost ~]# virsh net-list
     Name                 State      Autostart     Persistent
    ----------------------------------------------------------
     default              active     yes           yes
     net1                 active     yes           yes
    

      

    virsh 常用命令

    virsh list #显示活动虚拟
    virsh list –all #显示所有的虚拟机
    virsh define vm-name.xml   #通过配置文件定义一个虚拟机(可以理解为导入配置文件)
    virsh start vm-name        #启动虚拟机
    virsh create vm-name       #创建虚拟机
    virsh suspend vm-name      #暂停虚拟机
    virsh resume vm-name       #启动暂停的虚拟机
    virsh shutdown vm-name     #正常关闭虚拟机
    virsh destroy vm-name      #强制关闭虚拟机
    virsh undefine vm-name     #删除虚拟机
    virsh dominfo vm-name      #显示虚拟机的基本信息
    virsh domname vm-id         #显示对应id虚拟机名
    virsh domid vm-name         #显示虚拟机id号
    virsh domuuid vm-name       #显示虚拟机的uuid
    virsh domstate vm-name      #显示虚拟机的当前状态
    virsh dumpxml vm-name       #显示虚拟机的当前配置文件
    virsh setmem vm-name 512M #动态调整内存大小(不能大于最大内存)
    virsh setmaxmem             #设置最大内存(非活动)
    virsh setvcpus vm-name 4    #给虚拟机设置cpu个数
    virsh edit vm-name          #编辑配置文件
    virsh save	                #存储虚拟机的状态
    virsh restore	            #恢复虚拟机的状态
    virsh autostart vm-name      #虚拟机开机启动
    virsh autostart --disable vm-name  #虚拟机开机启动
    

      

      

      

  • 相关阅读:
    2021.4.4(每周总结)
    2021.4.2
    2021.4.1
    2021.3.31
    2021.3.30
    2021.3.29
    2021.3.28(每周总结)
    2021.3.26
    C语言中指针与取地址符&详解
    使用JDBC连接、操作数据库、实现数据处理
  • 原文地址:https://www.cnblogs.com/37yan/p/6879393.html
Copyright © 2011-2022 走看看