磁盘扩容-分配磁盘剩余内存
<p>[TOC]</p>
<h3>说明:请谨慎使用此方法进行操作,建议做好数据备份再进行操作</h3>
<p><code>本次在CentOS7中扩容的分区是这块磁盘上的最后一个分区,也就是磁盘扩展后增加的空间是加在磁盘的最后一个分区,可以通过growpart工具修改硬盘的分区表,将这部分空间直接append到最后一个分区,如果你要扩容的分区在你的系统上并非是磁盘上的最后一个分区的话,可能无法直接无损扩充分区。</code></p>
<h2>安装扩容工具</h2>
<ul>
<li>
<p><strong>检查是否安装扩容工具</strong>
yum源:<code>rpm -qa | grep cloud-utils-growpart</code>
apt源:<code>dpkg -l | grep &quot;cloud-guest-utils&quot;</code>
<strong>[点击下载相关依赖包](<a href="http://101.69.243.254:5010/share/r1HzHXku">http://101.69.243.254:5010/share/r1HzHXku</a> "点击下载相关依赖包")</strong></p>
</li>
<li>
<p><strong>安装growpart工具</strong>
yum源:<code>yum install -y cloud-utils-growpart</code>
apt源:<code>apt-get install -y cloud-guest-utils</code></p>
</li>
<li><strong>安装gdisk工具</strong>
检查分区格式:df -Th
分区为GPT格式,必须执行此步骤:
yum源:<code>yum install -y gdisk</code>
apt源:<code>apt-get install -y gdisk</code></li>
</ul>
<h2>一、普通分区挂载在线扩容</h2>
<p><strong>扩容流程:1 扩容磁盘分区---->>2 扩容分区对应挂载的文件系统</strong></p>
<h3>1.1.查看云服务器磁盘分区情况</h3>
<p>使用<code>lsblk</code>确认云盘分区信息
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/2b9473356252e0e6c564aff477746842" alt="" />
以下命令确认已有分区的文件系统类型
<code>df -Th</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/ccdcb1ed4eb1a6000fa8732f6c657f10" alt="" />
<code>fdisk -lu</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/2dbce45f49a9d2f54de769934c97a540" alt="" /></p>
<h3>1.2.扩容分区</h3>
<p>通过上面查询的信息可以发现<code>sdb</code>还有<code>5G</code>内存未分配,现在我们需要把内存全部给<code>sdb1</code>
<code>growpart /dev/sdb 1</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/c2137c48c822a3be804a8d4f11acea27" alt="" />
<strong>问题</strong>:运行growpart /dev/sdb 1时,提示如下报错
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/145a5b02a19133ea09b189bd15c706c0" alt="" />
<strong>解决方案</strong>:运行<code>locale</code>命令查看服务器的字符编码类型
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/112c73897eeb4e53b13e773899182400" alt="" /></p>
<pre><code class="language-shell">**a**.运行以下命令,切换字符编码类型
LANG=en_US.UTF-8
**b**.如果问题仍未解决,运行以下命令,切换字符编码类型
export LC_ALL=en_US.UTF-8
**c**.如果问题仍未解决,运行以下命令,切换字符编码类型
localectl set-locale LANG=en_US.UTF-8
**d**.如果采用以上方案无法解决问题时,运行以下命令,切换字符编码类型
export LANGUAGE=en_US.UTF-8</code></pre>
<p>分区拓展完成
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/894aef109b029f4e7908bd70fb20b9ff" alt="" /></p>
<h3>1.3.扩容文件系统</h3>
<p>xfs文件系统使用<code>xfs_growfs</code>,ext文件系统使用<code>resize2fs</code>;xfs文件系统只支持增大不支持缩小</p>
<p><strong>拓展ext文件系统:<code>resize2fs /dev/sdb1</code></strong></p>
<p><strong>拓展xfs文件系统:<code>xfs_growfs /dev/sdb1</code></strong>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/ba77e4c0f7751b7df82af797ca3c936a" alt="" />
<strong>查看是否扩容成功:<code>df -Th</code></strong>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/5bb30ab9c6505dbe6d4bf520cb84044c" alt="" />
<strong>扩容完成</strong></p>
<h2>二、逻辑卷分区挂载在线扩容</h2>
<p><strong>扩容流程:1 扩容磁盘分区---->>2 扩容分区的物理卷和绑定的卷组---->>3 扩容卷组中的逻辑卷---->>4 扩容逻辑卷对应挂载的文件系统</strong></p>
<h3>2.1.查看云服务器磁盘分区情况</h3>
<p>使用<code>lsblk</code>确认云盘分区信息
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/27440b081fedd886ea96f7b9b96fec8c" alt="" />
以下命令确认已有分区的文件系统类型
<code>df -Th</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/9a4e2beb33be4319d9dd6401ab6ded30" alt="" />
<code>fdisk -lu</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/2dbce45f49a9d2f54de769934c97a540" alt="" /></p>
<h3>2.2.扩容分区</h3>
<p>通过上面查询的信息可以发现<code>sdb</code>还有<code>3G</code>内存未分配,现在我们需要把内存全部给<code>sdb1</code>
<code>growpart /dev/sdb 1</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/4d0e5e88b14bf1142a233a43a2062122" alt="" />
<strong>问题</strong>:运行growpart /dev/sdb 1时,提示如下报错
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/145a5b02a19133ea09b189bd15c706c0" alt="" />
<strong>解决方案</strong>:运行<code>locale</code>命令查看服务器的字符编码类型
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/112c73897eeb4e53b13e773899182400" alt="" /></p>
<pre><code class="language-shell">**a**.运行以下命令,切换字符编码类型
LANG=en_US.UTF-8
**b**.如果问题仍未解决,运行以下命令,切换字符编码类型
export LC_ALL=en_US.UTF-8
**c**.如果问题仍未解决,运行以下命令,切换字符编码类型
localectl set-locale LANG=en_US.UTF-8
**d**.如果采用以上方案无法解决问题时,运行以下命令,切换字符编码类型
export LANGUAGE=en_US.UTF-8</code></pre>
<h3>2.3.扩容物理卷</h3>
<p><strong>通过命令查看分区和物理卷情况:<code>lsblk</code> 和 <code>pvs</code></strong>
发现growpart扩容后只是增加了物理容量,物理卷和文件系统还是原先的大小,这时进行LVM分区与文件系统扩容操作
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/4bafaf4e9ec0a62ef0dcbae8ed633593" alt="" />
<strong>扩展物理卷:<code>pvresize /dev/sdb1</code></strong>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/0d12e9cbc5bd1bc17d9415350db587af" alt="" /></p>
<h3>2.4.扩容逻辑卷</h3>
<p><strong>再查看物理卷和卷组情况:<code>pvs</code> 和 <code>vgs</code></strong>
可以看到物理卷<code>sdb1</code>大小由<code>2G</code>变成<code>5G</code>,<code>risen</code>卷组也有<code>3G</code>空余内存
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/168e08786ae975824e2d7f844b93ead4" alt="" />
扩展逻辑卷:<code>lvextend -l +100%FREE /dev/risen/lvdata</code>
分配<code>risen</code>卷组全部可用内存到<code>lvdata</code>逻辑卷
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/eb7debe60133a6870a72f0d98ccdbf2a" alt="" /></p>
<h3>2.5.扩容文件系统</h3>
<p><strong>查看逻辑卷和挂载文件系统情况:<code>lvs</code> 和 <code>df -Th</code></strong>
逻辑卷lvdata由原来的<code>5G</code>扩展到了<code>8G</code>,但是挂载盘data文件大小还是<code>5G</code>,需要扩容文件系统<code>data</code>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/7fcdab654e3e45aa5f7ddf93206adace" alt="" />
<strong>拓展文件系统:<code>xfs_growfs /data</code></strong>
注意:xfs文件系统使用<code>xfs_growfs</code>,ext文件系统使用<code>resize2fs</code>;xfs文件系统只支持增大不支持缩小
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/1bd62bd9ee98a05f65d431f4f4ffbb9f" alt="" />
<strong>查看是否扩容成功:<code>df -Th</code></strong>
<img src="http://60.191.64.5:16100/server/index.php?s=/api/attachment/visitFile/sign/d984fc2637e8a6c0f7f092d664d1bd53" alt="" /></p>
<p><strong>扩容完成</strong></p>