yegen00 2017-07-16
用户可以在任何 Linux 操作系统的安装过程中或者是其它必要的时候创建交换空间。如果你在安装 Linux 的时候忘记了创建或是你想要再增加交换分区的空间,你随时都可以再创建或增加。
有时候在你安装后摇升级 RAM 的时候需要增加一点交换分区的空间,比如你要将你的系统的 RAM 从 1GB 升级到 2GB 你,那么你就不得不将你的交换分区空间也升级一下(从 2GB 到 4GB),这是因为它使用的容量是物理 RAM 的双倍容量。(LCTT 译注:其实这里是个误区,交换分区不一定非得是双倍的物理内存容量,只是惯例如此。事实上,如果你的物理内存足够的话,你完全可以不用交换分区——在这里的情形下,或许你增加了物理内存,就没必要增加交换分区大小了。)
交换空间是当物理内存(RAM 随机存取存储器)的用量已满时,被保留用作虚拟内存的磁盘上的空间。 如果系统在 RAM 满载时需要更多的内存资源,内存中的非活动页面将被移动到交换空间,这样可以帮助系统运行应用程序更多的时间,但不应该把它当做 RAM 的扩展。
建议你创建一个专用的交换分区,但是如果你没有可用的分区,那么可以使用交换文件,或交换分区和交换文件的组合。 交换空间通常建议用户至少 4 GB,用户也可以根据自己的要求和环境创建交换空间。
我发现大部分 VM 和 云服务器都没有交换分区,所以在这种情况下,我们可以使用以下三种方法创建,扩展或增加交换空间。
通过 free & swapon
命令来检测当前的交换分区空间的大小。
<span class="pln">$ free </span><span class="pun">-</span><span class="pln">h</span>
<span class="pln">total used free shared buff</span><span class="pun">/</span><span class="pln">cache available</span>
<span class="typ">Mem</span><span class="pun">:</span><span class="lit">2.0G</span><span class="lit">1.3G</span><span class="lit">139M</span><span class="lit">45M</span><span class="lit">483M</span><span class="lit">426M</span>
<span class="typ">Swap</span><span class="pun">:</span><span class="lit">2.0G</span><span class="lit">655M</span><span class="lit">1.4G</span>
<span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">655.2M</span><span class="pun">-</span><span class="lit">1</span>
上面的输出显示了当前的交换分区空间是 2GB
。
fallocate
程序是立即创建预分配大小的文件的最佳方法。
下面这个命令会创建一个 1GB 大小 的 /swapfile
。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> fallocate </span><span class="pun">-</span><span class="pln">l </span><span class="lit">1G</span><span class="pun">/</span><span class="pln">swapfile</span>
检查一下创建的文件的大小是否正确。
<span class="pln">$ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">lh </span><span class="pun">/</span><span class="pln">swapfile</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">1.0G</span><span class="typ">Jun</span><span class="lit">7</span><span class="lit">09</span><span class="pun">:</span><span class="lit">49</span><span class="pun">/</span><span class="pln">swapfile</span>
将该文件的权限设置为 600
这样只有 root 用户可以访问这个文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">chmod</span><span class="lit">600</span><span class="pun">/</span><span class="pln">swapfile</span>
通过运行以下的命令来将此文件转换为交换文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">swapfile</span>
<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">1024</span><span class="typ">MiB</span><span class="pun">(</span><span class="lit">1073737728</span><span class="pln"> bytes</span><span class="pun">)</span>
<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">cda50e0e</span><span class="pun">-</span><span class="lit">41f3</span><span class="pun">-</span><span class="lit">49c7</span><span class="pun">-</span><span class="pln">af61</span><span class="pun">-</span><span class="pln">b8cb4a33a464</span>
通过运行以下的命令来使交换文件生效。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapon</span><span class="pun">/</span><span class="pln">swapfile</span>
将新创建的交换文件添加到 fstab
文件中,这样交换分区空间的修改即使在重启后也可以生效。
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
<span class="pun">/</span><span class="pln">swapfile swap swap defaults </span><span class="lit">0</span><span class="lit">0</span>
检查一下新创建的交换文件。
<span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">657.8M</span><span class="pun">-</span><span class="lit">1</span>
<span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">2</span>
现在我可以看到一个新的 1GB 的 /swapfile1
文件了。重启系统以使新的交换文件生效。
dd
命令是另一个实用程序,可以帮助我们立即创建预分配大小的文件。
以下 dd 命令将创建 1GB 的 /swapfile1
。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">dd</span><span class="kwd">if</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">zero of</span><span class="pun">=/</span><span class="pln">swapfile1 bs</span><span class="pun">=</span><span class="lit">1G</span><span class="pln"> count</span><span class="pun">=</span><span class="lit">1</span>
<span class="lit">1</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records </span><span class="kwd">in</span>
<span class="lit">1</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records out</span>
<span class="lit">1073741824</span><span class="pln"> bytes </span><span class="pun">(</span><span class="lit">1.1</span><span class="pln"> GB</span><span class="pun">,</span><span class="lit">1.0</span><span class="typ">GiB</span><span class="pun">)</span><span class="pln"> copied</span><span class="pun">,</span><span class="lit">16.6154</span><span class="pln"> s</span><span class="pun">,</span><span class="lit">64.6</span><span class="pln"> MB</span><span class="pun">/</span><span class="pln">s</span>
详解:
if=/dev/zero
是输入文件,/dev/zero
是类 Unix 操作系统中的一个特殊文件,它提供从它读取的尽可能多的空字符(ASCII NUL,0x00)。of=/swapfile1
设置输出文件。bs=1G
一次性读写的大小为 1GBcount=1
仅复制一个输入块检查一下创建的文件的大小是否正确。
<span class="pln">$ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">lh </span><span class="pun">/</span><span class="pln">swapfile1</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">1.0G</span><span class="typ">Jun</span><span class="lit">7</span><span class="lit">09</span><span class="pun">:</span><span class="lit">58</span><span class="pun">/</span><span class="pln">swapfile1</span>
将该文件的权限设置为 600
这样只有 root 用户可以访问这个文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">chmod</span><span class="lit">600</span><span class="pun">/</span><span class="pln">swapfile1</span>
通过运行以下的命令来将此文件转换为交换文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">swapfile1</span>
<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">1024</span><span class="typ">MiB</span><span class="pun">(</span><span class="lit">1073737728</span><span class="pln"> bytes</span><span class="pun">)</span>
<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">96def6d7</span><span class="pun">-</span><span class="pln">b2da</span><span class="pun">-</span><span class="lit">4954</span><span class="pun">-</span><span class="pln">aa72</span><span class="pun">-</span><span class="pln">aa32316ec993</span>
通过运行以下的命令来使交换文件生效。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapon</span><span class="pun">/</span><span class="pln">swapfile1</span>
将新创建的交换文件添加到 fstab
文件中,这样交换分区空间的修改即使在重启后也可以生效。
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
<span class="pun">/</span><span class="pln">swapfile1 swap swap defaults </span><span class="lit">0</span><span class="lit">0</span>
检查新创建的交换文件。
<span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">1.3G</span><span class="pun">-</span><span class="lit">1</span>
<span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">2</span>
<span class="pun">/</span><span class="pln">swapfile1 </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">3</span>
现在我可以看到一个新的 1GB 的 /swapfile1
了。重启系统以使新的交换文件生效。
我们也推荐使用通过硬盘分区的方式来创建交换分区。
如果你已经在你的另一个硬盘上通过 fdisk
命令创建了一个新的分区,假设我们已经创建了一个叫做 /dev/sda4
的分区。
使用 mkswap
命令来将这个分区转换成交换分区。
<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">sda4</span>
通过运行以下命令来使交换文件生效。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">swapon</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda4</span>
把新增的交换文件添加到 fstab
文件中,这样即使是重启了系统交换分区的修改也能生效。
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda4 swap swap defaults </span><span class="lit">0</span><span class="lit">0</span>
检查新创建的交换文件。
<span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">1.3G</span><span class="pun">-</span><span class="lit">1</span>
<span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">2</span>
<span class="pun">/</span><span class="pln">swapfile1 </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">3</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda4 partition </span><span class="lit">1G</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">4</span>
我可以看到新的交换分区 1GB 的 /dev/sda4
。重启系统就可以使用新的交换分区了。
(题图:Pixabay,CC0)
via: http://www.2daygeek.com/add-extend-increase-swap-space-memory-file-partition-linux/
作者:2DAYGEEK 译者:chenxinlong 校对:wxy