如何在 CentOS 7 中添加新磁盘而不用重启系统

对大多数系统管理员来说扩充 Linux 服务器的磁盘空间是日常的工作之一。因此这篇文章会通过使用 Linux 命令,在 CentOS 7 系统上演示一些简单的操作步骤来扩充您的磁盘空间而不需要重启您的生产服务器。关于扩充和增加新的磁盘到 Linux 系统,我们会提及多种方法和多种可行性,可按您所需选择最适用的一种。

如何在 CentOS 7 中添加新磁盘而不用重启系统

 

1. 在虚拟机客户端扩充磁盘空间:

在为 Linux 系统增加磁盘卷之前,您首先需要添加一块新的物理磁盘,或在 VMware vShere、VMware 工作站以及你使用的其它虚拟环境软件中进行设置来增加一块虚拟磁盘的容量。

如何在 CentOS 7 中添加新磁盘而不用重启系统

Increase disk

 

2. 检查磁盘空间:

运行如下命令来检查当前磁盘空间大小。

  1. <span class="com">#</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h</span>
  2. <span class="com">#</span><span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

Fdisk check

可以看到,虽然我们已经在后端给其增加到 50 GB 的空间,但此时的总磁盘大小仍然为 10 GB。

 

3. 扩展空间而无需重启虚拟机

现在运行如下命令,通过重新扫描 SCSI (注:Small Computer System Interface 小型计算机系统接口)总线并添加 SCSI 设备,系统就可以扩展操作系统的物理卷磁盘空间,而且不需要重启虚拟机。

  1. <span class="com">#</span><span class="kwd">ls</span><span class="pun">/</span><span class="pln">sys</span><span class="pun">/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span>
  2. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host0</span><span class="pun">/</span><span class="pln">scan</span>
  3. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host1</span><span class="pun">/</span><span class="pln">scan</span>
  4. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host2</span><span class="pun">/</span><span class="pln">scan</span>

使用下面的命令来检查 SCSI 设备的名称,然后重新扫描 SCSI 总线。

  1. <span class="com">#</span><span class="kwd">ls</span><span class="pun">/</span><span class="pln">sys</span><span class="pun">/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span>
  2. <span class="com">#</span><span class="kwd">echo</span><span class="lit">1</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pun">/</span><span class="pln">device</span><span class="pun">/</span><span class="pln">rescan</span>
  3. <span class="com">#</span><span class="kwd">echo</span><span class="lit">1</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span><span class="lit">2</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pun">/</span><span class="pln">device</span><span class="pun">/</span><span class="pln">rescan</span>

如下图所示,会重新扫描 SCSI 总线,随后我们在虚拟机客户端设置的磁盘大小会正常显示。

如何在 CentOS 7 中添加新磁盘而不用重启系统

Rescan disk device

 

4. 创建新磁盘分区:

一旦在系统中可以看到扩展的磁盘空间,就可以运行如下命令来格式化您的磁盘以创建一个新的分区。请按如下操作步骤来扩充您的物理磁盘卷。

  1. <span class="com">#</span><span class="kwd">fdisk</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span>
  2. <span class="typ">Welcome</span><span class="pln"> to </span><span class="kwd">fdisk</span><span class="pun">(</span><span class="pln">util</span><span class="pun">-</span><span class="pln">linux </span><span class="lit">2.23</span><span class="pun">.</span><span class="lit">2</span><span class="pun">)</span><span class="pln"> press the </span><span class="str">'m'</span><span class="pln"> key </span><span class="kwd">for</span><span class="pln"> help</span>
  3. <span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> m</span>
  4. <span class="typ">Command</span><span class="pln"> action</span>
  5. <span class="pln">a toggle a bootable flag</span>
  6. <span class="pln">b edit bsd disklabel</span>
  7. <span class="pln">c toggle the dos compatibility flag</span>
  8. <span class="pln">d </span><span class="kwd">delete</span><span class="pln"> a partition</span>
  9. <span class="pln">g create a </span><span class="kwd">new</span><span class="kwd">empty</span><span class="pln"> GPT partition table</span>
  10. <span class="pln">G create an IRIX </span><span class="pun">(</span><span class="pln">SGI</span><span class="pun">)</span><span class="pln"> partition table</span>
  11. <span class="pln">l </span><span class="kwd">list</span><span class="pln"> known partition types</span>
  12. <span class="pln">m </span><span class="kwd">print</span><span class="kwd">this</span><span class="pln"> menu</span>
  13. <span class="pln">n add a </span><span class="kwd">new</span><span class="pln"> partition</span>
  14. <span class="pln">o create a </span><span class="kwd">new</span><span class="kwd">empty</span><span class="pln"> DOS partition table</span>
  15. <span class="pln">p </span><span class="kwd">print</span><span class="pln"> the partition table</span>
  16. <span class="pln">q quit without saving changes</span>
  17. <span class="pln">s create a </span><span class="kwd">new</span><span class="kwd">empty</span><span class="typ">Sun</span><span class="pln"> disklabel</span>
  18. <span class="pln">t change a partition</span><span class="str">'s system id</span>
  19. <span class="str">u change display/entry units</span>
  20. <span class="str">v verify the partition table</span>
  21. <span class="str">w write table to disk and exit</span>
  22. <span class="str">x extra functionality (experts only)</span>
  23. <span class="str">Command (m for help):</span>

键入 'p' 来查看当前的分区表信息,然后键入 'n' 键来创建一个新的主分区,选择所有可用的扇区。 使用 't' 命令改变磁盘类型为 'Linux LVM',然后选择编码 '8e' 或者默认不选,它默认的类型编码为 '83'。

现在输入 'w' 来保存分区表信息并且退出命令环境,如下示:

  1. <span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="kwd">w</span>
  2. <span class="typ">The</span><span class="pln"> partition table has been altered</span><span class="pun">!</span>
  3. <span class="typ">Calling</span><span class="pln"> ioctl</span><span class="pun">()</span><span class="pln"> to re</span><span class="pun">-</span><span class="pln">read partition table</span><span class="pun">.</span>
  4. <span class="pln">WARNING</span><span class="pun">:</span><span class="typ">Re</span><span class="pun">-</span><span class="pln">reading the partition table failed </span><span class="kwd">with</span><span class="pln"> error </span><span class="lit">16</span><span class="pun">:</span><span class="typ">Device</span><span class="kwd">or</span><span class="pln"> resource busy</span><span class="pun">.</span>
  5. <span class="typ">The</span><span class="pln"> kernel still uses the old table</span><span class="pun">.</span><span class="typ">The</span><span class="kwd">new</span><span class="pln"> table will be used at</span>
  6. <span class="pln">the </span><span class="kwd">next</span><span class="kwd">reboot</span><span class="kwd">or</span><span class="pln"> after you run partprobe</span><span class="pun">(</span><span class="lit">8</span><span class="pun">)</span><span class="kwd">or</span><span class="pln"> kpartx</span><span class="pun">(</span><span class="lit">8</span><span class="pun">)</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

New disk Volume

 

5. 创建物理��:

根据上述提示,运行 'partprob' 或 'kpartx' 命令以使分区表生效,然后使用如下的命令来创建新的物理卷。

  1. <span class="com">#</span><span class="pln"> partprobe</span>
  2. <span class="com">#</span><span class="pln"> pvresize </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda3</span>

要检查新创建的卷,运行如下的命令可以看出新的物理卷是否已经被创建,是否可用。接下来,我们就可以使用这个新的物理卷来扩展 'centos' 卷组了,如下示:

  1. <span class="com">#</span><span class="pln"> pvdisplay</span>
  2. <span class="com">#</span><span class="pln"> vgextend centos </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda3</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

Extend volume Group

 

6. 扩展逻辑卷:

现在我们使用如下的命令扩展逻辑卷,以增加我们系统正使用的磁盘空间。

  1. <span class="com">#</span><span class="pln"> lvextend </span><span class="pun">-</span><span class="pln">L </span><span class="pun">+</span><span class="lit">40G</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">centos</span><span class="pun">-</span><span class="pln">root</span>

