Linux中如何克隆KVM虚拟机

 

背景

当需要批量部署虚拟机时,通常有几种做法,使用模板重新安装。

或者使用已有的虚拟机克隆。

使用模板安装可以参考kickstart脚本的编写方法。

《install kvm hosts use kickstart in CentOS 6 in text mode》

本文介绍一下在已经安装好的虚拟机上,克隆虚拟机的方法。

从0开始安装一个虚拟机

在服务器安装必要的包, 不再需要图形相关的包.

# yum install -y qemu-img qemu-kvm virt-manager libvirt libvirt-python python-virtinst libvirt-client libvirt libvirt-client virt-what  

创建一个虚拟磁盘目录

# mkdir /data03/kvmdisk  

创建虚拟磁盘, 用于虚拟机的系统盘

# qemu-img create -f qcow2 -o encryption=off,cluster_size=2M,preallocation=full /data03/kvmdisk/disk01.img 32G  

or 

# qemu-img create -f raw /data03/kvmdisk/disk01.img 32G  

下载安装镜像

# mkdir /data03/iso  
# cd iso  
# wget http://mirrors.aliyun.com/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso  

配置

vi /etc/libvirt/libvirtd.conf

listen_tls = 0

启动libvirtd

# service libvirtd start  
# /etc/init.d/messagebus start  
# /etc/init.d/avahi-daemon start   
# /etc/init.d/libvirtd start  

# chkconfig libvirtd on  
# chkconfig libvirt-guests off  
# chkconfig avahi-daemon on  
# chkconfig messagebus on 

查看当前启动的网桥

# brctl show  
bridge name     bridge id               STP enabled     interfaces  
virbr0          8000.5254001263b0       yes             virbr0-nic  

# ifconfig  
em1       Link encap:Ethernet  HWaddr 00:22:19:60:77:8F    
          inet addr:172.16.3.150  Bcast:172.16.3.255  Mask:255.255.255.0  
          inet6 addr: fe80::222:19ff:fe60:778f/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:5469716 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:2830916 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000   
          RX bytes:5147311077 (4.7 GiB)  TX bytes:198552462 (189.3 MiB)  

lo        Link encap:Local Loopback    
          inet addr:127.0.0.1  Mask:255.0.0.0  
          inet6 addr: ::1/128 Scope:Host  
          UP LOOPBACK RUNNING  MTU:65536  Metric:1  
          RX packets:79073 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:79073 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:24506711 (23.3 MiB)  TX bytes:24506711 (23.3 MiB)  

virbr0    Link encap:Ethernet  HWaddr 52:54:00:12:63:B0    
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  

或者你也可以手工添加网桥

brctl addbr br0  

ip link set br0 up  

ip addr add 192.168.122.1/24 dev br0 

查看虚拟机用到的virbr0网桥地址配置

# grep -r 192.168.122 /etc/libvirt  
/etc/libvirt/qemu/networks/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  
/etc/libvirt/qemu/networks/autostart/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/autostart/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  

text 交互式安装虚拟机操作系统

在没有图形环境时, 可以使用text模式安装操作系统.

virt-install    --name=centos6_6_x64    --disk path=/data03/kvmdisk/disk01.img,device=disk,bus=virtio,perms=rw,cache=writethrough    --graphics none    --vcpus=4 --ram=4096    --location=/data03/iso/CentOS-6.6-x86_64-bin-DVD1.iso    --network bridge=virbr0    --os-type=linux    --os-variant=rhel6    --extra-args="console=tty0 console=ttyS0,115200n8"

安装完后,连接到虚拟机的console的方法

# virsh  
> console $domainID  
> 退出console 按下 ctrl+]  

例如 :

[root@db-172-16-3-150 ~]# virsh  
Welcome to virsh, the virtualization interactive terminal.  

Type:  ‘help‘ for help with commands  
       ‘quit‘ to quit  

virsh # list  
 Id    Name                           State  
----------------------------------------------------  
 2     centos6_6_x64                  running  

virsh # console 2            # 使用Id或者Name都可以连接
Connected to domain centos6_6_x64  
Escape character is ^]  

CentOS release 6.6 (Final)  
Kernel 2.6.32-504.el6.x86_64 on an x86_64  

digoal.sky-mobi.com login: root  
Password:   
Last login: Thu Apr  2 00:12:27 on ttyS0  
[root@digoal ~]#   
[root@digoal ~]#   
[root@digoal ~]# exit  
logout  

CentOS release 6.6 (Final)  
Kernel 2.6.32-504.el6.x86_64 on an x86_64  

digoal.sky-mobi.com login:           # 这里按下ctrl+]返回本地控制台  
virsh #   
virsh #   

设置开机自动启动虚拟机

# vi /etc/rc.local  
/usr/bin/virsh start centos6_6_x64  

优化虚拟机配置

1. 主要是删掉一些不必要的控制器(如USB), 然后添加CPU模块, 使用本地CPU的flag.

#virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  ‘help‘ for help with commands
       ‘quit‘ to quit

virsh # 
virsh # list
 Id Name                 State
----------------------------------
  6 kvm101               running
  8 kvm103               running
  9 kvm104               running
 10 kvm105               running
 11 kvm106               running
 12 kvm102               running

优化例子

virsh # edit kvm101
<domain type=‘kvm‘>
  <name>kvm101</name>
  <uuid>366072c0-2ee0-027a-e887-e60d50bad5a7</uuid>
  <memory>83886080</memory>
  <currentMemory>83886080</currentMemory>
  <vcpu>10</vcpu>
  <cpu mode=‘host-passthrough‘>    # 注意这里可能要指定CPU,而不是host-passthrough,否则可能保存会失败,或者自动抹除。 /usr/libexec/qemu-kvm -cpu ? 可以得到支持的CPU
    <model fallback=‘allow‘/>    
  </cpu>
--------- 例如 /usr/share/libvirt/cpu_map.xml
  <cpu match=‘exact‘>
     <model fallback=‘forbid‘>Nehalem</model>
     <vendor>Intel</vendor>
     <feature policy=‘require‘ name=‘fma‘/>    #  这里可以指定CPU flag
     <feature policy=‘require‘ name=‘pse‘/>    #  这里可以指定CPU flag
  </cpu>
---------
  <os>
    <type arch=‘x86_64‘ machine=‘rhel6.2.0‘>hvm</type>
    <boot dev=‘hd‘/>
  </os>
  <clock offset=‘utc‘/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</
上一篇:输入法编辑器(IME)程序设计(3)


下一篇:lr_continue_on_error函数