Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Virtualization / KVM / Manage KVM Virtual Machines from CLI with Virsh

May 31, 2021 CentOSKVMQuestions and AnswersRHEL

Manage KVM Virtual Machines from CLI with Virsh

In a previous article we discussed how to install the KVM hypervisor and create a virtual machine. In a single article we were not able to cover the KVM virtual machine management in detail. Today we will try to show how to manage virtual machines from the command line with virsh: how to change VM settings, add additional virtual devices, and which commands to use to manage KVM virtual machines on Linux.

Contents:
  • Using Virsh (Virtual Shell) Commands to Manage KVM Virtual Machines
  • How to Add Memory and vCPU to a KVM Virtual Machine?
  • Add a Disk to an Existing KVM VM
  • How to Add a Second NIC to the KVM Virtual Machine?

Using Virsh (Virtual Shell) Commands to Manage KVM Virtual Machines

Let’s take a look on how to view the registered KVM virtual machines, how to start, stop or delete them. Virsh (based on libvirt API) can be used to manage KVM virtual machines in the Linux console.

# virsh list – shows the list of running VMs

# virsh list --all – shows the list of all registered virtual machines (including those that are shut down)

virsh list virtual machines on kvm host

As you can see in the screenshot, in the first case a shutdown VM was not displayed.

# virsh shutdown <vm name> — shutdown the virtual machine

# virsh start <vm name> — start the virtual machine

# virsh suspend <vm name> — suspend the VM

# virsh resume <vm name> — start a suspended virtual machine

# virsh reboot <vm name> — restart VM

# virsh destroy <vm name> — initiates an immediate ungraceful shutdown and stops the VM (use this command when the guest virtual machine is unresponsive)

# virsh undefine <vm name> — unregister the VM and delete its storage volumes

# virsh vcpuinfo <vm name> — shows the information about the vCPU of the VM

virsh vcpuinfo

Here are some other commands to get different information about a virtual machine:

# virsh domid <vm name> — get the virtual machine ID

# virsh domuuid <vm name> — show the UUID of a virtual machine

# virsh dominfo <vm name> — displays information about a virtual machine

# virsh domstate <vm name> — view VM state

Virsh commands cheatsheet to manage KVM virtual machine

# virsh dumpxml <vm name> — list the configuration file of a virtual machine in the XML format

How to Add Memory and vCPU to a KVM Virtual Machine?

In the KVM console, you can add or reduce CPU or memory resources assigned to a VM in two ways:

  • Using virsh
  • Via a VM configuration XML file

If a virtual machine is running, shut it down:

# virsh shutdown test-centos

Domain test-centos is being shutdown

Then use virsh to change the number of virtual CPUs (vCPU) to 6:

# virsh setvcpus <vm name> <vcpu_count> --config

For example:

# virsh setvcpus test-centos 6 --config

After I had run the command, an error appeared:

error: invalid argument: requested vcpus is greater than max allowable vcpus for the persistent domain: 6 > 4

You cannot set the number of cores greater than the maximum number. To increase the maximum allowed number of VM vCPUs, run this command:

# virsh setvcpus test-centos 6 --config --maximum

Then run the previous command and start the virtual machine:

virsh setvcpus

Let’s check the number of vCPUs in the VM settings:

# virsh dumpxml test-centos

<domain type='kvm'>
<name>test-centos</name>
<uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>6</vcpu>

In the same way, you can add additional RAM to the virtual machine:

# virsh setmem <vm_name> <memsize> --config

For example:

# virsh setmem test-centos 4G --config

An error occurred on the same reason:

error: invalid argument: cannot set memory higher than max memory.

Let’s increase the maximum amount of memory:

# virsh setmaxmem test-centos 6G --config

Then you can add memory to the VM.

Before making any changes, be sure to shutdown the virtual machine and then start it.

Also, you can change the amount of resources of a KVM VM in its XML configuration file. You can edit it online or backup the VM XML file, modify it and apply to the virtual machine.

Let’s edit the VM XML file online:

# virsh edit <vm_name>

In the vi editor that appears, make the changes you want by pressing the Insert button.

Change the following block:

<domain type='kvm'>
<name>test-centos</name>
<uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid>
<memory unit='KiB'>6291456</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>6</vcpu>
<os>

For example, let’s allocate 2 processor cores and 1 GB of memory to the VM:

How to add memory, vCPU, hard disk to Linux KVM virtual machine?

Note that the memory size is shown in kilobytes.

Save the changes in the file and reboot your VM:

# virsh reboot <vm_name>

You can do the same if you backup the XML file:

