Getty Images/iStockphoto

Tip

Basic virsh network commands to know

It's helpful to know how to use virsh commands to manage VMs and virtual networks; some cloud providers don't support Ansible or VirtualBox. Get started with a few commands.

The virsh program, or command line utility, is an admin's one-stop shop to manage virtual domains. The virsh commands let admins create, pause, restart and shut down guest VMs, as well as view networks, network information and autostart networks. Note that virsh is also, itself, a command.

Admins might wonder why they should know the virsh utility instead of the commands that ship with VirtualBox or Ansible. Most often, cloud-native VMs don't use either of those technologies, so admins who know how to use a more universal tool, such as virsh, are prepared for just about any VM management or virtual network scenario.

How to install virsh

It's likely that an admin's cloud-based offering of choice already has virsh. On the off chance it doesn't, use either of the following commands:

  • Ubuntu-based distributions: sudo apt-get install libvirt-clients libvirt-daemon libvirt-login-shell -y
  • RHEL-based distributions: sudo dnf install virt-install virt-viewer -y

Depending on the VM technology in place, the installation might also require one of the additional commands:

  • libvirt-daemon-driver-storage-gluster
  • libvirt-daemon-driver-storage-iscsi-direct
  • libvirt-daemon-driver-storage-rbd
  • libvirt-daemon-driver-storage-zfs
  • libvirt-daemon-driver-xen
  • libvirt-daemon-system numad

Once admins install virsh, they can create dump files, spin up guest VMs and manage online networks.

How to create a VM XML dump

A dump of an existing VM configuration file can help create new VMs, though admins must know the names of the available domains. The sudo virsh list -all command lists all available domains, by ID, name and state. With that information, admins can create the XML dump file.

On the same machine that hosts the VM, issue the command sudo virsh dumpxml DOMAIN > dump.xml, where DOMAIN is either the ID or the name from the list -all command.

The sudo virsh dumpxml DOMAIN > dump.xml command creates the file dump.xml that contains all possible VM configurations.

Create a new guest VM from an XML dump

Admins can also create a new guest VM from the newly created XML dump file. This is done with the sudo virsh create dump.xml command.

This new dump file looks like the following:

<domain type='xen' id='13'>
    <name>software-testing</name>
    <uuid>6c8bdd59-d172-49f2-9c55-1a2df92afbae</uuid>
    <bootloader>/usr/bin/pygrub</bootloader>
    <os>
    <type>linux</type>
    <kernel>/var/lib/libvirt/vmlinuz.2dgnU_</kernel>
    <initrd>/var/lib/libvirt/initrd.UQafMw</initrd>
    <cmdline>ro root=/dev/VolGroup00/LogVol00 rhgb quiet</cmdline>
    </os>
    <memory>512000</memory>
    <vcpu>1</vcpu>
    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>restart</on_crash>
    <devices>
    <interface type='bridge'>
        <source bridge='xenbr0'/>
        <mac address='00:12:7b:21:12:1b'/>
        <script path='vif-bridge'/>
    </interface>
    <graphics type='vnc' port='5900'/>
    <console tty='/dev/pts/4'/>
    </devices>
</domain>

Be sure to edit the new dump file's name. Otherwise, the new guest VM will have the same name as the dump file.

Then, create the new guest VM with the sudo virsh create dump.xml command.

Start the new guest VM with sudo virsh start DOMAIN, where DOMAIN is the name of the guest VM.

Manage virtual networks with virsh

To effectively manage virtual networks, first list all available virtual networks with the sudo virsh net-list command. This lists out all virtual networks by name, state and if they have autostart.

To view a particular network's information -- such as bridge name, IP address, netmask, Dynamic Host Configuration Protocol information and forward delay status -- issue the sudo virsh net-dumpxml NAME command. In this command, NAME is the name of the virtual network in question.

The command's output looks like the following:

<network>
    <name>vnet2</name>
    <uuid>f2a07618-5b78-4692-a6ab-2d5c6cff2aec</uuid>
    <forward dev='enp5'/>
    <bridge name='vnet0' stp='on' forwardDelay='0' />
    <ip address='192.168.10.100' netmask='255.255.255.0'>
        <dhcp>
        <range start='192.168.10.128' end='192.168.10.254' />
        </dhcp>
    </ip>
</network>

To start an inactive network, issue the sudo virsh net-start NETWORK command. Note that NETWORK is the name of the inactive network.

This command starts the inactive network, but doesn't set it to autostart. Autostart means the network will automatically restart, should the host restart.

To set a virtual network to autostart, use the virsh net-autostart NETWORK command, where NETWORK is the name of the virtual network that will autostart.

Create and destroy virtual networks

With virsh, admins can create an XML dump file for a guest VM and then launch a new guest VM based on that file; there are also commands to do the same task for virtual networks. With this command, admins can more quickly create virtual networks based on previous successful deployments.

First, create the XML dump file with the command sudo virsh net-dumpxml NETWORK > netdump.xml, where NETWORK is the name of the virtual network in question.

Then, admins can edit the newly created XML file, save it and start the new virtual network with the command sudo virsh net-create netdump.xml.

Once an admin no longer requires a virtual network, there's no reason to keep the network around because it uses up precious resources. Destroy an existing virtual network with the sudo virsh net-destroy NETWORK command, where NETWORK is the name of the virtual network to destroy.

Dig Deeper on IT systems management and monitoring

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close