KVM (Kernel-based Virtual Machine) is an open-source hardware virtualization solution that is part of the Linux kernel, allowing virtual machines to run on hardware that supports virtualization technologies such as Intel VT-x or AMD-V.
KVM transforms the Linux kernel into a bare-metal hypervisor, enabling users to run multiple isolated virtual machines, each capable of running its own operating system (such as Linux, Windows, or others).
1. Basic Virtual Machine Operations
1.1 View Virtual Machine List
virsh list --all
–all: Displays all virtual machines, including those that are running and stopped.
1.2 Start Virtual Machine
virsh start <virtual machine name or ID>
1.3 Shutdown Virtual Machine
virsh shutdown <virtual machine name or ID>
This is a safe shutdown, equivalent to pressing the power button in the virtual machine.
1.4 Force Shutdown Virtual Machine
virsh destroy <virtual machine name or ID>
Note: This is a forced operation and may result in data loss.
1.5 Reboot Virtual Machine
virsh reboot <virtual machine name or ID>
Equivalent: shutdown + start.
1.6 Suspend and Resume Virtual Machine
Suspend virtual machine:
virsh suspend <virtual machine name>
Resume suspended virtual machine:
virsh resume <virtual machine name>
1.7 Delete Virtual Machine
virsh undefine <virtual machine name>
Note: This command only removes the virtual machine configuration and does not delete the disk files.
2. Virtual Machine Resource Management
2.1 View Virtual Machine Information
virsh dominfo <virtual machine name>
The output includes ID, status, memory, CPU, etc.
2.2 Get Virtual Machine’s XML Configuration
virsh dumpxml <virtual machine name>
virsh dumpxml my-vm > my-vm.xml
2.3 Edit Virtual Machine’s XML Configuration
virsh edit <virtual machine name>
Opens the virtual machine’s XML file for editing using the system’s default editor.
2.4 Define New Virtual Machine
virsh define <XML configuration file>
2.5 Adjust Virtual Machine Memory
virsh setmem <virtual machine name> <memory size>[K|M|G]
virsh setmem my-vm 2G
2.6 Adjust Virtual Machine CPU
virsh setvcpus <virtual machine name> <number of CPUs> --config
–config: Makes the change permanent.
3. Storage Management
3.1 View Storage Pools
virsh pool-list --all
3.2 Create Storage Pool
virsh pool-create <XML configuration file>
virsh pool-create pool.xml
3.3 View Storage Volumes
virsh vol-list <storage pool name>
virsh vol-list default
3.4 Create Storage Volume
virsh vol-create <storage pool name> <XML configuration file>
virsh vol-create default volume.xml
4. Network Management
4.1 List Networks
virsh net-list --all
4.2 Create Network
virsh net-create <XML configuration file>
4.3 Delete Network
virsh net-destroy <network name>
virsh net-undefine <network name>
5. Snapshot Management
5.1 Create Snapshot
virsh snapshot-create-as <virtual machine name> <snapshot name> --description "<description>"
5.2 Restore Snapshot
virsh snapshot-revert <virtual machine name> <snapshot name>
6. Image Management
6.1 Create Disk Image
qemu-img create -f qcow2 <image path> <size>
qemu-img create -f qcow2 /var/lib/libvirt/images/my-vm.qcow2 20G
6.2 View Image Information
qemu-img info <image path>
7. Virtual Machine Console
7.1 Connect to Virtual Machine Console
virsh console <virtual machine name>
Exit console: Press Ctrl + ].
8. Performance Monitoring
8.1 View Real-time Performance
virsh domstats <virtual machine name>
8.2 View Virtual Machine VCPU Information
virsh vcpuinfo <virtual machine name>
8.3 View Virtual Machine I/O Information
virsh blkstat <virtual machine name>
View Help Information
virsh help
virsh help <command>
These commands are applicable to most KVM management scenarios and can be used more efficiently with graphical tools like virt-manager to manage KVM virtualization environments.
If there are any errors or omissions, please correct them! If you find this useful, please give a thumbs up and follow!