Kvm 虚拟化 ▪ 一

系统环境为Elementary os Freya(基于Ubuntu 14.04)

(一) 虚拟化环境搭建

     1 测试CPU是否支持虚拟化  

    egrep -o '(vmx|svm)' /proc/cpuinfo

           2 安装Kvm相关软件包

    sudo apt-get install qemu-kvm libvirt-bin virt-manager bridge-utils

     3 检测Kvm是否配置好 

    lsmod | grep kvm


(二) 安装Guest机 

    方法一:通过可视化窗口安装,通过命令qemu或者virt-manager即可进入窗口,点击新建,一步步创建即可;

    方法二:通过virsh命令安装:

        1:把iso镜像文件复制到/var/lib/libvirt/images

                               2 : 在/etc/libvirt/qemu目录下,创建qcow2磁盘文件,qcow2文件方便创建快照,命令:qemu-img create -f qcow2 ****.qcow2 10G(大小自己估计),

          3:在iso文件目录下,创建XML配置文件,内容如下,需要修改的已经在右侧注释说明:

 

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit xxxx
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>Test2</name>                                                             <!--Name -->
  <memory unit='KiB'>1740800</memory>
  <currentMemory unit='KiB'>1740800</currentMemory>                              <!--RAM -->
  <vcpu placement='static'>2</vcpu>                                              <!--the number of the virtual cpu-->
  <os>
    <type arch='i686' machine='pc-i440fx-trusty'>hvm</type>                      <!--iXXX x86_64  -->
    <boot dev='cdrom'/>                                                          <!-- the way to start the domain at the first ,usually cdrom-->
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='localtime'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>                                       <!-- the adress qemu-kvm -->
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/Alecyrus.qcow2'/>                     <!-- the file of disk ,QCOW2  not img : qemu-img create -f qcow2 xxxx.qcow2 xG -->
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/cd_Windows7.iso'/>                     <!-- the adress of ISO file,-->
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <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:37:cb:d7'/>                                           <!--Mac adress,important-->
      <source network='default'/>                                                  <!-- the model of network-->
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes'/>
    <sound model='ich6'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='vga' vram='9216' 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='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>
View Code

 


                               4:打开终端,输入以下命令

          virsh define ××××.xml  || virsh start ××××

        5 : 注意事项:因为是通过iso安装,所以引导方式那里 选择 cdrom ,否则 start后会显示 No Bootable devices

 (三)创建磁盘快照

  1 名词解释: 来源于Kashyap Chamarthy的文章

        内置快照 -- 单个qcow2镜像文件存储了包括数据以及快照的状态信息,

            内置快照又可以细分一下:-

                内置磁盘快照:

                快照点的磁盘状态,数据和快照保存在单个qcow2文件中,虚拟机运行状态和关闭状态都

                可以创建.

                        Libvirt 使用 'qemu-img' 命令创建关机状态的磁盘快照.
                        Libvirt 使用 'savevm' 命令创建运行状态的磁盘快照.

                内置系统还原点:

                内存状态,设备状态和磁盘状态,可以为运行中的虚拟机创建,所有信息都存储在

                同一个qcow2文件中,只有在运行状态才能创建内置系统还原点.

                        Libvirt 使用 'savevm' 命令来创建这种快照

        外置快照 -- 当一个快照被创建时,创建时当前的状态保存在当前使用

        的磁盘文件中,即成为一个backing file.

        此时一个新的overlay被创建出来保存往后的数据.

        这个也可以细分一下:-

                外置磁盘快照:

                磁盘的快照被保存在一个文件中,创建时间点以后的数据被记录到一个新的qcow2文件中.

                同样可以在运行和关闭状态创建.

                        Libvirt 使用 'transaction' 命令来为运行状态创建这种快照.
                        Libvirt 使用 'qemu-img' 命令为关闭状态创建这种快照(截止目前功能还在开发中).

                外置系统还原点(External system checkpoint):

                虚拟机的磁盘状态将被保存到一个文件中,内存和设备的状态将被保存到另外一个新的文件中,

                (这个功能也还在开发中).

        VM状态(VM state):

        保存运行状态虚拟机的内存设备状态信息至文件,可以通过此文件恢复到保存时的状态,有点类似系统

        的休眠.(注意创建VM状态保存的时候VM磁盘必须是未发生写入改动的)

                Libvirt使用 'migrate' (to file)命令来完成VM状态转储.

  

   2 快照的创建

    内置磁盘快照的创建 ,假设为guest机 Ale 创建一个名为Ale_shot1的内置磁盘快照

        # virsh snapshot-create-as Ale Ale_shot1

      外置磁盘快照的创建,假设为guest机 Ale 创建一个名为Ale_shot2的外置磁盘快照

        # virsh destroy ppd_centos   //关机

        # virsh snapshot-create-as Ale Ale_shot2 "snap1 description" --disk-only --atomic

    查看磁盘快照列表

        # virsh snapshot-list Ale

    显示磁盘快照详细信息

        # virsh qemu-img info Ale.qcow2

    查看虚拟机磁盘列表

        # virsh domblklist Ale

      删除快照

        # virsh snapshot-delete Ale_shot1

         

 

posted @ 2015-07-16 15:08  路则  阅读(269)  评论(0编辑  收藏  举报