一旦返回增加成功的消息,就可以运行如下命令来扩展您的逻辑卷大小。

  1. <span class="com">#</span><span class="pln"> xfs_growfs </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">centos</span><span class="pun">-</span><span class="pln">root</span>

'/' 分区的大小已经成功的增加了,可以使用 'df' 命令来检查您磁盘驱动器的大小。如图示。

如何在 CentOS 7 中添加新磁盘而不用重启系统

Increase disk space

 

7. 通过增加新的磁盘来扩充根分区而不用重启系统:

这是第二种方法,它使用的命令非常简单, 用来增加 CentOS 7 系统上逻辑卷空间大小。

所以第一步是打开您的虚拟机客户端的设置页面,点击 ‘增加’ 按纽,然后继续下一步操作。

如何在 CentOS 7 中添加新磁盘而不用重启系统

Add new disk

选择新磁盘所需要的配置信息,如下图所示的,选择新磁盘的大小和它的类型。

如何在 CentOS 7 中添加新磁盘而不用重启系统

New disk setup

然后进入服务端重复如下的命令来扫描您的磁盘设备,以使新磁盘在系统中可见。

  1. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host0</span><span class="pun">/</span><span class="pln">scan</span>
  2. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host1</span><span class="pun">/</span><span class="pln">scan</span>
  3. <span class="com">#</span><span class="kwd">echo</span><span class="str">"- - -"</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_host</span><span class="pun">/</span><span class="pln">host2</span><span class="pun">/</span><span class="pln">scan</span>

列出您的 SCSI 设备的名称:

  1. <span class="com">#</span><span class="kwd">ls</span><span class="pun">/</span><span class="pln">sys</span><span class="pun">/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span>
  2. <span class="com">#</span><span class="kwd">echo</span><span class="lit">1</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span><span class="lit">1</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pun">/</span><span class="pln">device</span><span class="pun">/</span><span class="pln">rescan</span>
  3. <span class="com">#</span><span class="kwd">echo</span><span class="lit">1</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span><span class="lit">2</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pun">/</span><span class="pln">device</span><span class="pun">/</span><span class="pln">rescan</span>
  4. <span class="com">#</span><span class="kwd">echo</span><span class="lit">1</span><span class="pun">></span><span class="str">/sys/</span><span class="kwd">class</span><span class="pun">/</span><span class="pln">scsi_device</span><span class="pun">/</span><span class="lit">3</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pln">\:</span><span class="lit">0</span><span class="pun">/</span><span class="pln">device</span><span class="pun">/</span><span class="pln">rescan</span>
  5. <span class="com">#</span><span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

Scanning new disk

一旦新增的磁盘可见,就可以运行下面的命令来创建新的物理卷,然后增加到卷组,如下示。

  1. <span class="com">#</span><span class="pln"> pvcreate </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb</span>
  2. <span class="com">#</span><span class="pln"> vgextend centos </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb</span>
  3. <span class="com">#</span><span class="pln"> vgdisplay</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

Extending Volume Group

现在根据此磁盘的空间大小来扩展逻辑卷,然后添加到根分区。

  1. <span class="com">#</span><span class="pln"> lvextend </span><span class="pun">-</span><span class="pln">L </span><span class="pun">+</span><span class="lit">20G</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">centos</span><span class="pun">-</span><span class="pln">root</span>
  2. <span class="com">#</span><span class="pln"> xfs_growfs </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">centos</span><span class="pun">-</span><span class="pln">root</span>
  3. <span class="com">#</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h</span>

如何在 CentOS 7 中添加新磁盘而不用重启系统

Increase / Partition

 

结论:

在 Linux CentOS 7 系统上管理磁盘分区的操作过程是非常简单的,可以使用这篇文章所述的操作步骤来扩充您的任意逻辑卷的磁盘空间。您不需要重启生产线上的服务器,只是简单的重扫描下 SCSI 设备,和扩展您想要的 LVM(逻辑卷管理)。我们希望这文章对您有用。请随意的发表有用的评论和建议。


via: http://linoxide.com/linux-how-to/add-new-disk-centos-7-without-rebooting/

作者:Kashif S 译者:runningwater 校对: wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