# virsh dumpxml <vm_name> > /root/test.xml
# vi /root/test.xml

Change the settings you want, save the file and apply it to the virtual machine:

# virsh shutdown test-centos

Domain test-centos is being shutdown

# virsh define /root/test.xml

Domain test-centos defined from /root/test.xml

# virsh start test-centos

Domain test-centos started
Sometimes when you change a VM configuration file online, the assigned resources are reset after the reboot. In this case, stop the virtual machine and then just start it.

Add a Disk to an Existing KVM VM

In one of our articles we described how to extend or reduce the disk size of KVM virtual machine. But we did not tell how to add an additional disk.

First of all, create a new disk file for a virtual machine:

# qemu-img create -f qcow2 -o size=20G /vz/disk/test.img

Instead of qcow2, you can use the disk format you want. Also specify the file path. My disk storage is located in /vz/disk/.

Then you can add a virtual disk device to the VM:

# virsh attach-disk <vm_name> /vz/disk/test.img vdb --type disk --persistent

Shutdown and reboot your VM, then see what you have got:

# virsh shutdown test-centos

Domain test-centos is being shutdown

# virsh start test-centos

Domain test-centos started

# virsh dumpxml test-centos

<domain type='kvm' id='14'>
<name>test-centos</name>
<uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>6</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='yes'/>
</os>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/vz/disk/test.img'/>
<backingStore/>
<target dev='vdb' bus='virtio'/>
<alias name='virtio-disk1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</disk>

As you can see, the disk has been added.

How to Add a Second NIC to the KVM Virtual Machine?

Let’s try to add another network interface to a VM. First of all, check the network interfaces on the Linux host:

# brctl show

Configure KVM Networking

I have one virtual machine with one network interface on my KVM host. I want to add another virtual network interface to br0. Run these commands:

# virsh shutdown test-centos
# virsh attach-interface test-centos --type bridge --source br0 --persistent
# virsh start test-centos

Make sure that another network interface has appeared in your VM:

How to Setup Bridge Networking with KVM on Centos or RHEL?

<interface type='bridge'>
<mac address='52:54:00:7e:c1:9f'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:2f:23:79'/>
<source bridge='br0'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
</interface>

Also you can change network settings of your virtual machine directly via the XML file:

# virsh edit test-centos

After the first network interface, add these lines:

<interface type='bridge'>
<source bridge='br0'/>
</interface>

Save the file and start the VM. KVM will add the rest configuration (MAC address, etc.) itself.

In this article we have covered the main things you may need to manage KVM virtual machines from the Linux server console. In the next article we will show how to manage virtual machines through the virt-manager GUI.

0 comment
0
Facebook Twitter Google + Pinterest
previous post
Enable Automatic Package Updates on RHEL/CentOS
next post
How to Check Who Created a User Account in AD?

Related Reading

Can’t Remove Language Keyboard Layout in Windows 10

April 14, 2022

Installing PowerShell Core on Linux Distros

February 28, 2022

Configuring NFS Server and Client on Linux CentOS/RHEL

November 11, 2021

How to Install and Use ClamAV Antivirus on...

October 13, 2021

Configuring Cron Jobs with Crontab on CentOS/RHEL Linux

July 12, 2021

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows 7
  • Windows Server 2019
  • Windows Server 2016
  • Windows Server 2012 R2
  • PowerShell
  • VMWare
  • Hyper-V
  • MS Office

Recent Posts

  • Create Organizational Units (OU) Structure in Active Directory with PowerShell

    May 17, 2022
  • Windows Security Won’t Open or Shows a Blank Screen on Windows 10/ 11

    May 17, 2022
  • How to Manually Install Windows Updates from CAB and MSU Files?

    May 16, 2022
  • RDS and RemoteApp Performance Issues on Windows Server 2019/2016

    May 16, 2022
  • Deploying Software (MSI Packages) Using Group Policy

    May 12, 2022
  • Updating VMware ESXi Host from the Command Line

    May 11, 2022
  • Enable or Disable MFA for Users in Azure/Microsoft 365

    April 27, 2022
  • Fix: You’ll Need a New App to Open This Windows Defender Link

    April 27, 2022
  • How to Reset an Active Directory User Password with PowerShell and ADUC?

    April 27, 2022
  • How to Completely Uninstall Previous Versions of Office with Removal Scripts?

    April 26, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • Using iPerf to Test Network Speed and Bandwidth (Throughput)
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Install and Configure SNMP on RHEL/CentOS/Fedor
Footer Logo

@2014 - 2018 - Windows OS Hub. All about operating systems for sysadmins


Back To Top