Linux下 如何通过命令行使用现有的镜像创建、启动kvm虚拟机
这里假定已经创建好了相应的镜像:
eg:我这里制作的镜像名称为zu1-centos7.img
# ls
zu1-centos7.img
1、拷贝这个镜像到某一个目录
cp zu1-centos7.img /data2/
2、编写镜像的配置文件,当然一般情况是从其他镜像的配置文件拷贝一份,重新命名在修改
# cd /etc/libvirt/qemu 进入到镜像配置文件目录
# cp test2.xml test3.xml test2.xml是其他镜像存在的配置文件,test3.xml是当前准备启动的镜像的配置文件
test2.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 test2
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>test2</name> ---虚拟机名字,记得修改,随便起什么名字都行,只要不跟其他虚拟机名字一样就行
<uuid>92047c95-c5b8-4e4d-916c-fb2218ca055f</uuid> --uuid 记得修改,一般来说,把最好的四位数字重新修改一下,例如我把最后四位改成0001,具体效果看下个test3.xml文件
<memory unit='KiB'>10485760</memory> ---当前虚拟机使用内存,看自己情况是否需要修改
<currentMemory unit='KiB'>10485760</currentMemory> ---当前虚拟机可以使用的最大内存,使用内存不得超过这个内存,当使用内存不足的时候,可以在虚拟机里面动态调整内存。
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='rhel6.6.0'>hvm</type> ---架构,一般不用修改
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/> ---driver类型,需要跟镜像的文件格式一致。以前老版本默认是raw类型,现在默认是qcow2类型。查看镜像的文件格式在该配置文件的下面:
<source file='/data1/iso/zu1-centos7.img'/> --- 镜像文件路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='qcow2'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='usb' index='0' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<interface type='bridge'>
<mac address='52:54:00:5a:07:de'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> ---这里的slot号记得修改 把最后两位改成其他的就行,但不要跟其他的虚拟机配置的值一样,同时,这个值不超过 0x0f
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
</domain>
3、查看镜像的driver类型
# qemu-img info /data1/zu1-centos7.img
image: /data1/virtcentos/pool/zu1-centos7.img
file format: qcow2 ----这里就是镜像的文件格式
virtual size: 20G (21474836480 bytes)
disk size: 18G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
4、修改test3.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 test2
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>yjt</name> ---这里改成了yjt
<uuid>92047c95-c5b8-4e4d-916c-fb2218ca0001</uuid>
<memory unit='KiB'>10485760</memory>
<currentMemory unit='KiB'>10485760</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='rhel6.6.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data2/zu1-centos7.img'/> ---这里修改了镜像的路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='qcow2'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='usb' index='0' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<interface type='bridge'>
<mac address='52:54:00:5a:07:de'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x10' function='0x0'/> 这里改成了0x10,注意,该值不得超过0x0f
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
</domain>
5、定义虚拟机
# virsh define test3.xml ---定义虚拟机跟上对应的文件即可
6、启动虚拟机
virsh start yjt ---yjt这个名字在配置文件定义的,也就是虚拟机的名字。
到这里就ok了。
如果虚拟机不要用了,可以使用如下方法销毁
7、停止虚拟机
# virsh shutdown yjt ---正常停止
# virsh destroy yjt ---强制停止
8、取消虚拟机的定义,也就是删除
# virsh undefine yjt ---默认会删除/etc/libvirt/qemu下的test3.xml文件