KVM虚拟化之KVM虚拟机克隆

KVM虚拟化之KVM虚拟机克隆

作者:侃豺小哥 2020-06-18 16:39:10

云计算

虚拟化 kvm虚拟机的克隆分为两种情况,本文也就通过以下两种情况进行克隆,克隆虚拟机为OEL5.8X64。本文到此通过两种方式进行kvm虚拟机克隆,其实各有用处。

kvm虚拟机的克隆分为两种情况,本文也就通过以下两种情况进行克隆,克隆虚拟机为OEL5.8X64。

[[330647]]

(1) KVM主机本机虚拟机直接克隆。

(2) 通过复制配置文件与磁盘文件的虚拟机复制克隆(适用于异机的静态迁移)。

1. 本机虚拟机直接克隆

(1) 查看虚拟机配置文件

  1. [root@node1 ~]# cat /etc/libvirt/qemu/oeltest01.xml  
  2. <!-- 
  3. WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE 
  4. OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: 
  5. virsh edit oeltest01 
  6. or other application using the libvirt API. 
  7. --> 
  8. <domain type='kvm'
  9. <name>oeltest01</name
  10. <uuid>8f2bb4a7-c7ed-32aa-3676-9fb05923269d</uuid> 
  11. <memory unit='KiB'>524288</memory> 
  12. <currentMemory unit='KiB'>524288</currentMemory> 
  13. <vcpu placement='static'>1</vcpu> 
  14. <os> 
  15. <type arch='x86_64' machine='rhel6.4.0'>hvm</type> 
  16. <boot dev='hd'/> 
  17. </os> 
  18. <features> 
  19. <acpi/> 
  20. <apic/> 
  21. <pae/> 
  22. </features> 
  23. <clock offset='utc'/> 
  24. <on_poweroff>destroy</on_poweroff> 
  25. <on_reboot>restart</on_reboot> 
  26. <on_crash>restart</on_crash> 
  27. <devices> 
  28. <emulator>/usr/libexec/qemu-kvm</emulator> 
  29. <disk type='file' device='disk'
  30. <driver name='qemu' type='raw' cache='none'/> 
  31. <source file='/data/test01.img'/> 
  32. <target dev='hda' bus='ide'/> 
  33. <address type='drive' controller='0' bus='0' target='0' unit='0'/> 
  34. </disk> 
  35. <disk type='block' device='cdrom'
  36. <driver name='qemu' type='raw'/> 
  37. <target dev='hdc' bus='ide'/> 
  38. <readonly/> 
  39. <address type='drive' controller='0' bus='1' target='0' unit='0'/> 
  40. </disk> 
  41. <controller type='usb' index='0'
  42. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> 
  43. </controller> 
  44. <controller type='ide' index='0'
  45. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> 
  46. </controller> 
  47. <interface type='bridge'
  48. <mac address='52:54:00:82:39:01'/> 
  49. <source bridge='br0'/> 
  50. <model type='virtio'/> 
  51. <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> 
  52. </interface> 
  53. <serial type='pty'
  54. <target port='0'/> 
  55. </serial> 
  56. <console type='pty'
  57. <target type='serial' port='0'/> 
  58. </console> 
  59. <input type='tablet' bus='usb'/> 
  60. <input type='mouse' bus='ps2'/> 
  61. <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'
  62. <listen type='address' address='0.0.0.0'/> 
  63. </graphics> 
  64. <video> 
  65. <model type='cirrus' vram='9216' heads='1'/> 
  66. <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> 
  67. </video> 
  68. <memballoon model='virtio'
  69. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> 
  70. </memballoon> 
  71. </devices> 
  72. </domain> 

虚拟机磁盘文件: /data/test01.img

虚拟机名称:oeltest01

 

(2) 开始克隆

  1. # virt-clone -o oeltest01 -n oeltest02 -f /data/test02.img 

说明:以oeltest01做为源,克隆oeltest01虚拟机,并创建名称为oeltest02虚拟机,使用磁盘文件/data/test02.img

(3) 启动虚拟机并配置主机名,IP地址等

 

[root@node1 data]# virsh console oeltest02

  1. 连接到域 oeltest02 
  2. Escape character is ^] 
  3. Found volume group "vg" using metadata type lvm2 
  4. 2 logical volume(s) in volume group "vg" now active 
  5. Welcome to Oracle Linux Oracle Linux Server release 5.8 
  6. Press 'I' to enter interactive startup. 
  7. Starting udev: [ OK ] 
  8. Loading default keymap (us): [ OK ] 
  9. Setting hostname test01: [ OK ] 
  10. Setting up Logical Volume Management: 2 logical volume(s) in volume group "vg" now active 
  11. [ OK ] 
  12. Checking filesystems 
  13. Checking all file systems. 
  14. [/sbin/fsck.ext3 (1) -- /] fsck.ext3 -a /dev/vg/root 
  15. /dev/vg/root: clean, 135384/1540096 files, 851398/1540096 blocks 
  16. [/sbin/fsck.ext3 (1) -- /boot] fsck.ext3 -a /dev/hda1 
  17. /boot: clean, 39/26104 files, 26897/104388 blocks 
  18. [ OK ] 
  19. Remounting root filesystem in read-write mode: [ OK ] 
  20. Mounting local filesystems: [ OK ] 
  21. Enabling local filesystem quotas: [ OK ] 
  22. Enabling /etc/fstab swaps: [ OK ] 
  23. INIT: Entering runlevel: 5 
  24. Entering non-interactive startup 
  25. Applying Intel CPU microcode update: [ OK ] 
  26. Starting background readahead: [ OK ] 
  27. Checking for hardware changes [ OK ] 
  28. [ OK ] iSCSI daemon: [ OK ] 
  29. [ OK ] 
  30. Bringing up loopback interface: [ OK ] 
  31. Bringing up interface eth0: 
  32. Determining IP information for eth0... done. 
  33. [ OK ] 
  34. Starting auditd: [ OK ] 
  35. Starting system logger: [ OK ] 
  36. Starting kernel logger: [ OK ] 
  37. Starting irqbalance: [ OK ] 
  38. iscsid (pid 1555) is running... 
  39. Setting up iSCSI targets: iscsiadm: No records found 
  40. [ OK ] 
  41. Starting portmap: [ OK ] 
  42. Starting NFS statd: [ OK ] 
  43. Starting RPC idmapd: [ OK ] 
  44. Starting system message bus: [ OK ] 
  45. Starting o2cb: [ OK ] 
  46. [ OK ] Bluetooth services:[ OK ] 
  47. Mounting other filesystems: [ OK ] 
  48. Starting PC/SC smart card daemon (pcscd): [ OK ] 
  49. Starting acpi daemon: [ OK ] 
  50. Starting HAL daemon: [ OK ] 
  51. Starting hidd: [ OK ] 
  52. Starting monitoring for VG vg: 2 logical volume(s) in volume group "vg" monitored 
  53. [ OK ] 
  54. Starting autofs: Loading autofs4: [ OK ] 
  55. Starting automount: [ OK ] 
  56. [ OK ] 
  57. Starting hpiod: [ OK ] 
  58. Starting hpssd: [ OK ] 
  59. Starting sshd: [ OK ] 
  60. Starting cups: [ OK ] 
  61. Starting xinetd: [ OK ] 
  62. Starting console mouse services: [ OK ] 
  63. Starting crond: [ OK ] 
  64. Starting xfs: [ OK ] 
  65. Starting anacron: [ OK ] 
  66. [ OK ] atd: [ OK ] 
  67. Starting background readahead: [ OK ] 
  68. Starting yum-updatesd: [ OK ] 
  69. Starting Avahi daemon... [ OK ] 
  70. Starting smartd: hdc: drive_cmd: status=0x41 { DriveReady Error } 
  71. hdc: drive_cmd: error=0x04 { AbortedCommand } 
  72. ide: failed opcode was: 0xec 
  73. [ OK ] 
  74. Oracle Linux Server release 5.8 
  75. Kernel 2.6.18-308.el5 on an x86_64 
  76. test01 login: mtrr: type mismatch for f0000000,100000 old: uncachable new: write-combining 
  77. mtrr: type mismatch for f0000000,400000 old: uncachable new: write-combining 
  78. Oracle Linux Server release 5.8 
  79. Kernel 2.6.18-308.el5 on an x86_64 
  80. test01 login: 
  81. Oracle Linux Server release 5.8 
  82. Kernel 2.6.18-308.el5 on an x86_64 
  83. test01 login: root 
  84. Password
  85. Last login: Sat Sep 7 05:21:11 on ttyS0 
  86. [root@test01 ~]# 
  87. [root@test01 ~]# 
  88. [root@test01 ~]# 
  89.   
  90.   
  91. (4) 修改主机名,IP地址等 
  92. # vi /etc/hosts 
  93. # Do not remove the following line, or various programs  
  94. # that require network functionality will fail.  
  95. 127.0.0.1 test02 localhost.localdomain localhost  
  96. ::1 localhost6.localdomain6 localhost6 
  97. # vi /etc/sysconfig/network 
  98. NETWORKING=yes  
  99. NETWORKING_IPV6=no  
  100. HOSTNAME=test02  
  101. GATEWAY=192.168.233.2 
  102. 修改IP地址 
  103. # vi /etc/sysconfig/network-script/ifcfg-eth0 
  104. # Virtio Network Device  
  105. DEVICE=eth0  
  106. BOOTPROTO=static  
  107. ONBOOT=yes  
  108. HWADDR=52:54:00:56:bd:2b  
  109. IPADDR=192.168.233.142  
  110. NETMASK=255.255.255.0 
  111. [root@test01 ~]# service network restart 
  112. Shutting down interface eth0: [ OK ]  
  113. Shutting down loopback interface: [ OK ]  
  114. Bringing up loopback interface: [ OK ]  
  115. Bringing up interface eth0: [ OK ]  
  116. [root@test01 ~]#  

2. 复制配置文件与磁盘文件克隆

(1) 导入kvm虚拟机配置文件

这里采用oeltest01做为模板,进行克隆。

  1. # virsh shutdown oeltest01 

  1. # virsh dumpxml oeltest01 > /etc/libvirt/qemu/oeltest03.xml 

(2) 复制kvm虚拟机磁盘文件

本系列文章都是采用虚拟磁盘文件进行测试,没有使用lvm卷。

通过查看配置文件:虚拟磁盘位置

(3) 直接编辑修改配置文件

修改name,uuid,disk位置,vnc端口

此时还是将该配置文件注册进来,无法通过virsh edit进行编辑。

  1. [root@node1 data]# vi /etc/libvirt/qemu/oeltest03.xml  
  2. <domain type='kvm'>  
  3.  <name>oeltest03</name>  
  4.  <uuid>8f2bb4a7-c7ed-32aa-3676-9fb05923260d</uuid> 
  5.  <memory unit='KiB'>524288</memory>  
  6.  <currentMemory unit='KiB'>524288</currentMemory>  
  7.  <vcpu placement='static'>1</vcpu>  
  8.  <os>  
  9.  <type arch='x86_64' machine='rhel6.4.0'>hvm</type>  
  10.  <boot dev='hd'/>  
  11.  </os>  
  12.  <features>  
  13.  <acpi/>  
  14.  <apic/>  
  15.  <pae/>  
  16.  </features>  
  17.  <clock offset='utc'/>  
  18.  <on_poweroff>destroy</on_poweroff>  
  19.  <on_reboot>restart</on_reboot>  
  20.  <on_crash>restart</on_crash>  
  21.  <devices>  
  22.  <emulator>/usr/libexec/qemu-kvm</emulator>  
  23.  <disk type='file' device='disk'>  
  24.  <driver name='qemu' type='raw' cache='none'/>  
  25.  <source file='/data/test03.img'/> 
  26.  <target dev='hda' bus='ide'/>  
  27.  <address type='drive' controller='0' bus='0' target='0' unit='0'/>  
  28.  </disk>  
  29.  <disk type='block' device='cdrom'>  
  30.  <driver name='qemu' type='raw'/>  
  31.  <target dev='hdc' bus='ide'/>  
  32.  <readonly/>  
  33.  <address type='drive' controller='0' bus='1' target='0' unit='0'/>  
  34.  </disk>  
  35.  <controller type='usb' index='0'>  
  36.  <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>  
  37.  </controller>  
  38.  <controller type='ide' index='0'>  
  39.  <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>  
  40.  </controller>  
  41.  <interface type='bridge'>  
  42.  <mac address='52:54:00:82:39:01'/>  
  43.  <source bridge='br0'/>  
  44.  <model type='virtio'/>  
  45.  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>  
  46.  </interface>  
  47.  <serial type='pty'>  
  48.  <target port='0'/>  
  49.  </serial>  
  50.  <console type='pty'>  
  51.  <target type='serial' port='0'/>  
  52.  </console>  
  53.  <input type='tablet' bus='usb'/>  
  54.  <input type='mouse' bus='ps2'/>  
  55.  <graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0'
  56.  <listen type='address' address='0.0.0.0'/>  
  57.  </graphics>  
  58.  <video>  
  59.  <model type='cirrus' vram='9216' heads='1'/>  
  60.  <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>  
  61.  </video>  
  62.  <memballoon model='virtio'>  
  63.  <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>  
  64.  </memballoon>  
  65.  </devices>  
  66. </domain> 

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

  1. [root@node1 data]# virsh define /etc/libvirt/qemu/oeltest03.xml  

启动虚拟机

  1. [root@node1 data]# virsh start oeltest03  
  2. 域 oeltest03 已开始 

(5) 登录虚拟机进行主机名,IP等修改

  1. [root@node1 data]# virsh console oeltest03  
  2. 连接到域 oeltest03  
  3. Escape character is ^]  
  4. Oracle Linux Server release 5.8  
  5. Kernel 2.6.18-308.el5 on an x86_64  
  6. test01 login: mtrr: type mismatch for f0000000,100000 old: uncachable new: write-combining  
  7. mtrr: type mismatch for f0000000,400000 old: uncachable new: write-combining  
  8. Oracle Linux Server release 5.8  
  9. Kernel 2.6.18-308.el5 on an x86_64  
  10. test01 login: root  
  11. Password:  
  12. Last login: Sat Sep 7 05:21:11 on ttyS0  
  13. [root@test01 ~]#  
  14. [root@test01 ~]#  
  15. 修改主机名 
  16. # vi /etc/hosts 
  17. # Do not remove the following line, or various programs  
  18. # that require network functionality will fail.  
  19. 127.0.0.1 test03 localhost.localdomain localhost  
  20. ::1 localhost6.localdomain6 localhost6 
  21. # vi /etc/sysconfig/network 
  22. NETWORKING=yes  
  23. NETWORKING_IPV6=no  
  24. HOSTNAME=test03  
  25. GATEWAY=192.168.233.2 
  26. 修改IP地址 
  27. # vi /etc/sysconfig/network-script/ifcfg-eth0 
  28. # Virtio Network Device  
  29. DEVICE=eth0  
  30. BOOTPROTO=static  
  31. ONBOOT=yes  
  32. HWADDR=52:54:00:56:bd:3b  
  33. IPADDR=192.168.233.143  
  34. NETMASK=255.255.255.0 
  35. service network restart 
  36. Shutting down interface eth0: [ OK ]  
  37. Shutting down loopback interface: [ OK ]  
  38. Bringing up loopback interface: [ OK ]  
  39. Bringing up interface eth0: [ OK ]  

本文到此通过两种方式进行kvm虚拟机克隆,其实各有用处。

再次说明:本文克隆的虚拟机是oel5.8,如果克隆的虚拟机是RHEL/CentOS/OEL6.x操作系统,还需要修改

/etc/udev/rules.d/70-persistent-net.rules文件。

 

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/271840.html<

(0)
管理的头像管理
上一篇2025-05-08 11:29
下一篇 2025-05-08 11:31

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注