New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a guide to swap #52

Closed
philips opened this Issue Nov 4, 2013 · 15 comments

Comments

Projects
None yet
@philips
Member

philips commented Nov 4, 2013

Here is a starting point. It would be great to make a utility that handled it a bit better.

Setup a swap file in the stateful partition

Run these commands as root to create a 512 megabyte swap.

fallocate -l 512m /media/state/512MiB.swap
chmod 600 /media/state/512MiB.swap
mkswap /media/state/512MiB.swap

Create a unit to initialize it on boot

Create a unit file at /media/state/units/swapon.service with these contents:

[Unit]
Description=Turn on swap

[Service]
Type=oneshot
ExecStart=/sbin/swapon /media/state/512MiB.swap

[Install]
WantedBy=local.target

Enable the unit and start using swap

systemctl enable --runtime /media/state/units/swapon.service
systemctl start swapon

On your next reboot the swap will be turned on automatically.

@ClashTheBunny

This comment has been minimized.

Show comment
Hide comment
@ClashTheBunny

ClashTheBunny Jun 7, 2014

This no longer works due to btrfs not being compatible with swap files. To make it work, I had to adjust the directions so that I created the file somewhere on btrfs (/4GiB.swap) and then used this unit file:

[Unit]
Description=Turn on swap

[Service]
Type=oneshot
Environment="SWAPFILE=/4GiB.swap"
RemainAfterExit=true
ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

[Install]
WantedBy=local.target

It's important to have the "RemainAfterExit" or else the Stop stanzas will be executed immediately.

They say that btrfs performs poorly in this situation, but if it's all you got...

ClashTheBunny commented Jun 7, 2014

This no longer works due to btrfs not being compatible with swap files. To make it work, I had to adjust the directions so that I created the file somewhere on btrfs (/4GiB.swap) and then used this unit file:

[Unit]
Description=Turn on swap

[Service]
Type=oneshot
Environment="SWAPFILE=/4GiB.swap"
RemainAfterExit=true
ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

[Install]
WantedBy=local.target

It's important to have the "RemainAfterExit" or else the Stop stanzas will be executed immediately.

They say that btrfs performs poorly in this situation, but if it's all you got...

@marineam

This comment has been minimized.

Show comment
Hide comment
@marineam

marineam Jun 7, 2014

Contributor

Should work a little better if you disable COW for the swap file when you create it.

Contributor

marineam commented Jun 7, 2014

Should work a little better if you disable COW for the swap file when you create it.

@coderfi

This comment has been minimized.

Show comment
Hide comment
@coderfi

coderfi Aug 5, 2014

Contributor

FYI, to disable COW, I followed the instructions from: https://wiki.archlinux.org/index.php/Btrfs

So this is what I did:

sudo fallocate -l 2048m /2GiB.swap
sudo chmod 600 /2GiB.swap
sudo chattr +C /2GiB.swap
sudo mkswap /2GiB.swap
# ... added the swap.service unit to /var/lib/coreos-install/user_data ...
sudo reboot

After the reboot, I took a look at top and saw the swap active and used

    Swap:  2097148k total,    10144k used,  2087004k free,   245512k cached

Not sure if COW was actually disabled, nor do I know the actual performance gain.

Here is the units snippet in my user_data file (per the above comments):

units:
    - name: swap.service
      command: start
      content: |
        [Unit]
        Description=Turn on swap

        [Service]
        Type=oneshot
        Environment="SWAPFILE=/2GiB.swap"
        RemainAfterExit=true
        ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
        ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
        ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
        ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

        [Install]
        WantedBy=local.target

Hope this helps.

  • Fi
Contributor

coderfi commented Aug 5, 2014

FYI, to disable COW, I followed the instructions from: https://wiki.archlinux.org/index.php/Btrfs

So this is what I did:

sudo fallocate -l 2048m /2GiB.swap
sudo chmod 600 /2GiB.swap
sudo chattr +C /2GiB.swap
sudo mkswap /2GiB.swap
# ... added the swap.service unit to /var/lib/coreos-install/user_data ...
sudo reboot

After the reboot, I took a look at top and saw the swap active and used

    Swap:  2097148k total,    10144k used,  2087004k free,   245512k cached

Not sure if COW was actually disabled, nor do I know the actual performance gain.

Here is the units snippet in my user_data file (per the above comments):

units:
    - name: swap.service
      command: start
      content: |
        [Unit]
        Description=Turn on swap

        [Service]
        Type=oneshot
        Environment="SWAPFILE=/2GiB.swap"
        RemainAfterExit=true
        ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
        ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
        ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
        ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

        [Install]
        WantedBy=local.target

