KVM(kernel-based VM)和它的伙伴(三)

举报
黄生 发表于 2023/01/02 21:03:06 2023/01/02
【摘要】 上接KVM(kernel-based VM)和它的伙伴(二)storage pool就是libvirt存放镜像的地方。virsh pool-list Name State Autostart------------------------------- default active yes #一般在/var/lib/libvirt/images下面 hii-img ...

上接
KVM(kernel-based VM)和它的伙伴(二)

storage pool就是libvirt存放镜像的地方。

image.png


virsh pool-list
 Name      State    Autostart
-------------------------------
 default   active   yes #一般在/var/lib/libvirt/images下面
 hii-img   active   yes
 vm        active   yes

virsh pool-info default
Name:           default
UUID:           817f7fec-f248-4835-bea4-3581b878f65e
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       91.11 GiB
Allocation:     9.83 GiB
Available:      81.28 GiB

virsh pool-dumpxml default

在defalut pool里新建一个volumn就是一个镜像存储文件。

HA>virsh vol-create-as default cirros-image.qcow2 128M --format qcow2
Vol cirros-image.qcow2 created

HA>virsh vol-list --pool=default
 Name                 Path
------------------------------------------------------------------
 cirros-image.qcow2   /var/lib/libvirt/images/cirros-image.qcow2

HA>sudo qemu-img info /var/lib/libvirt/images/cirros-image.qcow2
image: /var/lib/libvirt/images/cirros-image.qcow2
file format: qcow2
virtual size: 128 MiB (134217728 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 0.10
    refcount bits: 16

#用下载的cirros镜像文件来填充这个vloumn    
HA>virsh vol-upload cirros-image.qcow2 ~/cirros-0.4.0-x86_64-disk.img --pool default
 
HA>sudo qemu-img info /var/lib/libvirt/images/cirros-image.qcow2
image: /var/lib/libvirt/images/cirros-image.qcow2
file format: qcow2
virtual size: 44 MiB (46137344 bytes)
disk size: 12.1 MiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

然后配置一下网络

HA>cat  /tmp/test-network.xml
<network>
  <name>test-network</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr-test' stp='on' delay='0'/>
  <ip address='192.168.200.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.200.2' end='192.168.200.254'/>
    </dhcp>
  </ip>
</network>

HA>virsh net-define /tmp/test-network.xml
Network test-network defined from /tmp/test-network.xml

HA>virsh net-start test-network
Network test-network started

#启动成功后看一下网络规则
HA>sudo iptables -S -t nat
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-N LIBVIRT_PRT
-A POSTROUTING -j LIBVIRT_PRT

-A LIBVIRT_PRT -s 192.168.200.0/24 -d 224.0.0.0/24 -j RETURN
-A LIBVIRT_PRT -s 192.168.200.0/24 -d 255.255.255.255/32 -j RETURN
-A LIBVIRT_PRT -s 192.168.200.0/24 ! -d 192.168.200.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.200.0/24 ! -d 192.168.200.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.200.0/24 ! -d 192.168.200.0/24 -j MASQUERADE

# 200 是上面新建的;下面 122是默认安装建立的

-A LIBVIRT_PRT -s 192.168.122.0/24 -d 224.0.0.0/24 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 -d 255.255.255.255/32 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE

HA>brctl show virbr-test
bridge name	bridge id		STP enabled	interfaces
virbr-test		8000.52540092e182	yes		virbr-test-nic

HA>ip addr show dev virbr-test
23: virbr-test: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:92:e1:82 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.1/24 brd 192.168.200.255 scope global virbr-test
       valid_lft forever preferred_lft forever

HA>ip addr show dev virbr0
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:fa:a1:17 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

HA>ps ax|grep dnsmasq
   7301 ?        S      0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
   7302 ?        S      0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
2158247 ?        S      0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/test-network.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
2158248 ?        S      0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/test-network.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper

HA>sudo cat /var/lib/libvirt/dnsmasq/test-network.conf 
##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
##    virsh net-edit test-network
## or other application using the libvirt API.
##
## dnsmasq conf file created by libvirt
strict-order
user=libvirt-dnsmasq
pid-file=/run/libvirt/network/test-network.pid
except-interface=lo
bind-dynamic
interface=virbr-test
dhcp-range=192.168.200.2,192.168.200.254,255.255.255.0
dhcp-no-override
dhcp-authoritative
dhcp-lease-max=253
dhcp-hostsfile=/var/lib/libvirt/dnsmasq/test-network.hostsfile
addn-hosts=/var/lib/libvirt/dnsmasq/test-network.addnhosts

sudo virt-install \
  --name test-instance \
  --memory 512 \
  --vcpus 1 \
  --import \
  --disk vol=default/cirros-image.qcow2,format=qcow2,bus=virtio \
  --network network=test-network \
  --graphics vnc,keymap=local --noautoconsole 
  
WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

Starting install...
Domain creation completed.

HA>virsh list
 Id   Name            State
-------------------------------
 19   test-instance   running
#以上都不需要图形界面的

然后到VMM里open看一下

image.png

这个cirros是为云准备的,在KVM里启动会很慢,所以耐心等待,要好几分钟。登录后,网络非常的正常,可以访问到外面,当然是NAT,外面看不到里面的虚拟机。

参考:https://leftasexercise.com/2020/05/18/managing-kvm-virtual-machines-part-ii-the-libvirt-toolkit/

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。