Linux中如何克隆KVM虛擬機


轉載:https://yq.aliyun.com/articles/64860

作者

digoal

日期

2016-11-11

標簽

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</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='raw' cache='writeback'/> <source file='/u01/kvmdisk/disk01.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <interface type='bridge'> <mac address='52:54:00:3e:78:0d'/> <source bridge='virbr0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <console type='pty'> <target type='serial' port='0'/> </console> </devices> </domain> "/tmp/virshKtG0oG.xml" 36L, 1133C written Domain kvm101 XML configuration edited. 

重啟虛擬機后生效。

克隆虛擬機

http://www.cnblogs.com/5201351/p/4461000.html

kvm虛擬機的克隆分為兩種情況,第一種kvm宿主機上對虛擬機直接克隆

第二種通過復制配置文件與磁盤文件的虛擬機復制克隆(適用於異機的靜態遷移)。

現筆者將分別兩種kvm虛擬機克隆的的詳細操作過程都記錄如下:

方法一: kvm宿主機上對虛擬機直接克隆(需要在關機或暫停的狀態下操作)

1、查看所有的虛擬機、以及需要克隆的虛擬機的硬盤文件的位置。

[root@5201351_kvm ~]# virsh list --all //查看已安裝的所有的kvm虛擬機 

2、我們可以通過編輯需要克隆的源虛擬機配置文件,去發現它的磁盤文件位置,命令如下:

[root@5201351_kvm ~]# virsh edit kvm_client00 //通過編輯虛擬機的配置文件,查看其硬盤文件的位置 

如通過如下的內容,可以看出磁盤文件的位置

<source file='/var/lib/libvirt/images/kvm_client00.img'/> 

3、開始克隆,將kvm_client00虛擬機克隆成新的虛擬機kvm_client01,新的虛擬機的磁盤文件為/var/lib/libvirt/images/kvm_client01.img

[root@5201351_kvm ~]# virt-clone -o kvm_client00 -n kvm_client01 -f /var/lib/libvirt/images/kvm_client01.img 

4、這時克隆就完了、我們可以通過virsh list --all進行查看,如果需要刪除克隆的虛擬機,我們只需要執行如下命令即可。

[root@5201351_kvm ~]# virsh undefine kvm_client01 //該命令只是刪除wintest01的配置文件,並不刪除虛擬磁盤文件 

方法二:復制配置文件與磁盤文件進行克隆(可以不用關閉源虛擬機)

1、這里我們還是克隆kvm_client00,我們通過如下命令創建新虛擬機的配置文件

[root@5201351_kvm ~]# virsh dumpxml kvm_client00 > /etc/libvirt/qemu/kvm_client02.xml //創建新虛擬機的配置文件 

2、復制原虛擬機的磁盤文件,通過方法一、我們知道,磁盤默認位置為/var/lib/libvirt/images,我們執行如下命令進行復制

[root@5201351_kvm ~]# cd /var/lib/libvirt/images [root@5201351_kvm images]# cp kvm_client00.img kvm_client02.img 

3、直接編輯修改配置文件kvm_client02.xml,修改name,uuid,disk文件位置,mac地址,vnc端口

4、通過新虛擬機的配置文件,定義新的虛擬機,只需要執行如下一條命令即可。

[root@5201351_kvm ~]# virsh define /etc/libvirt/qemu/kvm_client02.xml //通過配置文件定義新的kvm虛擬機 

需要特別說明的是、以上兩種方法克隆的虛擬機、我們都需要進入克隆的新虛擬機里

修改網卡設備文件/etc/udev/rules.d/70-persistent-net.rules,或者直接將其刪除,再重啟克隆的目的虛擬機

同時還需要修改虛擬機內對應網卡的ip, mac,與重啟后新生成的/etc/udev/rules.d/70-persistent-net.rules中的MAC和設備號內容一致。

然后才能重啟新建的虛擬機的網卡。

