KVM虚拟化笔记(四)------kvm虚拟机日常管理

简介:

一,查看KVM虚拟机配置文件及运行状态

 1,KVM虚拟机默认的配置文件路径:/etc/libvirt/qemu/

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

1
2
3
4
5
6
7
8
9
[root@KVM qemu] # cd /etc/libvirt/qemu
[root@KVM qemu] # ll
total 24
drwxr-xr-x 2 root root 4096 Feb  2 15:03 autostart       ###开机自启动目录  
-rw------- 1 root root 2998 Feb  2 13:54 hadoop1.xml
-rw------- 1 root root 2988 Feb  2 11:39 hadoop2.xml
-rw------- 1 root root 2965 Feb  2 14:03 hadoop4.xml
drwx------ 3 root root 4096 Jan 26 16:47 networks
-rw------- 1 root root 3036 Feb  2 15:52 win7.xml

 2,virsh命令帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM autostart] # virsh --help
virsh [options]... [<command_string>]
virsh [options]... < command > [args...]
   options:
     -c | --connect=URI      hypervisor connection URI
     -r | -- readonly          connect  readonly
     -d | --debug=NUM        debug level [0-4]
     -h | --help             this help
     -q | --quiet            quiet mode
     -t | --timing           print timing information
     -l | --log=FILE         output logging to  file
     - v                       short version
     -V                      long version
          --version[=TYPE]   version, TYPE is short or long (default short)
     -e | --escape <char>     set  escape sequence  for  console

或者直接输入virsh,进入虚拟化交互式终端,输入exit或quit进行退出

1
2
3
4
5
[root@KVM autostart] # virsh 
Welcome to virsh, the virtualization interactive terminal.
Type:   'help'  for  help with commands
        'quit'  to quit
virsh  #


 3,查看kvm虚拟机状态

1
2
3
4
5
6
7
8
[root@KVM autostart] # virsh list --all
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  23    hadoop4                        running
  -     hadoop1                        shut off
  -     hadoop2                        shut off
  -     oeltest01                      shut off

 4,虚拟机的开机:

1
2
3
4
5
6
7
[root@KVM autostart] # virsh start hadoop1
Domain hadoop1 started
[root@KVM autostart] # virsh list --all   
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  24    hadoop1                        running

 5,虚拟机的关机或断电

 (1)默认情况下virsh工具是不能对Linux虚拟机进行关机操作的,需要开启acpid服务。在安装kvm Linux虚拟机必须配置此服务。

1
2
[root@localhost ~] #chkconfig acpid on
[root@localhost ~] #service acpid restart

然后使用virsh进行关机

1
2
[root@KVM autostart] # virsh shutdown hadoop1
Domain hadoop1 is being  shutdown

 (2)强制关闭电源(就是直接断电,重要的业务不要这么操作)

1
2
[root@KVM autostart] # virsh destroy hadoop4
Domain hadoop4 destroyed

 

 6,通过配置文件启动虚拟机

1
2
[root@KVM qemu] # virsh create /etc/libvirt/qemu/hadoop4.xml  
Domain hadoop4 created from  /etc/libvirt/qemu/hadoop4 .xml

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

1
2
3
4
5
6
[root@KVM autostart] # virsh autostart hadoop4
Domain hadoop4 marked as autostarted
[root@KVM autostart] # ll
total 0
lrwxrwxrwx 1 root root 29 Feb  2 17:09 hadoop4.xml ->  /etc/libvirt/qemu/hadoop4 .xml
lrwxrwxrwx 1 root root 26 Feb  2 15:03 win7.xml ->  /etc/libvirt/qemu/win7 .xml

 8,导出kvm虚拟机配置文件,可以通过这种方式进行配置文件的备份。

1
2
3
4
5
[root@KVM qemu] # virsh dumpxml hadoop4 > /etc/libvirt/qemu/hadoop4.bak.xml    
[root@KVM qemu] # ll
total 32
drwxr-xr-x 2 root root 4096 Feb  2 17:09 autostart
-rw-r--r-- 1 root root    1 Feb  2 17:13 hadoop1.bak.xml

 9,删除虚拟机,由于kvm虚拟机是由配置文件和磁盘文件组成,如果删除的话,把两个文件都要删除

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@KVM qemu] # virsh undefine hadoop4       ###删除hadoop.xml文件,但没有删除磁盘文件
Domain hadoop4 has been undefined
[root@KVM qemu] # ll
total 28
drwxr-xr-x 2 root root 4096 Feb  2 17:18 autostart
-rw-r--r-- 1 root root    1 Feb  2 17:13 hadoop1.bak.xml
-rw------- 1 root root 2998 Feb  2 13:54 hadoop1.xml
-rw------- 1 root root 2988 Feb  2 11:39 hadoop2.xml
-rw-r--r-- 1 root root 3205 Feb  2 17:13 hadoop4.bak.xml
drwx------ 3 root root 4096 Jan 26 16:47 networks
-rw------- 1 root root 3036 Feb  2 15:52 win7.xml
[root@KVM qemu] # rm -rf /images/test/hadoop4.img     ###删除磁盘文件
[root@KVM qemu] #

wKiom1awdUiAXz3vAABB6021GzY021.png

10,重新定义虚拟机配置文件。可通过刚才导出的配置文件来恢复虚拟机,并重新定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@KVM qemu] # mv hadoop4.bak.xml hadoop4.xml        ###恢复配置文件  
[root@KVM qemu] # ll
total 28
drwxr-xr-x 2 root root 4096 Feb  2 17:18 autostart
-rw-r--r-- 1 root root    1 Feb  2 17:13 hadoop1.bak.xml
-rw------- 1 root root 2998 Feb  2 13:54 hadoop1.xml
-rw------- 1 root root 2988 Feb  2 11:39 hadoop2.xml
-rw-r--r-- 1 root root 3205 Feb  2 17:13 hadoop4.xml
drwx------ 3 root root 4096 Jan 26 16:47 networks
-rw------- 1 root root 3036 Feb  2 15:52 win7.xml
[root@KVM qemu] # virsh define /etc/libvirt/qemu/hadoop4.xml  ###重新定义配置文件
Domain hadoop4 defined from  /etc/libvirt/qemu/hadoop4 .xml
[root@KVM qemu] # virsh list --all
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  24    hadoop1                        running
  25    hadoop2                        running
  -     hadoop4                        shut off          ###定义生效
  -     oeltest01                      shut off

11,编辑kvm虚拟机配置文件可以使用virsh edit 虚拟机名。不过也可以直接通过vi /etc/libvirt/qemu/hadoop4.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
[root@KVM qemu] # virsh edit hadoop4
<domain  type = 'qemu' >
   <name>hadoop4< /name >
   <uuid>586b3cae-943f-d283-d8e7-ed62b01bfa34< /uuid >
   <memory unit= 'KiB' >1048576< /memory >
   <currentMemory unit= 'KiB' >1048576< /currentMemory >
   <vcpu placement= 'static' >1< /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 = '/images/test/hadoop4.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 = 'network' >
       <mac address= '52:54:00:fe:f5:a3' />
       < source  network= 'default' />
       <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= '5914'  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 >

使用vi也可以进行编辑修改该配置文件

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
[root@KVM qemu] # vi /etc/libvirt/qemu/hadoop4.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 hadoop4
or other application using the libvirt API.
-->
<domain  type = 'qemu' >
   <name>hadoop4< /name >
   <uuid>586b3cae-943f-d283-d8e7-ed62b01bfa34< /uuid >
   <memory unit= 'KiB' >1048576< /memory >
   <currentMemory unit= 'KiB' >1048576< /currentMemory >
   <vcpu placement= 'static' >1< /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 = '/images/test/hadoop4.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 = 'network' >
       <mac address= '52:54:00:fe:f5:a3' />
       < source  network= 'default' />
       <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= '5914'  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 >

12,其他virsh命令:

  (1)挂起服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@KVM qemu] # virsh list --all
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  24    hadoop1                        running
  25    hadoop2                        running
  -     hadoop4                        shut off
  -     oeltest01                      shut off
[root@KVM qemu] # virsh suspend hadoop1
Domain hadoop1 suspended
[root@KVM qemu] # virsh list --all     
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  24    hadoop1                        paused
  25    hadoop2                        running
  -     hadoop4                        shut off
  -     oeltest01                      shut off

  (2)恢复服务器

1
2
3
4
5
6
7
8
9
10
[root@KVM qemu] # virsh resume hadoop1
Domain hadoop1 resumed
[root@KVM qemu] # virsh list --all    
  Id    Name                           State
----------------------------------------------------
  21    win7                           running
  24    hadoop1                        running
  25    hadoop2                        running
  -     hadoop4                        shut off
  -     oeltest01                      shut off

如果想知道更多的命令,可以通过virsh --help进行查询。



本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1740740


