mkswap (设置Linux交换区)

瑞兹 发表于 2021-02-02 09:07




1 mkswap 运行系统环境

2 mkswap 描述

3 mkswap 语法

4 mkswap 例子

mkswap 运行系统环境


mkswap 描述


器件 参数通常是一个磁盘 分区(类似的/ dev / sdb7),也可以是一个文件。Linux内核不查看分区ID,但是许多安装脚本会假定十六进制类型82LINUX_SWAP)的分区是交换分区。(Solaris也使用这种类型。请注意不要通过混合使用来意外杀死Solaris分区(如果有)!)

size参数是多余的,但为了向后兼容而包含了该参数。(它以1024字节 块为单位指定交换区域的所需大小。如果省略,mkswap将使用整个分区或文件。指定这样做非常危险:错字可能会破坏您的数据。重复,在指定分区时非常小心手动调整大小!输入错误会破坏磁盘上的数据!

创建交换区域后,您需要swapon命令才能开始使用它。通常,交换区域在/ etc / fstab中列出,以便在引导时通过某些引导脚本中的swapon -a命令可以使用它们。




但是,mkswap拒绝擦除带有磁盘标签的设备(SUNBSD等)和整个磁盘(例如/ dev / sda)上的第一个块。

mkswap sets up a Linux swap area on a device or in a file.

The device argument will usually be a disk partition (something like /dev/sdb7) but can also be a file. The Linux kernel does not look at partition IDs, but many installation scripts will assume that partitions of hex type 82 (LINUX_SWAP) are meant to be swap partitions. (Solaris also uses this type. Be careful not to accidentally kill your Solaris partitions, if you have any, by mixing them up!)

The size parameter is superfluous but included for backward compatibility. (It specifies the desired size of the swap area in 1024-byte blocksmkswap will use the entire partition or file if it is omitted. Specifying it is very dangerous: a typo can destroy your data. REPEAT, BE VERY CAREFUL WHEN SPECIFYING PARTITION SIZES MANUALLY! A TYPO CAN DESTROY THE DATA ON YOUR DISK!)

After creating the swap area, you need the swapon command to start using it. Usually, swap areas are listed in /etc/fstab so that they can be taken into use at boot time by a swapon -a command in some boot script.

Important information and warnings

The swap header does not touch the first block. A boot loader or disk label can be there, but it is not a recommended setup. The recommended setup is to use a separate partition for a Linux swap area.

mkswap, like many others mkfs-like utilities, erases the first partition block to make any previous filesystem invisible.

However, mkswap refuses to erase the first block on a device with a disk label (SUNBSD, etc.) and on a whole disk (e.g., /dev/sda).



mkswap 语法

mkswap [options] device [size]


-c--check 在创建交换区之前,请检查设备(如果是块设备)是否有坏块。如果找到任何坏块,则打印计数。
-f--force 即使该命令是愚蠢的,也请继续。强制创建大于其所在文件或分区的交换区域。

同样,如果没有此选项,mkswap将拒绝擦除具有分区表的设备以及整个磁盘(例如/ dev / sda)上的第一个块。
-L--label label  指定设备的标签,以允许按标签交换。
-p,-- pagesize size 指定要使用的页面大小(以字节为单位)。通常不需要此选项。mkswap从内核读取大小。
-U,-- uuid UUID 指定要使用的UUID。默认是生成一个UUID。
-v,-- swapversion 1 指定交换空间版本。(此选项目前没有意义的,作为老-v 0选项已经过时了,现在只能-v 1是支持的。因为2.5.22(2002年6月)的内核不支持V0交换空间格式。新版本V1是自2.1.117(1998年8月)开始受支持。)
-h--help 显示帮助文本并退出。
-V,-- version 显示版本信息并退出。


交换区的最大可用大小取决于体系结构和内核版本。在i386,PPC,m68k和ARM上大约为2 GiB,在sparc上约为1 GiB ,在mips上约为512 MiB,在alpha上约为128 GiB ,在sparc64上约为3 TiB。对于2.3.3之后的内核(1999年5月),没有这种限制。

请注意,在2.1.117版之前,内核为每个页面分配了一个字节,而现在它分配了两个字节,因此,使用2 GiB的交换区域可能需要2 MiB的内核内存。

目前,Linux允许32个交换区域(在Linux 2.4.10(2001年9月)之前是8个)。可以在文件/ proc / swaps中查看使用中的区域(自2.1.25开始(1997年9月))。


如果您不知道计算机使用的页面大小,则可以使用“ cat / proc / cpuinfo ”查找(或者您可能不知道-该文件的内容取决于体系结构和内核版本)。要设置交换文件,必须先使用mkswap初始化该文件,然后再使用以下命令:

dd if = / dev / zero of = swapfile bs = 1024 count = 65536


mkswap [options] device [size]


-c--check Check the device (if it is a block device) for bad blocks before creating the swap area. If any bad blocks are found, the count is printed.
-f--force Go ahead even if the command is stupid. Forces the creation of a swap area larger than the file or partition of where it resides.

Also, without this option, mkswap will refuse to erase the first block on a device with a partition table and on a whole disk (e.g., /dev/sda).
-L--label label Specify a label for the device, to allow swapon by label.
-p--pagesize size Specify the page size (in bytes) to use. This option is usually unnecessary; mkswap reads the size from the kernel.
-U--uuid UUID Specify the UUID to use. The default is to generate a UUID.
-v--swapversion 1 Specify the swap-space version. (This option is currently pointless, as the old -v 0 option has become obsolete and now only -v 1 is supported. The kernel has not supported v0 swap-space format since 2.5.22 (June 2002). The new version v1 is supported since 2.1.117 (August 1998).)
-h--help Display help text and exit.
-V--version Display version information and exit.


The maximum useful size of a swap area depends on the architecture and the kernel version. It is roughly 2 GiB on i386PPC, m68k and ARM, 1 GiB on sparc, 512 MiB on mips, 128 GiB on alpha, and 3 TiB on sparc64. For kernels after 2.3.3 (May 1999), there is no such limitation.

Note that before version 2.1.117 the kernel allocated one byte for each page, while it now allocates two bytes, so that taking into use a swap area of 2 GiB might require 2 MiB of kernel memory.

Presently, Linux allows 32 swap areas (this was 8 before Linux 2.4.10 (Sep 2001)). The areas in use can be seen in the file /proc/swaps (since 2.1.25 (Sep 1997)).

mkswap refuses areas smaller than 10 pages.

If you don't know the page size that your machine uses, you may be able to look it up with "cat /proc/cpuinfo" (or you may not -- the contents of this file depend on architecture and kernel version). To set up a swap file, it is necessary to create that file before initializing it with mkswap, e.g., using a command like:

dd if=/dev/zero of=swapfile bs=1024 count=65536

Note that a swap file must not contain any holes (so, using cp to create the file, for example, is not acceptable).



mkswap 例子

mkswap /dev/sdc1

准备分区/ dev / sdc1,将其设置为交换分区。然后可以使用swapon命令激活该空间。

mkswap /dev/sdc1

Prepares the partition /dev/sdc1, setting it up for use as a swap partition. This space can then be activated using the swapon command.




mail | mailcompat | merge | mesg | mii-tool | mkdir | mv | mkfs | modinfo | modprobe | more | mt | mach |