Tag: virsh

  • How to delete KVM Virtual machine using virsh

    How to delete KVM Virtual machine using virsh

    Deleting a KVM virtual machine using “virsh” is a multi-step process. You need to stop the virtual machine, find the storage device used by them VM, and remove the storage devices. Then undefine the virtual machine.

    Here are the commands used to delete a KVM VM with the name win10.

    First shutdown the VM

    virsh shutdown win10
    

    If it did not stop, you can force stop with the command

    virsh destroy win10
    

    Find information about the VM with the command “virsh dumpxml –domain VM_NAME”

    root@mail:~# virsh dumpxml --domain win10
    
      win10
      e40399f7-9936-41ce-9a70-0251cb948cae
      
        
          
        
      
      16777216
      16777216
      4
      
        hvm
        
      
      
        
        
        
          
          
          
        
        
      
      
      
        
        
        
        
      
      destroy
      restart
      destroy
      
        
        
      
      
        /usr/bin/qemu-system-x86_64
        
          
          
          
          

    In the above result, you can see we are using the following storage device for this VM

        
          
          
          
          

    This VM uses the storage device /dev/vg1/win10, we need to remove it.

    lvremove /dev/vg1/win10
    

    The VM also uses ISO file /var/lib/libvirt/images/Win10_21H2_English_x64.iso, if you don’t need it, you can delete it.

    rm -f /var/lib/libvirt/images/Win10_21H2_English_x64.iso
    

    To delete the VM, you can use

    virsh undefine win10
    

    Example

    root@mail:~# virsh undefine win10
    Domain win10 has been undefined
    
    root@mail:~# 
    

    Back to virsh

  • How to force shutdown a KVM VM with virsh

    How to force shutdown a KVM VM with virsh

    I have a KVM virtual machine running Windows, and when I try to shutdown, it never stopped.

    root@sok:~# virsh list
     Id   Name       State
    --------------------------
     1    iredmail   running
     3    win10      running
    
    root@sok:~# virsh shutdown win10
    Domain win10 is being shutdown
    
    root@sok:~# virsh list
     Id   Name       State
    --------------------------
     1    iredmail   running
     3    win10      running
    
    root@sok:~# 
    

    To force shutdown a KVM virtual machine using virsh, you can use the command

    virsh destroy VM_NAME
    

    Example

    root@sok:~# virsh destroy win10
    Domain win10 destroyed
    
    root@sok:~# virsh list 
     Id   Name       State
    --------------------------
     1    iredmail   running
    
    root@sok:~# virsh start win10
    Domain win10 started
    
    root@sok:~# virsh list
     Id   Name       State
    --------------------------
     1    iredmail   running
     4    win10      running
    
    root@sok:~#
    

    Back to virsh

  • virsh

    virsh command is used to manage KVM vps. It is part of libvirt package.

    http://libvirt.org

    To list KVM virtual machines, run

    virsh list

    Start a VM

    virsh start VM_ID

    To shutdown a VM, run

    virsh shutdown VM_ID

    To delete a VM, run

    virsh shutdown VM_ID

    Connect to a Remote Server

    virsh --connect qemu+ssh://[email protected]/system

    You need SSH Key based authentication setup.

    List All VM

    boby@fwhlin:~ $ virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     vm1                            shut off
    
    boby@fwhlin:~ $

    List All Running VM

    [root@server70 ~]# virsh list
     Id    Name                           State
    ----------------------------------------------------
     1     vm10                           running
     3     vm12                           running
     5     vm15                           running
     7     vm17                           running
     12    vm16                           running
    
    [root@server70 ~]#

    Get Info About a VM

    virsh dominfo VM_NAME

    example

    [root@server70 ~]# virsh dominfo vm18
    Id:             10
    Name:           vm18
    UUID:           b6c3a3d8-d96a-c3fa-1fad-04aff28e476a
    OS Type:        hvm
    State:          running
    CPU(s):         2
    CPU time:       422.9s
    Max memory:     524288 KiB
    Used memory:    524288 KiB
    Persistent:     yes
    Autostart:      disable
    Managed save:   no
    Security model: none
    Security DOI:   0
    
    [root@server70 ~]#

    Change the Memory of a VM

    virsh dumpxml vm18 > vm18.xml
    virsh shutdown vm18
    vi vm18.xml

    Find

    <memory unit='KiB'>524288</memor<memory unit='KiB'>524288</memory>
    <currentMemory unit='KiB'>524288</currentMemory>

    To change Memory to 1 GB, Replace it with

    <memory unit='KiB'>1048576</memory>
    <currentMemory unit='KiB'>1048576</currentMemory>

    Load configuration

    virsh define vm18.xml 

    Example

    [root@server70 ~]# virsh define vm18.xml
    Domain vm18 defined from vm18.xml
    
    [root@server70 ~]#

    Start VM

    virsh start vm18
    [root@server70 ~]# virsh start vm18
    Domain vm18 started
    
    [root@server70 ~]#

    See kvm