相关文章
|
存储 运维 数据挖掘
虚拟化数据恢复—误还原快照导致虚拟机上数据库丢失的数据恢复案例
虚拟化数据恢复环境&故障: vmfs文件系统,存储的数据是SqlServer数据库及其他办公文件。 工作人员误将快照还原,导致了SqlServer数据库数据的丢失,需要恢复原来的SqlServer数据库文件。
325 22
|
编解码 Linux 虚拟化
在Hyper-V虚拟化的图形处理中,怎么调整虚拟机的屏幕分辨率?
在Hyper-V虚拟化中,调整虚拟机屏幕分辨率对提升用户体验和确保应用程序兼容性至关重要。高分辨率可提供清晰图像、提高工作效率,并避免显示异常。调整方法包括通过增强会话模式、虚拟机内部设置或手动编辑配置文件。注意事项包括正确安装显卡驱动、避免过高分辨率及及时保存设置。
|
存储 SQL 数据库
虚拟化数据恢复—Vmware虚拟机误还原快照的数据恢复案例
虚拟化数据恢复环境: 一台虚拟机从物理机迁移到ESXI虚拟化平台,迁移完成后做了一个快照。虚拟机上运行了一个SQL Server数据库,记录了数年的数据。 ESXI虚拟化平台上有数十台虚拟机,EXSI虚拟化平台连接了一台EVA存储,所有的虚拟机都存放在EVA存储上。 虚拟化故障: 工组人员误操作将数年前迁移完成后做的快照还原了,也就意味着虚拟机状态还原到数年前,近几年数据都被删除了。 还原快照相当于删除数据,意味着部分存储空间会被释放。为了不让这部分释放的空间被重用,需要将连接到这台存储的所有虚拟机都关掉,需要将不能长时间宕机的虚拟机迁移到别的EXSI虚拟化平台上。
463 50
|
安全 Linux KVM
Linux虚拟化技术:从Xen到KVM
Xen和KVM是Linux平台上两种主要的虚拟化技术,各有优缺点和适用场景。通过对比两者的架构、性能、安全性、管理复杂性和硬件依赖性,可以更好地理解它们的适用场景和选择依据。无论是高性能计算、企业虚拟化还是云计算平台,合理选择和配置虚拟化技术是实现高效、稳定和安全IT环境的关键。
921 8
|
存储 数据挖掘 数据库
虚拟化数据恢复—VMFS简介&误删除虚拟机的数据恢复案例
物理区:物理上连续的磁盘空间,即通常意义上的分区。 本地区:VMFS管理的物理区分为保留区和本地区,前面一部分是保留区,后面部分是本地区。本地区又分为元文件区和数据区。 元文件:与NTFS的元文件类似,属于FS的管理用数据。VMFS有6个元文件:.VH.SF/.FBB.SF/.FDC.SF/.SBC.SF/.PBC.SF/.PB2.SF。 元文件区:6个元文件占用的所有空间,在本地区的前面部分。 数据区:用于存放文件数据。 datastore:在ESX服务器上看到的VMFS存储空间。 LV:logical volume,所指的范围其实和本地区一样,即虚拟化卷。 LVM逻辑卷组:用来管理跨dis
|
存储 持续交付 虚拟化
|
存储 运维 虚拟化
虚拟化数据恢复——Hyper-V虚拟化故障导致虚拟机文件丢失的数据恢复案例
在Windows Server上部署的Hyper-V虚拟化环境中,因存储中虚拟机数据文件丢失导致服务瘫痪。北亚企安数据恢复工程师通过物理检测、操作系统及文件系统检测,确定为人为格式化造成,并通过镜像硬盘、重组RAID、分析并恢复文件索引项等步骤,成功恢复数据,最终在新Hyper-V环境中验证并迁移所有虚拟机,确保用户业务恢复正常运行。
|
存储 Linux 调度
KVM 虚拟化的功能特性
【10月更文挑战第13天】虚拟化技术创建实体资源的虚拟版本,提升资源利用率。KVM(Kernel-based Virtual Machine)作为全虚拟化解决方案,借助Linux内核实现Hypervisor功能,通过模块化方式提供高效的虚拟化环境。
|
7月前
|
机器学习/深度学习 监控 安全
解密虚拟化弹性内存:五大核心技术与实施策略
本文深入解析虚拟化环境中实现内存弹性管理的五大核心技术与实施策略。内容涵盖内存架构演进、关键技术原理、性能优化方法及典型问题解决方案,助力提升虚拟机密度与资源利用率。
330 0
|
存储 安全 虚拟化
虚拟化技术:实现资源高效利用和灵活管理的利器
虚拟化技术作为实现资源高效利用和灵活管理的重要手段,在数字化时代背景下,正逐步改变传统IT架构模式。本文概述了虚拟化技术的概念、原理及其在数据中心管理、云计算平台、企业信息化建设、科研教育及医疗行业的应用,并探讨了其面临的挑战与未来发展趋势。
737 3