构建LVM
<p class="shortdesc"></p>
<p class="p">从上节的LVM架构图中可以看出,使用LVM管理云硬盘的基本操作步骤如下:</p>
<ol class="ol" id="BuildLVM__ol_yqt_vss_slb">
<li class="li"><a class="xref" href="#BuildLVM__p_xhm_xvj_tlb">安装LVM</a>。</li>
<li class="li"><a class="xref" href="#BuildLVM__p_sx1_jsj_tlb">将云硬盘创建为物理卷</a>。</li>
<li class="li"><a class="xref" href="#BuildLVM__p_tnv_xvj_tlb">将多个物理卷组合成卷组</a>。</li>
<li class="li"><a class="xref" href="#BuildLVM__p_cgc_yvj_tlb">在卷组中创建逻辑卷</a>。</li>
<li class="li"><a class="xref" href="#BuildLVM__p_hhh_yvj_tlb">在逻辑卷之上创建文件系统</a>。</li>
</ol>
<section class="section" id="BuildLVM__section_lcb_mx3_tlb"><h2 class="doc-tairway">背景信息</h2>
<p class="p">本文以/dev/vdb、/dev/vdc和/dev/vdd三块云硬盘为例说明如何创建LVM。如下图所示,系统已经挂载了三块云硬盘,大小是20GB。</p>
<img class="image" id="BuildLVM__image_t3n_xrj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171842-1c6dd8cc96cc.png">
</section>
<section class="section" id="BuildLVM__section_gkz_mx3_tlb"><h2 class="doc-tairway">前提条件</h2>
<p class="p">已经完成云硬盘的创建,并且挂载在对应的云服务器上。请参见<span class="xref">创建云硬盘</span>。</p>
</section>
<section class="section" id="BuildLVM__section_kdt_nx3_tlb"><h2 class="doc-tairway">注意事项</h2>
<p class="p">在单台实例已挂载多块云硬盘创建LVM时,由于快照只能针对独立的每个云硬盘创建,当使用LVM后,使用快照回滚云盘时会造成数据差异。</p>
</section>
<section class="section" id="BuildLVM__section_cj2_4x3_tlb"><h2 class="doc-tairway">操作步骤</h2>
<p class="p" id="BuildLVM__p_xhm_xvj_tlb"><strong class="ph b">步骤一:安装LVM</strong></p>
<ol class="ol" id="BuildLVM__ol_jgv_csj_tlb">
<li class="li">执行如下命令,检查系统是否安装过LVM。<pre class="pre codeblock"><code>rpm -qa |grep lvm2</code></pre><ul class="ul" id="BuildLVM__ul_j5w_2sj_tlb">
<li class="li">如果出现如下显示信息,则说明已经安装,可以跳过步骤一,直接执行<a class="xref" href="#BuildLVM__p_sx1_jsj_tlb">步骤二</a>。<img class="image" id="BuildLVM__image_gyq_kwj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171842-1d8bac479fa9.png"></li>
<li class="li">如果没有出现如上显示信息,则执行<a class="xref" href="#BuildLVM__p_sx1_jsj_tlb">步骤二</a>。</li>
</ul></li>
<li class="li">执行如下命令,安装LVM。<pre class="pre codeblock"><code>yum install lvm2</code></pre><p class="p">执行后,若出现如下信息,则说明安装成功。</p><img class="image" id="BuildLVM__image_izf_mwj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-1796ffd39d89.png"></li>
</ol>
<p class="p" id="BuildLVM__p_sx1_jsj_tlb"><strong class="ph b">步骤二:将云硬盘创建为物理卷</strong></p>
<ol class="ol" id="BuildLVM__ol_m4t_wtj_tlb">
<li class="li">以root用户登录云服务器。</li>
<li class="li">执行如下命令,查看当前设备挂载的云硬盘。<pre class="pre codeblock"><code>fdisk -l</code></pre><img class="image" id="BuildLVM__image_jfg_nwj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-1583dc539699.png"><p class="p">从上述的显示信息中我们可以看出,当前系统挂载了三块云硬盘,分别是/dev/vdb、/dev/vdc和/dev/vdd。</p></li>
<li class="li">创建物理卷,将希望添加到卷组的所有分区或者云硬盘创建为物理卷。将整个硬盘创建为物理卷的命令为:<pre class="pre codeblock"><code>pvcreate <磁盘设备名1> <磁盘设备名2 > … <磁盘设备名n></code></pre><p class="p">多个磁盘之间用空格来分隔。</p><p class="p">本示例中,执行<code class="ph codeph">pvcreate
/dev/vdb /dev/vdc /dev/vdd</code>命令,显示如下。</p><img class="image" id="BuildLVM__image_f2h_4wj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-1ca60be79059.png"></li>
<li class="li">执行如下命令,查看新创建的物理卷详细信息。<pre class="pre codeblock"><code>pvdisplay</code></pre><img class="image" id="BuildLVM__image_ctx_4wj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-1a5b19799882.png"><p class="p">从显示信息中,我们可以看到新创建的三个物理卷,分别是/dev/vdb、/dev/vdc 、/dev/vdd。</p></li>
</ol>
<p class="p" id="BuildLVM__p_tnv_xvj_tlb"><strong class="ph b">步骤三:创建卷组</strong></p>
<ol class="ol" id="BuildLVM__ol_ghn_25j_tlb">
<li class="li">将多个物理卷创建为一个卷组。<pre class="pre codeblock"><code>vgcreate <卷组名> <物理卷名称1> <物理卷名称2> … <物理卷名称n></code></pre><p class="p">本示例中,执行
<code class="ph codeph">vgcreate lvm_example /dev/vdb /dev/vdc
/dev/vdd</code>。</p><img class="image" id="BuildLVM__image_iyz_pwj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-12234ef0992f.png"><p class="p">如果出现如上提示信息,则说明卷组创建成功。</p></li>
<li class="li"> 查看卷组详细信息<pre class="pre codeblock"><code>vgdisplay</code></pre><img class="image" id="BuildLVM__image_ccx_qwj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-1b25febc9eca.png"></li>
<li class="li">(可选)卷组创建完成后,如果想将新的物理卷添加到该卷组,可以执行如下命令。<pre class="pre codeblock"><code>vgextend <卷组名> <物理卷名称1></code></pre></li>
</ol>
<p class="p" id="BuildLVM__p_cgc_yvj_tlb"><strong class="ph b">步骤四:在卷组中创建逻辑卷</strong></p>
<ol class="ol" id="BuildLVM__ol_gqc_gvj_tlb">
<li class="li">创建逻辑卷。<pre class="pre codeblock"><code>lvcreate -L <逻辑卷大小> -n <逻辑卷名称> <卷组名称></code></pre><p class="p">本示例中,执行<code class="ph codeph">lvcreate
-L 20G -n lv1 lvm_example</code>。</p><img class="image" id="BuildLVM__image_tzc_ywj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-14acc45597b3.png"></li>
<li class="li">查看逻辑卷详情。<pre class="pre codeblock"><code>lvdisplay</code></pre><img class="image" id="BuildLVM__image_pdw_ywj_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-14b6dcdc913f.png"></li>
</ol>
<p class="p" id="BuildLVM__p_hhh_yvj_tlb"><strong class="ph b">步骤五:创建并挂载文件系统</strong></p>
<ol class="ol" id="BuildLVM__ol_k1q_kvj_tlb">
<li class="li">创建文件系统。<pre class="pre codeblock"><code>mkfs.<文件格式> <逻辑卷路径></code></pre><p class="p">本示例中,执行<code class="ph codeph">mkfs.ext3
/dev/lvm_example/lv1</code>。</p></li>
<li class="li">
创建挂载目录。<pre class="pre codeblock"><code>mkdir <挂载目录></code></pre><p class="p">示例</p><pre class="pre codeblock"><code>mkdir /Data</code></pre></li>
<li class="li">
挂载逻辑卷。<pre class="pre codeblock"><code>mount <逻辑卷路径> <挂载目录></code></pre><p class="p">示例</p><pre class="pre codeblock"><code>mount /dev/lvm_example/lv1 /Data</code></pre></li>
<li class="li">查看挂载是否成功。<pre class="pre codeblock"><code>df -h</code></pre><img class="image" id="BuildLVM__image_dyz_qn4_tlb" src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20200707171843-13a2c9b29e72.png" width="800"><p class="p"> 可以看到该逻辑卷已经成功挂载在/Data下。</p></li>
</ol>
</section>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!