Advanced Storage and Network Management with Virsh

Quite Creating and starting up virtual machines are more than just managing. VMs also rely on fast storage for their disks and flexible networking to talk with one another as well as the outside world. Virsh offers powerful command lines to configure these important resources from the Command Prompt.

Why virsh?

  • Centralized Control: virsh provides consolidation of virtual machines, network, and storage management into a single command-line interface, thus streamlining operations across hypervisors supported by Libvirt.
  • Scriptability and Automation: Being a command-line tool, virsh is ideal for integration into automation and scripted workflows, enabling repetitive tasks to be executed reliably and efficiently.
  • Remote Management: Use virsh to connect to remote servers, making it a practical choice for managing headless environments or virtualized data centers.
  • Advanced Troubleshooting: Access detailed diagnostic information for VMs, networks, and storage pools to identify and resolve issues promptly.
🔥 TRENDING - Our #1 Selling eBook

Mastering KVM Virtualization - The Ultimate eBook

From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today

Only $10 $20
Get Instant Access →

In this guide, we will look at how to create and manage storage pools, allocate storage volumes​​ and network interface operation for different types of virtual networks. And by saavy with these features, administrators can build machines that scale more effectively, that are more tolerant of faults and the way things break in real life (believe me it happens), whilst providing better connectivity to a wider range of workloads.

1. Storage Pools

Note<storage-pool> should be replaced with the name of the storage pool as seen in virsh pool-list output.

List storage pools:

sudo virsh pool-list

Output information about storage pool:

sudo virsh pool-info <storage-pool>

Dump storage pool information in XML:

sudo virsh pool-dumpxml <storage-pool>
# virsh pool-dumpxml default > default.xml

Edit storage pool XML configuration:

sudo virsh pool-edit <storage-pool>

Refresh the metadata of a storage pool:

sudo virsh pool-refresh <storage-pool>

Create a storage pool from a set of args:

sudo mkdir -p /data/vms
sudo virsh pool-define-as --name vms --type dir --target /data/vms

Create a storage pool from XML:

$ vim vms-pool.xml
<pool type='dir'>
  <name>vms</name>
  <target>
    <path>/data/vms</path>
  </target>
</pool>

$ sudo virsh pool-define vms-pool.xml

Define an inactive storage pool or modify existing from XML file:

sudo virsh pool-define vms-pool.xml

Start a (previously defined) but inactive pool:

sudo pool-build <storage-pool>
sudo virsh pool-start <storage-pool>

Mark a storage pool for autostart:

sudo virsh pool-autostart <storage-pool>

Deactivate a storage pool:

sudo virsh pool-destroy <storage-pool>

Undefine an inactive pool:

sudo virsh pool-undefine <storage-pool>

2. Storage Volumes

List storage pools:

sudo virsh pool-list

List volumes in a storage pool:

sudo virsh vol-list <storage-pool>

Create a new volume from a set of arguments:

sudo virsh vol-create-as <storage-pool> <vol-name> <size>
# Example: virsh vol-create-as images disk2.qcow2 10G

Create a volume from an XML file:

sudo virsh vol-create <storage-pool> <xml-file>

Output volume information in XML:

sudo virsh vol-dumpxml --pool <storage-pool> <vol-name>

Get information about a storage volume:

sudo virsh vol-info <storage-pool> <vol-name>

Attach volume to VM:

# List created volumes
sudo virsh vol-list <storage-pool>

# Attache existing volume to a VM
sudo virsh attach-disk <vm-name> \
--source <sorce-disk-device> \
--target <target-disk> \
--persistent

Example:

sudo virsh attach-disk almalinux9 \
--source /var/lib/libvirt/images/test.qcow2 \
--target vdc \
--persistent

Resize volume:

# First get current volume size
sudo virsh vol-info <storage-pool> <vol-name>

# Resize volume
sudo virsh vol-resize --pool <storage-pool> \
--vol <vol-name> \
--capacity <new-capacity>
## You can use G for GiB, M for MiB, T fir TiB. E.g 20G

# Confirm new size
sudo virsh vol-info <storage-pool> <vol-name>

Use QEMU Monitor Command to list the allocated block devices to the VM:

sudo virsh qemu-monitor-command <vm-name> --hmp "info block"

Upload file contents to a volume:

virsh vol-upload --pool <storage-pool> --vol <vol-name> --file <file>

Delete a volume:

sudo virsh vol-delete <volume-name> <storage-pool>

3. Networking

List all networks defined:

sudo virsh net-list --all

List active networks only:

sudo virsh net-list

Edit XML configuration for a network:

sudo net-edit <network-name>

Define an inactive persistent virtual network or update existing:

# Example: Define NATed virtual network
$ vim network-nat.xml
<network>
  <name>privnat</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr1' stp='on' delay='0'/>
  <ip address='192.168.123.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.123.10' end='192.168.123.254'/>
    </dhcp>
  </ip>
</network>

$ sudo virsh net-define network-nat.xml

# Example: bridged host network; where br-mgmt is a host bridge
$ vim network-bridge.xml
<network>
  <name>my-network</name>
  <forward mode="bridge"/>
  <bridge name="br-mgmt"/>
</network>

Output network configuration XML contents:

sudo virsh net-dumpxml <network-name>

Display information about a network:

sudo virsh net-info <network-name>

Output network UUID from name:

sudo virsh net-uuid <network-name>

Start a (previously defined) inactive network:

sudo virsh net-start <network-name>

Set network to automatic start:

sudo virsh net-autostart <network-name>

Edit XML configuration for a network:

sudo virsh net-edit --network <network-name>

Note the above command will make live modifications to a virtual network configuration.

Attach bridged network interface to a VM:

virsh attach-interface \
  --domain <vm-name> \
  --type bridge \
  --source <bridge-name> \
  --model virtio \
  --config

If the VM is currently running, you can use the --live option to apply changes to the active domain immediately.

List network ports (guests connections):

sudo virsh net-port-list <network-name>

Output network port information in XML:

sudo virsh net-port-list <network-name> <network-port-uuid>

Create a network port from an XML file:

sudo virsh net-port-create --network <network-name> --file <xml-file>

Delete the specified network port:

sudo virsh net-port-delete <network> <port>

More guides from us:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

For the system administrators and DevOps engineers using KVM as their virtualized environment, performing complex virtual machine (VM) operations effectively […]

For the system administrators and DevOps engineers using KVM as their virtualized environment, controlling virtual machines (VMs) effectively can be […]

A Kickstart installation is an automated method for installing Linux distributions such as Red Hat Enterprise Linux (RHEL), Rocky , AlmaLinux, CentOS, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.