Skip to content
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
Closed

add a guide to swap #52

philips opened this issue Nov 4, 2013 · 15 comments

Comments

@philips
Copy link
Contributor

@philips 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
Copy link

@ClashTheBunny 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
Copy link
Contributor

@marineam marineam commented Jun 7, 2014

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

@coderfi
Copy link
Contributor

@coderfi 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
Copy link
Contributor

@marineam 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
Copy link

@cboettig 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
Copy link

@romaninsh romaninsh commented Dec 10, 2014

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

@joshperry
Copy link

@joshperry 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
Copy link
Member

@crawford crawford commented Dec 1, 2015

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

@joelchen 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
Copy link
Member

@crawford 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
Copy link

@joelchen joelchen commented Apr 21, 2016

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

@crawford
Copy link
Member

@crawford 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
Copy link

@HaleTom 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
Copy link
Contributor Author

@philips 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
Copy link
Contributor

@pop 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 mentioned this issue Oct 30, 2016
2 of 3 tasks complete
pop added a commit to pop/coreos-docs that referenced this issue Nov 7, 2016
Outlines use-cases, a systemd unit file, and Ignition example.
Refs to coreos#52
pop added a commit to pop/coreos-docs that referenced this issue Nov 7, 2016
Outlines use-cases, a systemd unit file, and Ignition example.
Refs to coreos#52
@joshix joshix closed this in #909 Nov 8, 2016
joshix pushed a commit that referenced this issue Nov 8, 2016
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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.