Openstack教程:使用virsh创建带有OS的image

企鹅博客
18942
文章
0
评论
2020年1月14日11:56:10 评论 0 views

在用virsh命令创建image之前,要保证你的kvm和libvirt已经安装且可以运行,如何安装kvm和libvirt可以参考Linux公社的其他文章。
[email protected]:/var/run/libvirt# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

[email protected]:/var/run/libvirt# virsh list
 Id    Name                          State
----------------------------------------------------

[email protected]:/home/zxl# mkdir /image

[email protected]:/home/zxl# cd /image/
[email protected]:/image# ll
total 8
drwxr-xr-x  2 root root 4096 Aug 22 10:04 ./
drwxr-xr-x 26 root root 4096 Aug 22 10:04 ../

下面是一个虚拟机配置文件的模板,对其进行特定的修改便可(注,此模板中没有定义cdrom,需要添加,一般情况下如果你有一个带有os的raw格式的image,且将此raw文件作文qcow2的backing_file进行大批量虚拟机创建时,则不需要ios镜像)

下面的为cdrom的定义,添加进模板中第33行便可
<disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/image/Ubuntu-12.04.5-alternate-amd64.iso'/>
      <target dev='hdb' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>

[email protected]:/image# vim ubuntu-12.04.xml

 1 <domain type='kvm'>
  2  <name>%VM_NAME%</name>
  3  <uuid>%UUID%</uuid>
  4  <memory>1048576</memory>
  5  <currentMemory>1048576</currentMemory>
  6  <vcpu>1</vcpu>
  7  <os>
  8    <type arch='x86_64' machine='pc-0.14'>hvm</type>
  9    <boot dev='hd'/>
 10    <bootmenu enable='yes'/>
 11  </os>
 12  <cpu match='exact'>
 13    <model>core2duo</model>
 14    <feature policy='require' name='vmx'/>
 15  </cpu>
 16  <features>
 17    <acpi/>
 18    <apic/>
 19    <pae/>
 20  </features>
 21  <clock offset='localtime'/>
 22  <on_poweroff>destroy</on_poweroff>
 23  <on_reboot>restart</on_reboot>
 24  <on_crash>restart</on_crash>
 25  <devices>
 26    <emulator>/usr/bin/kvm</emulator>
 27    <disk type='file' device='disk'>
 28      <driver name='qemu' type='qcow2' cache='none'/>
 29      <source file='%IMAGE_PATH%'/>
 30      <target dev='vda' bus='virtio'/>
 31      <alias name='virtio-disk0'/>
 32    </disk>
 33
 34    <disk type='file' device='disk'>
 35      <driver name='qemu' type='raw' cache='none'/>
 36      <source file='%RAW_DISK_PATH%'/>
 37      <target dev='vdb' bus='virtio'/>
 38      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
 39    </disk>
 40
 41
 42    <controller type='ide' index='0'>
 43      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
 44    </controller>
 45    <controller type='fdc' index='0'/>
 46
 47    <interface type='bridge'>
 48      <mac address='%MAC%'/>
 49      <source bridge='br100'/>
 50      <target dev='vnet0'/>
 51      <alias name='net0'/>
 52      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
 53    </interface>
 54
 55    <interface type='network'>
 56      <mac address='%MAC2%'/> <!-- 192.168.222.%IP1% -->
 57      <source network='default'/>
 58      <target dev='vnet1'/>
 59      <alias name='net1'/>
 60      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
 61    </interface>
 62
 63    <interface type='network'>
 64      <mac address='%MAC3%'/> <!-- 192.168.111.%IP2% -->
 65      <source network='default'/>
 66      <target dev='vnet2'/>
 67      <alias name='net2'/>
 68      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
 69    </interface>
 70
 71
 72
 73    <serial type='pty'>
 74      <target port='0'/>
 75    </serial>
 76    <console type='pty'>
 77      <target type='serial' port='0'/>
 78    </console>
 79    <input type='tablet' bus='usb'/>
 80    <input type='mouse' bus='ps2'/>
 81    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>
 82      <listen type='address' address='0.0.0.0'/>
 83    </graphics>
 84    <sound model='ich6'>
 85      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
 86    </sound>
 87    <video>
 88      <model type='vga' vram='9216' heads='1'/>
 89      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
 90    </video>
 91    <memballoon model='virtio'>
 92      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
 93    </memballoon>
 94  </devices>
 95 </domain>

指定虚拟机名称

[email protected]:/image# sed -i "s,%VM_NAME%,ubuntu-12.04,g" ubuntu-12.04.xml 

指定UUID

[email protected]:/image# uuidgen 

f43b668f-f13c-4129-bee1-20673dcff5fb

[email protected]:/image# sed -i "s,%UUID%,f43b668f-f13c-4129-bee1-20673dcff5fb,g" ubuntu-12.04.xml 

 创建一个raw格式的image(注:如果制作的image需要长期使用,尽量采用raw格式)  

[email protected]:/image# qemu-img create -f raw ubuntu-12.04.raw 30G

Formatting 'ubuntu-12.04.raw', fmt=raw size=32212254720 

[email protected]:/image# sed -i "s,%IMAGE_PATH%,/image/ubuntu-12.04.raw,g" ubuntu-12.04.xml 

[email protected]:/image# 

设置虚拟光盘,用来安装操作系统  (此镜像需要你提前下载好)

[email protected]:/image# sed -i "s,%ISO_PATH%,/image/ubuntu-12.04.5-alternate-amd64.iso" ubuntu-12.04.xml 

 

 

[email protected]:/image# 

[email protected]:/image# ll

total 779280

drwxr-xr-x  2 root root       4096 Aug 22 10:15 ./

drwxr-xr-x 26 root root      4096 Aug 22 10:04 ../

-rw-r--r--  1 root root       797966336 Aug 13 06:17 ubuntu-12.04.5-alternate-amd64.iso

-rw-r--r--  1 root root       32212254720 Aug 22 10:11 ubuntu-12.04.raw

-rw-r--r--  1 root root       2894 Aug 22 10:12 ubuntu-12.04.xml

设置虚拟网卡,在此之前需要确保br100启用,用如下命令生成并替换MAC地址

[email protected]:/image# sed -i "s,%ISO_PATH%,/image/ubuntu-12.04.5-alternate-amd64.iso,g" ubuntu-12.04.xml 

 

[email protected]:/image# MAC="fa:95:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"

[email protected]:/image# sed -i "s,%MAC%,$MAC,g" ubuntu-12.04.xml 

[email protected]:/image# MAC2="52:54:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"

[email protected]:/image# sed -i "s,%MAC2%,$MAC2,g" ubuntu-12.04.xml 

 

这时ubuntu12.04.xml的文件就已经配置好了,下面就可以创建并且启动虚拟机了,在此之前先要进行define操作

[email protected]:/image# virsh define ubuntu-12.04.xml 

error: Failed to define domain from ubuntu-12.04.xml

error: XML error: unable to parse mac address '%MAC3%'

出现错误,删除ubuntu-12.04.xml中关于MAC3的定义后重新define

修改关于名称的定义为ubuntu-12.04

 

[email protected]:/image# virsh define ubuntu-12.04.xml 

Domain ubuntu-12.04  defined from ubuntu-12.04.xml

可以看到此虚拟机已经定义,处于关闭状态

[email protected]:/image# virsh list --all

 Id    Name                           State

----------------------------------------------------

 -    ubuntu-12.04                     shut off

 

[email protected]:/image# virsh start ubuntu-12.04

error: Failed to start domain %ubuntu-12.04%

error: Cannot access storage file '%RAW_DISK_PATH%' (as uid:0, gid:0): No such file or directory

出现错误,删除ubuntu-12.04.xml中关于vdb raw磁盘的定义后启动

 

 

[email protected]:~# virsh start ubuntu-12.04 

error: Failed to start domain ubuntu-12.04

error: unsupported configuration: guest and host CPU are not compatible: Host CPU does not provide required features: monitor

出现错误删除下面内容

<cpu match='exact'>

<model>core2duo</model>

<feature policy='require' name='vmx'/>

</cpu>

 

 

[email protected]:~# virsh start ubuntu-12.04 

error: Failed to start domain ubuntu-12.04

error: Cannot get interface MTU on 'br100': No such device

出现错误删除,没有配置基于网桥的虚拟网卡

[email protected]:~# vim /etc/network/interfaces

 

[email protected]:/image# virsh start ubuntu-12.04

error: Failed to start domain ubuntu-12.04

error: internal error: Process exited while reading console log output: char device redirected to /dev/pts/1

Unsupported cluster size: 2^0kvm: -drive file=/image/ubuntu-12.04.raw,if=none,id=drive-virtio-disk0,format=qcow2,cache=none: could not open disk image /image/ubuntu-12.04.raw: Invalid argument

 

磁盘格式错误将<driver name='qemu' type='qcow2' cache='none'/>中的qcow2该为raw

 

[email protected]:/image# virsh start ubuntu-12.04

Domain ubuntu-12.04 started

[email protected]:/image# virsh list

 Id    Name                           State

----------------------------------------------------

 11    ubuntu-12.04                   running

 

已经可以用virt-manager看到虚拟机正在安装

 

但是我们一般用vnc查看虚拟机

[email protected]:/image#apt-get install vncviewer

[email protected]:/image#vncviewer 192.168.139.5:5900 (vncviewer 宿主机IP:xml中配置的端口)

也可以通过如下命令确定虚拟机vnc的端口,可以看到显示的是:0

 

 

 

这是最终虚拟机配置文件

[email protected]:/image# vim ubuntu-12.04.xml 

<domain type='kvm'>

  <name>ubuntu-12.04</name>

  <uuid>f43b668f-f13c-4129-bee1-20673dcff5fb</uuid>

  <memory>1048576</memory>

  <currentMemory>1048576</currentMemory>

  <vcpu>1</vcpu>

  <os>

    <type arch='x86_64' machine='pc-0.14'>hvm</type>

    <boot dev='hd'/>

    <bootmenu enable='yes'/>

  </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</emulator>

    <disk type='file' device='disk'>

      <driver name='qemu' type='raw' cache='none'/>

      <source file='/image/ubuntu-12.04.raw'/>

      <target dev='vda' bus='virtio'/>

      <alias name='virtio-disk0'/>

    </disk>

    <disk type='file' device='cdrom'>

      <driver name='qemu' type='raw'/>

      <source file='/image/ubuntu-12.04.5-alternate-amd64.iso'/>

      <target dev='hdb' bus='ide'/>

      <readonly/>

      <address type='drive' controller='0' bus='1' unit='0'/>

    </disk>

 

    <controller type='ide' index='0'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>

    </controller>

    <controller type='fdc' index='0'/>

 

    <interface type='bridge'>

      <mac address='fa:95:d9:d9:9a:ca'/>

      <source bridge='br100'/>

      <target dev='vnet0'/>

      <alias name='net0'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

    </interface>

 

    <interface type='network'>

      <mac address='52:54:d8:c3:69:30'/> <!-- 192.168.222.%IP1% -->

      <source network='default'/>

      <target dev='vnet1'/>

      <alias name='net1'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' 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'/>

    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>

      <listen type='address' address='0.0.0.0'/>

    </graphics>

    <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>

继续阅读
  • 版权声明: 发表于 2020年1月14日11:56:10
  • 转载注明:https://www.qieseo.com/169244.html
你应该了解Nginx的7个原因 linux服务器应用

你应该了解Nginx的7个原因

Nginx (“engine x”)是一个高性能的HTTP和反向代理服务器,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 1 负载均衡实现可扩展的浏量管理 ...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: