kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

简介:

KVM虚拟机的管理主要是通过virsh命令对虚拟机进行管理。

1.  查看KVM虚拟机配置文件及运行状态

(1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/

autostart目录是配置kvm虚拟机开机自启动目录。 

fd7e73c0ce330a92248c9b3dcc8917c6.png-wh_

(2) virsh命令帮助

# virsh -help

或直接virsh命令和,再执行子命令。如下所示。

[root@node1 ~]# virsh   
欢迎使用 virsh,虚拟化的交互式终端。 

输入:'help' 来获得命令的帮助信息   
'quit' 退出 

virsh # help   
……

(3) 查看kvm虚拟机状态

# virsh list --all

8d6465daa466938c35e0a153430edd73.png-wh_

2.  KVM虚拟机开机

# virsh start CentOS6.5-01

ad5ff1101464f3554d03884dc3a3753e.png-wh_

3. KVM虚拟机关机或断电

(1) 关机

默认情况下virsh工具不能对linux虚拟机进行关机操作,linux操作系统需要开启与启动acpid服务。在安装KVM linux虚拟机必须配置此服务。

# chkconfig acpid on   
# service acpid restart

virsh关机

# virsh shutdown CentOS6.5-01

ebc882998c43cdf2b977e82f4b7b3a57.png-wh_

(2) 强制关闭电源

# virsh destroy Ser08R2-01

4d994aa7329c7743349d6212ed0567c7.png-wh_

4. 通过配置文件启动虚拟机

# virsh create /etc/libvirt/qemu/Ser08R2-01.xml

# virsh create /etc/libvirt/qemu/CentOS6.5-01.xml

1cc2e7e52b526a59563c34258c7260db.png-wh_

5. 配置开机自启动虚拟机

# virsh autostart CentOS6.5-01

autostart目录是kvm虚拟机开机自启动目录,可以看到该目录中有KVM配置文件链接。

3059c042a66a16da2da41180f8f8a0b7.png-wh_

6. 导出KVM虚拟机配置文件

# virsh dumpxml Ser08R2-01 > /etc/libvirt/qemu/Ser08R2-02.xml 

KVM虚拟机配置文件可以通过这种方式进行备份。

b334ba21c8b665801ff88c9d33736d09.png-wh_

7. 添加与删除KVM虚拟机

(1)  删除kvm虚拟机

# virsh undefine Ser08R2-01

说明:该命令只是删除wintest01的配置文件,并不删除虚拟磁盘文件。如下图所示。

2cb6cbb6463a0b3229873ab029bd34dc.png-wh_

(2)  重新定义虚拟机配置文件

通过导出备份的配置文件恢复原KVM虚拟机的定义,并重新定义虚拟机。

# mv /etc/libvirt/qemu/Ser08R2-02.xml /etc/libvirt/qemu/Ser08R2-01.xml 

# virsh define /etc/libvirt/qemu/Ser08R2-01.xml 

99a5995481a87df3a65b3631930c477d.png-wh_

8. 编辑KVM虚拟机配置文件

# virsh edit Ser08R2-01

virsh edit将调用vi命令编辑/etc/libvirt/qemu/Ser08R2-01.xml配置文件。也可以直接通过vi命令进行编辑,修改,保存。

可以但不建议直接通过vi编辑。

f68e934863ea0177551faba7d3ae2f54.png-wh_

[root@node1 qemu]# vim /etc/libvirt/qemu/Ser08R2-01.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!--
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 Ser08R2-01
or other application using the libvirt API.
-->
 
<domain  type = 'kvm' >
   <name>Ser08R2-01< /name >
   <uuid>9814b16e-d573-ace0-7bb3-b206fc66b8e1< /uuid >
   <memory unit= 'KiB' >2097152< /memory >
   <currentMemory unit= 'KiB' >2097152< /currentMemory >
   <vcpu placement= 'static' >2< /vcpu >
   <os>
     < type  arch= 'x86_64'  machine= 'rhel6.6.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/kvm/Ser08R2-01.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'  model= 'ich9-ehci1' >
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x04'  function = '0x7' />
     < /controller >
     <controller  type = 'usb'  index= '0'  model= 'ich9-uhci1' >
       <master startport= '0' />
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x04'  function = '0x0'  multifunction= 'on' />
     < /controller >
     <controller  type = 'usb'  index= '0'  model= 'ich9-uhci2' >
       <master startport= '2' />
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x04'  function = '0x1' />
     < /controller >
     <controller  type = 'usb'  index= '0'  model= 'ich9-uhci3' >
       <master startport= '4' />
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x04'  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:cd:11:41' />
       < 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= '0x05'  function = '0x0' />
     < /memballoon >
   < /devices >
< /domain >

9. virsh console 控制台管理linux虚拟机

# virsh console oeltest01 

10. 其它virsh命令

(1) 挂起服务器

# virsh suspend CentOS6.5-01 

ccac7a39f768c9338e3d77a8f639ee97.png-wh_

(2) 恢复服务器

# virsh  resume CentOS6.5-01 

7be2bef954ca6c5c4a5db6db704d0dd4.png-wh_

virsh命令丰富。可以执行各种维护任务,本文只是从维护与管理的角度例举了常用的命令,为该命令的使用提供一个思路。



本文转自 dengaosky 51CTO博客,原文链接:http://blog.51cto.com/dengaosky/1984011,如需转载请自行联系原作者

相关文章
|
1月前
|
存储 运维 API
云计算中的虚拟机管理
云计算中的虚拟机管理
31 0
|
4月前
|
Web App开发 Java Linux
Nexus【部署 02】最新版本 nexus-3.35.0-02-unix.tar.gz 安装配置启动及测试(JDK版本+虚拟机参数配置说明)
Nexus【部署 02】最新版本 nexus-3.35.0-02-unix.tar.gz 安装配置启动及测试(JDK版本+虚拟机参数配置说明)
116 0
|
1月前
|
PHP
百度虚拟机 bcloud_nginx_user.conf配置
百度虚拟机 bcloud_nginx_user.conf配置
22 0
|
1天前
|
Linux KVM 数据库
虚拟机数据恢复—误删除KVM虚拟机的数据恢复案例
虚拟化数据恢复环境&故障: KVM是Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux2.6.20版本之后集成在Linux的各个主要发行版本中。KVM使用Linux自身的调度器进行管理。 本案例中的服务器操作系统为Linux,文件系统为EXT4。操作系统上的部署的几台KVM虚拟机被删除,每台KVM虚拟机包含一个qcow2格式的磁盘文件和一个raw格式的磁盘文件,用户需要恢复的数据是raw格式的磁盘文件。这几台被误删除的虚拟机存放的是数据库,程序代码等数据。
|
1月前
|
XML 数据格式
virsh一个管理虚拟机的命令行工具
virsh是一个管理虚拟机的命令行工具,提供了丰富的命令来查看、创建、管理虚拟机。以下是一些常用的virsh命令: 1. **查看帮助和版本**: - `virsh --help`:查看virsh命令的帮助信息。 - `virsh -version`:查看virsh的版本信息。 2. **查看虚拟机**: - `virsh list`:显示当前正在运行的虚拟机。 - `virsh list --all`:显示主机下的所有虚拟机,包括未运行的。 3. **创建和管理虚拟机**: - `virsh define xml`:使用XML文件定义(创建)虚拟机。
|
3月前
|
Ubuntu Linux 开发工具
Linux(centOS7)虚拟机中配置 vim
Linux(centOS7)虚拟机中配置 vim
92 0
|
4月前
|
机器学习/深度学习 Ubuntu Linux
如何在Ubuntu 23.10部署KVM并创建虚拟机?
如何在Ubuntu 23.10部署KVM并创建虚拟机?
69 0
|
4月前
|
存储 Ubuntu Linux
虚拟机VMware Workstation Pro中配置Linux操作系统Ubuntu的方法
虚拟机VMware Workstation Pro中配置Linux操作系统Ubuntu的方法
|
存储 资源调度 数据中心