weewee 发表于 2015-7-13 10:25:23

kvm(三)客户机vm的克隆功能

一、开启客户端vm的console功能
二、虚拟机的克隆两种方式

1、开启客户端vm的console功能

2、
1.1、echo "ttyS0" >> /etc/securetty#添加ttyS0的许可,允许root登录
1.2、在/etc/grub.conf中加入如下内容console=ttyS0

1
2
3
4
5
6
7
8
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64)
      root (hd0,0)
      kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root nomodeset rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap crashkernel=auto LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_rootKEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0
      initrd /initramfs-2.6.32-431.el6.x86_64.img






1.3、在/etc/inittab中加入
S0:12345:respawn:/sbin/agetty ttyS0 115200

1
2
3
# cat /etc/inittab|grep -v ^#
id:3:initdefault:
S0:12345:respawn:/sbin/agetty ttyS0 115200




重启客户端;
reboot即可
1.4、验证:
在宿主机 virsh console vm2

二、虚拟机克隆
2.1、经过命令化克隆虚拟机
1>、首先关闭源vm

1
# virsh shutdown vm2




2>、命令化克隆vm

1
# virt-clone -o vm2 -n vm3 -f /kvm/vm3.img




-o 表示要克隆的源vm
-n 表示目标vm的名称
-f 表示目标vm所使用的disk

3>、启动目标vm 并 修改vm的配置
vm所要修改的大概有几点
1、修改hosts
2、修改主机名
3、修改网卡配置,ip、uuid、vnc端口等,不然会报错

4、6.0以上系统第一次开机最好要删除文件 (/etc/udev/rules.d/70-persistent-net.rules) 然后重启


2.2、经过copy配置文件和磁盘文件方式克隆虚拟机
1>、导出源vm的配置文件并编辑

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
# virsh dumpxml vm2 > /etc/libvirt/qemu/vm4.xml
# cat /etc/libvirt/qemu/vm4.xml
<domain type='kvm'>
<name>vm4</name>
<uuid>4e7de5d2-fab4-958f-1aba-2da41690fd07</uuid>
<memory unit='KiB'>524288</memory>
<currentMemory unit='KiB'>524288</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
    <type arch='x86_64' machine='rhel6.5.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='/kvm/vm4.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </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'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' 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:41:d6:44'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <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='5913' 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>
<seclabel type='none'/>
</domain>




2>、copy源vm的磁盘文件

1
2
3
4
5
# ll
总用量 3035208
-rwxr-xr-x 1 root root 7516192768 7月   9 17:34 vm2.img
-rwxr-xr-x 1 root root 7516192768 7月   9 18:01 vm3.img
# cp vm2.img vm4.img




3>、新定义目标vm

1
2
# virsh define /etc/libvirt/qemu/vm4.xml
定义域 vm4(从 /etc/libvirt/qemu/vm4.xml)





4>、启动vm并修改vm系统信息
vm所要修改的大概有几点
1、修改hosts
2、修改主机名
3、修改网卡配置,ip、uuid等
4、6.0以上系统第一次开机最好要删除文件 (/etc/udev/rules.d/70-persistent-net.rules) 然后重启





页: [1]
查看完整版本: kvm(三)客户机vm的克隆功能