譯者 | 趙青窕
本文將介紹導(dǎo)出 KVM 虛擬機磁盤和配置的步驟,然后將它們導(dǎo)入同一臺機器或不同的機器中。我還將向您展示如何使用 Bash 腳本導(dǎo)出和導(dǎo)入 KVM 虛擬機。
1.前言
最近,我將我的虛擬機管理程序從 Oracle VirtualBox 切換到了 KVM,因為它具有原生特性和性能。我實測發(fā)現(xiàn),與 Virtualbox VM 相比,KVM 虛擬機運行得更快。
定期備份您的虛擬機是非常有必要的。假如我們的KVM出現(xiàn)任何異常情況,我們就可以使用備份的KVM來恢復(fù)虛擬機。導(dǎo)出和導(dǎo)入 KVM 虛擬機是不同的兩個過程。
首先,您必須檢查磁盤映像的存儲位置并將磁盤映像復(fù)制到備份位置。
其次,導(dǎo)出機器配置,以便以后用于創(chuàng)建域。
2.虛擬機的配置
有兩種獲取虛擬機配置信息的方法。你可以使用“Virsh”或者“Virt-manager”。
你可以使用Virt-manager或執(zhí)行命令virsh獲取虛擬機的名稱。
$ virsh list --all
Id Name State
------------------------------------
Linux_Mint_20 shut off
mxlinux shut off
popos_21_10 shut off
rocky_linux_8 shut off
ubuntu20.04 shut off
ubuntu20.04-clone shut off
當(dāng)需要查看某個虛擬機的整個信息(即虛擬機配置)時,可以執(zhí)行以下命令。該命令會以XML的格式來顯示結(jié)果。
$ virsh dumpxml vm_name | less
$ virsh dumpxml ubuntu20.04-clone | less
您還可以通過管道連接到grep命令來獲取特定的機器配置。假設(shè)您想查看內(nèi)存分配情況,那么可以運行以下命令。
$ virsh dumpxml ubuntu20.04-clone | grep -i "MEMORY"
<memory unit='KiB'>5632000</memory>
<currentMemory unit='KiB'>5632000</currentMemory>
如果您更喜歡使用Virt-manager工具,那么您可以通過“虛擬硬件詳細信息窗口(show virtual hardware)”選項中的XML子選項來獲取以XML格式顯示的虛擬機配置信息。
3.虛擬機磁盤位置
虛擬機的磁盤以 qcow2 格式創(chuàng)建。默認情況下,虛擬機磁盤存儲在 /var/lib/libvirt/images/ 目錄中,除非您明確指定了磁盤路徑。
運行以下命令獲取磁盤目錄路徑。
$ virsh dumpxml ubuntu20.04-clone | grep -i "source"
<source file='/var/lib/libvirt/images/ubuntu20.04-clone.qcow2'/>
如果您使用的是Virt-manager工具,可以通過上圖中的disk選項來查看虛擬機磁盤信息。
您使用了不同的路徑來存儲磁盤映像,并且希望知道所有VM磁盤路徑,那么您可以在終端或shell腳本內(nèi)運行以下代碼片段:
$ virsh dumpxml ubuntu20.04-clone | grep -i "source"
<source file='/var/lib/libvirt/images/ubuntu20.04-clone.qcow2'/>
4.導(dǎo)出配置信息并備份磁盤鏡像
現(xiàn)在您已經(jīng)知道了如何獲取磁盤路徑和虛擬機的配置信息,接下來就可以導(dǎo)出VM的配置信息并將磁盤鏡像復(fù)制到不同的位置。
要導(dǎo)出虛擬機信息,可以運行以下“virsh dumpxml”并將輸出存儲在XML文件中。
$ virsh dumpxml vm-name > /path/to/xm_file.xml
$ virsh dumpxml ubuntu20.04-clone > ~/Documents/ubuntu.xml
接下來,我們使用下面的命令將磁盤映像復(fù)制到其他位置,以便以后使用。
$ sudo cp /var/lib/libvirt/images/ubuntu20.04-clone.qcow2 ~/Documents/
這樣,導(dǎo)出KVM已經(jīng)完成了。
注意:您應(yīng)該使用 sudo 或使用 root 用戶復(fù)制磁盤,因為磁盤歸 root 用戶所有。
5.導(dǎo)入虛擬機
要導(dǎo)入KVM虛擬機,您必須運行以下命令,利用導(dǎo)出的 XML 文件重新定義一個虛擬機。
$ virsh define --file <path-to-xml-file>
$ virsh define –file ~/Documents/ubuntu.xml
現(xiàn)在,您可以檢查 Virt-manager 或執(zhí)行virsh命令,成功創(chuàng)建域。同時您必須將磁盤映像復(fù)制到 XML 文件中定義的目錄路徑。在我的環(huán)境中,它是默認位置 /var/lib/libvirt/images/,因此我使用下面的命令進行復(fù)制。
sudo cp ~/Documents/ubuntu20.04-clone.qcow2 /var/lib/libvirt/images/
導(dǎo)入KVM到此就完成了。
6.導(dǎo)出和導(dǎo)入KVM虛擬機的Bash腳本
我已經(jīng)創(chuàng)建了一個bash腳本,用于導(dǎo)出所有配置和磁盤映像。如果你感興趣,你可以在我的GitHub庫中查看這個腳本。同時希望您能提供改進腳本的建議。
#!/usr/bin/env bash
# ----------------------------------------------------------------------------------------------------
# AUTHOR : KARTHICK S
# PURPOSE : THIS SCRIPT WILL EXPORT/IMPORT THE CONFIG AND VM DISK.
#
# usage:
# export function will take care of exporting the necessary for all VM. Run as "<scriptname.sh> export"
# import function will take care of importing the necessary for all VM. Run as "<scriptname.sh> import"
#
# NOTE: Do not add trailing / for the directory when giving export and import path.
#------------------------------------------------------------------------------------------------------
# Trigger the script with root user or exit.
if [[ ${UID} -ne 0 ]]; then
echo -e "[EXIT] - Run the script as root user or with sudo privilege..."
exit
fi
function export_vm(){
# Get the export location.
read -p "Provide the directory path where disk and config files to be exported: " EXPORT_LOCATION
# Create the destination directory if not exists.
[[ -d ${EXPORT_LOCATION} ]] || mkdir -p ${EXPORT_LOCATION}
# Exporting the config using virsh dumpxml command.
VM_NAMES=($(virsh list --all| awk '(NR>2)' | awk '{ print $2 }'))
for VM in ${VM_NAMES[@]}; do
virsh dumpxml ${VM} > ${EXPORT_LOCATION}/${VM}.xml
done
# Using rsync copy the entire directory from default location.
echo -e "\n[ Copying disk images ]\n" && sudo rsync -avxp --progress /var/lib/libvirt/images ${EXPORT_LOCATION}
echo -e "\n[ Exported Files ] \n" && ls -lR ${EXPORT_LOCATION}
}
function import_vm(){
# Get the source location.
read -p "Provide the directory path where disk and config files are stored: " SOURCE_LOCATION
# Throws error if directory is not available and exit.
[[ -d ${SOURCE_LOCATION} ]] || { echo "Directory not available"; exit 1 ; }
# Copy all the files to default disk location.
echo -e "[ Copying disk images ]\n" && sudo rsync -avxp --progress ${SOURCE_LOCATION}/images /var/lib/libvirt/
if [[ $? -eq 0 ]]; then
# Define VM
echo -e "\n[ Defining VM ]\n"
for XML_FILE in ${SOURCE_LOCATION}/*.xml; do
virsh define --file ${XML_FILE}
done
echo -e "\n[ Imported VM List ]\n" && virsh list --all
fi
}
case $1 in
export ) export_vm ;;
import ) import_vm ;;
*) echo -e "USAGE :
kvm_export_import.sh export - Export config and disk
kvm_export_import.sh import - Define VM and copy the disk"; exit
esac
該腳本的用法如下。
您可以運行以下命令從 GitHub中克隆 gist。
$ git clone https://gist.github.com/d6c671597592fe5634a39b7974bc8029.git
該腳本執(zhí)行備份(即導(dǎo)出)和恢復(fù)(即導(dǎo)出)。
讓我們使用如下腳本導(dǎo)出 KVM 虛擬機。
$ sudo bash kvm_export_import.sh export
您必須提供要導(dǎo)出XML文件和磁盤映像的目錄路徑。給出導(dǎo)出目錄時,不要在路徑后面添加斜杠(’ / ‘)。它將首先復(fù)制磁盤映像(.qcow2)并運行“virsh dumpxml”命令以導(dǎo)出所有的虛擬機配置。
下圖顯示了導(dǎo)出的工作原理。
當(dāng)您將“import”作為參數(shù)傳遞時,它將首先將磁盤映像復(fù)制到默認位置 /var/lib/libvirt/ 并針對所有已導(dǎo)出的XML文件運行“virsh define”命令。
$ sudo bash kvm_export_import.sh import
7.總結(jié)
在本文中,我向您展示了如何導(dǎo)出 KVM 虛擬機并將它們導(dǎo)入相同或不同的機器。從頭開始構(gòu)建虛擬機是一項耗時的任務(wù)。因此,使用適當(dāng)?shù)牟呗詠肀Wo我們的虛擬機環(huán)境不會因任何情況而損害。
譯者介紹
趙青窕,51CTO社區(qū)編輯,從事多年驅(qū)動開發(fā)。研究興趣包含安全OS和網(wǎng)絡(luò)安全領(lǐng)域,發(fā)表過網(wǎng)絡(luò)相關(guān)專利。