将系统镜像CentOS-7-x86_64-DVD-1908.iso上传到/var/lib/libvirt/images/下
编写虚拟机配置文件node1.xml文件并创建格式为qcow2磁盘镜像文件
cd /etc/libvirt/qemu/
vi node1.xml
node1.xml内容:
<domain type='kvm'>
<name>node1</name>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='cdrom'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Westmere-IBRS</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<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/node1.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/var/lib/libvirt/images/CentOS-7-x86_64-DVD-1908.iso'/>
<target dev='hda' bus='ide'/>
</disk>
<interface type='network'>
<source network='default'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
</devices>
</domain>
以下是部分内容解释:
<name>node1</name> #虚拟机名称
<memory unit='KiB'>1048576</memory>#虚拟机最大内存大小
<currentMemory unit='KiB'>1048576</currentMemory>#虚拟机当前占用内存大小
<vcpu placement='static'>1</vcpu>#虚拟机cpu数量
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/node1.qcow2'/>#设置虚拟磁盘文件的路径,为下一步创建虚拟磁盘做准备
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/var/lib/libvirt/images/CentOS-7-x86_64-DVD-1908.iso'/>#指定系统镜像,可以是iso格式
然后我们按编写的内容磁盘存放的路径来创建一个磁盘镜像,命令如下:
qemu-img create -f qcow2 /var/lib/libvirt/images/node1.qcow2 5G
-f 指定虚拟机格式
/var/lib/libvirt/images/node1.qcow2为虚拟机磁盘存放路径
5G 代表磁盘镜像大小
磁盘格式介绍:
全镜像格式(典型代表 raw),特点:裸磁盘不支持快照,设置多大就是多大,写入速度快,方便转换其他格式,raw性能比qcow2快,raw 创建多大磁盘,就占用多大空间直接分配。
稀疏格式(典型代表 qcow2),其特点:qcow2数据的基本组成单元是cluster,支持压缩、快照、镜像,qcow2动态的用多大占用多大空间。
virsh define node1.xml #使用xml定义一台虚拟机
virsh start node1 #启动虚拟机,"centos7-xml"是centos7-xml.xml文件里定义的虚拟机名称
virsh list #查看运行的虚拟机
virsh vncdisplay node1
#查看node1的vnc端口号,如果宿主机没有图形界面,找一台和宿主机通的windows机器,安装vncviewer软件,使用,宿主机ip:vnc端口号访问,注意宿主机防火墙一定要放行vnc端口
系统安装完成重启后再次进入安装界面,则需要将虚拟机配置文件中去掉挂载的iso文件配置
vim /etc/libvirt/qemu/node1.xml
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/var/lib/libvirt/images/CentOS-7-x86_64-DVD-1908.iso'/>
<target dev='hda' bus='ide'/>
</disk>
#在虚拟机中执行以下命令后可以使用virsh console控制台登录(换码符为 ^]卡住了问题)
cat /etc/securetty|grep ttyS0 #查看是否存在ttyS0,没有需要添加一行这个内容
vi /etc/default/grub 文件,找到下面行:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet”
改为:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet net.ifnames=0 console=ttyS0,115200"
运行grub2-mkconfig -o /boot/grub2/grub.cfg 来重新生成GRUB配置并更新内核参数
使用以下命令激活 服务 (这个是centos7才有的服务,但默认没有启动)
sudo systemctl start serial-getty@ttyS0.service
sudo systemctl enable serial-getty@ttyS0.service
virsh reboot node1 #重启虚机生效
virsh console node1 #进入虚拟机管理,退出用Ctrl+]
下面是删除虚拟机步骤:
virsh shutdown <虚拟机名称>
virsh undefine <虚拟机名称>
virsh destroy <虚拟机名称>
接着找到它的磁盘镜像rm -rf删除