Hope this helps.

  • Fi
@marineam

This comment has been minimized.

Show comment
Hide comment
@marineam

marineam Aug 5, 2014

Contributor

You may need to disable cow before using fallocate, you can only disable
cow on zero length files and off the top of my head I don't know if
fallocate counts towards that restriction or not.
On Aug 5, 2014 3:22 AM, "coderfi" notifications@github.com wrote:

FYI, to disable COW, I followed the instructions from:
https://wiki.archlinux.org/index.php/Btrfs

So this is what I did:

  • sudo fallocate -l 2048m /2GiB.swap
  • sudo chmod 600 /2GiB.swap
  • sudo chattr +C /2GiB.swap
  • sudo mkswap /2GiB.swap
  • added the swap.service unit to /var/lib/coreos-install/user_data
  • sudo reboot
  • took a look at top and saw the swap active and used Swap: 2097148k
    total, 10144k used, 2087004k free, 245512k cached

Not sure if COW was actually disabled, nor do I know the actual
performance gain.

Here is the units snippet in my user_data file (per the above comments):

units:

  • name: swap.service
    command: start
    content: |
    [Unit]
    Description=Turn on swap

    [Service]
    Type=oneshot
    Environment="SWAPFILE=/2GiB.swap"
    RemainAfterExit=true
    ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
    ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

    [Install]
    WantedBy=local.target


Reply to this email directly or view it on GitHub
#52 (comment).

Contributor

marineam commented Aug 5, 2014

You may need to disable cow before using fallocate, you can only disable
cow on zero length files and off the top of my head I don't know if
fallocate counts towards that restriction or not.
On Aug 5, 2014 3:22 AM, "coderfi" notifications@github.com wrote:

FYI, to disable COW, I followed the instructions from:
https://wiki.archlinux.org/index.php/Btrfs

So this is what I did:

  • sudo fallocate -l 2048m /2GiB.swap
  • sudo chmod 600 /2GiB.swap
  • sudo chattr +C /2GiB.swap
  • sudo mkswap /2GiB.swap
  • added the swap.service unit to /var/lib/coreos-install/user_data
  • sudo reboot
  • took a look at top and saw the swap active and used Swap: 2097148k
    total, 10144k used, 2087004k free, 245512k cached

Not sure if COW was actually disabled, nor do I know the actual
performance gain.

Here is the units snippet in my user_data file (per the above comments):

units:

  • name: swap.service
    command: start
    content: |
    [Unit]
    Description=Turn on swap

    [Service]
    Type=oneshot
    Environment="SWAPFILE=/2GiB.swap"
    RemainAfterExit=true
    ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
    ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

    [Install]
    WantedBy=local.target


Reply to this email directly or view it on GitHub
#52 (comment).

@cboettig

This comment has been minimized.

Show comment
Hide comment
@cboettig

cboettig Nov 19, 2014

Thanks for this, worked like a charm for me. Is it possible to have the commands sudo fallocate... also specified in the cloud-config file instead of having to issue these manually? I see there is no runcmd in a coreos cloud-config file...

cboettig commented Nov 19, 2014

Thanks for this, worked like a charm for me. Is it possible to have the commands sudo fallocate... also specified in the cloud-config file instead of having to issue these manually? I see there is no runcmd in a coreos cloud-config file...

@romaninsh

This comment has been minimized.

Show comment
Hide comment
@romaninsh

romaninsh Dec 10, 2014

@cboettig Here, I used the following service, which also executes fallocate: https://gist.github.com/romaninsh/118952ce61643914fb00

romaninsh commented Dec 10, 2014

@cboettig Here, I used the following service, which also executes fallocate: https://gist.github.com/romaninsh/118952ce61643914fb00

@joshperry

This comment has been minimized.

Show comment
Hide comment
@joshperry

joshperry Nov 1, 2015

Why not support the canonical swap configuration? http://cloudinit.readthedocs.org/en/latest/topics/examples.html#adjust-mount-points-mounted

Using this: https://gist.github.com/tsertkov/573d077012ae76443cbf but 50 lines of config just for a swapfile seems excessive.

joshperry commented Nov 1, 2015

Why not support the canonical swap configuration? http://cloudinit.readthedocs.org/en/latest/topics/examples.html#adjust-mount-points-mounted

Using this: https://gist.github.com/tsertkov/573d077012ae76443cbf but 50 lines of config just for a swapfile seems excessive.

@crawford

