Ubuntu20.04 KVM安装虚拟机

一、简介

1.1 介绍

基于内核的虚拟机 Kernel-based Virtual Machine(KVM)是一种内建于 Linux® 中的开源虚拟化技术。具体而言,KVM 可帮助您将 Linux 转变为虚拟机监控程序,使主机计算机能够运行多个隔离的虚拟环境,即虚拟客户机或虚拟机(VM)。

1.2 主流虚拟化架构

图1对比了几种主流虚拟化技术架构:ESXi、Xen与KVM,其主要差别在与各组件(CPU、内存、磁盘与网络IO)的虚拟化与调度管理实现组件有所不同。

  • ESXi所有虚拟化功能都在内核实现。
  • Xen内核仅实现CPU与内存虚拟化, IO虚拟化与调度管理由Domain0(主机上启动的第一个管理VM)实现。
  • KVM内核实现CPU与内存虚拟化,QEMU实现IO虚拟化,通过Linux进程调度器实现VM管理。

preview

二、 KVM需求说明

  • 虚拟机中使用KVM

    如果你是在虚拟机中使用KVM,请在VMware中打开虚拟化设置

    VMware中宿主机建议配置2核2G以上。

  • 物理机中使用KVM

    请开启物理机中的虚拟化,在BIOS中开启,每个主板的开启方式可能会不同,BIOS开启方法虚拟化自行百度

注:在虚拟机中安装的Ubuntu系统,以下统称ubuntu20_01,

apt update
# libvirt-bin 这个包拆分为2个包了 => libvirt-daemon-system libvirt-clients
apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients  bridge-utils  virt-manager
  • 启动服务

    service libvirtd start
    

三、创建KVM 虚拟机

3.1 安装Linux系统

3.1.1 传入镜像

可以使用shell工具,也可以使用lrzsz这个工具

我这边使用shell工具,一开始本想安装Ubuntu22的,但是发现自己的这个系统是20的,一查发现最高支持20版本的系统。

注意在安装系统前,一定要配置静态IP,且宿主机能够访问虚拟机(192.168.1.101)的系统,且虚拟机可以访问百度。

在这里插入图片描述

3.1.2 创建虚拟网卡

为什么要创建一块虚拟网卡,因为我们创建的虚拟机需要使用桥接网络,如果使用NAT网络,那么宿主机就不能与虚拟机中的虚拟机(Ubuntu20_01)进行访问了。

创建的虚拟网卡需要通过buntu20_01 网络进行数据转发,所以网卡配置如下:

  • 网卡配置
root@rion:~/tools# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
  	# ens33 每个系统的网卡名字不一样,通过ifconfig来查看系统的网卡名称
    ens33:
      dhcp4: false
  bridges:
    br0:
      interfaces: [ens33]
      dhcp4: false
      addresses: [192.168.1.101/24]
      gateway4: 192.168.1.2
      nameservers:
        addresses: [192.168.1.2,114.114.114.114]
  version: 2
  • 重启网络

    netplan apply
    
  • 测试是否配置成功

    发现已经成功添加了虚拟网卡,并设置了有效IP地址,且能够ping通百度。

    root@rion:~/tools# ifconfig
    br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::1408:35ff:fe6b:956c  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:ad:3e:d7  txqueuelen 1000  (Ethernet)
            RX packets 19  bytes 1252 (1.2 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 21  bytes 1918 (1.9 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            ether 00:0c:29:ad:3e:d7  txqueuelen 1000  (Ethernet)
            RX packets 2241784  bytes 3223246745 (3.2 GB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 380027  bytes 24712333 (24.7 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 188  bytes 15876 (15.8 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 188  bytes 15876 (15.8 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:16:f4:73  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    root@rion:~/tools# ping baidu.com
    PING baidu.com (220.181.38.148) 56(84) bytes of data.
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=108 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=121 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=145 ms
    

3.1.3 创建虚拟机

  • 创建虚拟机
virt-install -n ubuntu20_01\
  -r 2048 --vcpus=2 \
  --os-variant=ubuntu20.04 \
  --accelerate \
  -c /root/tools/ubuntu20.04-live.iso \
  --disk path=/home/vmsets/ubuntu20_01.img,format=qcow2,bus=virtio,size=30 \
  --network bridge=br0 \
  --vnc --vncport=5996 --vnclisten=0.0.0.0
  • 参数介绍
-n:指定生成的虚拟机名称,全局唯一
--os-type=linux	:不需要添加了,已删除配置
-os-variant:参数支持的os  		# osinfo-query os 查看系统支持的系统
-c:引用的iso文件
-r:内存大小
--vcpus:系统核心数
--disk:生成的磁盘文件存储目录
--format:磁盘格式
--network:指定网络模式,及其网卡
--size:磁盘空间大小
--vnc:指定宿主机开放的监控端口和监听地址(0.0.0.0 表示监听所有的IP),可以使用vnc连接系统
--accelerate 快速生成
  • 注意

    如果出现以下报错信息

    ERROR    internal error: process exited while connecting to monitor: 2022-07-02T06:46:48.302110Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/root/tools/ubuntu20.04-live.iso","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/root/tools/ubuntu20.04-live.iso': Permission denied
    

    修改 /etc/libvirt/qemu.conf,去掉 user 和 group 两行前的注释

在这里插入图片描述

然后重启 libvirtd

service libvirtd restart

3.1.4 安装Ubuntu系统

此时,这是在Ubuntu中使用KVM创建的ubuntu20_01 系统,如果上一小节配置无误则会弹出一个系统安装界面
在这里插入图片描述

安装即可,记得开启ssh服务

在这里插入图片描述

3.1.5 修改IP地址

  • virt-manager 管理工具

    查看宿主机中的所有虚拟机情况,执行后会出现图形管理界面

    virt-manager
    

在这里插入图片描述

  • 设置静态IP & 测试

    开始ping 百度,可能会不同,过一下下就可以了。

在这里插入图片描述

四、Virsh 管理工具

在安装之后,可以通过virsh来管理我们的虚拟机,常用命令如下

  • 常用命令

    virsh list					# 查看所有的虚拟机
    virsh start ubuntu20_01		# 启动虚拟机
    virsh shutdown ubuntu20_01	# 关闭虚拟机
    virsh destroy ubuntu20_01	# 清除Domain中的记录,同时也会强制关机
    virsh edit ubuntu20_01		# 编辑虚拟机的配置文件,虚拟机中的参数是以配置文件的形式来控制的
    virsh define ubuntu20_01	# 重载配置文件
    virsh undefine ubuntu20_01  # 删除配置文件
    
  • 系统配置文件

    cat /etc/libvirt/qemu/ubuntu20_01.xml

### 配置文件:cat /etc/libvirt/qemu/ubuntu20_01.xml ###
<domain type='qemu'>
  <name>ubuntu20_01</name>
  <uuid>3da6f3cd-4f8b-438a-b067-27b12e11f3d5</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://ubuntu.com/ubuntu/20.04"/>
    </libosinfo:libosinfo>
  </metadata>
  ##### 内存配置 ####
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  #### CPU核心数 ####
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-q35-4.2'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='none'>
    <model fallback='forbid'>qemu64</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>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    
    #### 磁盘配置 ####
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/vmsets/ubuntu20_01.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='sda' bus='sata'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
	......
	
	#### 网络配置 ####
    <interface type='bridge'>	# NAT网络: type='network'
      <mac address='52:54:00:c9:8a:62'/>
      <source bridge='br0'/>	# NAT网络: network='br0'
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>
    .....
  </devices>
</domain>

五、VNC

VNC(Virtual Network Computing)是一套实现远程网络操控的软件

那么我们为什么要使用VNC呢,细心的小伙伴应该注意到我们在安装系统时,通过--vnc --vncport=5996 --vnclisten=0.0.0.0 来开启了系统的5996端口用来被外部监听。

VNC 可以简单的理解为远程连接工具,可以实现对多台设备之间的管理,特别是对于Windows系统时,ssh无法连接,就可以使用VNC来连接Windows虚拟机。

KVM虚拟机安装后面看看有时间再出个详细教程,主要注意2个地方

  • 一个是创建镜像时,指定的系统它的--os-variant=win2k8,通过前面的指令可以查看

  • 另一个就是在安装时需要配置额外的驱动程序

  • 下载VNC软件

    大家百度下载VNC Viewer软件,
    在这里插入图片描述

  • 填写信息

    填写好宿主机的IP地址,和前面写的监听端口号

在这里插入图片描述

连接成功后,进入管理系统界面

在这里插入图片描述

参考链接:https://zhuanlan.zhihu.com/p/105499858
https://zhuanlan.zhihu.com/p/105499858

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值