Linux下 如何通過命令行使用現有的鏡像創建、啟動kvm虛擬機
這里假定已經創建好了相應的鏡像:
eg:我這里制作的鏡像名稱為zu1-centos7.img
# lszu1-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.1lazy 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文件
?