Bobcares

Manage KVM guest virtual machines using virsh commands

by | Feb 25, 2021

Wondering how to manage KVM guest virtual machines using virsh commands? We can help you.

Virsh is a management user interface for virsh guest domains. It can be used to create, pause, restart, and shutdown domains. In addition, it can be used to list current domains available in our Virtualization hypervisor platform.

Virsh interacts with Libvirt which is a library aimed at providing a long-term stable API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox, and VMware ESX.

Here at Bobcares we often use virsh commands to manage KVM guest virtual machines of our customers.

Today let’s see how our Support Engineers use Virsh commands as a part of our Server Management Services.

 

Various commands to Manage KVM guest virtual machines using Virsh

The basic structure of most virsh usage is given below:

virsh [OPTION]... <command> <domain> [ARG]...

Display node information

To display the host node information and the machines that support the virtualization process, we can use the following command:

$ sudo virsh nodeinfo

List all domains

To list both inactive and active domains, we can use the following command:

$ sudo virsh list --all

List only active domains

For listing only active domains we can use the following command:

$ sudo virsh list

To start a VM

For starting a VM we can use the following command by replacingdomain_name‘ with the required domain name:

$ sudo virsh start domain_name
$ sudo virsh list

Autostart VM

To set a VM to start automatically on system startup, we can use the following command:

$ sudo virsh autostart domain_name
$ sudo virsh dominfo domain_name

Autostart disable

To disable autostart feature for a VM, we can use the following command:

$ virsh autostart --disable domain_name
$ virsh dominfo domain_name

Stop/Shutdown VM

To shut down a running VM gracefully, we can use the following command:

$ sudo virsh shutdown domain_name
$ sudo virsh list

Force shutdown VM

To perform a forceful shutdown of the active domain we can use the following command:

$ sudo virsh destroy domain_name

Stop all running VMs

To shut down all running domains, we can use the following command:

$ for i in `sudo virsh list | grep running | awk '{print $2}'` do
sudo virsh shutdown $i
done

Reboot VM

To restart a VM we can use the following command:

$ sudo virsh reboot domain_name

Remove VM

To cleanly remove a VM including its storage columns, we can use the following commands:

$ sudo virsh destroy domain_name 2> /dev/null
$ sudo virsh undefine domain_name
$ sudo virsh pool-refresh default
$ sudo virsh vol-delete --pool default doamin_name.qcow2

Create a VM

To create a new virtual machine with virsh, we can use the following command:

$ sudo virt-install

Connect to VM console

To connect to the guest console, use the command:

$ sudo virsh console domain_name

Edit VM XML file

To edit a VM XML file, we can use the following command:

# To use vim text editor

$ sudo EDITOR=vim virsh edit domain_name

# To use nano text editor

$ sudo EDITOR=nano virsh edit domain_name

Suspend/Resume VM

To suspend a guest we can use the following command:

$ sudo virsh suspend domain_name

Resuming a guest VM

To restore a suspended guest VM, we can use the following command:

$ sudo virsh resume domain_name

Save VM

To save the current state of a VM to a file, we can use the following command:

$ sudo virsh save domain_name domain_name.save

$ ls -l domain_name.saved

Restoring a saved VM

To restore the saved VM from the file, we can use the following command:

$ virsh restore domain_name.save

$ sudo virsh list

Managing Volumes using virsh

Now let us see how to create a storage volume, attach it to a VM, detach it from a VM and how to delete a volume.

Creating volume

To create a 2GB volume named domain_name_vol2 on the default storage pool, we can use the following command:

$ sudo virsh vol-create-as default domain_name_vol2.qcow2 2G
$ sudo du -sh /var/lib/libvirt/images/domain_name_vol2.qcow2

Attach a volume to VM

To attach created volume above to VM domain_name, we can use the following command:

# virsh attach-disk --domain  domain_name \
--source /var/lib/libvirt/images/domain_name_vol2.qcow2 \
--persistent --target vdb

Detach volume on VM

To detach the above volume domain_name_vol2 from the VM domain_name, we can use the following command:

$ sudo virsh detach-disk --domain domain_name --persistent --live --target vdb

Delete volume

To delete volume, we can use the following commands:

$ sudo virsh vol-delete domain_name_vol2.qcow2 --pool default

$ sudo virsh pool-refresh default

$ sudo virsh vol-list default

 [Need any further assistance with Virsh commands? – We can help you]

 

Conclusion

In short, we saw various commands that our Support Techs use to manage KVM guest virtual machines using virsh commands.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF