Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Forked from kacinskas/CoreOS swap
Last active May 28, 2021 05:18
Show Gist options
  • Select an option

  • Save tsertkov/573d077012ae76443cbf to your computer and use it in GitHub Desktop.

Select an option

Save tsertkov/573d077012ae76443cbf to your computer and use it in GitHub Desktop.

Revisions

  1. tsertkov revised this gist Nov 6, 2014. 2 changed files with 49 additions and 39 deletions.
    39 changes: 0 additions & 39 deletions CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -1,39 +0,0 @@
    # swith to sudo
    sudo -i

    # create swap
    touch /2GiB.swap
    chattr +C /2GiB.swap
    fallocate -l 2048m /2GiB.swap
    chmod 600 /2GiB.swap
    mkswap /2GiB.swap

    # create service unit to start swap [/etc/systemd/system/swap.service]
    [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=multi-user.target

    # add service and start
    systemctl enable /etc/systemd/system/swap.service
    systemctl start swap

    # reboot
    reboot

    # Options
    cat /proc/sys/vm/swappiness
    sysctl vm.swappiness=10
    sysctl vm.vfs_cache_pressure=50

    https://github.com/coreos/docs/issues/52
    49 changes: 49 additions & 0 deletions cloud-config
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #cloud-config

    coreos:
    units:
    - name: systemd-sysctl.service
    command: restart
    - name: create-swap.service
    command: start
    runtime: true
    content: |
    [Unit]
    Description=Create swap file
    Before=swap.service

    [Service]
    Type=oneshot
    Environment="SWAPFILE=/2GiB.swap"
    ExecStart=/usr/bin/touch ${SWAPFILE}
    ExecStart=/usr/bin/chattr +C ${SWAPFILE}
    ExecStart=/usr/bin/fallocate -l 2048m ${SWAPFILE}
    ExecStart=/usr/bin/chmod 600 ${SWAPFILE}
    ExecStart=/usr/sbin/mkswap ${SWAPFILE}

    [Install]
    WantedBy=multi-user.target
    - 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=multi-user.target
    write_files:
    - path: /etc/sysctl.d/swap.conf
    permissions: 0644
    owner: root
    content: |
    vm.swappiness=10
    vm.vfs_cache_pressure=50
  2. @kacinskas kacinskas revised this gist Sep 12, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,6 @@ Type=oneshot
    Environment="SWAPFILE=/2GiB.swap"
    RemainAfterExit=true
    ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
    ExecStartPre=sysctl vm.swappiness=10
    ExecStartPre=sysctl vm.vfs_cache_pressure=50
    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)"
  3. @kacinskas kacinskas revised this gist Sep 12, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@ Type=oneshot
    Environment="SWAPFILE=/2GiB.swap"
    RemainAfterExit=true
    ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
    ExecStartPre=sysctl vm.swappiness=10
    ExecStartPre=sysctl vm.vfs_cache_pressure=50
    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)"
  4. @kacinskas kacinskas revised this gist Sep 12, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr
    ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

    [Install]
    WantedBy=local.target
    WantedBy=multi-user.target

    # add service and start
    systemctl enable /etc/systemd/system/swap.service
    @@ -34,5 +34,6 @@ reboot
    # Options
    cat /proc/sys/vm/swappiness
    sysctl vm.swappiness=10
    sysctl vm.vfs_cache_pressure=50

    https://github.com/coreos/docs/issues/52
  5. @kacinskas kacinskas revised this gist Sep 12, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -25,10 +25,14 @@ ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPF
    WantedBy=local.target

    # add service and start
    systemctl enable --runtime /etc/systemd/system/swap.service
    systemctl enable /etc/systemd/system/swap.service
    systemctl start swap

    # reboot
    reboot

    # Options
    cat /proc/sys/vm/swappiness
    sysctl vm.swappiness=10

    https://github.com/coreos/docs/issues/52
  6. @kacinskas kacinskas created this gist Sep 12, 2014.
    34 changes: 34 additions & 0 deletions CoreOS swap
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # swith to sudo
    sudo -i

    # create swap
    touch /2GiB.swap
    chattr +C /2GiB.swap
    fallocate -l 2048m /2GiB.swap
    chmod 600 /2GiB.swap
    mkswap /2GiB.swap

    # create service unit to start swap [/etc/systemd/system/swap.service]
    [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

    # add service and start
    systemctl enable --runtime /etc/systemd/system/swap.service
    systemctl start swap

    # reboot
    reboot

    https://github.com/coreos/docs/issues/52