libvirt环境搭建与使用

libvirt环境搭建与使用

环境搭建

yum remove libvirt

yum -y install gcc yajl-devel libxml2-devel device-mapper-devel libpciaccess-devel libnl-devel

//源码安装尝试
wget http://libvirt.org/sources/libvirt-1.2.21.tar.gz

checking for perl... no
configure: error: Failed to find perl.

//好像没啥用
//直接使用yum安装
yum install qemu-kvm libvirt virt-install bridge-utils virt-manager
//安装成功

libvirtd会创建一个virbr0网桥并挂载virbr0-nic

[root@localhost libvirt-1.2.21]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:27:2b:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.131.177/24 brd 192.168.131.255 scope global noprefixroute dynamic ens33
       valid_lft 1564sec preferred_lft 1564sec
    inet6 fe80::abac:3ad0:dac:b19b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:c7:76:fa:98 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:1e:5d:0c brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:1e:5d:0c brd ff:ff:ff:ff:ff:ff

virt-manager 使用

启动virt-manager

#virt-manager

(virt-manager:14788): Gtk-WARNING **: 20:14:18.377: cannot open display:

yum -y install xorg-x11-font-utils //没啥用
//验证
#grep X11Forwarding /etc/ssh/sshd_config
 X11Forwarding yes
//中间又安装了一些东西,但可能是重启解决了问题
#virt-manager

virt-manager页面

virt-manager页面

创建虚拟机

相关参数确定

相关参数确定

相关参数确定

相关参数确定

相关参数确定

相关参数确定

相关参数确定

相关参数确定

相关参数确定

安装完成

删除虚拟机

删除该虚拟机镜像

删除该虚拟机镜像

virsh创建虚拟机

创建磁盘镜像

qemu-img create -f qcow2 /opt/image/centos7-1.qcow2 30g

iso镜像

iso镜像

编写相关xml文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//查看相关宿主机参数
yum install qemu-system-x86

vi vm0.xml

<domain type='kvm'>

  <name>centos7-zal</name> #虚机名称
  <memory unit='KiB'>1048576</memory> #虚拟机内存
  <currentMemory unit='KiB'>1048576</currentMemory>#虚拟机可用内存

  <vcpu placement='static'>1</vcpu>#虚拟机cpu个数

  <os>

    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>  #机器类型

    <boot dev='cdrom'/>#启动盘

  </os>

  <features>

    <acpi/>

    <apic/>

  </features>

  <clock offset='localtime'/>  //虚拟机时钟设置,这里表示本地本机时间

  <cpu mode='custom' match='exact'>

    <model fallback='allow'>Nehalem</model>

  </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='/opt/image/centos7-zal.qcow2'/>#虚拟硬盘路径

      <target dev='hda' bus='ide'/>

    </disk>

    <disk type='file' device='cdrom'>

    <source file='/root/zal/CentOS-7-x86_64-Minimal-2009.iso'/>#镜像路径

    <target dev='vda' bus='virtio'/>

    </disk>
<interface type='network'>

#      <mac address='52:54:00:43:5b:60'/>

      <source network='default' bridge='virBR0'/>

    </interface>

    <input type='tablet' bus='usb'/>

    <input type='mouse' bus='ps2'/>

    <input type='keyboard' bus='ps2'/>

    <graphics type='vnc' port='5906' autopart='no' listen='0.0.0.0' keymap='en-us'/>#port,端口号

  </devices>

</domain>

创建磁盘镜像

qemu-img create -f qcow2 /opt/image/centos7-zal.qcow2 30g

启动虚拟机

virsh define vm0.xml

出现报错:

error: failed to connect to the hypervisor
error: no valid connection
error: Failed to connect socket to '/usr/local/var/run/libvirt/libvirt-sock': No such file or directory
systemctl restart libvirtd

systemctl status libvirtd

好像没有问题

网上有人说可以做个链接

在/var/run/libvirt/有该文件

[root@localhost libvirt]# ls
hostdevmgr  libvirt-admin-sock  libvirt-sock  libvirt-sock-ro  lxc  network  nwfilter-binding  qemu  storage  virtlockd-sock  virtlogd-admin-sock  virtlogd-sock
[root@localhost libvirt]# ln -s /var/run/libvirt/libvirt-sock /usr/local/var/run/libvirt/libvirt-sock

再次执行virsh define vm0.xml没有报错

[root@localhost zal]# virsh define vm0.xml
Domain centos7-zal defined from vm0.xml
[root@localhost zal]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-zal                    shut off

启动虚拟机

[root@localhost zal]# virsh start centos7-zal
Domain centos7-zal started

[root@localhost zal]# virsh list
 Id    Name                           State
----------------------------------------------------
 1     centos7-zal                    running

连接虚拟机

在宿主机(win10)安装vnc

打开vnc连接虚拟机(port为xml中定义)

进入系统

进入系统

显示安装页面

显示安装页面

取消虚拟机

virsh undefine centos7-1
updatedupdated2023-02-282023-02-28