使用libvirt的xml配置文件创建虚拟机

1. 前言

kvm支持的镜像很多,常用的是原始镜像(*.img),还有支持动态大小扩张的qocw2格式(首选)。
更优的选择是系统盘如C盘用img格式,数据盘用qcow2格式以减少服务器磁盘闲置空间。
本文仅记录如何用ubuntu.iso制作系统镜像ubuntu.qcow2并创建启动虚拟机

2. 本文分析内容安排

  • 制作虚拟机镜像
  • 使用制作好的虚拟机镜像启动虚拟机

3. 制作虚拟机镜像

1、创建qcow2镜像,但是其实际占有磁盘大小仅为193K左右,而虚拟机内部显示磁盘大小为10G,也就是磁盘空间使用时才分配,即所谓动态扩张。

qemu-img create -f qcow2 ubuntu.qcow2 10G     

2、下载并复制ubuntu的iso镜像到指定目录,本文将所有镜像及配置文件放到 /home/createvm 目录下

2.1 创建配置文件setup.xml,内容如下

<domain type='kvm'> 
    <name>test_ubuntu</name>                     //虚拟机名称 
    <memory>1048576</memory>                  //最大内存 
    <currentMemory>1048576</currentMemory>    //可用内存 
    <vcpu>1</vcpu>                                                      //虚拟cpu个数 
    <os> 
      <type arch='x86_64' machine='pc'>hvm</type> 
      <boot dev='cdrom'/>                                           //光盘启动 
   </os> 
   <features> 
     <acpi/> 
     <apic/> 
     <pae/> 
   </features> 
   <clock offset='localtime'/> 
   <on_poweroff>destroy</on_poweroff> 
   <on_reboot>restart</on_reboot>   
   <on_crash>destroy</on_crash> 
   <devices> 
     <emulator>/usr/libexec/qemu-kvm</emulator> 
     <disk type='file' device='disk'> 
      <driver name='qemu' type='qcow2'/>            //此处关键,要求libvirt版本至少应该在0.9以上才能支持,libvirt版本升级http://blog.csdn.net/gg296231363/article/details/6891460 
       <source file='/home/createvm/ubuntu.qcow2'/>         //目的镜像路径 
       <target dev='hda' bus='ide'/> 
     </disk> 
     <disk type='file' device='cdrom'> 
       <source file='/home/createvm/ubuntu.iso'/>              //光盘镜像路径 
       <target dev='hdb' bus='ide'/> 
     </disk> 
    <interface type='bridge'>                                               //虚拟机网络连接方式 
      <source bridge='br0'/> 
      <mac address="00:16:3e:5d:aa:a8"/>    //为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突 
    </interface> 
    <input type='mouse' bus='ps2'/> 
     <graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>//vnc方式登录,端口号自动分配,自动加1 
   </devices> 
</domain> 

2.2 virsh define setup.xml //创建虚拟机 查看当前系统所有的虚拟机信息:virsh list --all
2.3 virsh start test_ubuntu //启动虚拟机
2.4 安装xmanager,并设置将xshell隧道转到xmanager,在xshell中输入virt-manager启动界面程序,启动后能看到操作系统安装的初始界面,开始安装系统,安装完成即表示镜像制作完成

4. 使用制作好的虚拟机镜像启动虚拟机

  1. 做完上一步说明操作系统已经安装完成,下一步就是启动并进入操作系统了,启动之前先关闭并销毁为安装虚拟机建立的xml
    virsh shutdown test_ubuntu //关闭虚拟机
    virsh undefine test_ubuntu //删除虚拟机
  2. 创建文件start.xml,这里主要是将上setup.xml中的启动盘由cdrom改为hd,否则启动虚拟机后依然会再次执行安装操作系统给的过程,其他的没有改变,内容如下:
<domain type='kvm'> 
<name>test_ubuntu</name> 
<memory>1048576</memory> 
<currentMemory>1048576</currentMemory> 
<vcpu>1</vcpu> 
<os> 
<type arch='x86_64' machine='pc'>hvm</type> 
<boot dev='hd'/>     //即harddisk,从磁盘启动 
</os> 
<features> 
<acpi/> 
<apic/> 
<pae/> 
</features> 
<clock offset='localtime'/> 
<on_poweroff>destroy</on_poweroff> 
<on_reboot>restart</on_reboot> 
<on_crash>destroy</on_crash> 
<devices> 
<emulator>/usr/libexec/qemu-kvm</emulator> 
<disk type='file' device='disk'> 
<driver name='qemu' type='qcow2'/> 
<source file='/home/createvm/ubuntu.qcow2'/> //目的镜像路径 
<target dev='hda' bus='ide'/> 
</disk> 
<disk type='file' device='cdrom'> 
<source file='/home/createvm/ubuntu.iso'/> //光盘镜像路径 
<target dev='hdb' bus='ide'/> 
</disk> 
<interface type='bridge'> 
<source bridge='br0'/> 
<mac address="00:16:3e:5d:aa:a8"/> 
</interface> 
<input type='mouse' bus='ps2'/> 
<graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/> 

</devices> 
</domain> 

3 使用制作好的镜像和start.xml配置文件来创建并启动虚拟机。
virsh define start.xml
virsh start test_ubuntu
启动后可以通过virt-manager再次进入虚拟机来设置好ip地址,之后就可以通过ssh登录了

5. 作者介绍

梁明远,国防科大并行与分布式计算国家重点实验室(PDL)应届研究生,14年入学伊始便开始接触docker,准备在余下的读研时间在docker相关开源社区贡献自己的代码,毕业后准备继续从事该方面研究。邮箱:liangmingyuanneo@gmail.com

6. 转自

http://javakill.iteye.com/blog/1945975

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值