Virsh for managing VMs

The virsh command provides hundreds of options to manage every aspect of your virtual machines. By using virsh, you can quickly connect to a server using secure shell (SSH) and perform operations on your VMs without access to a graphical interface.

Virsh

The virsh program is the main interface for managing virsh guest domains. It can be used to create, pause, and shutdown domains. It can also be used to list current domains.

Libvirt is a C toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes). The library aims at providing a long term stable C API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox and VMware ESX.

The basic structure of most virsh usage is:

virsh [OPTION]... <command> <domain> [ARG]...

virsh list

The virsh list command lists all running VMs:

# virsh list
Id Name State
------------------------------
3 vm-prod01 running

List all available VMs by adding the --all option:

# virsh list --all
Id Name State
------------------------------
3 vm-prod01 running
- vm-database shut off
- vm-beta01 shut off

Shut down, start or reboot VM :

# virsh shutdown vm-prod01 
Domain 'vm-prod01' is being shutdown

# virsh list
Id Name State
--------------------

You can start it up again using the start subcommand:

# virsh start vm-prod01 
Domain 'vm-prod01' started

# virsh list
Id Name State
--------------------------
4 vm-prod01 running

virsh domifaddr

The domifaddr subcommand lists all the IP addresses configured for all virtual interfaces in a given VM.

# virsh domifaddr vm-prod01 
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet1 00:42:00:ab:ef:11 ipv4 192.168.0.15/24
vnet2 00:42:00:cd:01:22 ipv4 192.168.0.16/24

By default, it lists the IP address leased by a DHCP server. If the hypervisor does not provide this information, you can also use the option --source agent to query the guest OS directly (requires virtualization agent installed in the guest OS).

virsh dumpxml

The dumpxml subcommand dumps the XML configuration for a given VM.

You can use it to export the configuration to a file to make changes to an existing VM or use it as a template to create another VM with a similar configuration.

# virsh dumpxml vm-prod01                

<domain type='kvm' id='4'>
<name>vm-prod01</name>
<uuid>66554433-abc1-234-5678-90abcdef1234</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/8.4"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>2</vcpu>

[...]
</domain>

virsh edit

The edit subcommand opens the current XML configuration in your default $EDITOR, allowing you to make live modifications in a VM:

# virsh edit vm-prod01
<domain type='kvm'>
<name>vm-prod01</name>
<uuid>66554433-abc1-234-5678-90abcdef1234</uuid>
<metadata>

[...]
</domain>

After making your modifications, save the file to apply them. Some modifications may only take effect after a reboot.

virsh net-edit

The net-edit subcommand allows you to make live modifications to a virtual network configuration.

# virsh net-edit --network hostonly
<network>
<name>hostonly</name>
<uuid>eddd03ff-5825-42ef-bc99-968bddf773c2</uuid>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:67:75:b1'/>
<domain name='hostonly'/>
<ip address='192.168.64.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.64.128' end='192.168.64.254'/>
</dhcp>
</ip>
</network>

You can also use other network-related subcommands starting with net- to manage different aspects of the hypervisor virtual networks.

Documentation

https://libvirt.org/
https://libvirt.org/uri.html
https://libvirt.org/manpages/virsh.html
https://www.redhat.com/sysadmin/virsh-subcommands
https://computingforgeeks.com/virsh-commands-cheatsheet/?expand_article=1

> Partager <