659 lines
479 KiB
HTML
659 lines
479 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html><head><title>2. KVM 安装配置</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="2. KVM 安装配置"/><meta property="og:description" content="如何在 Linux 系统上安装和配置 KVM 虚拟化技术,包括硬件要求、操作系统选择、KVM 安装步骤、网络桥接配置、存储池和存储卷管理、虚拟机创建和管理工具的使用。."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="如何在 Linux 系统上安装和配置 KVM 虚拟化技术,包括硬件要求、操作系统选择、KVM 安装步骤、网络桥接配置、存储池和存储卷管理、虚拟机创建和管理工具的使用。."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
|
|||
|
var _hmt = _hmt || [];
|
|||
|
(function() {
|
|||
|
var hm = document.createElement("script");
|
|||
|
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
|
|||
|
var s = document.getElementsByTagName("script")[0];
|
|||
|
s.parentNode.insertBefore(hm, s);
|
|||
|
})();
|
|||
|
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
|
|||
|
(function() {
|
|||
|
window.dataLayer = window.dataLayer || [];
|
|||
|
function gtag() {
|
|||
|
window.dataLayer.push(arguments);
|
|||
|
}
|
|||
|
gtag('js', new Date());
|
|||
|
gtag('config', 'G-MHMEL0F832');
|
|||
|
})();
|
|||
|
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Schibsted Grotesk:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Tech/operating-system/Virtualization/2.KVM虚拟化/2.-KVM-安装配置"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447
|
|||
|
<h3 id="硬件要求">硬件要求<a aria-hidden="true" tabindex="-1" href="#硬件要求" class="internal"> §</a></h3>
|
|||
|
<h4 id="检查-cpu-是否支持虚拟化技术intel-vt-x-或-amd-v">检查 CPU 是否支持虚拟化技术(Intel VT-x 或 AMD-V)<a aria-hidden="true" tabindex="-1" href="#检查-cpu-是否支持虚拟化技术intel-vt-x-或-amd-v" class="internal"> §</a></h4>
|
|||
|
<p>在部署 KVM 前,需要确保系统的 CPU 支持硬件虚拟化技术。这些技术包括 Intel 的 VT-x 和 AMD 的 AMD-V。以下是检查方法:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>Intel CPU</strong>:使用命令 <code>grep -E "vmx" /proc/cpuinfo</code> 检查 CPU 是否支持 VT-x。</li>
|
|||
|
<li><strong>AMD CPU</strong>:使用命令 <code>grep -E "svm" /proc/cpuinfo</code> 检查 CPU 是否支持 AMD-V。</li>
|
|||
|
</ol>
|
|||
|
<p>如果命令输出中包含 <code>vmx</code> 或 <code>svm</code>,则表示 CPU 支持相应的虚拟化技术。</p>
|
|||
|
<h4 id="启用-biosuefi-中的虚拟化支持">启用 BIOS/UEFI 中的虚拟化支持<a aria-hidden="true" tabindex="-1" href="#启用-biosuefi-中的虚拟化支持" class="internal"> §</a></h4>
|
|||
|
<p>**即使 CPU 支持虚拟化技术,也需要在 BIOS/UEFI 中启用相应的功能。**以下是通用的启用步骤,具体步骤可能因主板制造商不同而有所差异:</p>
|
|||
|
<ol>
|
|||
|
<li>启动计算机并进入 BIOS/UEFI 设置界面(通常按 <code>Delete</code>、<code>F2</code>、<code>F10</code> 或 <code>Esc</code> 键)。</li>
|
|||
|
<li>在 BIOS/UEFI 设置中,找到“高级”或“处理器”选项。</li>
|
|||
|
<li>启用 “Intel Virtualization Technology” 或 “AMD-V” 选项。</li>
|
|||
|
<li>保存更改并退出 BIOS/UEFI 设置。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="操作系统要求">操作系统要求<a aria-hidden="true" tabindex="-1" href="#操作系统要求" class="internal"> §</a></h3>
|
|||
|
<p>KVM 作为 Linux 内核的一部分,主要在 Linux 操作系统上运行。以下是一些常用的、支持 KVM 的 Linux 发行版:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>CentOS</strong>:企业级发行版,基于 Red Hat Enterprise Linux (RHEL) 的源代码构建,适合服务器环境。</li>
|
|||
|
<li><strong>RHEL</strong>:Red Hat Enterprise Linux,提供商业支持和长期维护,广泛应用于企业环境。</li>
|
|||
|
<li><strong>Ubuntu</strong>:广泛使用的发行版,提供了良好的社区支持和文档。</li>
|
|||
|
</ul>
|
|||
|
<h2 id="安装-kvm-和-qemu">安装 KVM 和 QEMU<a aria-hidden="true" tabindex="-1" href="#安装-kvm-和-qemu" class="internal"> §</a></h2>
|
|||
|
<h3 id="在-ubuntu-上安装">在 Ubuntu 上安装<a aria-hidden="true" tabindex="-1" href="#在-ubuntu-上安装" class="internal"> §</a></h3>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>更新软件包列表:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="bash" data-theme="default"><code data-language="bash" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>安装 KVM、QEMU 和相关工具:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="bash" data-theme="default"><code data-language="bash" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-y</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">qemu-kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirt-daemon-system</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirt-daemon</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virtinst</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libosinfo-bin</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>启用并启动 libvirtd 服务:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--now</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirtd</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="在-centosrhel-上安装">在 CentOS/RHEL 上安装<a aria-hidden="true" tabindex="-1" href="#在-centosrhel-上安装" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>更新系统:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>安装 KVM、QEMU 和相关工具:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">qemu-kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-install</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>启用并启动 libvirtd 服务:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--now</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirtd</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="在其他-linux-发行版上安装">在其他 Linux 发行版上安装<a aria-hidden="true" tabindex="-1" href="#在其他-linux-发行版上安装" class="internal"> §</a></h4>
|
|||
|
<p>以下是一些其他常见 Linux 发行版的安装方法:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p><strong>Debian</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">qemu-kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirt-clients</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirt-daemon-system</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridge-utils</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--now</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirtd</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>Fedora</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">@virtualization</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--now</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirtd</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<h3 id="安装虚拟机管理工具">安装虚拟机管理工具<a aria-hidden="true" tabindex="-1" href="#安装虚拟机管理工具" class="internal"> §</a></h3>
|
|||
|
<h4 id="安装-libvirt">安装 <code>libvirt</code><a aria-hidden="true" tabindex="-1" href="#安装-libvirt" class="internal"> §</a></h4>
|
|||
|
<p><code>libvirt</code> 是一个开源 API,用于管理平台虚拟化。前面已经在安装 KVM 和 QEMU 时安装了 <code>libvirt</code>。确保其已启用并正在运行:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--now</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libvirtd</span></span></code></pre></div>
|
|||
|
<h4 id="安装-virt-manager">安装 <code>virt-manager</code><a aria-hidden="true" tabindex="-1" href="#安装-virt-manager" class="internal"> §</a></h4>
|
|||
|
<p><code>virt-manager</code> 是一个基于 GUI 的虚拟机管理工具,适用于桌面用户。</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>在 Ubuntu 上安装:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 CentOS/RHEL 上安装:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在其他 Linux 发行版上安装:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>Debian:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>Fedora:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<h3 id="验证安装">验证安装<a aria-hidden="true" tabindex="-1" href="#验证安装" class="internal"> §</a></h3>
|
|||
|
<h4 id="检查-kvm-模块是否已加载">检查 KVM 模块是否已加载<a aria-hidden="true" tabindex="-1" href="#检查-kvm-模块是否已加载" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>检查 KVM 模块是否已加载:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">lsmod</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">grep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kvm</span></span></code></pre></div>
|
|||
|
<p>应该看到类似以下的输出:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">kvm_intel</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">204800</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">655360</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kvm_intel</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>如果使用 AMD 处理器,检查 <code>kvm-amd</code> 模块:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">lsmod</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">grep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kvm</span></span></code></pre></div>
|
|||
|
<p>应该看到类似以下的输出:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">kvm_amd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">204800</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">655360</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kvm_amd</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="确认虚拟化功能可用">确认虚拟化功能可用<a aria-hidden="true" tabindex="-1" href="#确认虚拟化功能可用" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>在 Ubuntu 和 Debian 上使用 <code>kvm-ok</code> 工具检查虚拟化功能:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cpu-checker</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kvm-ok</span></span></code></pre></div>
|
|||
|
<p>输出应显示:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">INFO:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">exists</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">KVM</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">acceleration</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">can</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">be</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">used</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 CentOS/RHEL 和其他发行版上,使用 <code>virt-host-validate</code> 工具检查:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-host-validate</span></span></code></pre></div>
|
|||
|
<p>输出应显示:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">QEMU:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Checking</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">for</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hardware</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virtualization:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PASS</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">QEMU:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Checking</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">for</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">device</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/kvm:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PASS</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">QEMU:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Checking</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">for</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">device</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/vhost-net:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PASS</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">QEMU:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Checking</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">for</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">device</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/net/tun:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PASS</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">……</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>通过以上步骤,可以确保 KVM 及其相关工具已正确安装并配置好。接下来可以开始创建和管理虚拟机,充分利用 KVM 提供的虚拟化功能。</p>
|
|||
|
<h2 id="基本配置">基本配置<a aria-hidden="true" tabindex="-1" href="#基本配置" class="internal"> §</a></h2>
|
|||
|
<h3 id="配置网络桥接">配置网络桥接<a aria-hidden="true" tabindex="-1" href="#配置网络桥接" class="internal"> §</a></h3>
|
|||
|
<p>网络桥接允许虚拟机直接与主机网络进行通信,使其像物理机一样访问网络。以下是使用 <code>nmcli</code> 创建和配置虚拟网络桥接的详细步骤:</p>
|
|||
|
<h4 id="创建虚拟网络桥接桥接模式">创建虚拟网络桥接(桥接模式)<a aria-hidden="true" tabindex="-1" href="#创建虚拟网络桥接桥接模式" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>安装必要的软件包</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>在 Ubuntu 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">network-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 CentOS/RHEL 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">NetworkManager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>配置网络桥接</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>创建一个新的网桥接口:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">type</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridge</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ifname</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">br0</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>将物理接口连接到网桥上,例如假设你正在使用 <code>eth0</code> 接口:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">type</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridge-slave</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ifname</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">eth0</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">master</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">br0</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>建议禁用网桥接口 IPv4 和 IPv6,网桥接口仅作为二层设备使用:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">modify</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">br0</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ipv4.method</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">disable</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">modify</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">br0</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ipv6.method</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">disable</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>激活网桥和物理接口:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">up</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">br0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">up</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">eth0</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="验证桥接配置">验证桥接配置<a aria-hidden="true" tabindex="-1" href="#验证桥接配置" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>检查网桥接口状态</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">connection</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">show</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">nmcli</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">device</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span></span></code></pre></div>
|
|||
|
<p>应该看到类似以下的输出,表示网桥 <code>br0</code> 和物理接口 <code>eth0</code> 已经配置并启用:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="text" data-theme="default"><code data-language="text" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">NAME UUID TYPE DEVICE</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">br0 12345678-1234-1234-1234-123456789abc bridge br0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">eth0 87654321-4321-4321-4321-abcdefabcdef ethernet eth0</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="配置-libvirt-使用桥接网络">配置 Libvirt 使用桥接网络<a aria-hidden="true" tabindex="-1" href="#配置-libvirt-使用桥接网络" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>编辑或创建网络配置文件</strong>:例如 <code>/etc/libvirt/qemu/networks/bridged.xml</code>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="xml" data-theme="default"><code data-language="xml" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);"><</span><span style="color:var(--shiki-token-string-expression);">network</span><span style="color:var(--shiki-color-text);">></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> <</span><span style="color:var(--shiki-token-string-expression);">name</span><span style="color:var(--shiki-color-text);">>bridged</</span><span style="color:var(--shiki-token-string-expression);">name</span><span style="color:var(--shiki-color-text);">></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> <</span><span style="color:var(--shiki-token-string-expression);">forward</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">mode</span><span style="color:var(--shiki-color-text);">=</span><span style="color:var(--shiki-token-string-expression);">'bridge'</span><span style="color:var(--shiki-color-text);">/></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> <</span><span style="color:var(--shiki-token-string-expression);">bridge</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">name</span><span style="color:var(--shiki-color-text);">=</span><span style="color:var(--shiki-token-string-expression);">'br0'</span><span style="color:var(--shiki-color-text);">/></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"></</span><span style="color:var(--shiki-token-string-expression);">network</span><span style="color:var(--shiki-color-text);">></span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>定义并启动该网络</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">net-define</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/etc/libvirt/qemu/networks/bridged.xml</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">net-start</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridged</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">net-autostart</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridged</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>通过这些步骤,您可以在 Ubuntu 和 CentOS 系列系统中使用 <code>nmcli</code> 来统一配置和管理网络桥接。这确保了配置的一致性,并简化了网络管理。</p>
|
|||
|
<h3 id="配置存储池和存储卷">配置存储池和存储卷<a aria-hidden="true" tabindex="-1" href="#配置存储池和存储卷" class="internal"> §</a></h3>
|
|||
|
<p>存储池和存储卷管理是 KVM 中虚拟机存储管理的重要组成部分。存储池可以是目录、LVM 或 NFS 等类型,存储卷则是具体的磁盘映像文件。</p>
|
|||
|
<h4 id="创建存储池">创建存储池<a aria-hidden="true" tabindex="-1" href="#创建存储池" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>目录类型存储池</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>创建目录:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">mkdir</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-p</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/var/lib/libvirt/images</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>定义存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-define-as</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--type</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dir</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--target</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/var/lib/libvirt/images</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>启动并自动启动存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-start</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-autostart</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>LVM 类型存储池</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>创建物理卷和卷组:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pvcreate</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/sdX</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vgcreate</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vg_kvm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/sdX</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>定义存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-define-as</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lvm-pool</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--type</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">logical</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--source-name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vg_kvm</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>启动并自动启动存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-start</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lvm-pool</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-autostart</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lvm-pool</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>NFS 类型存储池</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>安装 NFS 客户端:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>在 Ubuntu 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-common</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 CentOS/RHEL 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-utils</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>定义存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-define-as</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-pool</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">netfs</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--source-host</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-server</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--source-path</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/path/to/nfs/export</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--target</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/mnt/nfs</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>启动并自动启动存储池:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-start</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-pool</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pool-autostart</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nfs-pool</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="管理存储卷">管理存储卷<a aria-hidden="true" tabindex="-1" href="#管理存储卷" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>创建存储卷</strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>在默认存储池中创建一个新的存储卷:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vol-create-as</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">new-volume</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-token-string);">G</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 LVM 存储池中创建一个新的逻辑卷:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vol-create-as</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lvm-pool</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lv_new-volume</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-token-string);">G</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>查看存储卷</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vol-list</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vol-list</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lvm-pool</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>删除存储卷</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virsh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vol-delete</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">new-volume</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--pool</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">default</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>通过以上配置步骤,可以成功配置 KVM 的网络桥接、存储池和存储卷,确保虚拟机具备网络连接和存储资源。</p>
|
|||
|
<h2 id="创建和管理虚拟机">创建和管理虚拟机<a aria-hidden="true" tabindex="-1" href="#创建和管理虚拟机" class="internal"> §</a></h2>
|
|||
|
<h3 id="使用命令行工具创建虚拟机">使用命令行工具创建虚拟机<a aria-hidden="true" tabindex="-1" href="#使用命令行工具创建虚拟机" class="internal"> §</a></h3>
|
|||
|
<h4 id="使用-virt-install-创建虚拟机">使用 <code>virt-install</code> 创建虚拟机<a aria-hidden="true" tabindex="-1" href="#使用-virt-install-创建虚拟机" class="internal"> §</a></h4>
|
|||
|
<p><code>virt-install</code> 是一个命令行工具,用于创建虚拟机。它支持多种参数,可以灵活配置虚拟机的各种选项。</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>创建基本虚拟机</strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-install</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">myvm</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--ram</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2048</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--disk</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">path=/var/lib/libvirt/images/myvm.qcow2,size=</span><span style="color:var(--shiki-token-constant);">20</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--vcpus</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--os-type</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">linux</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--os-variant</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ubuntu20.04</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--network</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bridge=br0</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--graphics</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vnc</span><span style="color:var(--shiki-color-text);"> \</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--cdrom</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/path/to/ubuntu-20.04.iso</span></span></code></pre></div>
|
|||
|
<p>以上命令创建了一个名为 <code>myvm</code> 的虚拟机,分配 2 GB 内存、20 GB 磁盘空间和 2 个虚拟 CPU,使用桥接网络,并通过 VNC 访问图形界面。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>其他常用选项</strong>:</p>
|
|||
|
<p>在使用 <code>virt-install</code> 创建和安装虚拟机时,以下是一些常用的选项及其功能,列在一个表格中以便参考:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>功能</th></tr></thead><tbody><tr><td><code>--name</code></td><td>虚拟机的名称</td></tr><tr><td><code>--memory</code></td><td>分配给虚拟机的内存大小(单位:MB)</td></tr><tr><td><code>--vcpus</code></td><td>分配给虚拟机的虚拟 CPU 数量</td></tr><tr><td><code>--disk</code></td><td>定义虚拟机的磁盘配置</td></tr><tr><td><code>--cdrom</code></td><td>使用 CD-ROM 镜像文件作为安装源</td></tr><tr><td><code>--location</code></td><td>网络安装源的位置(URL 或本地路径)</td></tr><tr><td><code>--os-type</code></td><td>指定操作系统类型(如 <code>linux</code>、<code>windows</code>)</td></tr><tr><td><code>--os-variant</code></td><td>指定操作系统版本</td></tr><tr><td><code>--graphics</code></td><td>配置图形显示方式</td></tr><tr><td><code>--network</code></td><td>配置虚拟机的网络接口</td></tr><tr><td><code>--extra-args</code></td><td>向内核传递额外的启动参数</td></tr><tr><td><code>--boot</code></td><td>配置引导设备和引导顺序</td></tr><tr><td><code>--console</code></td><td>配置控制台类型</td></tr><tr><td><code>--import</code></td><td>从现有磁盘映像文件导入虚拟机</td></tr><tr><td><code>--autostart</code></td><td>安装后自动启动虚拟机</td></tr><tr><td><code>--cpu</code></td><td>配置 CPU 相关选项</td></tr><tr><td><code>--check</code></td><td>启用或禁用安装前的配置检查</td></tr><tr><td><code>--virt-type</code></td><td>指定虚拟化类型(如 <code>kvm</code>、<code>xen</code>)</td></tr><tr><td><code>--initrd-inject</code></td><td>指定需要注入到 initrd 的文件</td></tr><tr><td><code>--metadata</code></td><td>定义虚拟机的元数据</td></tr><tr><td><code>--noautoconsole</code></td><td>安装后不自动连接到控制台</td></tr><tr><td><code>--wait</code></td><td>等待安装完成的时间(单位:分钟)</td></tr></tbody></table>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="使用-virsh-管理虚拟机">使用 <code>virsh</code> 管理虚拟机<a aria-hidden="true" tabindex="-1" href="#使用-virsh-管理虚拟机" class="internal"> §</a></h4>
|
|||
|
<p><code>virsh</code> 是一个强大的命令行工具,用于管理和控制虚拟机。以下是一些常用的命令及其功能,列在一个表格中以便参考:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td><code>virsh list</code></td><td>列出当前运行的虚拟机</td></tr><tr><td><code>virsh list --all</code></td><td>列出所有虚拟机,包括停止的</td></tr><tr><td><code>virsh start <vm_name></code></td><td>启动指定的虚拟机</td></tr><tr><td><code>virsh shutdown <vm_name></code></td><td>关闭指定的虚拟机</td></tr><tr><td><code>virsh reboot <vm_name></code></td><td>重启指定的虚拟机</td></tr><tr><td><code>virsh destroy <vm_name></code></td><td>强制关闭指定的虚拟机</td></tr><tr><td><code>virsh suspend <vm_name></code></td><td>暂停指定的虚拟机</td></tr><tr><td><code>virsh resume <vm_name></code></td><td>恢复暂停的虚拟机</td></tr><tr><td><code>virsh create <xml_file></code></td><td>从 XML 配置文件创建并启动虚拟机</td></tr><tr><td><code>virsh define <xml_file></code></td><td>定义但不启动虚拟机</td></tr><tr><td><code>virsh undefine <vm_name></code></td><td>删除虚拟机定义,但不删除磁盘文件</td></tr><tr><td><code>virsh edit <vm_name></code></td><td>编辑虚拟机的 XML 配置</td></tr><tr><td><code>virsh dumpxml <vm_name></code></td><td>输出指定虚拟机的 XML 配置</td></tr><tr><td><code>virsh attach-disk <vm_name> <disk_path> <target_dev></code></td><td>附加磁盘到虚拟机</td></tr><tr><td><code>virsh detach-disk <vm_name> <target_dev></code></td><td>从虚拟机分离磁盘</td></tr><tr><td><code>virsh attach-interface <vm_name> <type> <source></code></td><td>附加网络接口到虚拟机</td></tr><tr><td><code>virsh detach-interface <vm_name> <type> <mac></code></td><td>从虚拟机分离网络接口</td></tr><tr><td><code>virsh snapshot-create-as <vm_name> <snapshot_name></code></td><td>创建虚拟机快照</td></tr><tr><td><code>virsh snapshot-list <vm_name></code></td><td>列出虚拟机的所有快照</td></tr><tr><td><code>virsh snapshot-revert <vm_name> <snapshot_name></code></td><td>恢复到指定快照</td></tr><tr><td><code>virsh migrate <vm_name> <dest_uri></code></td><td>迁移虚拟机到另一个主机</td></tr><tr><td><code>virsh console <vm_name></code></td><td>连接到虚拟机的控制台</td></tr><tr><td><code>virsh dominfo <vm_name></code></td><td>显示虚拟机的详细信息</td></tr><tr><td><code>virsh domstats <vm_name></code></td><td>显示虚拟机的统计信息</td></tr></tbody></table>
|
|||
|
<h3 id="使用图形化工具创建虚拟机">使用图形化工具创建虚拟机<a aria-hidden="true" tabindex="-1" href="#使用图形化工具创建虚拟机" class="internal"> §</a></h3>
|
|||
|
<h4 id="安装和使用-virt-manager">安装和使用 <code>virt-manager</code><a aria-hidden="true" tabindex="-1" href="#安装和使用-virt-manager" class="internal"> §</a></h4>
|
|||
|
<p><code>virt-manager</code> 是一个基于图形界面的虚拟机管理工具,适合那些更喜欢图形界面操作的用户。</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>安装 <code>virt-manager</code></strong>:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>在 Ubuntu 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 CentOS/RHEL 上:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">dnf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>启动 <code>virt-manager</code></strong>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">virt-manager</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<h4 id="通过-virt-manager-创建和配置虚拟机">通过 <code>virt-manager</code> 创建和配置虚拟机<a aria-hidden="true" tabindex="-1" href="#通过-virt-manager-创建和配置虚拟机" class="internal"> §</a></h4>
|
|||
|
<ol>
|
|||
|
<li><strong>创建新虚拟机</strong>:
|
|||
|
<ul>
|
|||
|
<li>打开 <code>virt-manager</code>,点击左上角的“创建新虚拟机”按钮。</li>
|
|||
|
<li>选择安装来源(例如,ISO 映像或 PXE 网络引导),然后点击“转到下一步”。</li>
|
|||
|
<li>设置虚拟机名称、内存和 CPU 配置,然后点击“转到下一步”。</li>
|
|||
|
<li>配置虚拟机硬盘(例如,创建新磁盘映像或导入现有磁盘),然后点击“转到下一步”。</li>
|
|||
|
<li>选择网络配置(例如,使用桥接网络),然后点击“转到下一步”。</li>
|
|||
|
<li>检查配置并点击“完成”以创建虚拟机。</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li><strong>配置虚拟机</strong>:
|
|||
|
<ul>
|
|||
|
<li>右键点击已创建的虚拟机,选择“打开”以启动虚拟机。</li>
|
|||
|
<li>在虚拟机启动后,可以通过 VNC 或 SPICE 连接访问虚拟机的图形界面。</li>
|
|||
|
<li>可以通过“虚拟机”菜单下的“编辑”选项修改虚拟机配置,如增加内存、添加硬盘或修改网络设置。</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>通过上述方法,可以使用命令行工具和图形化工具创建和管理 KVM 虚拟机。无论是命令行操作还是图形界面,都提供了丰富的选项和灵活的配置,使得 KVM 虚拟机的管理变得简单高效。</p></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#环境准备" data-for="环境准备">环境准备</a></li><li class="depth-1"><a href="#硬件要求" data-for="硬件要求">硬件要求</a></li><li class="depth-1"><a href="#操作系统要求" data-for="操作系统要求">操作系统要求</a></li><li class="depth-0"><a href="#安装-kvm-和-qemu" data-for="安装-kvm-和-qemu">安装 KVM 和 QEMU</a></li><li class="depth-1"><a href="#在-ubuntu-上安装" data-for="在-ubuntu-上安装">在 Ubuntu 上安装</a></li><li class="depth-1"><a href="#安装虚拟机管理工具" data-for="安装虚拟机管理工具">安装虚拟机管理工具</a></li><li class="depth-1"><a href="#验证安装" data-for="验证安装">验证安装</a></li><li class="depth-0"><a href="#基本配置" data-for="基本配置">基本配置</a></li><li class="depth-1"><a href="#配置网络桥接" data-for="配置网络桥接">配置网络桥接</a></li><li class="depth-1"><a href="#配置存储池和存储卷" data-for="配置存储池和存储卷">配置存储池和存储卷</a></li><li class="depth-0"><a href="#创建和管理虚拟机" data-for="创建和管理虚拟机">创建和管理虚拟机</a></li><li class="depth-1"><a href="#使用命令行工具创建虚拟机" data-for="使用命令行工具创建虚拟机">使用命令行工具创建虚拟机</a></li><li class="depth-1"><a href="#使用图形化工具创建虚拟机" data-for="使用图形化工具创建虚拟机">使用图形化工具创建虚拟机</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Basics","collapsed":true},{"path":"Basics/english","collapsed":true},{"path":"Blog","collapsed":true},{"path":"Book","collapsed":true},{"path":"Book/个人成长","collapsed":true},{"path":"Book/医学健康","collapsed":true},{"path":"Book/历史","collapsed":true},{"path":"Book/哲学宗教","collapsed":true},{"path":"Book/心理","collapsed":true},{"path":"Book/政治军事","collapsed":true},{"path":"Book/教育学习","collapsed":true},{"path":"Book/文学","collapsed":true},{"path":"Book/生活百科","collapsed":true},{"path":"Book/社会文化","collapsed":true},{"path":"Book/科学技术","collapsed":true},{"path":"Book/经济理财","collapsed":true},{"path":"Book/艺术","collapsed":true},{"path":"Book/计算机","collapsed":true},{"path":"Company","collapsed":true},{"path":"Company/信息化","co
|
|||
|
function toggleCallout() {
|
|||
|
const outerBlock = this.parentElement;
|
|||
|
outerBlock.classList.toggle(`is-collapsed`);
|
|||
|
const collapsed = outerBlock.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
|
|||
|
outerBlock.style.maxHeight = height + `px`;
|
|||
|
let current = outerBlock;
|
|||
|
let parent = outerBlock.parentElement;
|
|||
|
while (parent) {
|
|||
|
if (!parent.classList.contains(`callout`)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
const collapsed2 = parent.classList.contains(`is-collapsed`);
|
|||
|
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
|
|||
|
parent.style.maxHeight = height2 + `px`;
|
|||
|
current = parent;
|
|||
|
parent = parent.parentElement;
|
|||
|
}
|
|||
|
}
|
|||
|
function setupCallout() {
|
|||
|
const collapsible = document.getElementsByClassName(
|
|||
|
`callout is-collapsible`
|
|||
|
);
|
|||
|
for (const div of collapsible) {
|
|||
|
const title = div.firstElementChild;
|
|||
|
if (title) {
|
|||
|
title.removeEventListener(`click`, toggleCallout);
|
|||
|
title.addEventListener(`click`, toggleCallout);
|
|||
|
const collapsed = div.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? title.scrollHeight : div.scrollHeight;
|
|||
|
div.style.maxHeight = height + `px`;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
document.addEventListener(`nav`, setupCallout);
|
|||
|
window.addEventListener(`resize`, setupCallout);
|
|||
|
</script><script type="module">
|
|||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
});
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
});
|
|||
|
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|