Swap Space¶
What is Swap Space?¶
Swap space is a part of your disk that the Linux system uses to help manage memory. Think of it as an extension of your computer's RAM.
How Does Swap Space Work?¶
- Virtual Memory: Your system's virtual memory is a combination of RAM and swap space.
- Memory Overflow: When your system runs out of RAM, it uses swap space to hold inactive memory pages.
- Idle Pages: The kernel looks for memory pages (parts of processes) that are not being used (idle pages) and moves them to swap space.
- Reassigning RAM: This frees up RAM for other processes that need it.
- Page Recall: If a process needs a page that has been moved to swap, the kernel swaps it back into RAM, replacing another idle page if necessary.
Performance Considerations¶
- Speed: Swap space is much slower than RAM because it is located on the disk.
- Temporary Solution: Swap space can help when you run out of RAM, but it is not a permanent fix for low memory. It's best to have enough RAM to handle your workload.
Summary¶
- Swap Space: Disk area used to extend RAM.
- Usage: Holds inactive memory pages when RAM is full.
- Performance: Slower than RAM; should not be relied on as a permanent solution for low memory.
- Management: Kernel automatically manages swapping of pages between RAM and swap space.
Explanation of the Command "udevadm settle"¶
What is udevadm?¶
udevadm
is a command-line utility used to interact with the udev
device manager in Linux. udev
is responsible for managing device nodes in the /dev
directory.
Purpose of "udevadm settle"¶
The command udevadm settle
waits for all current udev events to be processed. Essentially, it ensures that all device events have been handled and the system's state is stable before proceeding.
When to Use¶
- System Initialization: During boot or when scripts need to ensure all devices are properly initialized and ready for use.
- Device Addition/Removal: After adding or removing hardware, to make sure all related events are processed.
- Automation Scripts: In scripts that depend on devices being fully ready before proceeding with further actions.
How it Works¶
When you run udevadm settle
, it waits until there are no more pending udev events. This means the command will not return until the system has finished processing all device-related actions, such as loading drivers or creating device nodes.
Example Usage¶
Running this command ensures that all udev events are processed and the system state is stable. This is useful in scripts that rely on devices being fully ready before moving forward.Summary¶
udevadm
: Utility to interact with udev device manager.settle
: Command that waits for all current udev events to be processed.Use Cases
: Ensuring system stability during initialization, hardware changes, or in automation scripts.Function
: Waits for all device events to be handled, ensuring the system's state is stable.
Using udevadm settle
helps ensure that all device-related events are fully processed before proceeding with subsequent actions or scripts.
Creating a Swap Partition with parted and mkswap¶
Using parted
to Create a Swap Partition¶
- Start parted Interactive Mode:
- Create a New Partition:
In the
parted
interactive mode, create a new partition for swap: This creates a swap partition from 1GB to 5GB on/dev/vdb
.
Using mkswap
after parted
¶
Even after creating the partition with parted
, you still need to use mkswap
to set up the swap area. parted
only creates the partition; it does not prepare it for use as swap space.
Steps to Complete Swap Setup¶
-
Create the Partition with parted:
-
Prepare the Partition as Swap with mkswap:
-
Enable the Swap Space with swapon:
Summary¶
- parted: Creates the partition but does not format it as swap.
- mkswap: Prepares the partition to be used as swap space.
- swapon: Activates the swap space for use by the system.
Yes, you still need mkswap
after creating a swap partition with parted
to properly set up the swap space.
Activating Swap Space Persistently¶
To ensure that your swap space is activated automatically at system boot, you need to create an entry in the /etc/fstab
file. Here is how you can do it based on a previously created swap space.
Steps to Add Swap Space to /etc/fstab¶
Example /etc/fstab Entry¶
Breakdown of the Example Entry¶
- UUID:
- Field:
UUID=39e2667a-9458-42fe-9665-c5c854605881
- Explanation: Specifies the swap device by its UUID. The
mkswap
command displays this UUID when formatting the device. If you lose this information, retrieve it using thelsblk --fs
command. -
Alternative: You can use the device name, such as
/dev/vdb1
. -
Mount Point:
- Field:
swap
-
Explanation: For swap devices, this field uses the
swap
placeholder, indicating the entry is for swap space. This is more informative than thenone
placeholder used in some documentation. -
File-System Type:
- Field:
swap
-
Explanation: Indicates the file system type is
swap
. -
Options:
- Field:
defaults
-
Explanation: Uses the default mount options. The
defaults
option includesauto
, which ensures the swap space is activated automatically at system boot. -
Dump Flag:
- Field:
0
-
Explanation: Set to
0
because swap spaces do not need to be backed up by thedump
command. -
fsck Order:
- Field:
0
- Explanation: Set to
0
because swap spaces do not require file system checking withfsck
.
Example Process¶
- Edit /etc/fstab:
- Add the Swap Entry:
- Save and Close: Save the file and exit the editor.
- Reload Systemd Daemon:
- Activate Swap Immediately (Optional):