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 --all
 Name   State    Autostart   Persistent
-----------------------------------------
 vbr    active   yes         yes
 
[root@harbor ~]# ifconfig vbr
vbr: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.254  netmask 255.255.255.0  broadcast 192.168.100.255
        ether 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 0
        TX 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 --all
 Id    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查看虚拟机硬盘信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值