cat /etc/udev/rules.d/70-persistent-net.rules 
# PCI device 0x1af4:0x1000 (virtio-pci) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:3e:78:0d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改ip mac 重啟network服務 

其他管理例子

(1) 刪除kvm虛擬機

# virsh undefine wintest01 

說明:該命令只是刪除wintest01的配置文件,並不刪除虛擬磁盤文件。

(2) 重新定義虛擬機配置文件

通過導出備份的配置文件恢復原KVM虛擬機的定義,並重新定義虛擬機。

# mv /etc/libvirt/qemu/wintest02.xml /etc/libvirt/qemu/wintest01.xml # virsh define /etc/libvirt/qemu/wintest01.xml 

(3) 編輯KVM虛擬機配置文件

# virsh edit wintest01 

virsh edit將調用vi命令編輯/etc/libvirt/qemu/wintest01.xml配置文件。也可以直接通過vi命令進行編輯,修改,保存。

可以但不建議直接通過vi編輯。

(1) 掛起服務器

# virsh suspend oeltest01 

(2) 恢復服務器

# virsh resume oeltest01 

導出KVM虛擬機配置文件

# virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml 

配置開機自啟動虛擬機

# virsh autostart oeltest01 

通過配置文件啟動虛擬機

# virsh create /etc/libvirt/qemu/wintest01.xml 

KVM虛擬機開機

# virsh start oeltest01 

KVM虛擬機關機或斷電

(1) 關機

默認情況下virsh工具不能對linux虛擬機進行關機操作,linux操作系統需要開啟與啟動acpid服務。在安裝KVM linux虛擬機時必須在虛擬機內配置此服務。

# chkconfig acpid on # service acpid restart 

virsh關機

# virsh shutdown oeltest01 

(2) 強制關閉電源

# virsh destroy wintest01 

給每個虛擬機CPU,指定具體的物理機CPU pinning綁定親和策略

<cputune> <vcpupin vcpu="0" cpuset="1-4,2"/> <vcpupin vcpu="1" cpuset="0,1"/> <vcpupin vcpu="2" cpuset="2,3"/> <vcpupin vcpu="3" cpuset="0,4"/> </cputune> or <cputune> <vcpupin vcpu="0" cpuset="2"/> <vcpupin vcpu="1" cpuset="3"/> <vcpupin vcpu="2" cpuset="4"/> <vcpupin vcpu="3" cpuset="5"/> <vcpupin vcpu="4" cpuset="6"/> <vcpupin vcpu="5" cpuset="7"/> <vcpupin vcpu="6" cpuset="8"/> <vcpupin vcpu="7" cpuset="9"/> <vcpupin vcpu="8" cpuset="10"/> <vcpupin vcpu="9" cpuset="11"/> </cputune> 

也可以使用emulatorpin的方式

emulatorpin 標簽可以指定一個特定的物理CPU,使虛擬機使用的CPU和存儲器都在一個物理機CPU內部

<cputune>
        <emulatorpin cpuset="1-3"/> </cputune> 

vcpu的設置

<vcpu placement='auto'>8</vcpu> <vcpu placement='static' cpuset='0-10,5'>8</vcpu> 

和 需要保持一致,配置的是物理CPU,配置的CPU的核,包括超線程產生的核;

使用static模式,也必須是;

也可以設置一個虛擬機給32個虛擬CPU,但是一開始只能使用8個,然后可以根據系統壓力,熱添加CPU給虛擬機。

<vcpu placement='auto' current='8'>32</vcpu> 

使用cgoup cpuset限制KVM虛擬機對CPU的訪問

使用vcpupin的效果可能不好,所以可以考慮cgroup的cpuset子系統。

/cgroup/cpuset/libvirt/qemu  

在這個目錄中,每個啟動的虛擬機都有一個子目錄,設置子目錄中的cpuset.cpus即可。

例如, 設置kvm101虛擬機只能使用宿主機的1-10號核。

cd /cgroup/cpuset/libvirt/qemu/kvm101 echo "1-10" > cpuset.cpus 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM