2021-11-11 ② enterprise virtualization Kvm management, virtual machine management, adding devices, Part II

KVM virtual machine management

Components of virtual machines
1. Virtual machine configuration file

[root@youngfit qemu]# ls /etc/libvirt/qemu
networks  vm1.xml

2. Media for storing virtual machines

[root@youngfit qemu]# ls /var/lib/libvirt/images/

These can be operated graphically. What if we can't operate graphically sometimes? At this time, we need to use commands to operate pull

Basic management commands of virtual machine:


View virtual machines:

[root@youngfit ~]# virsh list 		  #Only virtual machines in power on state are included
     Id    Name                           State
     2     vm1                            running

[root@youngfit ~]# virsh list --all   #Includes virtual machines that are not powered on
     Id    Name                           State
     2     vm1                            running

To view kvm virtual machine configuration files:

[root@youngfit ~]# virsh dumpxml name  # Name is the name of the virtual machine

Save the configuration file of node4 virtual machine to node6.xml, which can be ignored

[root@youngfit ~]# virsh dumpxml node4 > /etc/libvirt/qemu/node6.xml

Modify node6 configuration file: This is important

[root@youngfit ~]# virsh edit node6      
If you use it directly vim If the editor modifies the configuration file, it needs to be restarted libvirtd Service or restart define Profile, with edit Modification is not required.


[root@youngfit ~]# virsh start vm1
Domain vm1 started

Pause (suspend) virtual machine:

[root@youngfit ~]# virsh suspend vm_name  

Restore virtual machine:

[root@youngfit ~]# virsh resume vm_name   

Method 1:

[root@youngfit ~]# virsh shutdown vm1 / / normal shutdown
Domain vm1 is being shutdown

Method 2:

[root@youngfit ~]# virsh destroy vm1 	// Force close
Domain vm1 destroyed


[root@youngfit ~]# virsh reboot vm1
Domain vm1 is being reboote

Reset: it's no different from restarting

[root@youngfit ~]# virsh reset vm1
Domain vm1 was reset

Delete virtual machine:

[root@youngfit ~]# virsh undefine vm2 	// First, close it
Domain vm2 has been undefined

Note: undefine cannot be deleted when the virtual machine is turned on, but it will be deleted directly if it is destroy ed again

Automatic startup of virtual machine after startup:
#If the virtual machine starts automatically, the services inside should be set to start automatically, otherwise it doesn't make sense

[root@youngfit ~]# virsh autostart vm1  
field vm1 Mark as auto start
[root@youngfit ~]# ls /etc/libvirt/qemu/autostart / / / this directory does not exist by default. It is created automatically when there is a virtual machine to start
[root@youngfit ~]# virsh autostart --disable vm1
 field vm1 Unmark as auto start
[root@youngfit ~]# ls /etc/libvirt/qemu/autostart/

How to view ip addresses of started virtual machines
Suppose the vm2 virtual machine is started
Method 1: find your mac address first

[root@youngfit ~]# virsh dumpxml vm2|grep mac
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
      <mac address='52:54:00:cc:3e:79'/>

Filter view

[root@youngfit ~]# arp -a |grep "52:54:00:cc:3e:79"
? ( at 52:54:00:cc:3e:79 [ether] on virbr0

Method 2:

[root@youngfit ~]# virsh domifaddr vm2
 Name       MAC address          Protocol     Address
 vnet1      52:54:00:d3:37:16    ipv4

Virtual machine add device

1. Graphic mode: this direct point-to-point mode
First, shut down the virtual machine to which you want to add hardware
Double click the virtual machine, click View at the top of the open dialog box, click Details, and click Add Hardware to select the virtual hardware to be added

2. How to modify the configuration file:

Let's add a disk to the virtual machine vm9 as an example: copy one more disk file in the configuration file and change the name
Let's add a network card to the virtual machine vm9 as an example: copy one more network card file in the configuration file and change the name
First, you need to create the disk to be added

qemu-img create -f qcow2 /var/lib/libvirt/images/vm9-1.qcow2 5G

Create an empty disk file: Here we create a 5G disk, but when we create it, we can't see its size through ll -h. we can't see it until it is added

Modify profile

[root@youngfit qemu]# virsh edit vm9
 If used vim Go in. Remember to re define

Pay attention to modifying three locations, the file name in the absolute path, changing a number randomly in the slot, and hanging the directory name
After adding, start the virtual machine

virsh start vm9

Remote connection to vm9 virtual machine

[root@vm9 ~]# lsblk 
vda             252:0    0    5G  0 disk 
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0    4G  0 part 
  ├─centos-root 253:0    0  3.5G  0 lvm  /
  └─centos-swap 253:1    0  512M  0 lvm  [SWAP]
vdb             252:16   0    5G  0 disk 

You can see our newly added disk vdb
Then you can partition normally, create a file system, and mount it

Network card: same as adding disk

<interface type='network'>
      <mac address='52:54:00:fc:c6:0b'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

This is to modify two positions. The mac address and slot can be changed by any number
Login view

Tags: Linux Operation & Maintenance server

Posted on Thu, 11 Nov 2021 18:38:52 -0500 by consultant1027