Skip to content

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?

  1. Virtual Memory: Your system's virtual memory is a combination of RAM and swap space.
  2. Memory Overflow: When your system runs out of RAM, it uses swap space to hold inactive memory pages.
  3. Idle Pages: The kernel looks for memory pages (parts of processes) that are not being used (idle pages) and moves them to swap space.
  4. Reassigning RAM: This frees up RAM for other processes that need it.
  5. 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

sudo udevadm settle
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

  1. Start parted Interactive Mode:
    sudo parted /dev/vdb
    
  2. Create a New Partition: In the parted interactive mode, create a new partition for swap:
    (parted) mkpart primary linux-swap 1GB 5GB
    
    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

  1. Create the Partition with parted:

    sudo parted /dev/vdb mkpart primary linux-swap 1GB 5GB
    

  2. Prepare the Partition as Swap with mkswap:

    sudo mkswap /dev/vdb1
    

  3. Enable the Swap Space with swapon:

    sudo swapon /dev/vdb1
    

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

UUID=39e2667a-9458-42fe-9665-c5c854605881 swap swap defaults 0 0

Breakdown of the Example Entry

  1. UUID:
  2. Field: UUID=39e2667a-9458-42fe-9665-c5c854605881
  3. 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 the lsblk --fs command.
  4. Alternative: You can use the device name, such as /dev/vdb1.

  5. Mount Point:

  6. Field: swap
  7. Explanation: For swap devices, this field uses the swap placeholder, indicating the entry is for swap space. This is more informative than the none placeholder used in some documentation.

  8. File-System Type:

  9. Field: swap
  10. Explanation: Indicates the file system type is swap.

  11. Options:

  12. Field: defaults
  13. Explanation: Uses the default mount options. The defaults option includes auto, which ensures the swap space is activated automatically at system boot.

  14. Dump Flag:

  15. Field: 0
  16. Explanation: Set to 0 because swap spaces do not need to be backed up by the dump command.

  17. fsck Order:

  18. Field: 0
  19. Explanation: Set to 0 because swap spaces do not require file system checking with fsck.

Example Process

  1. Edit /etc/fstab:
    sudo nano /etc/fstab
    
  2. Add the Swap Entry:
    UUID=39e2667a-9458-42fe-9665-c5c854605881   swap   swap   defaults   0 0
    
  3. Save and Close: Save the file and exit the editor.
  4. Reload Systemd Daemon:
    sudo systemctl daemon-reload
    
  5. Activate Swap Immediately (Optional):
    sudo swapon -a