目录
2.1.2使用virsh 命令添加一块网卡,attach-interface后面接上虚拟机名字,
2.3.2在宿主机行通过virsh edit 虚拟机名称 命令打开虚拟机的配置文件,并且修改配置文件
2.3.3先关机虚拟机,再重启虚拟机,使修改的配置文件生效。
一、使用virsh 对虚拟机进行基本的管理
virsh命令提供两种模式,一种是交互式,一种是非交互式。本次实验使用非交互式对虚拟机进行基本的管理。
1、查看虚拟机
[root@zhoujunru_node1 zhou]# virsh list #显示只能运行的虚拟机
Id Name State
--------------------
[root@zhoujunru_node1 zhou]# virsh list --all #查看所有的虚拟机
Id Name State
-----------------------
- vm01 shut off
[root@zhoujunru_node1 zhou]#
2、启动虚拟机
[root@zhoujunru_node1 zhou]# vitsh start vm01 #start命令用于启动虚拟机,后面接虚拟机的名字
bash: vitsh: command not found...
[root@zhoujunru_node1 zhou]# virsh start vm01
Domain vm01 started
[root@zhoujunru_node1 zhou]# virsh list
Id Name State
----------------------
1 vm01 running
3、关闭虚拟机
[root@zhoujunru_node1 zhou]# virsh shutdown vm01
Domain vm01 is being shutdown
[root@zhoujunru_node1 zhou]# virsh list
Id Name State
--------------------
4、挂起虚拟机
[root@zhoujunru_node1 zhou]# virsh destroy vm01
Domain vm01 destroyed
5、查看虚拟机信息
[root@zhoujunru_node1 zhou]# virsh dominfo vm01
Id: -
Name: vm01
UUID: 1c899169-d49f-4b36-87cd-2da29061bec8
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 1572864 KiB
Used memory: 1572864 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
6、删除虚拟机
[root@zhoujunru_node1 zhou]# virsh undefine vm01 #undefine命令后面接虚拟机名字
7、显示当前虚拟机的配置文件
[root@zhoujunru_node1 zhou]# virsh dumpxml vm01
<domain type='kvm'>
<name>vm01</name>
<uuid>1c899169-d49f-4b36-87cd-2da29061bec8</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/8.3"/>
</libosinfo:libosinfo>
二、对虚拟机进行进阶管理
2.1给虚拟机添加网卡
2.1.1在宿主机上查看网卡信息
[root@zhoujunru_node1 zhou]# virsh domiflist vm01
Interface Type Source Model MAC
-------------------------------------------------------------
vnet1 network default virtio 52:54:00:97:8f:58
2.1.2使用virsh 命令添加一块网卡,attach-interface后面接上虚拟机名字,
是网络类型,default是虚拟机的网络源,default网络默认是NAT模式,--persistent选项代表永久,意味着虚拟机重启动后此网卡还会生效。
[root@zhoujunru_node1 zhou]# virsh attach-interface vm01 network default --persistent
2.2给虚拟机添加磁盘
2.2.1查看虚拟机磁盘情况
[root@zhoujunru_node1 zhou]# virsh domblklist vm01
Target Source
----------------------------------------------
vda /var/lib/libvirt/images/vm01.qcow2
2.2.2添加磁盘
[root@zhoujunru_node1 zhou]# dd if=/dev/zero of=/var/lib/libvirt/images/vm01_1.img bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 0.283027 s, 1.9 GB/s
[root@zhoujunru_node1 zhou]# virsh attach-disk vm01 /var/lib/libvirt/images/vm01_1.img vdb
Disk attached successfully
[root@zhoujunru_node1 zhou]# virsh domblklist vm01
Target Source
----------------------------------------------
vda /var/lib/libvirt/images/vm01.qcow2
vdb /var/lib/libvirt/images/vm01_1.img
sda -
[root@zhoujunru_node1 zhou]#
2.3通过修改配置文件来调整cpu个数和内存大小
2.3.1使用lscpu命令在虚拟机查看cpu
[zhou@localhost ~]$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: AuthenticAMD
CPU family: 23
Model: 1
Model name: AMD EPYC Processor (with IBPB)
Stepping: 2
CPU MHz: 1796.627
BogoMIPS: 3593.25
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 64K
L2 cache: 512K
L3 cache: 8192K
NUMA node0 CPU(s): 0
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext ssbd ibrs ibpb vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves clzero wbnoinvd arat umip arch_capabilities
[zhou@localhost ~]$
内存为1312MB
[zhou@localhost ~]$ free -m
total used free shared buff/cache available
Mem: 1312 839 65 15 407 315
Swap: 923 288 635
2.3.2在宿主机行通过virsh edit 虚拟机名称 命令打开虚拟机的配置文件,并且修改配置文件
<domain type='kvm'>
<name>vm01</name>
<uuid>1c899169-d49f-4b36-87cd-2da29061bec8</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/8.3"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>2572864</memory> #修改内存大小
<currentMemory unit='KiB'>1572864</currentMemory>
<vcpu placement='static'>2</vcpu> #修改cpu个数
<os>
<type arch='x86_64' machine='pc-q35-rhel8.2.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='host-model' check='partial'/>
<clock offset='utc'>
"/tmp/virshOIQ9J2.xml" 145L, 5647C
......
2.3.3先关机虚拟机,再重启虚拟机,使修改的配置文件生效。
[root@zhoujunru_node1 zhou]# lscpu | grep 'CPU(s)'
CPU(s): 2
On-line CPU(s) list: 0,1
NUMA node0 CPU(s): 0,1
[root@zhoujunru_node1 zhou]# free -m
total used free shared buff/cache available
Mem: 7741 3330 1686 13 2724 4125
Swap: 8063 50 8013