?
一、遠程管理kvm虛機



(2)有些情況下,有一個要配置的地方。 因為 KVM(準確說是 Libvirt)默認不接受遠程管理,需要按下面的內(nèi)容配置被管理宿主機中的兩個文件:
vim /etc/default/libvirt-bin start_libvirtd="yes" libvirtd_opts="-d -l"
vim /etc/libvirt/libvirtd.conf listen_tls = 0 listen_tcp = 1 unix_sock_group = "libvirtd" unix_sock_ro_perms = "0777" unix_sock_rw_perms = "0770" auth_unix_ro = "none" auth_unix_rw = "none" auth_tcp = "none"
然后重啟 Libvirtd 服務就可以遠程管理了。 systemctl restart libvirt-bin
二、virsh命令行下管理虛擬機
virsh既有命令行模式,又有交互模式;在命令行直接輸入virsh就進入交互模式,virsh后面跟命令參數(shù),則是命令行模式。
(1)基礎(chǔ)命令:
virsh help:列出命令下的所有參數(shù)
virsh list help : 列出list命令下的所有參數(shù)
virsh list : 列出當前宿主機處于運行狀態(tài)的虛擬機
virsh list --all : 列出當前宿主機上所有的虛擬機
virsh start 虛擬機名 : 開啟某一臺虛擬機
virsh shutdown 虛擬機名 : 正常關(guān)閉某一臺虛擬機
virsh destroy 虛擬機名 : 強制關(guān)閉某一臺虛擬機
virsh autostart 虛擬機名 : 開機自啟動虛擬機
virsh autostart --disable 虛擬機名 : 關(guān)閉開機自啟動
virsh edit 虛擬機名 : 編輯某個虛擬機的配置文件
virsh pool-list : 列出存儲池
virsh define???????????????? 從一個 XML 文件定義(但不開始)一個域 virsh undefine???????????? 刪除一個虛機域
創(chuàng)建一個大小為9G的,格式為qcow2的磁盤鏡像
[root@localhost ~]# qemu-img create -f qcow2 -o size=9G /var/lib/libvirt/images/test3.qcow2
Formatting '/var/lib/libvirt/images/test3.qcow2', fmt=qcow2 size=9663676416 encryption=off cluster_size=65536 lazy_refcounts=off
查看磁盤信息
[root@localhost ~]# qemu-img info /var/lib/libvirt/images/test3.qcow2
image: /var/lib/libvirt/images/test3.qcow2
file format: qcow2 格式
virtual size: 9.0G (9663676416 bytes) 大小
disk size: 196K 使用大小
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
【注意】disk size: 196K 這里不是9G的原因是:這里采用了精簡置備(主機像磁盤宣告所需空間大小,隨著使用慢慢增長,但是不能超過磁盤提供的最大空間,也就是用多少拿多少) 與之相對應的就是厚置備(給多少就占多少空間),類似與數(shù)據(jù)庫中的char和varchar;厚置備會浪費資源。
?
克隆一臺虛擬機
[root@localhost ~]# virsh list --all
Id 名稱 狀態(tài)
----------------------------------------------------
- 003 關(guān)閉
- test1 關(guān)閉
[root@localhost ~]# yum install -y virt*
[root@localhost ~]# virt-clone -o test1 -n test2 -f /var/lib/libvirt/images/test2.qcow2
正在分配 'test2.qcow2' | 9.0 GB 00:00:15
成功克隆 'test2'。
[root@localhost ~]# virsh list --all
Id 名稱 狀態(tài)
----------------------------------------------------
- 003 關(guān)閉
- test1 關(guān)閉
- test2 關(guān)閉
三、kvm通過 virsh console 進入虛擬機
開啟虛擬機的console功能就可以通過命令在遠端進入虛擬機了,這里就不贅述centos7以前的版本了,以下介紹centos7以后版本使用console進入虛擬機。
?
①查看是否可以連接
[root@localhost ~]# virsh console test1
連接到域 test1
換碼符為 ^]
②在虛擬機上進行以下操作并重啟
[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0"
[root@localhost ~]# reboot ③在宿主機上進入虛擬機
[root@localhost ~]# virsh console test1
連接到域 test1 CentOS Linux 7 (Core) Kernel 3.10.0-514.el7.x86_64 on an x86_64
localhost login: root 密碼: Last login: Tue May 28 19:04:13 on tty1
?
四、kvm虛擬化透傳
KVM 虛擬化需要處理器對虛擬化技術(shù)的支持,當我們需要進行虛擬機嵌套虛擬機時,我們需要讓虛擬機中處理器對 VT 功能的支持達到透傳的效果。
nested 虛擬機嵌套( kvm on kvm ):nested 技術(shù),簡單的說,就是在虛擬機上跑虛擬機。
KVM 虛擬機嵌套和 VMWare 原理不同, VMWare第一層是用的硬件虛擬化技術(shù),第二層就是完全軟件模擬出來的,所以 VMWare 只能做兩層嵌套。 KVM 是將物理 CPU 的特性全部傳給虛擬機,所有理論上可以嵌套 N 多層。
?
?
①查看一層虛擬機是否支持VT
[root@localhost ~]# egrep "(vmx|svm)" /proc/cpuinfo
[root@localhost ~]# ②在宿主機上為嵌套虛擬機做準備---cpu虛擬化透傳
[root@localhost ~]# cat /etc/modprobe.d/kvm-nested.conf options kvm_intel nested=1 #在宿主機上啟用kvm_intel的嵌套虛擬化功能,并使透傳永久起效 ③重新加載kvm模塊
[root@localhost ~]# modprobe -r kvm_intel
modprobe: FATAL: Module kvm_intel is in use.
原因是虛擬機沒有關(guān)機
[root@localhost ~]# modprobe -r kvm_intel
[root@localhost ~]# lsmod | grep kvm
[root@localhost ~]# modprobe kvm_intel
[root@localhost ~]# lsmod | grep kvm
kvm_intel 170181 0
kvm 554609 1 kvm_intel
irqbypass 13503 1 kvm ④驗證是否加載成功
[root@localhost ~]# cat /sys/module/kvm_intel/parameters/nested
Y #Y表示虛擬化透傳功能開啟 ⑤編輯需要做虛擬化透傳的虛擬機配置文件
[root@localhost ~]# virsh edit 003
<cpu mode='host-passthrough'/> ⑥在虛擬機中查看cpu是否透傳成功 [root@localhost ~]# lsmod | grep kvm kvm_intel???????????? 170181? 0 kvm?????????????????? 554609? 1 kvm_intel irqbypass????????????? 13503? 1 kvm ⑦在一層虛擬機中查看是否支持虛擬化 [root@localhost ~]# grep vmx /proc/cpuinfo flags?? ??? ?: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt
?
五、kvm存儲虛擬化
? 1.介紹
KVM 的存儲虛擬化是通過存儲池(Storage Pool)和卷(Volume)來管理的。
Storage Pool 是宿主機上可以看到的一片存儲空間,可以是多種型;
Volume 是在 Storage Pool 中劃分出的一塊空間,宿主機將 Volume 分配給虛擬機,Volume 在虛擬機中看到的就是一塊硬盤。
2.目錄類型的storage pool
文件目錄是最常用的 Storage Pool 類型。KVM 將宿主機目錄 /var/lib/libvirt/images/ 作為默認的 Storage Pool
Volume 是該目錄下面的文件,一個文件就是一個 Volume。
3.創(chuàng)建volume卷,






在虛擬機查看添加的卷

?
4.創(chuàng)建存儲池pool

?

?kvm默認/var/lib/libvirt/images目錄為存儲池。
那 KVM 是怎么知道要把 /var/lib/libvirt/images 這個目錄當做默認 Storage Pool 的呢? 實際上 KVM 所有可以使用的 Storage Pool 都定義在宿主機的 /etc/libvirt/storage 目錄下,每個 Pool 一個 xml 文件,
存儲池的配置文件存放的位置 [root@localhost ~]# cd /etc/libvirt/storage [root@localhost storage]# cat default.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 pool-edit default
or other application using the libvirt API.
-->
<pool type='dir'>
<name>default</name>
<uuid>623b2eaa-3f05-4c50-b5b8-bdd52f16a936</uuid>
<capacity unit='bytes'>0</capacity>
<allocation unit='bytes'>0</allocation>
<available unit='bytes'>0</available>
<source>
</source>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool> 虛擬機的配置文件存放的位置 [root@localhost qemu]# ls 003.xml? networks? test1.xml? test2.xml [root@localhost qemu]# pwd /etc/libvirt/qemu
六、虛擬機的刪除與恢復
①先將虛擬機的配置文件復制下來,以防以后有用到的時候 [root@localhost qemu]# cp -a test1.xml /root ②刪除虛擬機
[root@localhost qemu]# virsh undefine test1
域 test1 已經(jīng)被取消定義
[root@localhost qemu]# virsh list --all
Id 名稱 狀態(tài)
----------------------------------------------------
5 003 running
- test2 關(guān)閉
[root@localhost ~]# ls
test1.xml ③磁盤文件還保存在/var/lib/libvirt/images下
[root@localhost ~]# vim test1.xml
<source file='/var/lib/libvirt/images/test1.qcow2'/> ④恢復虛擬機,重新定義虛擬機配置文件即可 [root@localhost ~]# virsh define test1.xml 定義域 test1(從 test1.xml) ⑤查看已恢復 [root@localhost ~]# virsh list --all ?Id??? 名稱???????????????????????? 狀態(tài) ---------------------------------------------------- ?-???? 003??????????????????????????? 關(guān)閉 ?-???? test1????????????????????????? 關(guān)閉 ?-???? test2????????????????????????? 關(guān)閉
七、冷遷移與熱遷移
冷遷移:機器關(guān)機狀態(tài)進行遷移
①將虛擬機的配置文件和磁盤文件發(fā)送到遠程主機上 [root@localhost ~]# scp 001.xml 192.168.42.140:/root
The authenticity of host '192.168.42.140 (192.168.42.140)' can't be established.
ECDSA key fingerprint is a9:9d:91:68:6c:88:bd:b6:10:1d:cf:df:5e:5a:8f:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.42.140' (ECDSA) to the list of known hosts.
root@192.168.42.140's password:
001.xml 100% 4439 4.3KB/s 00:00
[root@localhost ~]# vim 001.xml
[root@localhost ~]# cd /var/lib/libvirt/images/
[root@localhost images]# ls
001.qcow2
[root@localhost images]# scp 001.qcow2 192.168.42.140:/root
root@192.168.42.140's password: 001.qcow2????????????????????????????????????????????????????????????????????????????? 100% 9218MB? 27.4MB/s?? 05:37
②在遠程主機查看配置文件并重新定義虛擬機,查看即可 [root@localhost ~]# ls 001.qcow2? 001.xml??? [root@localhost ~]# virsh list --all ?Id??? 名稱???????????????????????? 狀態(tài) ---------------------------------------------------- ?-???? 003??????????????????????????? 關(guān)閉 ?-???? test1????????????????????????? 關(guān)閉 ?-???? test2????????????????????????? 關(guān)閉
[root@localhost ~]# virsh define 001.xml 定義域 001(從 001.xml)
[root@localhost ~]# virsh list --all ?Id??? 名稱???????????????????????? 狀態(tài) ---------------------------------------------------- ?-???? 001??????????????????????????? 關(guān)閉 ?-???? 003??????????????????????????? 關(guān)閉 ?-???? test1????????????????????????? 關(guān)閉 ?-???? test2????????????????????????? 關(guān)閉
熱遷移:機器在開機狀態(tài)進行遷移 來源:http://www./content-4-213751.html
|