Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Puppet class to manage a swapfile on a node
# Class: base::swap
#
# This class manages swapspace on a node.
#
# Parameters:
# - $ensure Allows creation or removal of swapspace and the corresponding file.
# - $swapfile Defaults to /mnt which is a fast ephemeral filesystem on EC2 instances.
# This keeps performance reasonable while avoiding I/O charges on EBS.
# - $swapfilesize Size of the swapfile in MB. Defaults to memory size, but see Requires.
#
# Todo:
# Manage /etc/fstab
#
# Actions:
# Creates and mounts a swapfile.
# Umounts and removes a swapfile.
#
# Requires:
# memorysizeinbytes fact - See:
# https://blog.kumina.nl/2011/03/facter-facts-for-memory-in-bytes/
#
# Sample Usage:
# class { 'base::swap':
# ensure => present,
# }
#
# class { 'base::swap':
# ensure => absent,
# }
#
# If you have the swapsizeinbytes fact (see Requires), you could also do:
# if $::swapsizeinbytes == 0 {
# include base::swap
# }
#
class base::swap(
$ensure = 'present',
$swapfile = '/mnt/swap.1', # Where to create the swapfile.
$swapfilesize = $::memorysizeinbytes/1000000 # Size in MB. Defaults to memory size.
) {
if $ensure == 'present' {
exec { 'Create swap file':
command => "/bin/dd if=/dev/zero of=${swapfile} bs=1M count=${swapfilesize}",
creates => $swapfile,
}
exec { 'Attach swap file':
command => "/sbin/mkswap ${swapfile} && /sbin/swapon ${swapfile}",
require => Exec['Create swap file'],
unless => "/sbin/swapon -s | grep ${swapfile}",
}
}
elsif $ensure == 'absent' {
exec { 'Detach swap file':
command => "/sbin/swapoff ${swapfile}",
onlyif => "/sbin/swapon -s | grep ${swapfile}",
}
file { $swapfile:
ensure => absent,
require => Exec['Detach swap file'],
}
}
}
@gotpredictions

This comment has been minimized.

Copy link

gotpredictions commented Jul 27, 2013

I made a few changes to this to convert it from class to type, which allows to manage multiple swap file and also manage fstab:
``
define gnana::swap (
$ensure = 'present',
$swapfile = $name,
$swapsize = $::memorysize_mb
) {
$swapsizes = split("${swapsize}",'[.]')
$swapfilesize = $swapsizes[0]

file_line { "swap_fstab_line_${swapfile}":
ensure => $ensure,
line => "${swapfile} swap swap defaults 0 0",
path => "/etc/fstab",
match => "${swapfile}",
}

if $ensure == 'present' {
exec { 'Create swap file':
command => "/bin/dd if=/dev/zero of=${swapfile} bs=1M count=${swapfilesize}",
creates => $swapfile,
}

exec { 'Attach swap file':
  command => "/sbin/mkswap ${swapfile} && /sbin/swapon ${swapfile}",
  require => Exec['Create swap file'],
  unless  => "/sbin/swapon -s | grep ${swapfile}",
}

} elsif $ensure == 'absent' {
exec { 'Detach swap file':
command => "/sbin/swapoff ${swapfile}",
onlyif => "/sbin/swapon -s | grep ${swapfile}",
}

file { $swapfile:
  ensure  => absent,
  require => Exec['Detach swap file'],
  backup => false
}

}
}
``

@philfreo

This comment has been minimized.

Copy link

philfreo commented Sep 14, 2013

@gotpredictions thanks... but can you click 'Fork' here and then paste your updated version so it has better formatting?

@philfreo

This comment has been minimized.

Copy link

philfreo commented Sep 16, 2013

I cleaned it up and posted it here: https://gist.github.com/philfreo/6576492

@petems

This comment has been minimized.

Copy link

petems commented Mar 17, 2015

This is still a super-high google result for "puppet swap" so I thought I'd link that I made gist into a module:
https://forge.puppetlabs.com/petems/swap_file 😄

@khanderson

This comment has been minimized.

Copy link

khanderson commented Nov 22, 2018

This creates a world-readable swapfile, which would allow any local user to read swapped-out memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.