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.
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
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
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: