In order to facilitate the management of virtual machines, an interaction library has been developed in the form of libvirt, a management tool through which we can manage platform virtualization.
This tool provides a common programming interface for multiple technologies (KVM, LXC) being used by most open-source Cloud technologies (OpenStack, oVirt).
For network administrators, a console called virsh has been developed that uses the interface provided by libvirt to interact with virtual machines.
In order to use the libvirt features we have to install the libvirt-bin, virtinst, virt-viewer and virt-top packages.
Also, if the user is different from the root one, it should be added to the libvirtd group (as in kvm). Virsh is a virtualization solution management tool using the command line. It is written using libvirt. One advantage of virsh is that regardless of the virtualization solution it interacts with, the commands are the same.
In the following example, we will use the root user.
In the first step, we need to activate the libvirt networking service:
root @ user: ~ # virsh -c qemu: /// system net-start default
To create a virtual machine, use the virt-install utility:
root @ user: ~ # virt-install - connect qemu: /// system --name VM2 - hvm - ram 512 --disk path = base.qcow2, format = qcow2 --network network = default --vnc --import
At this time, an XML virtual machine file has been created that can be viewed in /etc/libvirt/qemu/VM1.xml.
The virtual machine console has opened and the virt-inst program still running. Use CTRL + C to close the virtual machine installation.
To control the virtual machine we will use the virsh console. We connect to the local daemon with virsh and display existing virtual machines using the list command:
root @ user: ~ # virsh - connect qemu: /// system
virsh> list
Id Name AAA
———————————-
2 VM2 running
Observe the status of the virtual computer and its id (2). Based on the Id you will perform all the operations.
We want to find the VNC port of the virtual machine with the vncdisplay command followed by the virtual computer id:
virsh # vncdisplay 2
: 0
Open a new console and connect to it using VNC using the vncviewer utility:
root @ user: ~ # vncviewer: 0
Close the console. WARNING: The virtual calculator will continue to run in the background!
Turn off the virtual machine with the shutdown command followed by its id:
virsh # shutdown 2
Domain 2 is being shutdown
virsh # list
Id Name State
———————————-
2 VM2 running
virsh # list
Id Name AAA
To display all virtual machines irrespective of their status, use the -all option of the list command:
virsh # list --all
Id Name AAA
———————————-
– VM2 shut off
We will start the VM2 virtual machine using the start command followed by the computer name:
virsh # start VM2
Domain VM1 started
virsh # list
Id Name AAA
———————————-
3 VM2 running
Note that IDs are allocated when virtual machines are started.
Run the destroy command on the virtual computer Id. You see that the virtual machine has been closed, NOT deleted.
virsh # destroy 3
Domain 3 destroyed
virsh # list --all
Id Name AAA
———————————-
– VM2 shut off
Delete the virtual machine defined by using the undefine command followed by the virtual computer name:
virsh # undefine VM2
Domain VM1 has been undefined
virsh # list
Id Name AAA
———————————-
6 VM2 running
Notice that your computer is still running. It will completely disappear after we close it:
virsh # shutdown 6
Domain 6 is being shutdown
virsh # list --all
Id Name AAA