美文网首页
KVM在ubuntu中用img模板装虚拟机

KVM在ubuntu中用img模板装虚拟机

作者: 卓尔忏悔者 | 来源:发表于2017-11-08 15:54 被阅读0次

1、修改以太网卡为桥接

编辑网卡配置文件

$ vim /etc/network/interfaces

改为以下内容,以将名为enp1s0f0的网卡改成桥接网卡
(具体数值配置请根据实际情况)

auto lo
iface lo inet loopback
auto enp1s0f0
iface enp1s0f0 inet manual
auto br0
iface br0 inet static
address 192.168.100.29
netmask 255.255.255.0
gateway 192.168.100.1
dns-nameservers 114.114.114.114
bridge_ports enp1s0f0
bridge_stp off
bridge_fs 0

2、安装KVM相关插件

apt-get install virtinst python-libvirt virt-viewer virt-manager qemu-kvm -y

安装之前需要配置好下载源,配置文件:/etc/apt/sources.list

3、配置安装虚拟机

需要一份img模板文件,和一份xml配置文件。

xml文件模板:

<domain type='kvm' id='26'>
  <name>centos2</name>
  <uuid>99dfa927-ef12-4a66-bba7-327208cf2e5b</uuid>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-xenial'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Westmere</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/data/kvm/centos/CentOS6.5_x64-template.img'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <alias name='usb'/>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <alias name='usb'/>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <alias name='usb'/>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:27:02:24:4d:93'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/4'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/4'>
      <source path='/dev/pts/4'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-centos1/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0' state='disconnected'/>
      <alias name='channel0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0' state='disconnected'/>
      <alias name='channel1'/>
      <address type='virtio-serial' controller='0' bus='0' port='2'/>
    </channel>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
      <image compression='off'/>
    </graphics>
    <sound model='ich6'>
      <alias name='sound0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir1'/>
    </redirdev>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-99dfa945-ef10-4a66-bba7-327208cf2e5a</label>
    <imagelabel>libvirt-99dfa945-ef10-4a66-bba7-327208cf2e5a</imagelabel>
  </seclabel>
</domain>

需要自定义的地方:

name 虚拟机名称
uuid 虚拟机ID,每台都不同
source file 你的img文件(每台虚拟机对应一个img文件,需要复制使用)
mac address 虚拟机的mac地址
graphics type 一般用VNC,端口可以自己设,也可以自动分配

修改完配置文件后,新建并打开虚拟机:

$ virsh define myVM.xml
$ virsh start myVM

新建的虚拟机还没有IP,用物理机IP+配置graphics type时的VNC端口可以进去虚拟机配置。
也可以用virt-manager工具,进行图形化配置。

相关文章

  • KVM在ubuntu中用img模板装虚拟机

    1、修改以太网卡为桥接 编辑网卡配置文件 改为以下内容,以将名为enp1s0f0的网卡改成桥接网卡(具体数值配置请...

  • centos7.3镜像制作

    在 KVM 环境上准备虚拟机磁盘 [root@localhost ~]# qemu-img create -f q...

  • 如何使用qcow2创建虚拟机

    kvm虚拟机安装 参考已下资料KVM虚拟机在Ubuntu16.04下的环境搭建 安装所需软件 配置网络桥接 修改完...

  • Kvm之九:vm迁移

    Centos7 Kvm 虚拟机迁移 1、虚拟机静态迁移 2、准备disk img和xml配置等文件 3、目标主机将...

  • 记一次虚机扩容

    本人工作用的ubuntu,由于工作中会用到QQ,钉钉等交流软件,遂在ubuntu中用kvm虚了个win7在用。当时...

  • kvm实践

    esxi安装部署 参考文档 kvm虚拟机迁移到esxi上 直接使用qemu-img convert 转换为vmdk...

  • kvm基本命令

    kvm常用管理命令 kvm连接方式 kvm磁盘磁盘 kvm快照管理 kvm克隆虚拟机 kvm桥接网络 kvm虚拟机...

  • Mac中实现iTerm2 SSH自动登录的方法

    在 Mac 中用 Parallel Desktop 安装了 Ubuntu Server 的虚拟机,而 Mac 下的...

  • kvm虚拟机安装,配置,活动迁移

    001KVM安装 Ubuntu 1.判断虚拟机是否支持虚拟化技术 2.安装kvm,virt管理工具 3.安装网桥设...

  • KVM虚拟机的磁盘大小修改

    kvm 虚拟机的磁盘大小可通过命令:qemu-img resize filename size 来改,要注意的是r...

网友评论

      本文标题:KVM在ubuntu中用img模板装虚拟机

      本文链接:https://www.haomeiwen.com/subject/plvdsxtx.html