Tutorials
goetia
Software
0x53.net

Swap

Similar to mount, there are mainly two ways to mount and enable swap partitions:

The second approach is highly recommended for the reasons listed on the mount page.

Unencrypted swap partition/file

Create the following oneshot source directory in the src/swap/swap-@PART@ subdirectory of the system configuration directory.

swap-@PART@
├── dependencies.d
│   ├── mount-dev     # empty/arbitrary
│   └── mount-sys     # empty/arbitrary
├── up                # see below
├── down              # see below
└── type              # oneshot
			

with the following up script:

#!/bin/execlineb -P

fdmove -c 2 1
swapon @PARTITION_OR_SWAPFILE@
			

and the following down script:

#!/bin/execlineb -P

fdmove -c 2 1
swapoff @PARTITION_OR_SWAPFILE@
			

replace @PARTITION_OR_SWAPFILE@ with the path of the partition or swap file; or generate it from a UUID or PARTUUID (see the mount page) and @PART@ with a descriptive name of the swap partition.

Encrypted swap partition

If an encrypted swap file is desired, just put it on an already encrypted, mounted partition and follow the procedure for unencrypted swap.

The following will format the partition with a random key. This will happen during every boot, which eliminates the need to decrypt the swap partition during boot.

Create the following oneshot source directory in the src/swap/swap-@PART@ subdirectory of the system configuration directory.

swap-@PART@
├── dependencies.d
│   ├── random-seed-load   # empty/arbitrary
│   ├── mount-dev          # empty/arbitrary
│   └── mount-sys          # empty/arbitrary
├── up                     # see below
├── down                   # see below
└── type                   # oneshot
			

with the following up script:

#!/bin/execlineb -P

fdmove -c 2 1

if {
	redirfd -a 2 /dev/null
	cryptsetup open
		--allow-discards
		--perf-no_read_workqueue
		--perf-no_write_workqueue
		--type plain
		--key-file /dev/urandom
		@PARTITION@ swap-@PART@
}

if { mkswap -q /dev/mapper/swap-@PART@ }

swapon -d /dev/mapper/swap-@PART@
			

and the following down script:

#!/bin/execlineb -P

fdmove -c 2 1

if -x0 { swapoff /dev/mapper/swap-@PART@ }

# hack since swapoff exits before finishing
# the actual swapoff
# resulting in device or resource busy
# during the command below
foreground { sleep 1 }

cryptsetup close swap-@PART@
			

replace @PARTITION@ with the path of the partition; or generate it from a UUID or PARTUUID (see the mount page) and @PART@ with a descriptive name of the swap partition.