如何在 Linux 中使用 LVM 创建和扩展交换分区

WindowsandLinux 2018-04-27

如何在 Linux 中使用 LVM 创建和扩展交换分区

我们使用 LVM 进行灵活的卷管理,为什么我们不能将 LVM 用于交换分区呢?

这可以让用户在需要时增加交换分区。如果你升级系统中的内存,则需要添加更多交换空间。这有助于你管理运行需要大量内存的应用的系统。

可以通过三种方式创建交换分区

  • 创建一个新的交换分区
  • 创建一个新的交换文件
  • 在现有逻辑卷(LVM)上扩展交换分区

建议创建专用交换分区而不是交换文件。

Linux 中推荐的交换大小是多少?

 

什么是交换空间

当物理内存 (RAM) 已满时,将使用 Linux 中的交换空间。当物理内存已满时,内存中的非活动页将移到交换空间。

这有助于系统连续运行应用程序,但它不能当做是更多内存的替代品。

交换空间位于硬盘上,因此它不能像物理内存那样处理请求。

 

如何使用 LVM 创建交换分区

由于我们已经知道如何创建逻辑卷,所以交换分区也是如此。只需按照以下过程。

创建你需要的逻辑卷。在我这里,我要创建 5GB 的交换分区。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvcreate </span><span class="pun">-</span><span class="pln">L </span><span class="lit">5G</span><span class="pun">-</span><span class="pln">n </span><span class="typ">LogVol_swap1</span><span class="pln"> vg00</span>
  2. <span class="typ">Logical</span><span class="pln"> volume </span><span class="str">"LogVol_swap1"</span><span class="pln"> created</span><span class="pun">.</span>

格式化新的交换空间。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span>
  2. <span class="typ">Setting</span><span class="pln"> up swapspace version </span><span class="lit">1</span><span class="pun">,</span><span class="kwd">size</span><span class="pun">=</span><span class="lit">5</span><span class="typ">GiB</span><span class="pun">(</span><span class="lit">5368705024</span><span class="pln"> bytes</span><span class="pun">)</span>
  3. <span class="kwd">no</span><span class="pln"> label</span><span class="pun">,</span><span class="pln"> UUID</span><span class="pun">=</span><span class="pln">d278e9d6</span><span class="pun">-</span><span class="lit">4c37</span><span class="pun">-</span><span class="lit">4cb0</span><span class="pun">-</span><span class="lit">83e5</span><span class="pun">-</span><span class="lit">2745ca708582</span>

将以下条目添加到 /etc/fstab 中。

  1. <span class="com">#</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
  2. <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">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span><span class="pln"> swap swap defaults </span><span class="lit">0</span><span class="lit">0</span>

启用扩展逻辑卷。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapon</span><span class="pun">-</span><span class="pln">va</span>
  2. <span class="kwd">swapon</span><span class="pun">:</span><span class="pun">/</span><span class="pln">swapfile</span><span class="pun">:</span><span class="pln"> already active </span><span class="pun">--</span><span class="pln"> ignored</span>
  3. <span class="kwd">swapon</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span><span class="pun">:</span><span class="pln"> found signature </span><span class="pun">[</span><span class="pln">pagesize</span><span class="pun">=</span><span class="lit">4096</span><span class="pun">,</span><span class="pln"> signature</span><span class="pun">=</span><span class="pln">swap</span><span class="pun">]</span>
  4. <span class="kwd">swapon</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span><span class="pun">:</span><span class="pln"> pagesize</span><span class="pun">=</span><span class="lit">4096</span><span class="pun">,</span><span class="pln"> swapsize</span><span class="pun">=</span><span class="lit">5368709120</span><span class="pun">,</span><span class="pln"> devsize</span><span class="pun">=</span><span class="lit">5368709120</span>
  5. <span class="kwd">swapon</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">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span>

测试交换空间是否已正确添加。

  1. <span class="pln">$ </span><span class="kwd">cat</span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">swaps</span>
  2. <span class="typ">Filename</span><span class="typ">Type</span><span class="typ">Size</span><span class="typ">Used</span><span class="typ">Priority</span>
  3. <span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1459804</span><span class="lit">526336</span><span class="pun">-</span><span class="lit">1</span>
  4. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">dm</span><span class="pun">-</span><span class="lit">0</span><span class="pln"> partition </span><span class="lit">5242876</span><span class="lit">0</span><span class="pun">-</span><span class="lit">2</span>
  5. <span class="pln">$ free </span><span class="pun">-</span><span class="pln">g</span>
  6. <span class="pln">total used free shared buff</span><span class="pun">/</span><span class="pln">cache available</span>
  7. <span class="typ">Mem</span><span class="pun">:</span><span class="lit">1</span><span class="lit">1</span><span class="lit">0</span><span class="lit">0</span><span class="lit">0</span><span class="lit">0</span>
  8. <span class="typ">Swap</span><span class="pun">:</span><span class="lit">6</span><span class="lit">0</span><span class="lit">6</span>

 

