Creating a Swap Area
New swap partitions are not created directly by the kernel itself. This task is delegated to a userspace tool (mkswap) whose sources are in the util-linux-ng tool collection. Since creating a swap area is a mandatory step that must be performed before swap memory can be used, let's briefly analyze the mode of operation of this utility.
The kernel need not provide any new system calls to support the creation of swap areas — after all, it also does not provide any system calls to create regular filesystems, and this is clearly not a kernel problem. The existing call variants for direct communication with block devices (or, in the case of a swap file, with a file on a block device) are quite sufficient to organize the swap area in accordance with kernel requirements.
mkswap requires just one argument — the name of the device file of the partition or file in which the swap area is to be created.3 The following actions are performed:
□ The size of the required swap area is divided by the page size of the machine concerned in order to determine how many page frames can be accommodated.
□ The blocks of the swap area are checked individually for read or write errors in order to find defective areas. As the machine's page size is used as the block size for swap areas, a defective block always means that the swap area's capacity is reduced by one page.
□ A list with the addresses of all defective blocks is written to the first page of the swap area.
2A comment in the kernel sources notes that measurements have demonstrated that on average only 0.3 list operations are, in fact, needed to create a mapping between a page slot and a block number.
3Other parameters such as the explicit size of the swap area or the page size can be specified. However, in most cases, this is pointless because these data can be calculated automatically and reliably. The authors of mkswap do not have a high opinion of users who make their own explicit specifications, as the source code shows:
/* this silly user specified the number of blocks explicitly */ }
□ To identify the swap area as such to the kernel (after all, it could simply be a normal partition with filesystem data which, of course, may not be inadvertently overwritten if the administrator uses an invalid swap area), the SWAPSPACE2 label is set to the end of the first page.4
□ The number of available pages is also stored in the header of the swap area. This figure is calculated by subtracting the number of defective pages from the total number of available pages. 1 must also be subtracted from this number since the first page is used for state information and for the list of defective blocks.
Although it may seem very important to deal with defective blocks when a swap area is created, this activity can simply be skipped. In this case, mkswap does not check the data area for errors and consequently does not write any data into the list of defective blocks. Since the quality of today's hardware means that very few errors occur on block devices, an explicit check is normally not needed.
Continue reading here: Activating a Swap Area
Was this article helpful?