xref: /OK3568_Linux_fs/kernel/Documentation/power/swsusp-and-swap-files.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun===============================================
2*4882a593SmuzhiyunUsing swap files with software suspend (swsusp)
3*4882a593Smuzhiyun===============================================
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun	(C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThe Linux kernel handles swap files almost in the same way as it handles swap
8*4882a593Smuzhiyunpartitions and there are only two differences between these two types of swap
9*4882a593Smuzhiyunareas:
10*4882a593Smuzhiyun(1) swap files need not be contiguous,
11*4882a593Smuzhiyun(2) the header of a swap file is not in the first block of the partition that
12*4882a593Smuzhiyunholds it.  From the swsusp's point of view (1) is not a problem, because it is
13*4882a593Smuzhiyunalready taken care of by the swap-handling code, but (2) has to be taken into
14*4882a593Smuzhiyunconsideration.
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunIn principle the location of a swap file's header may be determined with the
17*4882a593Smuzhiyunhelp of appropriate filesystem driver.  Unfortunately, however, it requires the
18*4882a593Smuzhiyunfilesystem holding the swap file to be mounted, and if this filesystem is
19*4882a593Smuzhiyunjournaled, it cannot be mounted during resume from disk.  For this reason to
20*4882a593Smuzhiyunidentify a swap file swsusp uses the name of the partition that holds the file
21*4882a593Smuzhiyunand the offset from the beginning of the partition at which the swap file's
22*4882a593Smuzhiyunheader is located.  For convenience, this offset is expressed in <PAGE_SIZE>
23*4882a593Smuzhiyununits.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunIn order to use a swap file with swsusp, you need to:
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun1) Create the swap file and make it active, eg.::
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun    # dd if=/dev/zero of=<swap_file_path> bs=1024 count=<swap_file_size_in_k>
30*4882a593Smuzhiyun    # mkswap <swap_file_path>
31*4882a593Smuzhiyun    # swapon <swap_file_path>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun2) Use an application that will bmap the swap file with the help of the
34*4882a593SmuzhiyunFIBMAP ioctl and determine the location of the file's swap header, as the
35*4882a593Smuzhiyunoffset, in <PAGE_SIZE> units, from the beginning of the partition which
36*4882a593Smuzhiyunholds the swap file.
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun3) Add the following parameters to the kernel command line::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun    resume=<swap_file_partition> resume_offset=<swap_file_offset>
41*4882a593Smuzhiyun
42*4882a593Smuzhiyunwhere <swap_file_partition> is the partition on which the swap file is located
43*4882a593Smuzhiyunand <swap_file_offset> is the offset of the swap header determined by the
44*4882a593Smuzhiyunapplication in 2) (of course, this step may be carried out automatically
45*4882a593Smuzhiyunby the same application that determines the swap file's header offset using the
46*4882a593SmuzhiyunFIBMAP ioctl)
47*4882a593Smuzhiyun
48*4882a593SmuzhiyunOR
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunUse a userland suspend application that will set the partition and offset
51*4882a593Smuzhiyunwith the help of the SNAPSHOT_SET_SWAP_AREA ioctl described in
52*4882a593SmuzhiyunDocumentation/power/userland-swsusp.rst (this is the only method to suspend
53*4882a593Smuzhiyunto a swap file allowing the resume to be initiated from an initrd or initramfs
54*4882a593Smuzhiyunimage).
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunNow, swsusp will use the swap file in the same way in which it would use a swap
57*4882a593Smuzhiyunpartition.  In particular, the swap file has to be active (ie. be present in
58*4882a593Smuzhiyun/proc/swaps) so that it can be used for suspending.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunNote that if the swap file used for suspending is deleted and recreated,
61*4882a593Smuzhiyunthe location of its header need not be the same as before.  Thus every time
62*4882a593Smuzhiyunthis happens the value of the "resume_offset=" kernel command line parameter
63*4882a593Smuzhiyunhas to be updated.
64