从镜像文件开出虚拟机

时间:2022-12-24 17:47:57

需求

利用qcow2格式的镜像文件,创建出虚拟机
虚拟机要求,4G内存,2核,外带200G磁盘
创建完,将磁盘挂载

步骤

1、 定义xml文件

创建虚拟机目录,复制镜像

 # mkdir  -p  /storage/instance/vm01
# cp /data0/centos6.6.qcow2 /storage/instance/vm01/

创建外挂磁盘文件

# qemu-img create  -f  qcow2  disk.qcow2  200G
# qemu-img info disk.qcow2
image: disk.qcow2
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 136K
cluster_size: 65536

定义xml文件

# vi vm01.xml
<domain type='kvm'>
<name>vm01</name>
<memory>4096000</memory>
<currentMemory>4096000</currentMemory>
<vcpu>2</vcpu>

<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>

<features>
<acpi/>
<apic/>
<pae/>
</features>

<clock offset='localtime'/>
<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='/storage/instance/vm01/centos6.6.qcow2'/>
<target dev='hda' bus='ide'/>
</disk>

<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/storage/instance/vm01/disk.qcow2'/>
<target dev='hdb' bus='ide'/>
</disk>

<disk type='file' device='cdrom'>
<source file='/iso/CentOS-6.6-x86_64-bin-DVD1.iso'/>
<target dev='hdc' bus='ide'/>
</disk>

<interface type='bridge'>
<source bridge='br0'/>
</interface>

<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>
</devices>
</domain>

2、 创建虚拟机

# virsh define /storage/instance/vm01/vm01.xml 
# virsh list --all
Id 名称 状态
----------------------------------------------------
- vm01 关闭

# virsh start vm01
# virsh vncdisplay vm01
192.168.10.233:0

通过vnc连接虚拟机即可

3、初始化磁盘并挂载