本文主要是介绍Linux学习-虚拟化平台安装和使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注:系统使用Rock8.6
下载链接
通过百度网盘分享的文件:cirros.qcow2,node_base.xml等2个文件
链接:https://pan.baidu.com/s/1hupGQsMjrXMgngCy3lQLhw?pwd=hlr6
提取码:hlr6
[root@harbor ~]# cat /etc/redhat-release
Rocky Linux release 8.6 (Green Obsidian)
验证 ecs 是否支持虚拟化
[root@harbor ~]# grep -Po "vmx|svm" /proc/cpuinfo
vmx
vmx
vmx
vmx
[root@harbor ~]# lsmod | grep kvm
kvm_intel 339968 4
kvm 905216 1 kvm_intel
irqbypass 16384 4 kvm
安装虚拟化平台
安装服务
[root@harbor ~]# dnf install -y qemu-kvm libvirt-daemon libvirt-client libvirt-daemon-driver-qemu libvirt-daemon-driver-network dnsmasq
[root@harbor ~]# systemctl enable --now libvirtd
[root@harbor ~]# virsh version
Compiled against library: libvirt 8.0.0
Using library: libvirt 8.0.0
Using API: QEMU 8.0.0
Running hypervisor: QEMU 6.2.0
创建网桥
命令 描述 virsh net-list [--all] 列出虚拟网络 virsh net-start 启动虚拟交换机 virsh net-destroy 强制停止虚拟交换机 virsh net-define 根据xml文件创建虚拟网络 virsh net-undefine 删除一个虚拟网络设备 virsh net-edit 修改虚拟交换机的配置 virsh net-autostart 设置开机自启动
# 官网手册: https://libvirt.org/docs.html
[root@harbor ~]# vim /etc/libvirt/qemu/networks/vbr.xml
<network><name>vbr</name><uuid>335ef8bf-f349-4194-9b79-687b56df7614</uuid><forward mode='nat'/><bridge name='vbr' stp='on' delay='0'/><mac address='52:54:00:14:a2:43'/><ip address='192.168.100.254' netmask='255.255.255.0'><dhcp><range start='192.168.100.128' end='192.168.100.200'/></dhcp></ip>
</network>
[root@harbor ~]# virsh net-define /etc/libvirt/qemu/networks/vbr.xml
Network vbr defined from /etc/libvirt/qemu/networks/vbr.xml
[root@harbor ~]# virsh net-autostart vbr
Network vbr marked as autostarted
# 启动网桥
[root@harbor ~]# virsh net-start vbr
Network vbr started
# 验证状态
[root@harbor ~]# virsh net-list --allName State Autostart Persistent
-----------------------------------------vbr active yes yes[root@harbor ~]# ifconfig vbr
vbr: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.100.254 netmask 255.255.255.0 broadcast 192.168.100.255ether 52:54:00:14:a2:43 txqueuelen 1000 (Ethernet)RX packets 27 bytes 2264 (2.2 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 15 bytes 1564 (1.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Linux 虚拟机
虚拟机的构成
虚拟机配置文件(*.xml) 文件默认路径:/etc/libvirt/qemu/ 用途:定义了虚拟机的设备,如CPU,内存,网卡等资源 存储文件(*.img) 文件默认路径:/var/lib/libvirt/imags/ 用途:仿真虚拟机磁盘,存储所有数据信息
创建虚拟机磁盘
# cirros.qcow2见文章首部下载链接
[root@harbor ~]# cp cirros.qcow2 /var/lib/libvirt/images/
[root@harbor ~]# cd /var/lib/libvirt/images/
[root@harbor ~]# qemu-img create -F qcow2 -f qcow2 -b cirros.qcow2 vmhost.img 20G
[root@harbor ~]# qemu-img info vmhost.img #查看信息
虚拟机配置文件
< domain type = ' kvm' > < name> vmhost</ name> < memory unit = ' KB' > 1048576</ memory> < currentMemory unit = ' KB' > 1048576</ currentMemory> < vcpu placement = ' static' > 2</ vcpu> < os> < type arch = ' x86_64' machine = ' pc' > hvm</ type> < boot dev = ' hd' /> < bootmenu enable = ' yes' /> < bios useserial = ' yes' /> </ os> < features> < acpi/> < apic/> </ features> < cpu mode = ' host-passthrough' > </ cpu> < clock offset = ' localtime' /> < 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 = ' /var/lib/libvirt/images/vmhost.img' /> < target dev = ' vda' bus = ' virtio' /> </ disk> < interface type = ' bridge' > < source bridge = ' vbr' /> < model type = ' virtio' /> </ interface> < channel type = ' unix' > < target type = ' virtio' name = ' org.qemu.guest_agent.0' /> </ channel> < serial type = ' pty' > </ serial> < console type = ' pty' > < target type = ' serial' /> </ console> < memballoon model = ' virtio' > </ memballoon> </ devices>
</ domain>
创建虚拟机
命令 说明 virsh list [--all] 列出虚拟机 virsh start/shutdown 启动/关闭虚拟机 virsh destroy 强制停止虚拟机 virsh define/undefine 创建/删除虚拟机 virsh console 连接虚拟机的 console
[root@harbor ~]# virsh define /etc/libvirt/qemu/vmhost.xml
Domain vmhost defined from /etc/libvirt/qemu/vmhost.xml
[root@harbor ~]# virsh list --allId Name State
----------------------------------------------------- vmhost shut off
[root@harbor ~]# virsh start vmhost
Domain vmhost started
[root@harbor ~]# virsh console vmhost # 两次回车
Connected to domain vmhost
Escape character is ^]login as 'cirros' user. default password: 'gocubsgo'. use 'sudo' for root.
cirros login:
退出使用 ctrl + ]
常用管理命令
命令 说明 virsh edit 修改虚拟机的配置 virsh autostart 设置虚拟机自启动 virsh dominfo 查看虚拟机摘要信息 virsh domiflist 查看虚拟机网卡信息 virsh domblklist 查看虚拟机硬盘信息
这篇关于Linux学习-虚拟化平台安装和使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!