This comment has been minimized.

Show comment
Hide comment
Member

crawford commented Dec 1, 2015

amcsi pushed a commit to amcsi/szeremi-nginx that referenced this issue Mar 5, 2016

@joelchen

This comment has been minimized.

Show comment
Hide comment
@joelchen

joelchen Apr 20, 2016

Are minimum of round(sqrt(RAM)) and maximum of twice the amount of RAM from https://help.ubuntu.com/community/SwapFaq recommendations we should follow on CoreOS?

joelchen commented Apr 20, 2016

Are minimum of round(sqrt(RAM)) and maximum of twice the amount of RAM from https://help.ubuntu.com/community/SwapFaq recommendations we should follow on CoreOS?

@crawford

This comment has been minimized.

Show comment
Hide comment
@crawford

crawford Apr 21, 2016

Member

@joelchen most CoreOS instances don't use swap at all, so the minimum is zero. Canonical's recommended upper bound seems reasonable.

Member

crawford commented Apr 21, 2016

@joelchen most CoreOS instances don't use swap at all, so the minimum is zero. Canonical's recommended upper bound seems reasonable.

@joelchen

This comment has been minimized.

Show comment
Hide comment
@joelchen

joelchen Apr 21, 2016

@crawford What are the recommended use cases for no swap, as well as for using swap in CoreOS?

joelchen commented Apr 21, 2016

@crawford What are the recommended use cases for no swap, as well as for using swap in CoreOS?

@crawford

This comment has been minimized.

Show comment
Hide comment
@crawford

crawford Apr 21, 2016

Member

@joelchen it depends heavily on how you plan on using the machine and the physical characteristics of the machine. I would suggest taking a look at what Linux recommends and then going with that.

Member

crawford commented Apr 21, 2016

@joelchen it depends heavily on how you plan on using the machine and the physical characteristics of the machine. I would suggest taking a look at what Linux recommends and then going with that.

@HaleTom

This comment has been minimized.

Show comment
Hide comment
@HaleTom

HaleTom Oct 25, 2016

Do not try what @coderfi suggested, namely, disabling CoW to try to use a swapfile with btrfs, until you read that it is supported in the official documentation.

swap file support relies on one function that btrfs intentionally does not implement due to potential corruptions.

However:

A workaround, albeit with poor performance, is to mount a swap file via a loop device.

HaleTom commented Oct 25, 2016

Do not try what @coderfi suggested, namely, disabling CoW to try to use a swapfile with btrfs, until you read that it is supported in the official documentation.

swap file support relies on one function that btrfs intentionally does not implement due to potential corruptions.

However:

A workaround, albeit with poor performance, is to mount a swap file via a loop device.

@philips

This comment has been minimized.

Show comment
Hide comment
@philips

philips Oct 28, 2016

Member

Someone just mentioned losteup leads to lockups https://groups.google.com/d/msg/coreos-user/xw4aeC68k6Q/vy8Iv0jWCAAJ. Can someone please write this guide? It is clearly tripping up a lot of users on something very simple. cc @ElijahCaine

Member

philips commented Oct 28, 2016

Someone just mentioned losteup leads to lockups https://groups.google.com/d/msg/coreos-user/xw4aeC68k6Q/vy8Iv0jWCAAJ. Can someone please write this guide? It is clearly tripping up a lot of users on something very simple. cc @ElijahCaine

@pop

This comment has been minimized.

Show comment
Hide comment
@pop

pop Oct 28, 2016

Contributor

@philips I'm writing and testing a doc now. PR should be up by the end of the day.

Contributor

pop commented Oct 28, 2016

@philips I'm writing and testing a doc now. PR should be up by the end of the day.

pop added a commit to pop/coreos-docs that referenced this issue Oct 30, 2016

@pop pop referenced this issue Oct 30, 2016

Merged

os: Added `adding-swap` guide. #909

2 of 3 tasks complete

pop added a commit to pop/coreos-docs that referenced this issue Nov 7, 2016

os: `adding-swap` guide
Outlines use-cases, a systemd unit file, and Ignition example.
Refs to #52

pop added a commit to pop/coreos-docs that referenced this issue Nov 7, 2016

os: `adding-swap` guide
Outlines use-cases, a systemd unit file, and Ignition example.
Refs to #52

@joshix joshix closed this in #909 Nov 8, 2016

joshix pushed a commit that referenced this issue Nov 8, 2016

os: `adding-swap` guide (#909)
Outlines use-cases, a systemd unit file, and Ignition example.
Refs to #52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment