In the last blog, we mainly talked about the usage and instructions of the command of virsh managing KVM virtual machine. Please refer to the review https://www.cnblogs.com/qiuhom-1874/p/13508231.html Today we will continue to talk about the topic of virsh managing KVM virtual machine;
1. Virsh monitoring KVM virtual machine related commands
Virsh domblklist: lists disk information on the specified virtual machine
[[email protected] ~]# virsh help domblklist
NAME
Domblklist - lists all domain blocks
SYNOPSIS
domblklist [--inactive] [--details]
DESCRIPTION
Get domain block device summary
OPTIONS
[--domain] domain name, id or uuid
--Inactive gets the configuration that is inactive rather than running
--Additional display of details type and device values
[[email protected] ~]# virsh list --all
ID name status
----------------------------------------------------
3 centos7.0 running
4 c2 running
6 generic running
[[email protected] ~]# virsh domblklist c2
Target source
------------------------------------------------
vda /kvm/images/c2.qcow2
hda -
[[email protected] ~]# virsh domblklist generic
Target source
------------------------------------------------
hda /kvm/images/cirros-0.5.0-x86_64-disk.img
[[email protected] ~]#
Virsh domiflist: lists all network cards on the specified virtual machine
[[email protected] ~]# virsh help domiflist
NAME
Domiflist - lists all domain virtual interfaces
SYNOPSIS
domiflist [--inactive]
DESCRIPTION
Get domain virtual interface summary
OPTIONS
[--domain] domain name, id or uuid
--Inactive gets the configuration that is inactive rather than running
[[email protected] ~]# virsh domiflist generic
Interface type source model mac
-------------------------------------------------------
vnet2 network default rtl8139 52:54:00:3e:3f:71
[[email protected] ~]# virsh domiflist centos7.0
Interface type source model mac
-------------------------------------------------------
vnet0 network default virtio 52:54:00:e3:8d:11
[[email protected] ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.0.41 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fe9a:dbd6 prefixlen 64 scopeid 0x20
ether 00:0c:29:9a:db:d6 txqueuelen 1000 (Ethernet)
RX packets 113557 bytes 34709850 (33.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 111108 bytes 27361452 (26.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 132700 bytes 348628216 (332.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 132700 bytes 348628216 (332.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4163 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:45:06:15 txqueuelen 1000 (Ethernet)
RX packets 2042 bytes 130515 (127.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1028 bytes 83137 (81.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vnet0: flags=4163 mtu 1500
inet6 fe80::fc54:ff:fee3:8d11 prefixlen 64 scopeid 0x20
ether fe:54:00:e3:8d:11 txqueuelen 1000 (Ethernet)
RX packets 571 bytes 46596 (45.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11055 bytes 593226 (579.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vnet1: flags=4163 mtu 1500
inet6 fe80::fc54:ff:fee3:8521 prefixlen 64 scopeid 0x20
ether fe:54:00:e3:85:21 txqueuelen 1000 (Ethernet)
RX packets 314 bytes 24074 (23.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6210 bytes 333740 (325.9 KiB)
TX errors 0 dropped 4072 overruns 0 carrier 0 collisions 0
vnet2: flags=4163 mtu 1500
inet6 fe80::fc54:ff:fe3e:3f71 prefixlen 64 scopeid 0x20
ether fe:54:00:3e:3f:71 txqueuelen 1000 (Ethernet)
RX packets 741 bytes 56211 (54.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4711 bytes 248155 (242.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]#
Tip: from the above information, we can see that all the network cards on the KVM virtual machine correspond to a virtual network card on the host computer;
Virsh domifaddr: gets the IP address on the specified virtual machine
[[email protected] ~]# virsh help domifaddr
NAME
domifaddr - Get network interfaces' addresses for a running domain
SYNOPSIS
domifaddr [--interface ] [--full] [--source ]
DESCRIPTION
Get network interfaces' addresses for a running domain
OPTIONS
[--domain] domain name, id or uuid
--interface network interface name
--full always display names and MACs of interfaces
--source address source: 'lease', 'agent', or 'arp'
[[email protected] ~]# virsh domifaddr c2
Name MAC address protocol address
-------------------------------------------------------------------------------
vnet1 52:54:00:e3:85:21 ipv4 192.168.122.168/24
[[email protected] ~]# virsh domifaddr centos7.0
Name MAC address protocol address
-------------------------------------------------------------------------------
vnet0 52:54:00:e3:8d:11 ipv4 192.168.122.222/24
[[email protected] ~]# virsh domifaddr generic
Name MAC address protocol address
-------------------------------------------------------------------------------
vnet2 52:54:00:3e:3f:71 ipv4 192.168.122.4/24
[[email protected] ~]#
Virsh domifstat: counts the status information of the specified network card on the specified virtual machine
[[email protected] ~]# virsh help domifstat
NAME
Domifstat - get domain network interface status
SYNOPSIS
domifstat
DESCRIPTION
Get the network interface status of the running domain.
OPTIONS
[--domain] domain name, id or uuid
[--interface] interface device specified by name or MAC Address
[[email protected] ~]# virsh domiflist c2
Interface type source model mac
-------------------------------------------------------
vnet1 network default virtio 52:54:00:e3:85:21
[[email protected] ~]# virsh domifstat c2 vnet1
vnet1 rx_bytes 345156
vnet1 rx_packets 6429
vnet1 rx_errs 0
vnet1 rx_drop 4072
vnet1 tx_bytes 24206
vnet1 tx_packets 316
vnet1 tx_errs 0
vnet1 tx_drop 0
[[email protected] ~]#
Virsh dominfo: gets the brief information of the specified virtual machine
[[email protected] ~]# virsh help dominfo
NAME
Dominfo - domain information
SYNOPSIS
dominfo
DESCRIPTION
Returns the basic information of this field.
OPTIONS
[--domain] domain name, id or uuid
[[email protected] ~]# virsh dominfo c2
Id: 4
Name: C2
UUID: 10d61aa8-b0fa-4433-a733-00a8721bee01
OS type: HVM
Status: running
CPU: 2
CPU time: 188.1s
Maximum memory: 2097152 KIB
Memory used: 2097152 KIB
Lasting: Yes
Auto start: Disabled
Managed save: no
Security mode: None
Security doi: 0
[[email protected] ~]#
Virsh dommemstat: gets the memory related state information of the specified virtual machine
[[email protected] ~]# virsh help dommemstat
NAME
Dommemstat - gets the memory statistics of the domain
SYNOPSIS
dommemstat [--period ] [--config] [--live] [--current]
DESCRIPTION
Get the memory statistics of the running domain
OPTIONS
[--domain] domain name, id or uuid
--Period sets the collection period in seconds
--Config affects next boot
--Live affects the domain in which it runs
--Current affects the current domain
[[email protected] ~]# virsh dommemstat c2
actual 2097152
swap_in 0
swap_out 0
major_fault 201
minor_fault 176924
unused 1761832
available 1883456
last_update 1597490486
rss 455384
[[email protected] ~]# virsh dommemstat centos7.0
actual 2097152
swap_in 0
swap_out 0
major_fault 179
minor_fault 169193
unused 1761272
available 1883456
last_update 1597480749
rss 560324
[[email protected] ~]#
Virsh domstate: gets the state information of the specified virtual machine
[[email protected] ~]# virsh help domstate
NAME
Domstate - domain state
SYNOPSIS
domstate [--reason]
DESCRIPTION
Returns the status of a domain.
OPTIONS
[--domain] domain name, id or uuid
--Reason output status reason at the same time
[[email protected] ~]# virsh domstate centos7.0
running
[[email protected] ~]# virsh domstate centos7.0 --reason
Running (booted)
[[email protected] ~]#
Virsh domstats: displays the detailed status information of the specified virtual machine. If the virtual machine name is not specified, it displays the detailed status information of all virtual machines on the current host
[[email protected] ~]# virsh help domstats
NAME
domstats - get statistics about one or multiple domains
SYNOPSIS
domstats [--state] [--cpu-total] [--balloon] [--vcpu] [--interface] [--block] [--perf] [--list-active] [--list-inactive] [--list-persistent] [--list-transient] [--list-running] [--list-paused] [--list-shutoff] [--list-other] [--raw] [--enforce] [--backing] [--nowait] []...
DESCRIPTION
Gets statistics about one or more (or all) domains
OPTIONS
--state report domain state
--cpu-total report domain physical cpu usage
--balloon report domain balloon statistics
--vcpu report domain virtual cpu information
--interface report domain network interface information
--block report domain block device statistics
--perf report domain perf event statistics
--list-active list only active domains
--list-inactive list only inactive domains
--list-persistent list only persistent domains
--list-transient list only transient domains
--list-running list only running domains
--list-paused list only paused domains
--list-shutoff list only shutoff domains
--list-other list only domains in other states
--raw do not pretty-print the fields
--enforce enforce requested stats parameters
--backing add backing chain information to block stats
--nowait report only stats that are accessible instantly
list of domains to get stats for
[[email protected] ~]# virsh domstats centos7.0
Domain: 'centos7.0'
state.state=1
state.reason=1
cpu.time=108764965089
cpu.user=850000000
cpu.system=14010000000
balloon.current=2097152
balloon.maximum=2097152
balloon.swap_in=0
balloon.swap_out=0
balloon.major_fault=187
balloon.minor_fault=174029
balloon.unused=1758704
balloon.available=1883456
balloon.last-update=1597581679
balloon.rss=561212
vcpu.current=2
vcpu.maximum=2
vcpu.0.state=1
vcpu.0.time=39050000000
vcpu.0.wait=0
vcpu.1.state=1
vcpu.1.time=14570000000
vcpu.1.wait=0
net.count=1
net.0.name=vnet1
net.0.rx.bytes=142006
net.0.rx.pkts=2577
net.0.rx.errs=0
net.0.rx.drop=0
net.0.tx.bytes=17972
net.0.tx.pkts=222
net.0.tx.errs=0
net.0.tx.drop=0
block.count=2
block.0.name=vda
block.0.path=/var/lib/libvirt/images/centos7.0.qcow2
block.0.rd.reqs=6517
block.0.rd.bytes=150312448
block.0.rd.times=32776473520
block.0.wr.reqs=647
block.0.wr.bytes=13009920
block.0.wr.times=10069609744
block.0.fl.reqs=238
block.0.fl.times=938612582
block.0.allocation=16855924224
block.0.capacity=21474836480
block.0.physical=21478379520
block.1.name=hda
block.1.rd.reqs=3
block.1.rd.bytes=82
block.1.rd.times=227269
block.1.wr.reqs=0
block.1.wr.bytes=0
block.1.wr.times=0
block.1.fl.reqs=0
block.1.fl.times=0
block.1.allocation=0
block.1.physical=0
[[email protected] ~]#
Tip: if you want to get the detailed information of an object, you can filter it by following the corresponding options;
[[email protected] ~]# virsh domstats centos7.0 --interface --balloon
Domain: 'centos7.0'
balloon.current=2097152
balloon.maximum=2097152
balloon.swap_in=0
balloon.swap_out=0
balloon.major_fault=187
balloon.minor_fault=174029
balloon.unused=1758704
balloon.available=1883456
balloon.last-update=1597581679
balloon.rss=561468
net.count=1
net.0.name=vnet1
net.0.rx.bytes=148978
net.0.rx.pkts=2710
net.0.rx.errs=0
net.0.rx.drop=0
net.0.tx.bytes=18326
net.0.tx.pkts=227
net.0.tx.errs=0
net.0.tx.drop=0
[[email protected] ~]#
Tip: if multiple options are specified, the relationship between them is or, which means that only the contents of the specified options are displayed;
Virsh domif getlink: gets the specified interface state of the specified virtual machine
[[email protected] ~]# virsh help domif-getlink
NAME
Domif getlink - get virtual interface link status
SYNOPSIS
domif-getlink [--config]
DESCRIPTION
Get domain virtual interface link status
OPTIONS
[--domain] domain name, id or uuid
[-- interface] interface failure (MAC address)
--Config gets the persistent interface state
[[email protected] ~]# virsh domif-getlink c2 vnet1
vnet1 up
[[email protected] ~]#
Virsh domblkinfo: gets the disk size information of the specified virtual machine
[[email protected] ~]# virsh domblkinfo centos7.0
Error: Command 'domblkinfo' requirements option
[[email protected] ~]#
[[email protected] ~]# virsh help domblkinfo
NAME
Domblkinfo - domain block device size information
SYNOPSIS
domblkinfo [--device ] [--human] [--all]
DESCRIPTION
Get domain block device size information
OPTIONS
[--domain] domain name, id or uuid
--Device block device
--human Human readable output
--all display all block devices info
[[email protected] ~]# virsh domblklist c2
Target source
------------------------------------------------
vda /kvm/images/c2.qcow2
hda -
[[email protected] ~]# virsh domblkinfo c2 vda
Capacity: 21474836480
Distribution: 21478375424
Physics: 21478375424
[[email protected] ~]# virsh domblkinfo c2 vda --human
Capacity: 20.000 gib
Allocation: 20.003 gib
Physics: 20.003 gib
[[email protected] ~]#
Virsh domblkerr: lists the error messages for the specified disk on the specified virtual machine
[[email protected] ~]# virsh help domblkerror
NAME
Domblkerr - display errors in block devices
SYNOPSIS
domblkerror
DESCRIPTION
Display block device error
OPTIONS
[--domain] domain name, id or uuid
[[email protected] ~]# virsh domblkerror c2
error not found
[[email protected] ~]# virsh domblkerror centos7.0
error not found
[[email protected] ~]#
Virsh domcontrol: gets the state information of the control interface on the specified virtual machine
[[email protected] ~]# virsh help domcontrol
NAME
Domcontrol - domain control interface state
SYNOPSIS
domcontrol
DESCRIPTION
Returns the control interface state to the domain
OPTIONS
[--domain] domain name, id or uuid
[[email protected] ~]# virsh domcontrol centos7.0
determine
[[email protected] ~]# virsh domcontrol generic
determine
[[email protected] ~]#
Tip: what I’m showing here is Chinese, and the normal situation is OK;
The above commands are mainly used to monitor various state related commands on virtual machines. We can write scripts based on the above commands, and then obtain the state information of multiple virtual machines in batch;
2. Virsh management network related commands
Virsh net list: List networks
[[email protected] ~]# virsh help net-list
NAME
Net list - list networks
SYNOPSIS
net-list [--inactive] [--all] [--persistent] [--transient] [--autostart] [--no-autostart] [--uuid] [--name] [--table]
DESCRIPTION
Return to network list
OPTIONS
--Inactive lists inactive networks
--All lists inactive and active networks
--Persistent lists persistent networks
--Transient lists temporary networks
--Autostart lists the networks that have autostart enabled
--No autostart lists the networks that have autostart disabled
--UUID only UUIDs are listed
--name list network names only
--Table list table (default)
[[email protected] ~]# virsh net-list
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
[[email protected] ~]#
Tip: by default, the active network is listed in the form of a table. If you want to specify the network in a certain state, you can follow the options. From the above information, you can see that there is only one default network, which is active and starts automatically;
Virsh net dumpxml: displays the content of the configuration file of the specified network to the standard output;
[[email protected] ~]# virsh help net-dumpxml
NAME
Net dumpxml - network information in XML
SYNOPSIS
net-dumpxml [--inactive]
DESCRIPTION
Output the network information as an XML to stdout.
OPTIONS
[-- network] network name or UUID
--Inactive displays the XML defined by inactivity
[[email protected] ~]# virsh net-dumpxml default
default
958c004e-7aa9-483e-a79e-c62884e88a08
[[email protected] ~]#
Tip: with the above command, we can export a specified network, and then use the network file as a template file. If we want to create a network, we can directly copy the template, and then modify the private information to generate a configuration file of the network we need to create;
Virsh net define: define a network from the specified XML configuration file or modify the existing network based on the specified XML; similar to the usage of virsh define command;
Example: import the existing configuration, modify the IP address, and then create a network using virsh net define based on the exported configuration
[[email protected] ~]# virsh net-dumpxml default >default.xml
[[email protected] ~]# cp default.xml test.xml
[[email protected] ~]# diff default.xml test.xml
2,3c2
< default
< 958c004e-7aa9-483e-a79e-c62884e88a08
---
> test
9,11c8,10
<
<
<
---
>
>
>
13c12
<
---
>
[[email protected] ~]# cat test.xml
test
[[email protected] ~]#
Tip: usually we need to modify the name of the network, delete UUID, let it be generated automatically when it is created, specify IP address, modify MAC address and DHCP address pool, bridge name and network type;
[[email protected] ~]# virsh help net-define
NAME
net-define - define an inactive persistent virtual network or modify an existing persistent one from an XML file
SYNOPSIS
net-define
DESCRIPTION
Define or modify a persistent virtual network.
OPTIONS
The [-- file] file contains an XML network description
[[email protected] ~]# virsh net-define test.xml
Define network from test test.xml
[[email protected] ~]# virsh net-list
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test inactive no yes
[[email protected] ~]#
Tip: define a network by default, and it will not be activated;
Virsh net start: activate a specified inactive network
[[email protected] ~]# virsh help net-start
NAME
Net start - start an inactive network (previously defined)
SYNOPSIS
net-start
DESCRIPTION
Start a network
OPTIONS
[-- network] network name or UUID
[[email protected] ~]# virsh net-start test
Network test started
[[email protected] ~]# virsh net-list
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
[[email protected] ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:db:d6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.41/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9a:dbd6/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: mtu 1500 qdisc noqueue state UP qlen 1000
link/ether 52:54:00:45:06:15 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
4: virbr0-nic: mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:45:06:15 brd ff:ff:ff:ff:ff:ff
5: vnet0: mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN qlen 1000
link/ether fe:54:00:3e:3f:71 brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fe3e:3f71/64 scope link
valid_lft forever preferred_lft forever
6: vnet1: mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN qlen 1000
link/ether fe:54:00:e3:8d:11 brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fee3:8d11/64 scope link
valid_lft forever preferred_lft forever
7: vnet2: mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN qlen 1000
link/ether fe:54:00:e3:85:21 brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fee3:8521/64 scope link
valid_lft forever preferred_lft forever
8: virbr1: mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:45:06:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.12.1/24 brd 192.168.12.255 scope global virbr1
valid_lft forever preferred_lft forever
9: virbr1-nic: mtu 1500 qdisc pfifo_fast master virbr1 state DOWN qlen 1000
link/ether 52:54:00:45:06:16 brd ff:ff:ff:ff:ff:ff
[[email protected] ~]#
Tip: you can see that the specified test network has been activated and a virbr1 bridge and a virbr1 NIC network card have been created on the host computer. In fact, virbr1 is the bridge specified in the configuration file. The bridge specified in the configuration file must not be used. If the specified bridge does not exist, it will be created, and the virbr1 NIC network card is directly bridged to virbr1;
Test: create a virtual machine named C3 based on test network
[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# ls
autostart c2.xml centos7.0.xml generic.xml networks
[[email protected] qemu]# cp c2.xml c3.xml
[[email protected] qemu]# cd /kvm/images/
[[email protected] images]# ls
c1.qcow2 c2.qcow2 cirros-0.5.0-x86_64-disk.img
[[email protected] images]# cp c2.qcow2 c3.qcow2
[[email protected] images]# diff /etc/libvirt/qemu/c2.xml /etc/libvirt/qemu/c3.xml
9c9
< c2
---
> c3
40c40
<
---
>
73,74c73,74
<
<
---
>
>
[[email protected] images]# virsh define /etc/libvirt/qemu/c3.xml
Domain C3 (from / etc / libvirt / QEMU / C3. XML)
[[email protected] images]# virsh list --all
ID name status
----------------------------------------------------
1 generic running
2 centos7.0 running
3 c2 running
-C3 off
[[email protected] images]# virsh start c3
Domain C3 started
[[email protected] images]#
Verification: connect to C3 console and check whether its IP address is test network? Can I Ping virbr1? Is the gateway virbr1?
[[email protected] images]# virsh domifaddr c3
Name MAC address protocol address
-------------------------------------------------------------------------------
vnet3 52:54:00:e3:86:e1 ipv4 192.168.12.179/24
[[email protected] images]# ssh 192.168.12.179
The authenticity of host '192.168.12.179 (192.168.12.179)' can't be established.
ECDSA key fingerprint is SHA256:heUVB8InZ+56B+vMgUz+B7Uus4EgklEU9DVeywf1Z7s.
ECDSA key fingerprint is MD5:0b:b1:75:a4:2b:ba:cf:f1:ac:51:cb:76:2a:74:89:42.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.12.179' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Sun Aug 16 09:46:30 2020
[[email protected] ~]# ping 192.168.12.1
PING 192.168.12.1 (192.168.12.1) 56(84) bytes of data.
64 bytes from 192.168.12.1: icmp_seq=1 ttl=64 time=0.514 ms
64 bytes from 192.168.12.1: icmp_seq=2 ttl=64 time=0.585 ms
^C
--- 192.168.12.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.514/0.549/0.585/0.042 ms
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.12.1 0.0.0.0 UG 100 0 0 eth0
192.168.12.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
[[email protected] ~]# exit
Log out
Connection to 192.168.12.179 closed.
[[email protected] images]#
Tip: from the above information, we can see that the internal address of the virtual machine created based on the test network is the DHCP address pool specified in the test network configuration file, and the gateway address is the address of virbr1;
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.525400450615 yes virbr0-nic
vnet0
vnet1
vnet2
virbr1 8000.525400450616 yes virbr1-nic
vnet3
[[email protected] ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 239 packets, 21179 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 68 packets, 8639 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 86 packets, 6827 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 86 packets, 6827 bytes)
pkts bytes target prot opt in out source destination
2 180 RETURN all -- * * 192.168.12.0/24 224.0.0.0/24
1 328 RETURN all -- * * 192.168.12.0/24 255.255.255.255
29 1740 MASQUERADE tcp -- * * 192.168.12.0/24 !192.168.12.0/24 masq ports: 1024-65535
94 7144 MASQUERADE udp -- * * 192.168.12.0/24 !192.168.12.0/24 masq ports: 1024-65535
1 84 MASQUERADE all -- * * 192.168.12.0/24 !192.168.12.0/24
2 267 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24
0 0 RETURN all -- * * 192.168.122.0/24 255.255.255.255
80 4800 MASQUERADE tcp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
269 20444 MASQUERADE udp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
0 0 MASQUERADE all -- * * 192.168.122.0/24 !192.168.122.0/24
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.12.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[[email protected] ~]# ip a l vnet3
10: vnet3: mtu 1500 qdisc pfifo_fast master virbr1 state UNKNOWN qlen 1000
link/ether fe:54:00:e3:86:e1 brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fee3:86e1/64 scope link
valid_lft forever preferred_lft forever
[[email protected] ~]#
Tip: if we create a virtual machine based on the test network, it will also bridge the virtual network card of the virtual machine to virbr1. If the network type we specify is NAT type and DHCP is turned on, it will point the gateway of the virtual machine to virbr1 by default. In addition, SNAT rules will be added to the NAT table of iptables on the host. It is because of these iptables rules that the virtual machine can be connected External network;
Virsh net Info: view the brief information of the specified network
[[email protected] ~]# virsh help net-info
NAME
Net Info - Network Information
SYNOPSIS
net-info
DESCRIPTION
Returns basic information about the network.
OPTIONS
[-- network] network name or UUID
[[email protected] ~]# virsh net-info test
Name: Test
UUID: be2af58a-c8cd-4b73-8ee3-36cb3fe9566b
Active: Yes
Lasting: Yes
Auto start: no
Bridge: virbr1
[[email protected] ~]#
Virsh net autostart: the flag specifies that the network is automatically started
[[email protected] ~]# virsh help net-autostart
NAME
Net autostart - automatically start the network
SYNOPSIS
net-autostart [--disable]
DESCRIPTION
Set up a network to start automatically at startup
OPTIONS
[-- network] network name or UUID
--Disable disable auto start
[[email protected] ~]# virsh net-autostart test
Network test is marked as auto start
[[email protected] ~]# virsh net-info test
Name: Test
UUID: be2af58a-c8cd-4b73-8ee3-36cb3fe9566b
Active: Yes
Lasting: Yes
Auto start: Yes
Bridge: virbr1
[[email protected] ~]#
Virsh net name: resolves the specified UUID to a name
[[email protected] ~]# virsh net-info test
Name: Test
UUID: be2af58a-c8cd-4b73-8ee3-36cb3fe9566b
Active: Yes
Lasting: Yes
Auto start: Yes
Bridge: virbr1
[[email protected] ~]# virsh help net-name
NAME
Net name - converts a network UUID to a network name
SYNOPSIS
net-name
OPTIONS
[-- network] network UUID
[[email protected] ~]# virsh net-name be2af58a-c8cd-4b73-8ee3-36cb3fe9566b
test
[[email protected] ~]#
Virsh net UUID: resolves the specified network name to UUID
[[email protected] ~]# virsh help net-uuid
NAME
Net UUID - converts a network name to a network UUID
SYNOPSIS
net-uuid
OPTIONS
[-- network] network name
[[email protected] ~]# virsh net-uuid test
be2af58a-c8cd-4b73-8ee3-36cb3fe9566b
[[email protected] ~]#
Virsh net DHCP Leases: displays the DHCP lease information of the specified network;
[[email protected] ~]# virsh help net-dhcp-leases
NAME
net-dhcp-leases - print lease info for a given network
SYNOPSIS
net-dhcp-leases [--mac ]
DESCRIPTION
Print lease info for a given network
OPTIONS
[-- network] network name or UUID
--Mac MAC address
[[email protected] ~]# virsh net-dhcp-leases test
Express time MAC address protocol IP address host name client ID or DUID
-------------------------------------------------------------------------------------------------------------------
2020-08-16 23:27:20 52:54:00:e3:86:e1 ipv4 192.168.12.179/24 - -
[[email protected] ~]# virsh net-dhcp-leases default
Express time MAC address protocol IP address host name client ID or DUID
-------------------------------------------------------------------------------------------------------------------
2020-08-16 23:32:45 52:54:00:3e:3f:71 ipv4 192.168.122.4/24 cirros 01:52:54:00:3e:3f:71
2020-08-16 23:11:38 52:54:00:e3:85:21 ipv4 192.168.122.168/24 - -
2020-08-16 23:14:35 52:54:00:e3:8d:11 ipv4 192.168.122.222/24 - -
[[email protected] ~]#
Virsh net edit: edit the configuration file of the specified virtual machine;
Tip: using virsh net edit, it will open the configuration file of the specified network by default. We can edit the configuration file to create a network, or change the configuration of the corresponding network, and then reread the configuration file to make the corresponding configuration effective;
Edit the DHCP address pool address and virbr1 address of the test network configuration file;
[[email protected] ~]# virsh net-edit test
test
f9452d96-55fa-4dbf-ad9c-b1ba99b7f2ad
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/virshJaE8MT.xml" 16L, 413C written
Network test XML configuration edited
[[email protected] ~]# cat /etc/libvirt/qemu/networks/test.xml
test
f9452d96-55fa-4dbf-ad9c-b1ba99b7f2ad
[[email protected] ~]# virsh net-info test
Name: Test
UUID: f9452d96-55fa-4dbf-ad9c-b1ba99b7f2ad
Active: Yes
Lasting: Yes
Auto start: no
Bridge: virbr1
[[email protected] ~]# ip a l virbr1
13: virbr1: mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:45:06:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.12.1/24 brd 192.168.12.255 scope global virbr1
valid_lft forever preferred_lft forever
[[email protected] ~]#
Prompt: after modifying the configuration file of the specified active network by default, it will not take effect immediately; after creating a persistent network, the default configuration file is stored in the XML format file with the same name as the corresponding network name in the directory / etc / libvirt / QEMU / network /;
Makes the specified network reread profile effective
[[email protected] ~]# virsh net-list
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
[[email protected] ~]# virsh net-destroy test
Network test deleted
[[email protected] ~]# virsh net-list
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test inactive no yes
[root[email protected] ~]# virsh net-start test
Network test started
[[email protected] ~]# ip a l virbr1
15: virbr1: mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:45:06:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.11.1/24 brd 192.168.11.255 scope global virbr1
valid_lft forever preferred_lft forever
[[email protected] ~]#
Tip: we have to restart the network to reread the configuration file, so in the above operation, stop the corresponding network first, and then start the corresponding network; the virsh net destory command is to delete the network in the specified active state, which is equivalent to stopping the operation;
Verification: will the virtual machine address on the corresponding network be updated?
[[email protected] ~]# virsh domifaddr c3
Name MAC address protocol address
-------------------------------------------------------------------------------
[[email protected] ~]# virsh list --all
ID name status
----------------------------------------------------
1 generic running
2 centos7.0 running
3 c2 running
4 c3 running
[[email protected] ~]# virsh destroy c3
Domain C3 was deleted
[[email protected] ~]# virsh list --all
ID name status
----------------------------------------------------
1 generic running
2 centos7.0 running
3 c2 running
-C3 off
[[email protected] ~]# virsh start c3
Domain C3 started
[[email protected] ~]# virsh domifaddr c3
Name MAC address protocol address
-------------------------------------------------------------------------------
vnet3 52:54:00:e3:86:e1 ipv4 192.168.11.179/24
[[email protected] ~]#
Tip: if the virtual machine on the corresponding network does not restart, it will not get the address by default; in fact, the above restart can use the virsh reboot command;
Using virsh edit to generate a network
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
[[email protected] ~]# virsh net-edit test
f9452d96-55fa-4dbf-ad9c-b1ba99b7f2ad
test1
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/virshWqhNS8.xml" 15L, 362C written
Edited network test1 XML configuration
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 12
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
-RW ------ 1 root 641 August 16 23:23 test1.xml
-RW ------ 1 root 639 August 16 23:09 test.xml
[[email protected] ~]# diff /etc/libvirt/qemu/networks/test.xml /etc/libvirt/qemu/networks/test1.xml
4c4
< virsh net-edit test
---
> virsh net-edit test1
9,10c9,10
< test
< f9452d96-55fa-4dbf-ad9c-b1ba99b7f2ad
---
> test1
> 8dbdad99-91c5-435b-95b7-02179597f39e
16,18c16,18
<
<
<
---
>
>
>
20c20
<
---
>
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
Test1 inactive no yes
[[email protected] ~]#
Tip: from the above demonstration, we can see that when we modify the network name, IP address, bridge name, MAC, and DHCP address pool in the network configuration file, and then save it, it will generate a network configuration file that we modified before in the corresponding directory, and apply our configuration to the system immediately (define operation, we don’t need to define it manually, it will automatically Define an inactive persistent network;
Virsh net create: create a non persistent temporary network from the specified XML file and activate it;
[[email protected] ~]# virsh help net-create
NAME
Net create - create a network from an XML file
SYNOPSIS
net-create
DESCRIPTION
Create a network
OPTIONS
The [-- file] file contains an XML network description
[[email protected] ~]# cat test.xml
test2
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 12
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
-RW ------ 1 root 641 August 16 23:23 test1.xml
-RW ------ 1 root 639 August 16 23:09 test.xml
[[email protected] ~]# virsh net-create test.xml
Create network from test2 test.xml
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
Test1 inactive no yes
Test2 activity no
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 12
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
-RW ------ 1 root 641 August 16 23:23 test1.xml
-RW ------ 1 root 639 August 16 23:09 test.xml
[[email protected] ~]#
Tip: the default network generated by define is persistent, while the network generated by create is not persistent. Persistence means whether to generate configuration files in / etc / libvirt / QEMU / network / directory. The network with configuration files is called persistent network, while the network without configuration files is called temporary network;
Virsh net undefine: to undefine a specified persistent network is to convert it into a non persistent network;
[[email protected] ~]# virsh help net-undefine
NAME
net-undefine - undefine a persistent network
SYNOPSIS
net-undefine
DESCRIPTION
Undefine the configuration for a persistent network.
OPTIONS
[-- network] network name or UUID
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
Test1 inactive no yes
Test2 activity no
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 12
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
-RW ------ 1 root 641 August 16 23:23 test1.xml
-RW ------ 1 root 639 August 16 23:09 test.xml
[[email protected] ~]# virsh net-undefine test1
Network test1 has been undefined
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity Yes No
Test2 activity no
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 8
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
-RW ------ 1 root 639 August 16 23:09 test.xml
[[email protected] ~]# virsh net-undefine test
Network test has been undefined
[[email protected] ~]# virsh net-list --all
Name state starts to persist automatically
----------------------------------------------------------
The default activity is
Test activity no no
Test2 activity no
[[email protected] ~]# ll /etc/libvirt/qemu/networks/
Total consumption 4
Drwx ------ 2 root 25 August 16 22:59 autostart
-RW ------ 1 root 576 August 13 23:31 default.xml
[[email protected] ~]#
Tip: net undefine will change the persistent network in active state to non persistent, its operation is to delete the corresponding configuration file, and it will not delete the active network instance; it will delete the configuration file and network instance of the persistent network in inactive state; it will delete the configuration file and network instance of the persistent network in inactive state;