7

I am looking at mkswap and trying to understand how exactly it works. From what I understand, it sets up Swap space on a partition by creating a swap header. How large is this swap header? Is it one block? Or is it always a consistent size?

I did try looking through the mkswap.c source code, but my C-knowledge is not enough to understand it. Specifically, I think the following line writes out the swap header:

strncpy((char*)signature_page+PAGE_SIZE-10,"SWAP-SPACE",10);
1

2 Answers 2

8

I can see that O'Reilly's "Understanding the Linux Kernel" by Mssrs Bovet and Cesati, page 714 (Swap Area), provides an answer to this question. To paraphrase, the swap_header is composed of two structures, "info" and "magic". The code I pasted above relates to the magic part - it literally writes out "SWAP-SPACE" as the final 10 characters of the first page (in some versions it is "SWAPSPACE2").

The info structure begins after the first 1024 bytes, and has various fields to describe the swap mechanism.

1
  • Please also mention the edition the page number refers to.
    – phk
    Jun 6, 2017 at 8:19
1

For those of us who are still curious as to what swapon actually writes in the swap partition. Here is how I did it and how you can check yourself:

dd if=/dev/zero of=/hdd/myswap bs=1K count=40
mkswap /hdd/myswap
hexdump -C /hdd/myswap

Result:

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  01 00 00 00 09 00 00 00  00 00 00 00 22 e8 83 6e  |............"..n|
00000410  88 06 4a 0b 83 bc 12 44  8e 3e a3 e0 00 00 00 00  |..J....D.>......|
00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000ff0  00 00 00 00 00 00 53 57  41 50 53 50 41 43 45 32  |......SWAPSPACE2|
00001000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
0000a000

It precisely only writes a header to the assigned area.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .