【OS】实验九 内核的虚拟化

385 阅读2分钟

实验九 内核的虚拟化

实验任务

任务一:搭建openEuler系统的qemu虚拟机 任务二:搭建和使用docker

实验步骤及结果

虚拟化环境准备

yum install libvirt qemu

img

验证安装是否成功

查看内核是否支持KVM虚拟化

ls /sys/module/kvmrpm -qi qemurpm -qi libvirt

img

img

确认 QEMU libvirt 是否安装成功。若安装成功则可以看到QEMU软件包信息

image-20211226221140116

启动libvirtd服务

 systemctl start libvirtd

查看libvirt服务是否启动成功;

systemctl status libvirtd

image-20211226221431507

网络配置

安装完libvirt后,libvirt会自动生成default虚拟网络virtbr0: image-20211226221519305

准备虚拟机安装引导固件

openEuler默认已安装BIOS启动对应的引导文件,不需要用户额外操作。此处使用UEFI方式引导,需要安装工具集EDK II;AArch64架构对应的安装包为edk2-aarch64:

yum install edk2-ovmf

img

准备虚拟机镜像

image-20211226221802308

yum install -y qemu-img

使用root用户安装qemu-img软件包

img 使用qemu-img工具的create命令,创建镜像文件,命令格式为:

qemu-img create -f <imgFormat> -o <fileOption> <fileName> <diskSize>

img

准备虚拟机的配置镜像virsh define vm_openEuler.xml

<domain type='kvm'>
  <name>openEulerVM</name>
  <memory unit='KiB'>8388608</memory>
  <currentMemory unit='KiB'>8388608</currentMemory>
  <vcpu placement='static'>4</vcpu>
  <iothreads>1</iothreads>
  <os>
    <type arch='x86_64' machine='pc-i440fx-4.0'>hvm</type>
  </os>
  <features>
    <acpi/>
  </features>
  <cpu mode='host-passthrough' check='none'>
    <topology sockets='2' cores='2' threads='1'/>
  </cpu>
  <clock offset='utc'/>
  <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' iothread='1'/>
      <source file='/openEuler-image.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <boot order='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>
    <controller type='scsi' index='0' model='virtio-scsi'>
    </controller>
    <controller type='virtio-serial' index='0'>
    </controller>
    <controller type='usb' index='0' model='ehci'>
    </controller>
    <controller type='sata' index='0'>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <interface type='bridge'>
      <mac address='52:54:00:c1:c4:23'/>
      <source bridge='virbr0'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='keyboard' bus='usb'>
      <address type='usb' bus='0' port='2'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='vga' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
    </memballoon>
  </devices>
</domain>

img

创建虚拟机

virsh define vm_openEuler.xml

img

启动虚拟机

virsh start openEulerVM

关闭虚拟机

virsh destroy openEulerVM

删除虚拟机

virsh undefined openEulerVM

任务2:在树莓派中搭建和使用docker

安装使用docker

安装docker

yum install docker

启动

启动容器有两种方式:

l 一种是基于镜像新建一个容器并启动;

l 另外一个是将在终止状态(stopped)的容器重新启动。

新建并启动

所需要的命令主要为 docker run

说明:

-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上;

-i 则让容器的标准输入保持打开。

启动已终止容器

利用 docker start container_id 或 docker start NAMES命令,启动运行一个已经终止的容器。

终止容器

使用 docker stop NAMES或docker stop container_id来终止一个运行中的容器

删除容器

使用 docker rm NAMES 或 docker rm container_id来删除一个处于终止状态的容器

编写Dockerfile 并创建镜像

mkdir docker_test #新建空目录
cd docker_test/
vim Dockerfile #编辑文件内容,见后文
docker image build -t helloworld_ubuntu ./ #创建镜像
sudo docker run --name test helloworld_ubuntu:latest  #镜像创建完成后,启动容器

Dockerfile文件内容:

# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..

# Base image to use, this must be set as the first line
FROM ubuntu

# Maintainer: docker_user <docker_user at email.com> (@docker_user)
MAINTAINER docker_user docker_user@email.com

# Commands to update the image
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list

# Commands when creating a new container
CMD echo “Hello world!”

image-20211226214142950