diff --git a/sources/tech/20180926 An introduction to swap space on Linux systems.md b/sources/tech/20180926 An introduction to swap space on Linux systems.md deleted file mode 100644 index da50208533..0000000000 --- a/sources/tech/20180926 An introduction to swap space on Linux systems.md +++ /dev/null @@ -1,302 +0,0 @@ -heguangzhi Translating - -An introduction to swap space on Linux systems -====== - -![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/fail_progress_cycle_momentum_arrow.png?itok=q-ZFa_Eh) - -Swap space is a common aspect of computing today, regardless of operating system. Linux uses swap space to increase the amount of virtual memory available to a host. It can use one or more dedicated swap partitions or a swap file on a regular filesystem or logical volume. - -There are two basic types of memory in a typical computer. The first type, random access memory (RAM), is used to store data and programs while they are being actively used by the computer. Programs and data cannot be used by the computer unless they are stored in RAM. RAM is volatile memory; that is, the data stored in RAM is lost if the computer is turned off. - -Hard drives are magnetic media used for long-term storage of data and programs. Magnetic media is nonvolatile; the data stored on a disk remains even when power is removed from the computer. The CPU (central processing unit) cannot directly access the programs and data on the hard drive; it must be copied into RAM first, and that is where the CPU can access its programming instructions and the data to be operated on by those instructions. During the boot process, a computer copies specific operating system programs, such as the kernel and init or systemd, and data from the hard drive into RAM, where it is accessed directly by the computer’s processor, the CPU. - -### Swap space - -Swap space is the second type of memory in modern Linux systems. The primary function of swap space is to substitute disk space for RAM memory when real RAM fills up and more space is needed. - -For example, assume you have a computer system with 8GB of RAM. If you start up programs that don’t fill that RAM, everything is fine and no swapping is required. But suppose the spreadsheet you are working on grows when you add more rows, and that, plus everything else that's running, now fills all of RAM. Without swap space available, you would have to stop working on the spreadsheet until you could free up some of your limited RAM by closing down some other programs. - -The kernel uses a memory management program that detects blocks, aka pages, of memory in which the contents have not been used recently. The memory management program swaps enough of these relatively infrequently used pages of memory out to a special partition on the hard drive specifically designated for “paging,” or swapping. This frees up RAM and makes room for more data to be entered into your spreadsheet. Those pages of memory swapped out to the hard drive are tracked by the kernel’s memory management code and can be paged back into RAM if they are needed. - -The total amount of memory in a Linux computer is the RAM plus swap space and is referred to as virtual memory. - -### Types of Linux swap - -Linux provides for two types of swap space. By default, most Linux installations create a swap partition, but it is also possible to use a specially configured file as a swap file. A swap partition is just what its name implies—a standard disk partition that is designated as swap space by the `mkswap` command. - -A swap file can be used if there is no free disk space in which to create a new swap partition or space in a volume group where a logical volume can be created for swap space. This is just a regular file that is created and preallocated to a specified size. Then the `mkswap` command is run to configure it as swap space. I don’t recommend using a file for swap space unless absolutely necessary. - -### Thrashing - -Thrashing can occur when total virtual memory, both RAM and swap space, become nearly full. The system spends so much time paging blocks of memory between swap space and RAM and back that little time is left for real work. The typical symptoms of this are obvious: The system becomes slow or completely unresponsive, and the hard drive activity light is on almost constantly. - -If you can manage to issue a command like `free` that shows CPU load and memory usage, you will see that the CPU load is very high, perhaps as much as 30 to 40 times the number of CPU cores in the system. Another symptom is that both RAM and swap space are almost completely allocated. - -After the fact, looking at SAR (system activity report) data can also show these symptoms. I install SAR on every system I work on and use it for post-repair forensic analysis. - -### What is the right amount of swap space? - -Many years ago, the rule of thumb for the amount of swap space that should be allocated on the hard drive was 2X the amount of RAM installed in the computer (of course, that was when most computers' RAM was measured in KB or MB). So if a computer had 64KB of RAM, a swap partition of 128KB would be an optimum size. This rule took into account the facts that RAM sizes were typically quite small at that time and that allocating more than 2X RAM for swap space did not improve performance. With more than twice RAM for swap, most systems spent more time thrashing than actually performing useful work. - -RAM has become an inexpensive commodity and most computers these days have amounts of RAM that extend into tens of gigabytes. Most of my newer computers have at least 8GB of RAM, one has 32GB, and my main workstation has 64GB. My older computers have from 4 to 8 GB of RAM. - -When dealing with computers having huge amounts of RAM, the limiting performance factor for swap space is far lower than the 2X multiplier. The Fedora 28 online Installation Guide, which can be found online at [Fedora Installation Guide][1], defines current thinking about swap space allocation. I have included below some discussion and the table of recommendations from that document. - -The following table provides the recommended size of a swap partition depending on the amount of RAM in your system and whether you want sufficient memory for your system to hibernate. The recommended swap partition size is established automatically during installation. To allow for hibernation, however, you will need to edit the swap space in the custom partitioning stage. - -_Table 1: Recommended system swap space in Fedora 28 documentation_ - -| **Amount of system RAM** | **Recommended swap space** | **Recommended swap with hibernation** | -|--------------------------|-----------------------------|---------------------------------------| -| less than 2 GB | 2 times the amount of RAM | 3 times the amount of RAM | -| 2 GB - 8 GB | Equal to the amount of RAM | 2 times the amount of RAM | -| 8 GB - 64 GB | 0.5 times the amount of RAM | 1.5 times the amount of RAM | -| more than 64 GB | workload dependent | hibernation not recommended | - -At the border between each range listed above (for example, a system with 2 GB, 8 GB, or 64 GB of system RAM), use discretion with regard to chosen swap space and hibernation support. If your system resources allow for it, increasing the swap space may lead to better performance. - -Of course, most Linux administrators have their own ideas about the appropriate amount of swap space—as well as pretty much everything else. Table 2, below, contains my recommendations based on my personal experiences in multiple environments. These may not work for you, but as with Table 1, they may help you get started. - -_Table 2: Recommended system swap space per the author_ - -| Amount of RAM | Recommended swap space | -|---------------|------------------------| -| ≤ 2GB | 2X RAM | -| 2GB – 8GB | = RAM | -| >8GB | 8GB | - -One consideration in both tables is that as the amount of RAM increases, beyond a certain point adding more swap space simply leads to thrashing well before the swap space even comes close to being filled. If you have too little virtual memory while following these recommendations, you should add more RAM, if possible, rather than more swap space. As with all recommendations that affect system performance, use what works best for your specific environment. This will take time and effort to experiment and make changes based on the conditions in your Linux environment. - -#### Adding more swap space to a non-LVM disk environment - -Due to changing requirements for swap space on hosts with Linux already installed, it may become necessary to modify the amount of swap space defined for the system. This procedure can be used for any general case where the amount of swap space needs to be increased. It assumes sufficient available disk space is available. This procedure also assumes that the disks are partitioned in “raw” EXT4 and swap partitions and do not use logical volume management (LVM). - -The basic steps to take are simple: - - 1. Turn off the existing swap space. - - 2. Create a new swap partition of the desired size. - - 3. Reread the partition table. - - 4. Configure the partition as swap space. - - 5. Add the new partition/etc/fstab. - - 6. Turn on swap. - - - - -A reboot should not be necessary. - -For safety's sake, before turning off swap, at the very least you should ensure that no applications are running and that no swap space is in use. The `free` or `top` commands can tell you whether swap space is in use. To be even safer, you could revert to run level 1 or single-user mode. - -Turn off the swap partition with the command which turns off all swap space: - -``` -swapoff -a - -``` - -Now display the existing partitions on the hard drive. - -``` -fdisk -l - -``` - -This displays the current partition tables on each drive. Identify the current swap partition by number. - -Start `fdisk` in interactive mode with the command: - -``` -fdisk /dev/ - -``` - -For example: - -``` -fdisk /dev/sda - -``` - -At this point, `fdisk` is now interactive and will operate only on the specified disk drive. - -Use the fdisk `p` sub-command to verify that there is enough free space on the disk to create the new swap partition. The space on the hard drive is shown in terms of 512-byte blocks and starting and ending cylinder numbers, so you may have to do some math to determine the available space between and at the end of allocated partitions. - -Use the `n` sub-command to create a new swap partition. fdisk will ask you the starting cylinder. By default, it chooses the lowest-numbered available cylinder. If you wish to change that, type in the number of the starting cylinder. - -The `fdisk` command now allows you to enter the size of the partitions in a number of formats, including the last cylinder number or the size in bytes, KB or MB. Type in 4000M, which will give about 4GB of space on the new partition (for example), and press Enter. - -Use the `p` sub-command to verify that the partition was created as you specified it. Note that the partition will probably not be exactly what you specified unless you used the ending cylinder number. The `fdisk` command can only allocate disk space in increments on whole cylinders, so your partition may be a little smaller or larger than you specified. If the partition is not what you want, you can delete it and create it again. - -Now it is necessary to specify that the new partition is to be a swap partition. The sub-command `t` allows you to specify the type of partition. So enter `t`, specify the partition number, and when it asks for the hex code partition type, type 82, which is the Linux swap partition type, and press Enter. - -When you are satisfied with the partition you have created, use the `w` sub-command to write the new partition table to the disk. The `fdisk` program will exit and return you to the command prompt after it completes writing the revised partition table. You will probably receive the following message as `fdisk` completes writing the new partition table: - -``` -The partition table has been altered! -Calling ioctl() to re-read partition table. -WARNING: Re-reading the partition table failed with error 16: Device or resource busy. -The kernel still uses the old table. -The new table will be used at the next reboot. -Syncing disks. -``` - -At this point, you use the `partprobe` command to force the kernel to re-read the partition table so that it is not necessary to perform a reboot. - -``` -partprobe -``` - -Now use the command `fdisk -l` to list the partitions and the new swap partition should be among those listed. Be sure that the new partition type is “Linux swap”. - -It will be necessary to modify the /etc/fstab file to point to the new swap partition. The existing line may look like this: - -``` -LABEL=SWAP-sdaX   swap        swap    defaults        0 0 - -``` - -where `X` is the partition number. Add a new line that looks similar this, depending upon the location of your new swap partition: - -``` -/dev/sdaY         swap        swap    defaults        0 0 - -``` - -Be sure to use the correct partition number. Now you can perform the final step in creating the swap partition. Use the `mkswap` command to define the partition as a swap partition. - -``` -mkswap /dev/sdaY - -``` - -The final step is to turn swap on using the command: - -``` -swapon -a - -``` - -Your new swap partition is now online along with the previously existing swap partition. You can use the `free` or `top` commands to verify this. - -#### Adding swap to an LVM disk environment - -If your disk setup uses LVM, changing swap space will be fairly easy. Again, this assumes that space is available in the volume group in which the current swap volume is located. By default, the installation procedures for Fedora Linux in an LVM environment create the swap partition as a logical volume. This makes it easy because you can simply increase the size of the swap volume. - -Here are the steps required to increase the amount of swap space in an LVM environment: - - 1. Turn off all swap. - - 2. Increase the size of the logical volume designated for swap. - - 3. Configure the resized volume as swap space. - - 4. Turn on swap. - - - - -First, let’s verify that swap exists and is a logical volume using the `lvs` command (list logical volume). - -``` -[root@studentvm1 ~]# lvs -  LV     VG                Attr       LSize  Pool   Origin Data%  Meta%  Move Log Cpy%Sync Convert -  home   fedora_studentvm1 -wi-ao----  2.00g                                                       -  pool00 fedora_studentvm1 twi-aotz--  2.00g               8.17   2.93                             -  root   fedora_studentvm1 Vwi-aotz--  2.00g pool00        8.17                                   -  swap   fedora_studentvm1 -wi-ao----  8.00g                                                       -  tmp    fedora_studentvm1 -wi-ao----  5.00g                                                       -  usr    fedora_studentvm1 -wi-ao---- 15.00g                                                       -  var    fedora_studentvm1 -wi-ao---- 10.00g                                                       -[root@studentvm1 ~]# -``` - -You can see that the current swap size is 8GB. In this case, we want to add 2GB to this swap volume. First, stop existing swap. You may have to terminate running programs if swap space is in use. - -``` -swapoff -a - -``` - -Now increase the size of the logical volume. - -``` -[root@studentvm1 ~]# lvextend -L +2G /dev/mapper/fedora_studentvm1-swap -  Size of logical volume fedora_studentvm1/swap changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents). -  Logical volume fedora_studentvm1/swap successfully resized. -[root@studentvm1 ~]# -``` - -Run the `mkswap` command to make this entire 10GB partition into swap space. - -``` -[root@studentvm1 ~]# mkswap /dev/mapper/fedora_studentvm1-swap -mkswap: /dev/mapper/fedora_studentvm1-swap: warning: wiping old swap signature. -Setting up swapspace version 1, size = 10 GiB (10737414144 bytes) -no label, UUID=3cc2bee0-e746-4b66-aa2d-1ea15ef1574a -[root@studentvm1 ~]# -``` - -Turn swap back on. - -``` -[root@studentvm1 ~]# swapon -a -[root@studentvm1 ~]# -``` - -Now verify the new swap space is present with the list block devices command. Again, a reboot is not required. - -``` -[root@studentvm1 ~]# lsblk -NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT -sda                                    8:0    0   60G  0 disk -|-sda1                                 8:1    0    1G  0 part /boot -`-sda2                                 8:2    0   59G  0 part -  |-fedora_studentvm1-pool00_tmeta   253:0    0    4M  0 lvm   -  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm   -  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  / -  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm   -  |-fedora_studentvm1-pool00_tdata   253:1    0    2G  0 lvm   -  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm   -  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  / -  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm   -  |-fedora_studentvm1-swap           253:4    0   10G  0 lvm  [SWAP] -  |-fedora_studentvm1-usr            253:5    0   15G  0 lvm  /usr -  |-fedora_studentvm1-home           253:7    0    2G  0 lvm  /home -  |-fedora_studentvm1-var            253:8    0   10G  0 lvm  /var -  `-fedora_studentvm1-tmp            253:9    0    5G  0 lvm  /tmp -sr0                                   11:0    1 1024M  0 rom   -[root@studentvm1 ~]# -``` - -You can also use the `swapon -s` command, or `top`, `free`, or any of several other commands to verify this. - -``` -[root@studentvm1 ~]# free -              total        used        free      shared  buff/cache   available -Mem:        4038808      382404     2754072        4152      902332     3404184 -Swap:      10485756           0    10485756 -[root@studentvm1 ~]# -``` - -Note that the different commands display or require as input the device special file in different forms. There are a number of ways in which specific devices are accessed in the /dev directory. My article, [Managing Devices in Linux][2], includes more information about the /dev directory and its contents. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/18/9/swap-space-linux-systems - -作者:[David Both][a] -选题:[lujun9972](https://github.com/lujun9972) -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/dboth -[1]: https://docs.fedoraproject.org/en-US/fedora/f28/install-guide/ -[2]: https://opensource.com/article/16/11/managing-devices-linux diff --git a/translated/tech/20180926 An introduction to swap space on Linux systems.md b/translated/tech/20180926 An introduction to swap space on Linux systems.md new file mode 100644 index 0000000000..0a36a44e9f --- /dev/null +++ b/translated/tech/20180926 An introduction to swap space on Linux systems.md @@ -0,0 +1,315 @@ + +Linux 系统上 swap 空间的介绍 +====== + +![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/fail_progress_cycle_momentum_arrow.png?itok=q-ZFa_Eh) + +当今无论什么操作系统 Swap 空间是非常常见的。Linux 使用 Swap 空间来增加主机可用的虚拟内存。它可以是常规文件或逻辑卷上使用一个或多个专用swap 分区或 swap 文件。 + +典型计算机中有两种基本类型的内存。第一种类型,随机存取存储器 (RAM),用于存储计算机使用的数据和程序。只有程序和数据存储在 RAM 中,计算机才能使用它们。随机存储器是易失性存储器;也就是说,如果计算机关闭了,存储在 RAM 中的数据就会丢失。 + + +硬盘是用于长期存储数据和程序的磁性介质。该磁介质可以很好的保存数据;即使计算机断电,存储在磁盘上的数据也会保留下来。CPU (中央处理器)不能直接访问硬盘上的程序和数据;他们必须首先复制到 RAM 中,RAM 是 CPU 访问代码指令和操作数据的地方。在引导过程中,计算机将特定的操作系统程序(如内核、init 或 systemd )以及硬盘上的数据复制到 RAM 中,在 RAM 中,计算机的处理器 CPU 可以直接访问这些数据。 + +### Swap 空间 + +Swap 空间是现代 Linux 系统中的第二种内存类型。Swap 空间的主要功能是当全部的 RAM 被占用并且需要更多内存时,用磁盘空间代替 RAM 内存。 + +例如,假设你有一个 8GB RAM 的计算机。如果你启动的程序没有填满 RAM,一切好,不需要 Swap。假设你在处理电子表格,当添加更多的行时,你电子表格会增长,加上所有正在运行的程序,将会占用全部的 RAM 。如果这时没有可用的 Swap 空间,你将不得不停止处理电子表格,直到关闭一些其他程序来释放一些 RAM 。 + +内核使用一个内存管理程序来检测最近没有使用的内存块,也就是内存页面。内存管理程序将这些相对不经常使用的内存页交换到硬盘上专门指定用于“分页”或 swap 的特殊分区。释放 RAM ,为输入电子表格更多数据腾出了空间。那些换出到硬盘的内存页面被内核的内存管理代码跟踪,如果需要,可以被分页回 RAM。 + +Linux 计算机中的内存总量是 RAM + swap 分区,swap 分区被称为虚拟内存. + +### Linux swap 分区类型 + +Linux 提供了两种类型的 swap 空间。默认情况下,大多数 Linux 在安装时都会创建一个 swap 分区,但是也可以使用一个特殊配置的文件作为 swap 文件。swap 分区顾名思义就是一个标准磁盘分区,由 `mkswap` 命令指定 swap 空间。 + +如果没有可用磁盘空间来创建新的 swap 分区,或者卷组中没有空间为 swap 空间创建逻辑卷,则可以使用 swap 文件。这只是一个创建并预分配指定大小的常规文件。然后运行 `mkswap` 命令将其配置为 swap 空间。除非绝对必要,否则我不建议使用文件来做 swap 空间。 + +### 频繁交换 + +当总虚拟内存( RAM 和 swap 空间 )变得快满时,可能会发生频繁交换 。系统花了太多时间在 swap 空间和 RAM 之间做内存块页面切换,以至于几乎没有时间用于实际工作。这种情况是显而易见的:系统变得缓慢或完全无反应,硬盘指示灯几乎持续亮起。 + +使用 `free` 的命令来显示 CPU 负载和内存使用情况,你会发现 CPU 负载非常高,可能达到系统中 CPU 内核数量的30到40倍。另一个情况是 RAM 和 swap 空间几乎完全被分配了。 + + +事实上,查看 SAR (系统活动报告)数据也可以显示这些内容。在我的每个系统上都安装 SAR ,并将这些用于数据分析。 + + +### swap 空间的正确大小是多少? + +许多年前,硬盘上分配给 swap 空间大小是计算机上的 RAM 的两倍(当然,这是大多数计算机的 RAM 以 KB 或 MB 为单位的时候)。因此,如果一台计算机有 64KB 的 RAM,应该分配 128KB 的 swap 分区。该规则考虑到了这样的事实情况,即 RAM 大小在当时非常小,分配超过2倍的 RAM 用于 swap 空间并不能提高性能。使用超过两倍的 RAM 进行交换,比实际执行有用的工作的时候,大多数系统将花费更多的时间。 + + +RAM 现在已经很便宜了,如今大多数计算机的 RAM 都达到了几十亿字节。我的大多数新电脑至少有 8GB 内存,一台有32GB 内存,我的主工作站有 64GB 内存。我的旧电脑有4到 8GB 的内存。 + + +当操作具有大 RAM 的计算机时,swap 空间的限制性能系数远低于 2倍。[Fedora 28在线安装指南][1] 定义了当前关于 swap 空间分配的方法。下面内容是我提出的建议。 + +下表根据系统中的 RAM 大小以及是否有足够的内存让系统休眠,提供了交换分区的推荐大小。建议的 swap 分区大小是在安装过程中自动建立的。但是,为了满足系统休眠,您需要在自定义分区阶段编辑 swap 空间。 + +_表 1: Fedora 28文档中推荐的系统 swap 空间_ + +| **系统内存大小 ** | **推荐 swap 空间 ** | **建议 swap 大小用休眠模式 ** | +|--------------------------|-----------------------------|---------------------------------------| +| 小于 2 GB | 2倍 RAM | 3 倍 RAM | +| 2 GB - 8 GB | 等于 RAM 大小 | 2 倍 RAM | +| 8 GB - 64 GB | 0.5 倍 RAM | 1.5 倍 RAM | +| 大于 64 GB | 工作量相关 | 不建议休眠模式 | + + +在上面列出的每个范围之间的边界(例如,具有 2GB、8GB 或 64GB 的系统 RAM),请根据所选 swap 空间和支持休眠功能请谨慎使用。如果你的系统资源允许,增加 swap 空间可能会带来更好的性能。 + +当然,大多数 Linux 管理员对多大的 swap 空间量有自己的想法。下面的表2包含了基于我在多种环境中的个人经历所做出的建议。这些可能不适合你,但是和表1一样,它们可能对你有所帮助。 + + +_表 2: 作者推荐的系统 swap 空间_ + +| RAM 大小 | 推荐 swap 空间 | +|---------------|------------------------| +| ≤ 2GB | 2X RAM | +| 2GB – 8GB | = RAM | +| >8GB | 8GB | + + +这两个表中共同点,随着 RAM 数量的增加,超过某一点增加更多 swap 空间只会导致在 swap 空间几乎被全部使用之前就发生频繁交换。根据以上建议,则应尽可能添加更多 RAM,而不是增加更多 swap 空间。如类似影响系统性能的情况一样,请使用最适合你的建议。根据 Linux 环境中的条件进行测试和更改是需要时间和精力的。 + + +### 向非 LVM 磁盘环境添加更多 swap 空间 + +面对已安装 Linux 的主机并对 swap 空间的需求不断变化,有时有必要修改系统定义的 swap 空间的大小。此过程可用于需要增加 swap 空间大小的任何情况。它假设有足够的可用磁盘空间。此过程还假设磁盘在 “raw” EXT4 和 swap 分区中分区,并且不使用逻辑卷 (LVM)。 + + + +要基本步骤很简单: + + 1. 关闭现有的 swap 空间。 + + 2. 创建所需大小的新 swap 分区。 + + 3. 重读分区表。 + + 4. 将分区配置为 swap 空间。 + + 5. 添加新分区到 /etc/fstab。 + + 6. 打开 swap 空间。 + + +不应需要重新启动机器。 + + +为了安全起见,在关闭 swap 空间前,至少你应该确保没有应用程序在运行,也没有 swap 空间在使用。`free` 或 `top` 命令可以告诉你 swap 空间是否在使用中。为了更安全,您可以恢复到运行级别1或单用户模式。 + +使用关闭所有 swap 空间的命令关闭 swap 分区: + +``` +swapoff -a + +``` + +现在查看硬盘上的现有分区。 + +``` +fdisk -l + +``` + +这将显示每个驱动器上的分区表。按编号标识当前的 swap 分区。 + + +使用以下命令在交互模式下启动 `fdisk`: + +``` +fdisk /dev/ + +``` + +例如: + +``` +fdisk /dev/sda + +``` + +此时,`fdisk` 是交互方式的,只在指定的磁盘驱动器上进行操作。 + +使用 fdisk `p` 子命令验证磁盘上是否有足够的可用空间来创建新的 swap 分区。硬盘上的空间以 512字节 以及起始和结束柱面编号的形式显示,因此您可能需要做一些计算来确定分配分区之间和末尾的可用空间。 + +使用 `n` 子命令创建新的交换分区。fdisk 会问你开始柱面。默认情况下,它选择编号最低的可用柱面。如果你想改变这一点,输入开始柱面的编号。 + +`fdisk` 命令允许你以多种格式输入分区的大小,包括最后一个柱面号或字节、KB 或 MB 的大小。键入 4000M ,这将在新分区上提供大约 4GB 的空间(例如),然后按 Enter 键。 + +使用 `p` 子命令来验证分区是否按照指定的方式创建的。请注意,除非使用结束柱面编号,否则分区可能与你指定的不完全相同。`fdisk` 命令只能在整个柱面上增量的分配磁盘空间,因此你的分区可能比你指定的稍小或稍大。如果分区不是您想要的,你可以删除它并重新创建它。 + +现在指定新分区是 swap 分区了 。子命令 `t` 允许你指定定分区的类型。所以输入 `t`,指定分区号,当它要求十六进制分区类型时,输入82,这是Linux swap 分区类型,然后按 Enter。 + + +当你对创建的分区感到满意时,使用 `w` 子命令将新的分区表写入磁盘。`fdisk` 程序将退出,并在完成修改后的分区表的编写后返回命令提示符。当`fdisk` 完成写入新分区表时,会收到以下消息: + +``` +The partition table has been altered! +Calling ioctl() to re-read partition table. +WARNING: Re-reading the partition table failed with error 16: Device or resource busy. +The kernel still uses the old table. +The new table will be used at the next reboot. +Syncing disks. +``` + + +此时,你使用 `partprobe` 命令强制内核重新读取分区表,这样就不需要执行重新启动机器。 + +``` +partprobe +``` + + +使用命令 `fdisk -l` 列出分区,新 swap 分区应该在列出的分区中。确保新的分区类型是 “Linux swap”。 + +修改 /etc/fstab 文件以指向新的 swap 分区。如下所示: + +``` +LABEL=SWAP-sdaX   swap        swap    defaults        0 0 + +``` + +其中 `X` 是分区号。根据新 swap 分区的位置,添加以下内容: + +``` +/dev/sdaY         swap        swap    defaults        0 0 + +``` + +请确保使用正确的分区号。现在,可以执行创建 swap 分区的最后一步。使用 `mkswap` 命令将分区定义为 swap 分区。 + +``` +mkswap /dev/sdaY + +``` + +最后一步是使用以下命令启用 swap 空间: + +``` +swapon -a + +``` + +你的新 swap 分区现在与以前存在的 swap 分区一起在线。您可以使用 `free` 或`top` 命令来验证这一点。 + +#### 在 LVM 磁盘环境中添加 swap 空间 + +如果你的磁盘使用 LVM ,更改 swap 空间将相当容易。同样,假设当前 swap 卷所在的卷组中有可用空间。默认情况下,LVM 环境中的 Fedora Linux 在安装过程将 swap 分区创建为逻辑卷。您可以非常简单地增加 swap 卷的大小。 + +以下是在 LVM 环境中增加 swap 空间大小的步骤: + + 1. 关闭所有 swap 。 + + 2. 增加指定用于 swap 的逻辑卷的大小。 + + 3. 为 swap 空间调整大小的卷配置。 + + 4. 启用 swap。 + + + +首先,让我们使用 `lvs` 命令(列出逻辑卷)来验证 swap 是否存在以及 swap 是否是逻辑卷。 + +``` +[root@studentvm1 ~]# lvs +  LV     VG                Attr       LSize  Pool   Origin Data%  Meta%  Move Log Cpy%Sync Convert +  home   fedora_studentvm1 -wi-ao----  2.00g                                                       +  pool00 fedora_studentvm1 twi-aotz--  2.00g               8.17   2.93                             +  root   fedora_studentvm1 Vwi-aotz--  2.00g pool00        8.17                                   +  swap   fedora_studentvm1 -wi-ao----  8.00g                                                       +  tmp    fedora_studentvm1 -wi-ao----  5.00g                                                       +  usr    fedora_studentvm1 -wi-ao---- 15.00g                                                       +  var    fedora_studentvm1 -wi-ao---- 10.00g                                                       +[root@studentvm1 ~]# +``` + +你可以看到当前的 swap 大小为 8GB。在这种情况下,我们希望将 2GB 添加到此 swap 卷中。首先,停止现有的 swap 。如果 swap 空间正在使用,终止正在运行的程序。 + + +``` +swapoff -a + +``` + +现在增加逻辑卷的大小。 + +``` +[root@studentvm1 ~]# lvextend -L +2G /dev/mapper/fedora_studentvm1-swap +  Size of logical volume fedora_studentvm1/swap changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents). +  Logical volume fedora_studentvm1/swap successfully resized. +[root@studentvm1 ~]# +``` + +运行 `mkswap` 命令将整个 10GB 分区变成 swap 空间。 + +``` +[root@studentvm1 ~]# mkswap /dev/mapper/fedora_studentvm1-swap +mkswap: /dev/mapper/fedora_studentvm1-swap: warning: wiping old swap signature. +Setting up swapspace version 1, size = 10 GiB (10737414144 bytes) +no label, UUID=3cc2bee0-e746-4b66-aa2d-1ea15ef1574a +[root@studentvm1 ~]# +``` + +重新启用 swap 。 + +``` +[root@studentvm1 ~]# swapon -a +[root@studentvm1 ~]# +``` + +现在,使用 `lsblk ` 命令验证新 swap 空间是否存在。同样,不需要重新启动机器。 + +``` +[root@studentvm1 ~]# lsblk +NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT +sda                                    8:0    0   60G  0 disk +|-sda1                                 8:1    0    1G  0 part /boot +`-sda2                                 8:2    0   59G  0 part +  |-fedora_studentvm1-pool00_tmeta   253:0    0    4M  0 lvm   +  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm   +  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  / +  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm   +  |-fedora_studentvm1-pool00_tdata   253:1    0    2G  0 lvm   +  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm   +  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  / +  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm   +  |-fedora_studentvm1-swap           253:4    0   10G  0 lvm  [SWAP] +  |-fedora_studentvm1-usr            253:5    0   15G  0 lvm  /usr +  |-fedora_studentvm1-home           253:7    0    2G  0 lvm  /home +  |-fedora_studentvm1-var            253:8    0   10G  0 lvm  /var +  `-fedora_studentvm1-tmp            253:9    0    5G  0 lvm  /tmp +sr0                                   11:0    1 1024M  0 rom   +[root@studentvm1 ~]# +``` + +您也可以使用`swapon -s` 命令或 `top` 、`free` 或其他几个命令来验证这一点。 + +``` +[root@studentvm1 ~]# free +              total        used        free      shared  buff/cache   available +Mem:        4038808      382404     2754072        4152      902332     3404184 +Swap:      10485756           0    10485756 +[root@studentvm1 ~]# +``` + +请注意,不同的命令以不同的形式显示或要求输入设备文件。在 /dev 目录中访问特定设备有多种方式。在我的文章[Managing Devices in Linux][2] 中有更多关于 /dev 目录及其内容说明。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/18/9/swap-space-linux-systems + +作者:[David Both][a] +选题:[lujun9972](https://github.com/lujun9972) +译者:[heguangzhi](https://github.com/heguangzhi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/dboth +[1]: https://docs.fedoraproject.org/en-US/fedora/f28/install-guide/ +[2]: https://opensource.com/article/16/11/managing-devices-linux