virt-manager启动虚拟机
首先需要确保hmcode是1.20版本,然后修改/boot/grub/grub.cfg文件,内核参数改为:
kvm_mem= nn[MG]@start[MG]
其中nn[MG]是为虚拟机预留的内存大小,即该部分内存划归虚拟机内存资源池,同样不对宿主机隐藏;start[MG]为预留内存的起始地址。
使用virt-manager启动虚拟机时分配完内存和CPU大小后需要添加配置以下硬件:
1、添加鼠标、键盘和数位板(“输入”选项全选,会有移位问题):
2、添加vnc(添加硬件—图形—VNC服务选项):
3、添加显卡(添加硬件—视频—选择Virtio选项):
4、配置COROM 1盘(source path中添加镜像路径,磁盘总线选择SATA):
5、配置磁盘1(添加.qcow2路径,磁盘总线选择Virtio):
6、配置Controller USB 0(型号选择USB3):
注意:使用.iso引导系统安装完成后,在引导选项中去除SATA COROM 1选项,使用制作好的卷virtIO启动虚拟机,否则会 重复安装系统(命令行制作虚拟机同样)
命令行启动虚拟机
首先制作挂载虚拟机磁盘
qemu-img create -f qcow2 test.qcow2 20G
然后使用命令启动虚拟机
qemu-kvm -machine core3 -smp 8 -m 4096 -enable-kvm -name duyl -device virtio-gpu-pci -cdrom 镜像位置 -drive file=test.qcow2,if=virtio -device usb-tablet -vga virtio -serial stdio -vnc :12 -device usb-kbd
安装vnc,可视化系统安装过程
虚拟机制作完毕后,使用test.qcow2引导启动虚拟机系统
Virsh 命令创建虚拟机
1.从iso文件引导创建虚拟机
制作挂载虚拟机磁盘
qemu-img create -f qcow2 test.qcow2 20G
通过下面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 generic-test
or other application using the libvirt API.
-->
<domain type="kvm">
<name>generic</name>
<uuid>04577327-010d-4820-b92d-f913a88d15ad</uuid>
<memory unit="KiB">8388608</memory>
<currentMemory unit="KiB">8388608</currentMemory>
<vcpu placement="static">8</vcpu>
<os>
<type arch="sw_64" machine="core3">hvm</type>
<boot dev="hd"/>
</os>
<clock offset="utc"/>
<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="/var/lib/libvirt/images/generic-test.qcow2"/>
<target dev="vda" bus="virtio"/>
<address type="pci" domain="0x0000" bus="0x04" slot="0x00" function="0x0"/>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"/>
<source file="/home/SWOS.iso"/>
<target dev="sda" bus="sata"/>
<readonly/>
<address type="drive" controller="0" bus="0" target="0" unit="0"/>
</disk>
<controller type="pci" index="0" model="pcie-root"/>
<controller type="pci" index="1" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="1" port="0x8"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x0" multifunction="on"/>
</controller>
<controller type="pci" index="2" model="pcie-to-pci-bridge">
<model name="pcie-pci-bridge"/>
<address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</controller>
<controller type="pci" index="3" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="3" port="0x9"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/>
</controller>
<controller type="pci" index="4" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="4" port="0xa"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x2"/>
</controller>
<controller type="pci" index="5" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="5" port="0xb"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x3"/>
</controller>
<controller type="pci" index="6" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="6" port="0xc"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x4"/>
</controller>
<controller type="pci" index="7" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="7" port="0xd"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x5"/>
</controller>
<controller type="pci" index="8" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="8" port="0xe"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x6"/>
</controller>
<controller type="pci" index="9" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="9" port="0xf"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x7"/>
</controller>
<controller type="sata" index="0">
<address type="pci" domain="0x0000" bus="0x02" slot="0x03" function="0x0"/>
</controller>
<controller type="virtio-serial" index="0">
<address type="pci" domain="0x0000" bus="0x03" slot="0x00" function="0x0"/>
</controller>
<controller type="usb" index="0" model="qemu-xhci" ports="15">
<address type="pci" domain="0x0000" bus="0x09" slot="0x00" function="0x0"/>
</controller>
<interface type="network">
<mac address="52:54:00:78:d1:3e"/>
<source network="default"/>
<model type="rtl8139"/>
<address type="pci" domain="0x0000" bus="0x02" slot="0x01" function="0x0"/>
</interface>
<serial type="pty">
<target port="0"/>
</serial>
<console type="pty">
<target type="serial" port="0"/>
</console>
<channel type="unix">
<target type="virtio" name="org.qemu.guest_agent.0"/>
<address type="virtio-serial" controller="0" bus="0" port="1"/>
</channel>
<input type="keyboard" bus="usb">
<address type="usb" bus="0" port="2"/>
</input>
<input type="mouse" bus="usb">
<address type="usb" bus="0" port="1.1"/>
</input>
<input type="tablet" bus="usb">
<address type="usb" bus="0" port="1.2"/>
</input>
<input type="keyboard" bus="virtio">
<address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>
</input>
<input type="tablet" bus="virtio">
<address type="pci" domain="0x0000" bus="0x08" slot="0x00" function="0x0"/>
</input>
<graphics type="vnc" port="-1" autoport="yes">
<listen type="address"/>
</graphics>
<audio id="1" type="none"/>
<video>
<model type="virtio" vram="65536" heads="1" primary="yes"/>
<address type="pci" domain="0x0000" bus="0x06" slot="0x00" function="0x0"/>
</video>
<hub type="usb">
<address type="usb" bus="0" port="1"/>
</hub>
<memballoon model="virtio">
<address type="pci" domain="0x0000" bus="0x05" slot="0x00" function="0x0"/>
</memballoon>
</devices>
</domain>
Xml文件中的虚拟机磁盘,虚拟机内存大小,cpu核心数、iso镜像目录对应系统实际修改
修改完成后
virsh create xxx.xml
通过vnc或者virt-manager查看镜像引导,按照步骤安装引导即可,引导完成后,virsh start 虚拟机名称(generic-test)即可。
virsh list 可查看已经创建的虚拟机
2.已有qcow2或raw虚拟机镜像文件创建虚拟机
通过下面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 test-clone-11
or other application using the libvirt API.
-->
<domain type="kvm">
<name>test</name>
<uuid>095df91a-6c21-43c9-ba4e-8f640cef7abd</uuid>
<description>quancloud</description>
<memory unit="KiB">16777216</memory>
<currentMemory unit="KiB">16777216</currentMemory>
<vcpu placement="static">4</vcpu>
<cputune>
<shares>1024</shares>
</cputune>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch="sw_64" machine="core3">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset="variable" adjustment="0" basis="utc"/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu-kvm</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2"/>
<source file="/home/generic.qcow2"/>
<backingStore/>
<target dev="vda" bus="virtio"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x06" function="0x0"/>
</disk>
<controller type="pci" index="0" model="pcie-root"/>
<controller type="pci" index="1" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="1" port="0x8"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x09" function="0x0"/>
</controller>
<controller type="virtio-serial" index="0">
<address type="pci" domain="0x0000" bus="0x00" slot="0x04" function="0x0"/>
</controller>
<controller type="usb" index="0" model="qemu-xhci">
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x0"/>
</controller>
<controller type="scsi" index="0" model="virtio-scsi">
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x0"/>
</controller>
<controller type="pci" index="2" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="2" port="0x28"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x05" function="0x0"/>
</controller>
<serial type="pty">
<target port="0"/>
</serial>
<console type="pty">
<target type="serial" port="0"/>
</console>
<channel type="unix">
<target type="virtio" name="org.qemu.guest_agent.0"/>
<address type="virtio-serial" controller="0" bus="0" port="1"/>
</channel>
<input type="keyboard" bus="usb">
<address type="usb" bus="0" port="1"/>
</input>
<input type="mouse" bus="usb">
<address type="usb" bus="0" port="2"/>
</input>
<input type="tablet" bus="usb">
<address type="usb" bus="0" port="3"/>
</input>
<input type="keyboard" bus="virtio">
<address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</input>
<input type="tablet" bus="virtio">
<address type="pci" domain="0x0000" bus="0x02" slot="0x00" function="0x0"/>
</input>
<graphics type="vnc" port="-1" autoport="yes" listen="0.0.0.0" keymap="en-us">
<listen type="address" address="0.0.0.0"/>
</graphics>
<audio id="1" type="none"/>
<video>
<model type="virtio" vram="65536" heads="1" primary="yes">
<acceleration accel3d="no"/>
</model>
<address type="pci" domain="0x0000" bus="0x00" slot="0x03" function="0x0"/>
</video>
<memballoon model="none"/>
</devices>
<seclabel type="dynamic" model="dac" relabel="yes"/>
</domain>
同上所述,对应修改xml文件中的虚拟机名称、内存、cpu核心数、虚拟机镜像文件地址等,修改完成后
virsh create xxx.xml
后面步骤同上。