This Blog is to share our knowledge and expertise on Linux System Administration and VMware Administration

Monday, November 2, 2015

Understanding the Virsh Command in Linux Virtualization

Connecting to a Hypervisor  (Unsupported now)
virsh connect <name>

Where <name> is the machine name of the hypervisor. If you want to initiate a read—only connection, append the above command with —readonly.

Creating a Virtual Machine
virsh create <path to XML configuration file>

Configuring an XML Dump
virsh dumpxml [domain-id | domain-name | domain-uuid]

This command outputs the domain information (in XML) to stdout . If you save the data to a file, you can use the create option to recreate the virtual machine.

Suspending a Virtual Machine
virsh suspend [domain-id | domain-name |domain-uuid]

When a domain is in a suspended state, it still consumes system RAM. There will also be no disk or network I/O when suspended. This operation is immediate and the virtual machine must be restarted with the resume option

Resuming a Virtual Machine
virsh resume [domain-id | domain-name | domain-uuid]

This operation is immediate and the virtual machine parameters are preserved in a suspend and resume cycle.

Saving a Virtual Machine
virsh save [domain-name][domain-id | domain-uuid][filename]

This stops the virtual machine you specify and saves the data to a file, which may take some time given the amount of memory in use by your virtual machine. You can restore the state of the virtual machine with the restore option

Restoring a Virtual Machine
virsh restore [filename]

This restarts the saved virtual machine, which may take some time. The virtual machine's name and UUID are preserved but are allocated for a new id.

Shutting Down a Virtual Machine
virsh shutdown [domain-id | domain-name | domain-uuid]

You can control the behavior of the rebooting virtual machine by modifying the on_shutdown parameter of the xmdomain.cfg file.

Rebooting a Virtual Machine
virsh reboot [domain-id | domain-name | domain-uuid]

 You can control the behavior of the rebooting virtual machine by modifying the on_reboot parameter of the xmdomain.cfg file.

Terminating a Domain
virsh destroy [domain-name | domain-id | domain-uuid]

This command does an immediate ungraceful shutdown and stops any guest domain sessions (which could potentially lead to file corrupted file systems still in use by the virtual machine). You should use the destroy option only when the virtual machine's
operating system is non-responsive. For a paravirtualized virtual machine, you should use the shutdown option .

Converting a Domain Name to a Domain ID
virsh domid [domain-name | domain-uuid]

Converting a Domain ID to a Domain Name
virsh domname [domain-name | domain-uuid]

Converting a Domain Name to a UUID
virsh domuuid [domain-id | domain-uuid]

Displaying Virtual Machine Information
virsh dominfo [domain-id | domain-name | domain-uuid]

Displaying Node Information
virsh nodeinfo

 The outputs displays something similar to:
CPU model                    x86_64
CPU (s)                      8
CPU frequency                2895 Mhz
CPU socket(s)                2    
Core(s) per socket           2
Threads per core:            2
Numa cell(s)                 1
Memory size:                 1046528 kb
This displays the node information and the machines that support the virtualization process.

Displaying the Virtual Machines
virsh list domain-name [ ——inactive  |  ——all]


The ——inactive option lists inactive domains (domains that have been defined but are not currently active).
The —all domain lists all domains, whether active or not. Your output should resemble the this example:
ID                 Name                 State
————————————————
0                   Domain0             running
1                   Domain202           paused
2                   Domain010           inactive
3                   Domain9600          crashed

Here are the six domain states:
running           lists domains currently active on the CPU
blocked           lists domains that are blocked
paused            lists domains that are suspended
shutdown          lists domains that are in process of shutting down
shutoff           lists domains that are completely down.
crashed           lists domains that are crashed

Displaying Virtual CPU Information
virsh vcpuinfo [domain-id | domain-name | domain-uuid]

Configuring Virtual CPU Affinity
virsh vcpupin [domain-id | domain-name | domain-uuid] [vcpu] , [cpulist]

Where [vcpu] is the virtual VCPU number and [cpulist] lists the physical number of CPUs.

Configuring Virtual CPU Count
virsh setvcpus [domain-name | domain-id | domain-uuid] [count]

 Note that the new count cannot exceed the amount you specified when you created the Virtual Machine

Configuring Memory Allocation
virsh setmem [domain-id | domain-name]  [count]

You must specify the [count] in kilobytes. Note that the new count cannot exceed the amount you specified when you created the Virtual Machine. Values lower than 64 MB probably won't work.You can adjust the Virtual Machine memory as necessary.

Configuring Maximum Memory
virsh setmaxmem  [domain-name | domain-id | domain-uuid] [count]

You must specify the [count] in kilobytes. Note that the new count cannot exceed the amount
you specified when you created the Virtual Machine. Values lower than 64 MB probably won't work. The maximum memory doesn't affect the current use of the Virtual Machine (unless the new value is lower which should shrink memory usage).

BASIC MANAGEMENT OPTIONS


Resource Management Options

setmem         : changes the allocated memory.
setmaxmem  : changes maximum memory limit
setvcpus        : changes number of virtual CPUs.
vcpuinfo         : domain vcpu information.
vcpupin          : control the domain vcpu affinity.

Monitoring and troubleshooting Options

version        : show version
dumpxml        : domain information in XML
nodeinfo        : node information

virsh command output

The following are example outputs from common virsh commands:
the list command:
virsh # list

Id  Name                 State
----------------------------------
0   Domain-0             running
13  r5b2-mySQL01         blocked

the dominfo domain command:
virsh # dominfo r5b2-mySQL01

Id:             13
Name:           r5b2-mySQL01
UUID:           4a4c59a7-ee3f-c781-96e4-288f2862f011
OS Type:                 linux
State:          blocked
CPU(s):         1
CPU time:               11.0s
Max memory:     512000 kB
Used memory:    512000 kB

the domstate domain command:

virsh # domstate r5b2-mySQL01
blocked

the domuuid domain command:

virsh # domuuid r5b2-mySQL01
4a4c59a7-ee3f-c781-96e4-288f2862f011

the vcpuinfo domain command:

virsh # vcpuinfo r5b2-mySQL01
VCPU:           0
CPU:            0
State:          blocked
CPU time:       0.0s
CPU Affinity:   yy

the dumpxml domain command:

virsh # dumpxml r5b2-mySQL01
<domain type='xen' id='13'>
            <name>r5b2-mySQL01</name>
            <uuid>4a4c59a7ee3fc78196e4288f2862f011</uuid>
            <bootloader>/usr/bin/pygrub</bootloader>
            <os>
                                 <type>linux</type>
                                 <kernel>/var/lib/xen/vmlinuz.2dgnU_</kernel>
                                 <initrd>/var/lib/xen/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:16:3e:49:1d:11'/>
                                                     <script path='vif-bridge'/>
                                 </interface>
                                 <graphics type='vnc' port='5900'/>
                                 <console tty='/dev/pts/4'/>
            </devices>

the version domain command:

virsh # version
Compiled against library: libvir 0.1.7
Using library: libvir 0.1.7
Using API: Xen 3.0.1
Running hypervisor: Xen 3.0.0

No comments:

Post a Comment