linux导入导出kvm虚拟机配置——筑梦之路
2023-09-14 09:09:35 时间
#查看所有虚拟机
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 "<source file")
echo "${VM} ${DISK_PATH}"
done | column -t
#导出磁盘信息并备份磁盘镜像
virsh dumpxml vm-name > /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 <path-to-xml-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 "<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
相关文章
- 远程桌面Linux远程桌面配置指南(linux如何配置)
- Linux三网卡配置使用指南(linux三网卡)
- Linux修改文件夹名称的简单步骤(linux改文件夹名)
- 解开Linux下的GZ文件之旅(linux下gz文件解压)
- Linux下快速配置邮件服务(linux邮件服务配置)
- Yum源配置:Linux迈入新纪元(yum源配置linux)
- Linux域名服务器:尽在手中实现完美配置(linux域名服务器配置)
- 实现Linux驱动快速安装:探究命令行的奥秘(linux驱动安装命令)
- Linux双网卡同网段配置方法(linux双网卡同网段)
- 轻松设置Linux网关:命令行简易操作(linux命令设置网关)
- 令人惊叹的Gvim在Linux中的配置(gvim配置linux)
- Linux解压文件乱码:快速解决方法(linux解压文件乱码)
- 探索Linux程序的暂停之路(linux程序暂停)
- 关闭Linux系统防火墙方法指南(关闭linux的防火墙)
- 如何在Linux系统恢复上次配置(linux恢复上次配置)
- Linux如何停用网卡?快速了解关闭网卡的命令(linux关闭网卡命令)
- 配置Linux 无缝转换:SS 全局配置指南(linux全局ss)
- 让Linux保驾护航:强化密码复杂度配置(linux密码复杂度设置)
- 轻松搞定:Linux下配置Wifi热点(linux配置wifi)
- Linux下图形化配置串口终端助你成功(linux图形化串口)
- 服务器Linux下架设Sftp服务器,快速上手(linux下搭建sftp)
- 使用VirtualBox轻松安装Linux系统:一步步指南(virtualbox安装linux)
- 配置Linux服务器,开启一片新世界(服务器配置linux)
- Linux下查看内存配置的正确方法(linux 内存配置查看)
- Linux下如何配置打印机?(linux配置打印机)
- 探索Linux系统重启历史记录(查看linux重启记录)
- Linux下如何配置C语言开发环境(linux配置c环境)
- Linux安装gzip:一步一步指引(linux 安装gzip)
- Linux MySQL配置启动指南(linux mysql启动)
- Linux系统:探索无限可能(linux系统能做什么)