Page MenuHomeDevCentral

Create swap file if no swap partition exist
ClosedPublic

Authored by dereckson on Feb 12 2020, 02:44.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Apr 14, 13:24
Unknown Object (File)
Sat, Apr 13, 16:55
Unknown Object (File)
Sat, Apr 13, 08:09
Unknown Object (File)
Sat, Apr 13, 05:28
Unknown Object (File)
Sat, Apr 13, 05:28
Unknown Object (File)
Sat, Apr 13, 05:28
Unknown Object (File)
Sat, Apr 13, 05:28
Unknown Object (File)
Tue, Apr 9, 23:41
Subscribers
None

Details

Summary

Eglide has consumed all memory 2020-02-11 around 17:32. After investigation,
no swap partition exists on this server.

This scenario will most certainly occur on Scaleway nodes, where no
partition are available, and the image is mounted on /.

This change so fixes this situation by generating a swap file.

Fixes T1590.

Reference: https://devconnected.com/how-to-add-swap-space-on-debian-10-buster/
Signed-off-by: Nidal Iguer <nidal.iguer@gmail.com>

Test Plan

Deploy on Eglide with
salt eglide state.sls roles/core/memory

Check swap with free or swapon

Diff Detail

Repository
rOPS Nasqueron Operations
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

dereckson created this revision.

I've run it on Eglide, it worked like a charm.

First pass

Salt master
$ salt eglide state.sls roles/core/memory
eglide:
----------
          ID: create_swap_file
    Function: cmd.run
        Name: dd if=/dev/zero of=/swapfile bs=1MiB count=8192
      Result: True
     Comment: Command "dd if=/dev/zero of=/swapfile bs=1MiB count=8192" run
     Started: 03:28:11.288879
    Duration: 129354.47 ms
     Changes:
              ----------
              pid:
                  20967
              retcode:
                  0
              stderr:
                  8192+0 records in
                  8192+0 records out
                  8589934592 bytes (8.6 GB, 8.0 GiB) copied, 129.303 s, 66.4 MB/s
              stdout:
----------
          ID: secure_swap_file
    Function: file.managed
        Name: /swapfile
      Result: True
     Comment:
     Started: 03:30:20.667424
    Duration: 307.528 ms
     Changes:
              ----------
              mode:
                  0600
------------
          ID: secure_swap_file
    Function: file.managed
        Name: /swapfile
      Result: True
     Comment:
     Started: 03:30:20.667424
    Duration: 307.528 ms
     Changes:
              ----------
              mode:
                  0600
----------
          ID: enable_swap_file
    Function: cmd.run
        Name: mkswap /swapfile
swapon /swapfile
touch /etc/.swap-enabled

      Result: True
     Comment: Command "mkswap /swapfile
              swapon /swapfile
              touch /etc/.swap-enabled
              " run
     Started: 03:30:20.976252
    Duration: 5521.323 ms
     Changes:
              ----------
              pid:
                  21204
              retcode:
                  0
              stderr:
              stdout:
                  Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
                  no label, UUID=4efc55d7-8945-4c7f-b24c-bc9b53692e71
----------
          ID: configure_fstab_for_swap_file
    Function: file.append
        Name: /etc/fstab
      Result: True
     Comment: Appended 1 lines
     Started: 03:30:26.499496
    Duration: 42.958 ms
     Changes:
              ----------
              diff:
                  ---

                  +++

                  @@ -1 +1,2 @@

                   # UNCONFIGURED FSTAB FOR BASE SYSTEM
                  +/swapfile none swap sw 0 0

Summary for eglide
------------
Succeeded: 4 (changed=4)
Failed:    0
------------
Total states run:     4

It has created correctly 8 GB swap file and enabled it.

Second pass

Salt master
$ salt eglide state.sls roles/core/memory
eglide:
----------
          ID: create_swap_file
    Function: cmd.run
        Name: dd if=/dev/zero of=/swapfile bs=1MiB count=8192
      Result: True
     Comment: /swapfile exists
     Started: 03:36:04.372525
    Duration: 6.852 ms
     Changes:
----------
          ID: secure_swap_file
    Function: file.managed
        Name: /swapfile
      Result: True
     Comment: File /swapfile exists with proper permissions. No changes made.
     Started: 03:36:04.391696
    Duration: 53.085 ms
     Changes:
----------
          ID: enable_swap_file
    Function: cmd.run
        Name: mkswap /swapfile
swapon /swapfile
touch /etc/.swap-enabled

      Result: True
     Comment: /etc/.swap-enabled exists
     Started: 03:36:04.446072
    Duration: 6.358 ms
     Changes:
----------
          ID: configure_fstab_for_swap_file
    Function: file.append
        Name: /etc/fstab
      Result: True
     Comment: File /etc/fstab is in correct state
     Started: 03:36:04.453700
    Duration: 25.928 ms
     Changes:

Summary for eglide
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
Total run time:  92.223 ms

All operations are no op and checked correctly as already done.

@jokerozen confirmed on IRC it looks fine.

This revision is now accepted and ready to land.Feb 12 2020, 14:04