作者: 耗子007
需要的材料
qemu-kvm
能运行的qemu虚拟机镜像文件(如果你的机器是远端服务器,没办法使用virt-view等图形界面的话)
已有的qemu系统镜像安装了openssh-server端
安装qemu相关软件
1
| yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer
|
准备可用的qemu虚拟机镜像文件
条件:有一台PC机器,安装好了qemu
新建磁盘文件
1
| qemu-img create -f qcow2 /root/my.qcow2 20G
|
安装虚拟机
1
| virt-install -r 1024 –accelerate -n test -f /root/my.qcow2 –cdrom mini.iso –graphics=vnc,listen=0.0.0.0
|
注:mini.iso是ubuntu的最小安装盘,你可以用其他的代替
图形界面正常的安装流程
然后进入虚拟机安装openssh-server端,打开root的ssh登录权限。
此时,我们安装的虚拟机系统的所有文件都在/root/my.qcow2里面。所以,我们把它拷贝到你的centos上面,然后可以直接用这个镜像文件创建虚拟机了。
启动虚拟机的xml配置文件示例
把自己的配置写到test.xml文件,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <domain type=’kvm’> <name>test</name> <memory unit=’MiB’>1024</memory> <currentMemory unit=’MiB’>1024</currentMemory> <vcpu placement=’static’>1</vcpu> <os> <type arch=’x86_64′ machine=’pc’>hvm</type> <boot dev=’cdrom’/> <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’/> <source file=’/root/guest.qcow2’/> <target dev=’vda’ bus=’virtio’/> </disk> <controller type=’usb’ index=’0′> </controller> <interface type=’network’> <mac address=’52:54:00:00:00:12’/> <source network=’default’/> <model type=’virtio’/> </interface> <serial type=’pty’> <target port=’0’/> </serial> <console type=’pty’> <target type=’serial’ port=’0’/> </console> <channel type=’spicevmc’> <target type=’virtio’ name=’com.redhat.spice.0’/> </channel> <input type=’tablet’ bus=’usb’/> <input type=’mouse’ bus=’ps2’/> <input type=’keyboard’ bus=’ps2’/> <graphics type=’spice’ autoport=’yes’/> <sound model=’ich6′> </sound> <video> <model type=’qxl’ vram=’9216′ heads=’1’/> </video> <redirdev bus=’usb’ type=’spicevmc’> </redirdev> <memballoon model=’virtio’> </memballoon> </devices> </domain>
|
注:/usr/libexec/qemu-kvm 其他系统的位置不一样(如,ubuntu是在/user/bin/qemu-system-x86_64)
然后直接运行virsh create test.xml,此时你就可以通过ssh root@ip到你的虚拟机上了。