如何使用 LVM 扩展交换分区

只需按照以下过程来扩展 LVM 交换逻辑卷。

禁用相关逻辑卷的交换。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapoff</span><span class="pun">-</span><span class="pln">v </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span>
  2. <span class="kwd">swapoff</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span>

调整逻辑卷的大小。我将把交换空间从 5GB 增加到 11GB

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvresize </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span><span class="pun">-</span><span class="pln">L </span><span class="pun">+</span><span class="lit">6G</span>
  2. <span class="typ">Size</span><span class="pln"> of logical volume vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span><span class="pln"> changed </span><span class="kwd">from</span><span class="lit">5.00</span><span class="typ">GiB</span><span class="pun">(</span><span class="lit">1280</span><span class="pln"> extents</span><span class="pun">)</span><span class="pln"> to </span><span class="lit">11.00</span><span class="typ">GiB</span><span class="pun">(</span><span class="lit">2816</span><span class="pln"> extents</span><span class="pun">).</span>
  3. <span class="typ">Logical</span><span class="pln"> volume vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span><span class="pln"> successfully resized</span><span class="pun">.</span>

格式化新的交换空间。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span>
  2. <span class="kwd">mkswap</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">vg00</span><span class="pun">/</span><span class="typ">LogVol_swap1</span><span class="pun">:</span><span class="pln"> warning</span><span class="pun">:</span><span class="pln"> wiping old swap signature</span><span class="pun">.</span>
  3. <span class="typ">Setting</span><span class="pln"> up swapspace version </span><span class="lit">1</span><span class="pun">,</span><span class="kwd">size</span><span class="pun">=</span><span class="lit">11</span><span class="typ">GiB</span><span class="pun">(</span><span class="lit">11811155968</span><span class="pln"> bytes</span><span class="pun">)</span>
  4. <span class="kwd">no</span><span class="pln"> label</span><span class="pun">,</span><span class="pln"> UUID</span><span class="pun">=</span><span class="lit">2e3b2ee0</span><span class="pun">-</span><span class="pln">ad0b</span><span class="pun">-</span><span class="lit">402c</span><span class="pun">-</span><span class="pln">bd12</span><span class="pun">-</span><span class="lit">5a9431b73623</span>

启用扩展逻辑卷。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapon</span><span class="pun">-</span><span class="pln">va</span>
  2. <span class="kwd">swapon</span><span class="pun">:</span><span class="pun">/</span><span class="pln">swapfile</span><span class="pun">:</span><span class="pln"> already active </span><span class="pun">--</span><span class="pln"> ignored</span>
  3. <span class="kwd">swapon</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span><span class="pun">:</span><span class="pln"> found signature </span><span class="pun">[</span><span class="pln">pagesize</span><span class="pun">=</span><span class="lit">4096</span><span class="pun">,</span><span class="pln"> signature</span><span class="pun">=</span><span class="pln">swap</span><span class="pun">]</span>
  4. <span class="kwd">swapon</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span><span class="pun">:</span><span class="pln"> pagesize</span><span class="pun">=</span><span class="lit">4096</span><span class="pun">,</span><span class="pln"> swapsize</span><span class="pun">=</span><span class="lit">11811160064</span><span class="pun">,</span><span class="pln"> devsize</span><span class="pun">=</span><span class="lit">11811160064</span>
  5. <span class="kwd">swapon</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">vg00</span><span class="pun">-</span><span class="typ">LogVol_swap1</span>

测试逻辑卷是否已正确扩展。

  1. <span class="pln">$ free </span><span class="pun">-</span><span class="pln">g</span>
  2. <span class="pln">total used free shared buff</span><span class="pun">/</span><span class="pln">cache available</span>
  3. <span class="typ">Mem</span><span class="pun">:</span><span class="lit">1</span><span class="lit">1</span><span class="lit">0</span><span class="lit">0</span><span class="lit">0</span><span class="lit">0</span>
  4. <span class="typ">Swap</span><span class="pun">:</span><span class="lit">12</span><span class="lit">0</span><span class="lit">12</span>
  5. <span class="pln">$ </span><span class="kwd">cat</span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">swaps</span>
  6. <span class="typ">Filename</span><span class="typ">Type</span><span class="typ">Size</span><span class="typ">Used</span><span class="typ">Priority</span>
  7. <span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1459804</span><span class="lit">237024</span><span class="pun">-</span><span class="lit">1</span>
  8. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">dm</span><span class="pun">-</span><span class="lit">0</span><span class="pln"> partition </span><span class="lit">11534332</span><span class="lit">0</span><span class="pun">-</span><span class="lit">2</span>

via: https://www.2daygeek.com/how-to-create-extend-swap-partition-in-linux-using-lvm/

作者:Ramya Nuvvula 译者:geekpi 校对:wxy

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

相关推荐