Summary of KVM virtual machine technology learning

  • 2020-05-17 07:05:03
  • OfStack

Recently, I have been learning KVM, and the process is not too fast. Recently, I have sorted out a study note of KVM virtual machine technology, so I would like to share it with you and give you a reference. Need a friend can come to understand 1.

The KVM virtual machine is managed mainly through the virsh command.

1. View the KVM virtual machine configuration file and its running status

(1) default configuration file location of KVM virtual machine: /etc/libvirt/qemu/

The autostart directory is the directory where you configure the kvm virtual machine to boot.

(2) virsh commands help

# virsh -help

Or directly virsh command and, then execute the subcommand. This is shown below.

[root@node1 ~]# virsh

Welcome to virsh, a virtualized interactive terminal.

Type 'help' to get help with the command

'quit' exit

virsh # help

...

(3) check the state of kvm virtual machine

# virsh list --all

2. Boot the KVM virtual machine

# virsh start oeltest01

3. KVM virtual power off or off

(1) to turn it off

By default, the virsh tool cannot shut down the linux virtual machine. The linux operating system needs to turn on and start the acpid service. This service must be configured when installing the KVM linux virtual machine.

# chkconfig acpid on
# service acpid restart

virsh shutdown

# virsh shutdown oeltest01

(2) forced power off

# virsh destroy wintest01

4. Start the virtual machine with a configuration file
# virsh create /etc/libvirt/qemu/wintest01.xml

5. Configure the boot - up virtual machine
# virsh autostart oeltest01

The autostart directory is the kvm virtual machine startup directory, and you can see the KVM configuration file link in this directory.

6. Export the KVM virtual machine configuration file
# virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml

The KVM virtual machine configuration file can be backed up in this way.

7. Add and remove the KVM virtual machine

(1) delete kvm virtual machine
# virsh undefine wintest01

Note: this command only deletes the wintest01 configuration file, not the virtual disk file. As shown in the figure below.

(2) redefine the virtual machine configuration file

Restore the original KVM virtual machine definition by exporting the backup configuration file and redefine the virtual machine.
# mv /etc/libvirt/qemu/wintest02.xml /etc/libvirt/qemu/wintest01.xml
# virsh define /etc/libvirt/qemu/wintest01.xml

8. Edit the KVM virtual machine configuration file
# virsh edit wintest01

virsh edit will call vi command editor/etc libvirt/qemu/wintest01 xml configuration file. You can also edit, modify, and save directly through the vi command.

Editing via vi is possible but not recommended.


[root@node1 qemu]# vi /etc/libvirt/qemu/wintest01.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit wintest01
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>wintest01</name>
<uuid>fe31ea48-7d6a-f3cb-cede-2f9bd9dec2bd</uuid>
<memory unit='KiB'>524288</memory>
<currentMemory unit='KiB'>524288</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='rhel6.4.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/data/wintest01.img'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:2b:2f:fe'/>
<source bridge='br0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</memballoon>
</devices>
<seclabel type='none'/>
</domain>

9. Other virsh commands

(1) suspend the server
# virsh suspend oeltest01

(2) restore the server
# virsh resume oeltest01

The virsh command is rich. Can perform a variety of maintenance tasks, this article just from the point of view of maintenance and management examples of common commands, for the use of the command to provide a train of thought.


Related articles: