使用qemu-kvm在CentOS8上通過GUI創(chuàng)建虛擬機(jī)睡陪。
1巴席、安裝系統(tǒng)組件
首先我們需要安裝epel源和相應(yīng)的虛擬機(jī)工具
yum install epel-release -y
yum install qemu-kvm qemu-img libvirt virt-manager virt-top.x86_64 virtio-win.noarch virt-viewer.x86_64 virt-install.noarch -y
virt-install
libvirt #安裝libvirt會(huì)將libvirt-client作為依賴進(jìn)行安裝矮冬,libvirt-client即libvirt的客戶端谈宛,最重要的功能之一就是就在宿主機(jī)關(guān)機(jī)時(shí)可以通知虛擬機(jī)也關(guān)機(jī),使虛擬機(jī)系統(tǒng)正常關(guān)機(jī)胎署,而不是被強(qiáng)制關(guān)機(jī)吆录,造成數(shù)據(jù)丟失
ipxe-roms-qemu #虛擬機(jī)iPXE的啟動(dòng)固件,支持虛擬機(jī)從網(wǎng)絡(luò)啟動(dòng)
qemu-kvm #KVM在用戶空間運(yùn)行的程序
virt-manager #基于 Libvirt 的圖像化虛擬機(jī)管理軟件
libvirt #用于管理虛擬機(jī)琼牧,它提供了一套虛擬機(jī)操作API
virt-viewer #顯示虛擬機(jī)的控制臺(tái)console
virt-top #類似于top命令恢筝,查看虛擬機(jī)的資源使用情況
virt-what #在虛擬機(jī)內(nèi)部執(zhí)行,查看虛擬機(jī)運(yùn)行的虛擬化平臺(tái)
qemu-img #用于操作虛擬機(jī)硬盤鏡像的創(chuàng)建巨坊、查看和格式轉(zhuǎn)化
一些主要組件的功能介紹如下:
-
virt-install
Description : Package includes several command line utilities, including virt-install
: (build and install new VMs) and virt-clone (clone an existing virtual
: machine). -
virt-who
Description : Agent that collects information about virtual guests present in the system and
: report them to the subscription manager. -
virt-top
Description : virt-top is a 'top(1)'-like utility for showing stats of virtualized
: domains. Many keys and command line options are the same as for
: ordinary 'top'.
:
: It uses libvirt so it is capable of showing stats across a variety of
: different virtualization systems. -
virt-viewer
Description : Virtual Machine Viewer provides a graphical console client for connecting
: to virtual machines. It uses the GTK-VNC or SPICE-GTK widgets to provide
: the display, and libvirt for looking up VNC/SPICE server details. -
virt-manager
Description : Virtual Machine Manager provides a graphical tool for administering virtual
: machines for KVM, Xen, and LXC. Start, stop, add or remove virtual devices,
: connect to a graphical or serial console, and see resource usage statistics
: for existing VMs on local or remote machines. Uses libvirt as the backend
: management API. -
libvirt
Description : Libvirt is a C toolkit to interact with the virtualization capabilities
: of recent versions of Linux (and other OSes). The main package includes
: the libvirtd server exporting the virtualization support. -
qemu-kvm
Description : qemu-kvm is an open source virtualizer that provides hardware
: emulation for the KVM hypervisor. qemu-kvm acts as a virtual
: machine monitor together with the KVM kernel modules, and emulates the
: hardware for a full system such as a PC and its associated peripherals.
2撬槽、創(chuàng)建虛擬機(jī)
2.1 配置虛擬機(jī)存儲(chǔ)目錄
這里我們直接開啟宿主機(jī)Linux系統(tǒng)上面的GUI模式并且使用xrdp遠(yuǎn)程到桌面,然后使用virt-manager
來進(jìn)行GUI模式的創(chuàng)建虛擬機(jī)趾撵。
這里需要注意的是因?yàn)槲覀兪褂玫氖荌SO鏡像文件來進(jìn)行虛擬機(jī)系統(tǒng)的安裝侄柔,因此必須需要一個(gè)GUI界面來進(jìn)行最開始的系統(tǒng)安裝,等待系統(tǒng)安裝完成之后占调,我們可以直接使用命令行操作來克隆虛擬機(jī)暂题,就不用每次都打開GUI模式安裝系統(tǒng)這么麻煩了。
接著我們創(chuàng)建一個(gè)目錄專門用來存放虛擬機(jī)相關(guān)的鏡像和文件究珊,同時(shí)我們還需要注意權(quán)限的問題:
sudo mkdir -p /kvm/iso
sudo chown tinychen:tinychen /kvm -R
接著我們把相應(yīng)的系統(tǒng)安裝鏡像復(fù)制進(jìn)去(對(duì)應(yīng)的系統(tǒng)安裝鏡像文件均可直接在對(duì)應(yīng)的官網(wǎng)下載)薪者,然后打開Virtual Machine Manager
(以下簡稱VMM)來創(chuàng)建虛擬機(jī)
首先我們?cè)?code>Edit→Preferences里面開啟XML文件編輯功能,XML模式可以最大程度的控制虛擬機(jī)的所有配置剿涮,一些特殊的配置或者組件操作無法通過VMM的GUI界面完成的我們可以直接編輯對(duì)應(yīng)的XML文件塊來實(shí)現(xiàn)言津。
2.2 創(chuàng)建虛擬機(jī)
直接點(diǎn)擊加號(hào)
或者上面的File→New Virtual Machine
然后選擇使用本地媒體創(chuàng)建虛擬機(jī)(這里我們使用剛剛復(fù)制進(jìn)去的ISO鏡像文件)
接著我們可以看到有四種創(chuàng)建虛擬機(jī)的方式:
- 從本地的媒體資源安裝(iso文件或者CD光盤)
- 通過網(wǎng)絡(luò)協(xié)議獲取安裝的鏡像文件然后進(jìn)行安裝
- 通過PXE進(jìn)行安裝
- 導(dǎo)入已有的虛擬機(jī)硬盤鏡像文件
我們選擇第一種方式進(jìn)行新建虛擬機(jī)操作。
由于是第一次創(chuàng)建虛擬機(jī)取试,我們需要先在VMM中添加一個(gè)池(pool)悬槽,把先前創(chuàng)建的存放iso鏡像文件的目錄作為一個(gè)池添加到VMM中。
添加完成之后我們就可以看到里面的鏡像文件瞬浓,這里我們選擇CentOS7的版本進(jìn)行安裝初婆。
選擇鏡像之后VMM會(huì)自動(dòng)檢測(cè)鏡像文件內(nèi)包含的操作系統(tǒng)。
接下來是設(shè)定CPU和內(nèi)存瑟蜈,注意這里的內(nèi)存單位是MB烟逊,而CPU的單位是個(gè),即并不是我們常說的CPU核心數(shù)量或者是超線程的線程數(shù)量铺根,而是虛擬設(shè)定的多少個(gè)CPU宪躯,這里選擇4即為4個(gè)CPU而不是一個(gè)4核CPU,這個(gè)選項(xiàng)我們后面需要再進(jìn)行修改位迂。
接下來便是進(jìn)行虛擬機(jī)的硬盤參數(shù)設(shè)置访雪,我們這里選擇手動(dòng)創(chuàng)建一個(gè)硬盤详瑞。
和之前一樣我們?cè)賱?chuàng)建一個(gè)池專門用來存放虛擬機(jī)的系統(tǒng)鏡像文件。
接著在對(duì)應(yīng)的池中新建一個(gè)qcow2磁盤文件臣缀。
這里我們可以看到硬盤的格式主要有raw和qcow2兩種坝橡。qcow2 鏡像格式是 QEMU 模擬器支持的一種磁盤鏡像。它也是可以用一個(gè)文件的形式來表示一塊固定大小的塊設(shè)備磁盤精置。與普通的 raw 格式的鏡像相比计寇,有以下特性:
- 更小的空間占用,即使文件系統(tǒng)不支持空洞(holes)脂倦;
- 支持寫時(shí)拷貝(COW, copy-on-write)番宁,鏡像文件只反映底層磁盤的變化;
- 支持快照(snapshot)赖阻,鏡像文件能夠包含多個(gè)快照的歷史蝶押;
- 可選擇基于 zlib 的壓縮方式
- 可以選擇 AES 加密
qcow2最大的特點(diǎn)就是可以節(jié)省硬盤空間,只有在虛擬機(jī)實(shí)際占用了磁盤空間時(shí)火欧,其文件才會(huì)增長棋电。比如這里我們創(chuàng)建了一個(gè)40G的硬盤鏡像文件,但是實(shí)際占用并沒有40G苇侵,只有在虛擬機(jī)實(shí)際使用了硬盤空間之后赶盔,qcow2格式的鏡像文件才會(huì)增大。
最后需要設(shè)置的就是虛擬機(jī)的網(wǎng)卡衅檀,這里我們要勾選Customize configuration before install
招刨,在系統(tǒng)開始安裝之前進(jìn)行一些參數(shù)的設(shè)置霎俩。
2.3 配置虛擬機(jī)其他參數(shù)
首先我們看到Overview
中的Chipset
和Firmware
兩個(gè)參數(shù)哀军,它們分別相當(dāng)于物理機(jī)電腦主板的芯片組和固件模式(BIOS或UEFI),如果有新版本的需求(如虛擬機(jī)KVM直通GPU)可以選擇UEFI(OVMF)來進(jìn)行啟動(dòng)打却,需要注意VMM中的快照(snapshot)功能不支持UEFI(OVMF)固件的虛擬機(jī)杉适,因此這里我們選擇默認(rèn)的Q35和BIOS即可。
接下來我們需要對(duì)前面設(shè)定的CPU參數(shù)進(jìn)行更改柳击。這里我們編輯對(duì)應(yīng)的XML文件將CPU模式修改為host-passthrough
以便將CPU的全部特性傳遞到虛擬機(jī)中猿推,同時(shí)修改CPU個(gè)數(shù)為1,核心數(shù)為4捌肴,每個(gè)核心的線程數(shù)為1蹬叭。
<cpu mode="host-passthrough">
<topology sockets="1" cores="4" threads="1"/>
</cpu>
設(shè)定安裝系統(tǒng)的ISO為只讀,保證ISO文件不會(huì)被修改状知,同時(shí)設(shè)置數(shù)據(jù)總線方式為SATA保證最好的兼容性秽五。
配置兩個(gè)網(wǎng)卡,分別為橋接宿主機(jī)的網(wǎng)卡和NAT模式饥悴,其中橋接網(wǎng)卡使用e1000e型號(hào)保證最好的兼容性坦喘,NAT模式的網(wǎng)卡使用VirtIO型號(hào)可以達(dá)到最大100G的傳輸速度盲再。
其他還會(huì)默認(rèn)添加一些常用的組件,我們根據(jù)自己的需求進(jìn)行修改瓣铣,這里我只保留了常用的USB重定向設(shè)備答朋,聲卡、網(wǎng)卡棠笑、串行總線等基本設(shè)備梦碗,如果不清楚具體需求,建議保留蓖救。
最后整個(gè)完整的xml文件如下:
<domain type="kvm">
<name>centos7_demo</name>
<uuid>2e80a698-c4f9-464b-bb3e-28c4987cbe9a</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://centos.org/centos/7.0"/>
</libosinfo:libosinfo>
</metadata>
<memory>8388608</memory>
<currentMemory>8388608</currentMemory>
<vcpu current="4">4</vcpu>
<os>
<type arch="x86_64" machine="q35">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
<vmport state="off"/>
</features>
<cpu mode="host-passthrough">
<topology sockets="1" cores="4" threads="1"/>
</cpu>
<clock offset="utc">
<timer name="rtc" tickpolicy="catchup"/>
<timer name="pit" tickpolicy="delay"/>
<timer name="hpet" present="no"/>
</clock>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2"/>
<source file="/kvm/vm/centos7_demo.qcow2"/>
<target dev="vda" bus="virtio"/>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"/>
<source file="/kvm/iso/centos7.7.iso"/>
<target dev="sda" bus="sata"/>
<readonly/>
</disk>
<interface type="network">
<source network="default"/>
<mac address="52:54:00:83:cc:6d"/>
<model type="virtio"/>
</interface>
<console type="pty"/>
<channel type="unix">
<source mode="bind"/>
<target type="virtio" name="org.qemu.guest_agent.0"/>
</channel>
<channel type="spicevmc">
<target type="virtio" name="com.redhat.spice.0"/>
</channel>
<input type="tablet" bus="usb"/>
<graphics type="spice" port="-1" tlsPort="-1" autoport="yes">
<image compression="off"/>
</graphics>
<video>
<model type="qxl"/>
</video>
<memballoon model="virtio"/>
<rng model="virtio">
<backend model="random">/dev/urandom</backend>
</rng>
<interface type="direct">
<source dev="enp6s0" mode="bridge"/>
<mac address="52:54:00:31:ad:1d"/>
<model type="e1000e"/>
</interface>
</devices>
</domain>
2.4開始安裝
接下來的安裝就和正常的物理機(jī)安裝操作系統(tǒng)一樣叉弦,這里我們就不再贅述了,由于是CentOS的虛擬機(jī)藻糖,為了節(jié)約系統(tǒng)資源淹冰,我選擇了最小化安裝(Minimal Install)、關(guān)閉了KDUMP功能巨柒,并且使用LVM來設(shè)定分區(qū)以便后期擴(kuò)容
最后可以通過virsh命令來查看虛擬機(jī)的狀態(tài)
如果需要開啟虛擬機(jī)只需要使用virsh start 虛擬機(jī)名
即可樱拴。