mkswap 中文man頁(yè)面
NAME
mkswap - 建立一個(gè)linux交換區(qū)
總覽
mkswap [-c] [-vN] [-f] device [size]
描述
mkswap 在一個(gè)設(shè)備上或者在一個(gè)文件里創(chuàng)建一個(gè)linux交換區(qū)。
(該交換區(qū)創(chuàng)建后,必須使用 swapon 命令來(lái)啟用它。通常交換區(qū)被列在 /etc/fstab 中,從而使得在啟動(dòng)時(shí)可通過(guò)某些啟動(dòng)腳本中的 swapon -a 命令來(lái)啟用。)
參數(shù) device 通常是一個(gè)磁盤分區(qū)(類似于 /dev/hda4 或者 /dev/sdb7 ),但也可以是一個(gè)文件。 Linux內(nèi)核不會(huì)著眼于分區(qū)號(hào),但是一些安裝腳本會(huì)假定類型為 16進(jìn)制的82(LINUX_SWAP)的分區(qū)意味著交換分區(qū)。
參數(shù) size 是多余的,但為了向兼容性而被保留。(它指定期望的以1024 字節(jié)為單位的交換區(qū)大小。如果它未被指定, mkswap 將使用整個(gè)分區(qū)或者文件。錯(cuò)誤地使用"-a"選項(xiàng)將會(huì)損壞您的磁盤。)
Linux能理解兩種類型的交換區(qū):舊類型和新類型。交換區(qū)第一頁(yè)的最后 10個(gè)字節(jié)區(qū)分兩種類型:舊類型以"SWAP_SPACE",新類型以"SWAPSPACE2" 作標(biāo)識(shí)。
在舊類型里,第一頁(yè)的其它部分是一個(gè)位圖,每一位指出交換區(qū)中的可用頁(yè)面。因?yàn)榈谝豁?yè)保存位圖,所以第一位為0。同樣的,最后10位保存標(biāo)識(shí)。所以,如果頁(yè)面大小為S,舊類型交換區(qū)可以描述最多8*(S-10)-1 個(gè)可用于交換的頁(yè)面。對(duì)于S=4096(象i386上),最大可用區(qū)域是 133890048字節(jié)(如果1 MB=2^20 bytes,則差不多為128 MB),而其他部分將被浪費(fèi)。在alpha和sparc64上,S=8192,最大可用區(qū)域是535560992字節(jié)(與上述同樣條件下,差不多為512 MB)。
因?yàn)榱阄槐硎緣牡膲K或者超過(guò)交換區(qū)末尾的塊,舊的設(shè)置浪費(fèi)了大部分的位圖頁(yè)面,一個(gè)簡(jiǎn)單的整數(shù)就可以滿足指出交換區(qū)大小的需要,而且如果有壞的塊,也可以簡(jiǎn)單的列出來(lái)。沒(méi)有人想使用有許多壞塊的交換區(qū)(我甚至不會(huì)使用包含一個(gè)壞塊的交換空間)。在新類型交換區(qū)正好是這樣做的。現(xiàn)在交換區(qū)的最大可用數(shù)目取決于具體結(jié)構(gòu)。大致來(lái)說(shuō),在i386, PPC, m68k, ARM上為2GB,在sparc上為1GB,在mips上為512MB,在alpha 上為128GB,在sparc64上為3TB。
注意在2.1.117內(nèi)核之前,每一頁(yè)分配一個(gè)字節(jié),而現(xiàn)在為兩個(gè)字節(jié),所以為了獲得2GB的可用交換區(qū),可能需要2MB的核心內(nèi)存。
目前,Linux允許8個(gè)交換區(qū)。這些正在使用的區(qū)域可從文件 /proc/swaps (從2.1.25起)中查到。
mkswap 不允許小于10個(gè)頁(yè)面的區(qū)域。
如果不知道您的機(jī)器所用的頁(yè)面大小,可以用"cat /proc/cpuinfo"來(lái)查看(或者不能查看 - 這個(gè)文件的內(nèi)容取決于系統(tǒng)結(jié)構(gòu)和內(nèi)核版本)。
為了設(shè)置一個(gè)交換文件,需要在執(zhí)行 mkswap , 前創(chuàng)建這個(gè)文件,例如,象如下命令:
- # dd if=/dev/zero of=swapfile bs=1024 count=65536
注意一個(gè)交換文件必須不包括任何空洞(所以,不能使用 cp(1) 來(lái)創(chuàng)建該文件)。
選項(xiàng)
- -c
- 在創(chuàng)建交換區(qū)之前檢查設(shè)備(如果是塊設(shè)備)的壞塊。如果發(fā)現(xiàn)任何壞塊,壞塊的總數(shù)將被打印出來(lái)。
- -f
- 強(qiáng)行向前執(zhí)行,即使該命令是愚蠢的。該選項(xiàng)允許建立比所在的文件或者分區(qū)還大的交換區(qū)。在SPARC上,強(qiáng)行創(chuàng)建交換區(qū)。無(wú)該選項(xiàng)的 mkswap 將拒絕在包含有效的SPARC超級(jí)塊的設(shè)備上創(chuàng)建vo交換區(qū),因?yàn)槟菢涌赡芤馕吨鴮h除分區(qū)表。
- -v0
- 創(chuàng)建一個(gè)舊類型的交換區(qū)。
- -v1
- 創(chuàng)建一個(gè)新類型的交換區(qū)。
如果未指定-v選項(xiàng),而且交換區(qū)大小沒(méi)有超過(guò)舊類型交換區(qū)的最大值,同時(shí)當(dāng)前內(nèi)核比2.1.117老(且PAGE_SIZE小于 2048), mkswap 將默認(rèn)創(chuàng)建舊類型的交換區(qū)。萬(wàn)一您的引導(dǎo)分區(qū)或者磁盤卷標(biāo)在第一塊,新類型的頭部將不接觸那些部分,所以可能更好(如果同樣交換區(qū)較?。?,
參考
fdisk(8), swapon(8)
#p#
NAME
mkswap - set up a Linux swap area
SYNOPSIS
mkswap [-c] [-vN] [-f] [-p PSZ] device [size]
DESCRIPTION
mkswap sets up a Linux swap area on a device or in a file.
(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.)
The device argument will usually be a disk partition (something like /dev/hda4 or /dev/sdb7) but can also be a file. The Linux kernel does not look at partition Id's, but many installation scripts will assume that partitions of hex type 82 (LINUX_SWAP) are meant to be swap partitions. (Warning: Solaris also uses this type. Be careful not to kill your Solaris partitions.)
The size parameter is superfluous but retained for backwards compatibility. (It specifies the desired size of the swap area in 1024-byte blocks. mkswap will use the entire partition or file if it is omitted. Specifying it is unwise - a typo may destroy your disk.)
The PSZ parameter specifies the page size to use. It is almost always unnecessary (even unwise) to specify it, but certain old libc versions lie about the page size, so it is possible that mkswap gets it wrong. The symptom is that a subsequent swapon fails because no swap signature is found. Typical values for PSZ are 4096 or 8192.
Linux knows about two styles of swap areas, old style and new style. The last 10 bytes of the first page of the swap area distinguishes them: old style has `SWAP_SPACE', new style has `SWAPSPACE2' as signature.
In the old style, the rest of this first page was a bit map, with a 1 bit for each usable page of the swap area. Since the first page holds this bit map, the first bit is 0. Also, the last 10 bytes hold the signature. So, if the page size is S, an old style swap area can describe at most 8*(S-10)-1 pages used for swapping. With S=4096 (as on i386), the useful area is at most 133890048 bytes (almost 128 MiB), and the rest is wasted. On an alpha and sparc64, with S=8192, the useful area is at most 535560992 bytes (almost 512 MiB).
The old setup wastes most of this bitmap page, because zero bits denote bad blocks or blocks past the end of the swap space, and a simple integer suffices to indicate the size of the swap space, while the bad blocks, if any, can simply be listed. Nobody wants to use a swap space with hundreds of bad blocks. (I would not even use a swap space with 1 bad block.) In the new style swap area this is precisely what is done. The maximum useful size of a swap area now depends on the architecture. It is roughly 2GiB on i386, PPC, m68k, ARM, 1GiB on sparc, 512MiB on mips, 128GiB on alpha and 3TiB on sparc64.
Note that before 2.1.117 the kernel allocated one byte for each page, while it now allocates two bytes, so that taking a swap area of 2 GiB in use might require 2 MiB of kernel memory.
Presently, Linux allows 32 swap areas (this was 8 before Linux 2.4.10). The areas in use can be seen in the file /proc/swaps (since 2.1.25).
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 setup 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(1) to create the file is not acceptable).
OPTIONS
- -c
- Check the device (if it is a block device) for bad blocks before creating the swap area. If any are found, the count is printed.
- -f
- Force - go ahead even if the command is stupid. This allows the creation of a swap area larger than the file or partition it resides on. On SPARC, force creation of the swap area. Without this option mkswap will refuse to create a v0 swap on a device with a valid SPARC superblock, as that probably means one is going to erase the partition table.
- -p PSZ
- Specify the page size to use.
- -v0
- Create an old style swap area.
- -v1
- Create a new style swap area.
If no -v option is given, mkswap will default to new style, but use old style if the current kernel is older than 2.1.117 (and also if PAGE_SIZE is less than 2048). The new style header does not touch the first block, so may be preferable, in case you have a boot loader or disk label there. If you need to use both 2.0 and 2.2 kernels, use the -v0 option when creating the swapspace.
SEE ALSO
fdisk(8), swapon(8)


















