VIRSH - Documentation

From IT-Arts.net
Revision as of 07:09, 17 January 2026 by Admin (talk | contribs) (Text replacement - "Category:Wiki" to "Category:Wiki '''''[https://it-arts.net/index.php/Category:Wiki Return to Wiki Index]''''' ")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Return to Wiki Index


Basic Usage

Connect to a Hypervisor

You can connect to a local or remote hypervisor with the following command:

virsh connect qemu:///system

This connects to the local system's hypervisor. For remote systems, replace `qemu:///system` with a URI of the remote host, such as `qemu+ssh://user@hostname/system`.

List Virtual Machines

To list all virtual machines on the system, including running, paused, and shut off VMs:

virsh list --all

The command outputs the status of all VMs, showing their IDs, names, and states.

Start and Stop Virtual Machines

To start a VM:

virsh start <vm_name>

To stop a VM:

virsh shutdown <vm_name>

For an immediate shutdown (without graceful shutdown):

virsh destroy <vm_name>

Rebooting a Virtual Machine

To reboot a running virtual machine:

virsh reboot <vm_name>

Suspend and Resume Virtual Machines

To suspend a running VM:

virsh suspend <vm_name>

To resume a suspended VM:

virsh resume <vm_name>

Define a Virtual Machine from an XML File

A VM can be defined using an XML configuration file. To define a VM from an XML file:

virsh define /path/to/vm-config.xml

This command registers the VM with libvirt without starting it.

Virtual Machine Information

Get Detailed Information on a Virtual Machine

You can retrieve detailed information about a VM by using the `dominfo` command:

virsh dominfo <vm_name>

This will display information like CPU usage, memory, and network stats.

Check the State of a Virtual Machine

To get the current state of a specific VM:

virsh domstate <vm_name>

This will return the state of the virtual machine (e.g., running, shut off, paused).

Virtual Networks

List Virtual Networks

To list all the available virtual networks:

virsh net-list --all

This shows the status of virtual networks, whether active or inactive.

Start and Stop Virtual Networks

To start a virtual network:

virsh net-start <network_name>

To stop a virtual network:

virsh net-destroy <network_name>

Define Virtual Network

To define a virtual network from an XML configuration file:

virsh net-define /path/to/network-config.xml

This command defines a virtual network, but it does not start it.

Autostart Virtual Networks

To enable a virtual network to start automatically with the hypervisor:

virsh net-autostart <network_name>

Virtual Storage Pools

List Storage Pools

To list the defined storage pools:

virsh pool-list --all

Start and Stop Storage Pools

To start a storage pool:

virsh pool-start <pool_name>

To stop a storage pool:

virsh pool-destroy <pool_name>

Define Storage Pool

To define a storage pool from an XML file:

virsh pool-define /path/to/storage-pool.xml

Autostart Storage Pools

To enable a storage pool to automatically start with the hypervisor:

virsh pool-autostart <pool_name>

Virtual Machine Snapshots

List Snapshots of a VM

To list all snapshots of a VM:

virsh snapshot-list <vm_name>

Create a Snapshot

To create a snapshot of a running VM:

virsh snapshot-create-as <vm_name> <snapshot_name> --description "Snapshot description"

This command creates a snapshot with an optional description.

Revert to a Snapshot

To revert a VM to a specific snapshot:

virsh snapshot-revert <vm_name> <snapshot_name>

Delete a Snapshot

To delete a snapshot:

virsh snapshot-delete <vm_name> <snapshot_name>

Disk and Storage Management

Attach a Disk to a Virtual Machine

To attach a disk to a running VM:

virsh attach-disk <vm_name> /path/to/disk.img vda --type disk --mode shareable

Detach a Disk from a Virtual Machine

To detach a disk from a VM:

virsh detach-disk <vm_name> vda