栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux导入导出kvm虚拟机配置——筑梦之路

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux导入导出kvm虚拟机配置——筑梦之路

#查看所有虚拟机

virsh list --all

#查看某个虚拟机的配置,xml形式输出

virsh dumpxml vm_name | less

virsh dumpxml ubuntu20.04-clone | less

#查看某个虚拟机指定信息

virsh dumpxml ubuntu20.04-clone | grep -i "MEMORY"

#查看磁盘目录路径

virsh dumpxml ubuntu20.04-clone | grep -i "source"

#查看所有虚拟机的磁盘目录路径
#!/bin/bash

VM_NAMES=($(virsh list --all| awk '(NR>2)' | awk '{ print $2 }'))
for VM in ${VM_NAMES[@]}; do
DISK_PATH=$(virsh dumpxml ${VM} | grep -i " /path/to/xm_file.xml

virsh dumpxml ubuntu20.04-clone > ~/Documents/ubuntu.xml

sudo cp /var/lib/libvirt/images/ubuntu20.04-clone.qcow2 ~/Documents/


#导入kvm虚拟机
#重新定义一个虚拟机

virsh define --file 

virsh define –file ~/Documents/ubuntu.xml

sudo cp ~/Documents/ubuntu20.04-clone.qcow2 /var/lib/libvirt/images/

#shell脚本

#!/bin/bash

#!/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 " export"

# import function will take care of importing the necessary for all VM. Run as " 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





转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号