首页 > 其他 > 详细

使用virsh创建虚拟机

时间:2020-09-21 23:41:50      阅读:56      评论:0      收藏:0      [点我收藏+]

环境:CentOS 16.04  图形界面

 

1、首先将防火墙关闭,否则后面使用VNC Viewer无法连接

1 systemctl stop firewalld.service      #关闭防火墙
2 systemctl disable firewalld.service      #关闭开机自启

2、编写xml配置文件(以centos7-0.xml为例)

 1 <domain type=kvm>
 2   <name>centos7-0</name>      #虚拟机名称
 3   <memory unit=KiB>2048576</memory>      #虚拟机内存大小
 4   <currentMemory unit=KiB>2048576</currentMemory>      #虚拟机在开机时分配的内存大小
 5   <vcpu placement=static>2</vcpu>      #cpu个数
 6   <os>
 7     <type arch=x86_64 machine=pc-i440fx-rhel7.0.0>hvm</type>
 8     <boot dev=cdrom/>      #启动方式  cdrom为从光盘启动(ISO镜像),后面会再次修改启动方式
 9   </os>
10   <features>
11     <acpi/>
12     <apic/>
13   </features>
14   <cpu mode=custom match=exact>
15     <model fallback=allow>Nehalem</model>
16   </cpu>
17   <clock offset=localtime/>
18   <on_poweroff>destroy</on_poweroff>
19   <on_reboot>restart</on_reboot>
20   <on_crash>restart</on_crash>
21   <devices>
22     <emulator>/usr/libexec/qemu-kvm</emulator>
23     <disk type=file device=disk>
24       <driver name=qemu type=qcow2/>
25       <source file=/opt/image/centos7-0.qcow2/>      #虚拟硬盘
26       <target dev=hda bus=ide/>
27     </disk>
28     <disk type=file device=cdrom>
29     <source file=/mnt/ISO/CentOS-7-x86_64-DVD-1810.iso/>      #ISO镜像路径
30     <target dev=hdb bus=ide/>
31     </disk>
32     <interface type=bridge>      #网络模式为桥接
33       <source network=virbr0 bridge=virbr0/>      #此处要和自己虚拟交换机的配置对应
34     </interface>
35     <input type=tablet bus=usb/>
36     <input type=mouse bus=ps2/>
37     <input type=keyboard bus=ps2/>
38     <graphics type=vnc port=5905 autopart=no listen=0.0.0.0 keymap=en-us/>  #5905为端口号
39   </devices>
40 </domain>

关于虚拟硬盘的创建

qemu-img create -f qcow2 /opt/image/centos7-0.qcow2 20G  #此处的路径与上方xml文件中的硬盘路径一致

3、创建虚拟机

virsh define centos7-0.xml    #定义虚拟机
virsh start centos7-0    #启动虚拟机

查看是否启动

virsh list --all

技术分享图片

4、连接VNC Viewer   -> 宿主机IP:端口号

5、连接以后进入安装系统界面,安装完成以后,要将虚拟机的开机方式改为从硬盘启动(否则将一直重复系统安装引导)

virsh shutdown centos7-0    #将虚拟机关机
// 关机后先查看一下虚拟机的状态,如果还是运行态,则进行强制关机
// virsh destroy centos7-0
virsh undefine centos7-0    #取消定义

然后重新编辑xml文件

技术分享图片

重新创建虚拟机

virsh define centos7-0.xml
virsh start centos7-0

至此完成

使用virsh创建虚拟机

原文:https://www.cnblogs.com/zhimao/p/13709173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!