Skip to content

Instantly share code, notes, and snippets.

@archerslaw
Last active August 17, 2022 13:38
Show Gist options
  • Save archerslaw/9798802 to your computer and use it in GitHub Desktop.
Save archerslaw/9798802 to your computer and use it in GitHub Desktop.
Simple virsh commands.
# /bin/systemctl restart libvirtd.service
# /bin/systemctl enable libvirtd.service
Note:
How to turn on the debug logs for libvirtd.
1)open /etc/libvirt/libvirtd.conf in your favourite editor.
2)find & replace, or set these variables:
log_level = 1
log_filters="3:remote 4:event 3:json 3:rpc" | log_filters="1:qemu_monitor_json"
log_outputs="1:file:/var/log/libvirt/libvirtd.log"
3)save and exit the configure file.
4)restart libvirtd service.
# systemctl restart libvirtd.service
= I. Domain Management =
- define (but don't start) a domain from an XML file
# virsh define $domain_name.xml
Domain domain_name defined from domain_name.xml
- start a (previously defined) inactive domain
# virsh start $domain_name
Domain domain_name started
- create a domain from an XML file
# virsh create domain_name.xml
Domain domain_name created from domain_name.xml
virsh:suspend/resume = HMP/QMP:stop/cont
# virsh suspend 1
Domain 1 suspended
# virsh list --all
Id Name State
----------------------------------------------------
1 domain_name paused
# virsh resume 1
Domain 1 resumed
# virsh list --all
Id Name State
----------------------------------------------------
1 domain_name running
# virsh reboot $domain_name
Domain $domain_name is being rebooted
# virsh qemu-monitor-command $domain_name --hmp info qtree
# virsh attach-device $domain_name $device_name.xml
- destroy(stop)/undefine a domain
# virsh destroy $domain_name
Domain domain_name destroyed
# virsh undefine $domain_name
Domain domain_name has been undefined
{ "execute": "guest-suspend-ram"}
{ "execute": "guest-suspend-disk"}
#virsh dompmsuspend $domain-name mem
#virsh dompmwakeup $domain-name
#virsh dompmsuspend $domain-name disk
- migrate domain to another host
# virsh migrate --live $domain_name qemu+ssh://$dst_ip/system
migrate-setmaxdowntime set maximum tolerable downtime
migrate-compcache get/set compression cache size
migrate-setspeed Set the maximum migration bandwidth
migrate-getspeed Get the maximum migration bandwidth
- vnc display
# virsh vncdisplay $domain_name
127.0.0.1:0
connetion ]# vncviewer 127.0.0.1:0 # remote-viewer vnc://127.0.0.1:5900
- refer to
virsh help
# virsh --help
# virsh $command --help
- Send keycodes to the guest
# virsh send-key $domain_name $keycode_string
$keycode_string: alt-sysrq-h/alt-sysrq-t/alt-sysrq-m/alt-sysrq-c
e.g.:# virsh send-key sluo_kvm_testing_example KEY_LEFTALT KEY_SYSRQ KEY_H
- dump the core of a domain to a file for analysis, dumpxml domain information in XML.
# virsh dump $domain_name $file_name
# virsh dumpxml $domain_name
- attach/detach disk device
# virsh attach-disk $domain_name /home/my-data-disk.raw vda
Disk attached successfully
# virsh dumpxml $domain_name
......
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/home/my-data-disk.raw'/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
</controller>
<controller type='scsi' index='0'>
<alias name='scsi0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</controller>
......
# virsh detach-disk sluo_kvm_testing_example vda
Disk detached successfully
- QEMU Monitor Command
# virsh qemu-monitor-command $domain_name --hmp 'info qtree'
# virsh qemu-monitor-command rhel7.1 --pretty '{"execute":"xxxxxx"}'
e.g.
# virsh qemu-monitor-command rhel7.1 --pretty '{"execute":"query-iothreads"}'
{
"return": [
],
"id": "libvirt-11"
}
- QEMU Guest Agent Command
# virsh qemu-agent-command $domain_name --async --cmd 'xxx'
# virsh qemu-agent-command test '{"execute":"guest-info"}'
= II. Domain Monitoring (help keyword 'monitor') =
- list domains
# virsh list
Id Name State
----------------------------------------------------
1 domain_name running
- list inactive & active domains
# virsh list --all
- domain state
# virsh domstate $domain_name
running
- get memory statistics for a domain
# virsh dommemstat $domain_name
actual 2097152
rss 1030884
- domain information
# virsh dominfo $domain_name
Id: 12
Name: sluo_kvm_testing_example
UUID: d1a23b70-ab58-47a7-9ad7-0de528202542
OS Type: hvm
State: running
CPU(s): 2
CPU time: 76.3s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c179,c990 (permissive)
- list all domain virtual interfaces
# virsh domiflist $domain_name
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network default virtio-net-pci 0c:01:38:b6:40:24
- get link state of a virtual interface
# virsh domif-getlink $domain_name --interface 0c:01:38:b6:40:24
0c:01:38:b6:40:24 default
- get network interface stats for a domain
# virsh domifstat $domain_name vnet0
vnet0 rx_bytes 45472
vnet0 rx_packets 870
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 0
vnet0 tx_packets 0
vnet0 tx_errs 0
vnet0 tx_drop 0
- domain control interface state
# virsh domcontrol $domain_name
ok
- list all domain blocks
# virsh domblklist $domain_name
Target Source
------------------------------------------------
hda /home/RHEL-7.0-20140507.0-Server-x86_64.qcow2
- domain block device size information
# virsh domblkinfo $domain_name
Capacity: 10737418240
Allocation: 6684946432
Physical: 6684946432
- show errors on block devices
# virsh domblkerror $domain_name
No errors found
Install guest via network using virsh XML.
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<kernel>/var/lib/libvirt/boot/vmlinuz</kernel>
<initrd>/var/lib/libvirt/boot/initrd.img</initrd>
<cmdline>method=http://download.englab.nay.redhat.com/pub/rhel/rel-eng/latest-RHEL-7/compose/Server/x86_64/os
ks=http://fileshare.englab.nay.redhat.com/pub/section3/run/http-ks/ks-rhel7-x86_64.cfg
</cmdline>
<boot dev='hd'/>
</os>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment