GithubHelp home page GithubHelp logo

petems-swap_file's Introduction

swap_file

Puppet Forge Build Status Puppet Forge Downloads Puppet Forge Endorsement

Table of Contents

  1. Overview
  2. Module Description
  3. Setup
  4. Usage
  5. Limitations
  6. Upgrading from 1.0.1 Release
  7. Development

Overview

Manage swap files for your Linux environments. This is based on the gist by @Yggdrasil, with a few changes and added specs.

Setup

What swap_file affects

  • Creating a swap-file on disk. This uses dd by default, but can use fallocate optionally for performance reasons. Note: Using fallocate to create a ZFS file system will fail: https://bugzilla.redhat.com/show_bug.cgi?id=1129205
  • Swapfiles on the system
  • Any mounts of swapfiles

Usage

The simplest use of the module is this:

swap_file::files { 'default':
  ensure   => present,
}

By default, the module it will:

  • create a file using /bin/dd at /mnt/swap.1 with the default size taken from the $::memorysize fact in megabytes (eg. 8GB RAM will create an 8GB swap file)
  • A mount for the swapfile created

For a custom setup, you can do something like this:

swap_file::files { 'tmp file swap':
  ensure    => present,
  swapfile  => '/tmp/swapfile',
  add_mount => false,
}

To use fallocate for swap file creation instead of dd:

swap_file::files { 'tmp file swap':
  ensure    => present,
  swapfile  => '/tmp/swapfile',
  cmd       => 'fallocate',
}

To remove a prexisting swap, you can use ensure absent:

swap_file::files { 'tmp file swap':
  ensure   => absent,
}

To choose the size of the swap file instead of defaulting to memory size:

swap_file::files { '5GB Swap':
  ensure       => present,
  swapfile     => '/mnt/swap.5gb',
  swapfilesize => '5GB',
}

hiera

You can also use hiera to call this module and set the configuration.

The simplest use of the module with hiera is this:

classes:
  - swap_file

swap_file::files:
  'default':
    ensure: 'present'

This hiera setup will create a file using /bin/dd atr /mnt/swap.1 with the default size taken from the $::memorysize fact and add a mount resource for it.

You can use all customizations mentioned above in hiera like this:

classes:
  - swap_file

swap_file::files:
  'custom setup':
    ensure: 'present'
    swapfile: '/tmp/swapfile.custom'
    add_mount: false
  'use fallocate':
    swapfile: '/tmp/swapfile.fallocate'
    cmd: 'fallocate'
  'remove swap file'
    ensure: 'absent'
    swapfile: '/tmp/swapfile.old'

This hiera config will respectively:

  • create a file /tmp/swapfile.custom using /bin/dd with the default size taken from the $::memorysize fact without creating a mount for it.
  • create a file /tmp/swapfile.fallocate using /usr/bin/fallocate with the default size taken from the $::memorysize fact and creating a mount for it.
  • deactivates the swapfile /tmp/swapfile.old, deletes it and removes the mount.

Set $files_hiera_merge to true to merge all found instances of swap_file::files in Hiera. This is useful for specifying swap files at different levels of the hierachy and having them all included in the catalog.

##Upgrading from 1.0.1 Release

Previously you would create swapfiles with the swap_file class:

class { 'swap_file':
  ensure => 'present',
}

However, this had many problems, such as not being able to declare more than one swap_file because of duplicate class errors. Since 2.x.x the swapfiles are created by a defined type instead. The swap_file class is now a wrapper and can handle multiple swap_files.

You can now use:

class { 'swap_file':
  files => {
    'freetext resource name' => {
      ensure => 'present',
    },
  },
}

You can also safely declare mutliple swap file definitions:

class { 'swap_file':
  files => {
    'swapfile' => {
      ensure => 'present',
    },
    'use fallocate' => {
      swapfile => '/tmp/swapfile.fallocate',
      cmd      => 'fallocate',
    },
    'remove swap file' => {
      ensure   => 'absent',
      swapfile => '/tmp/swapfile.old',
    },
  },
}

Limitations

Primary support is for Debian and RedHat, but should work on all Linux flavours.

Right now there is no BSD support, but I'm planning on adding it in the future

Development

Follow the CONTRIBUTING guidelines! :)

petems-swap_file's People

Contributors

biandrews avatar bodgit avatar jtreminio avatar juame avatar mattock avatar ozbillwang avatar petems avatar phil-friderici avatar yalcinsurkultay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

petems-swap_file's Issues

Cannot change size of existing swap file

Once a swap file exists, trying to change its size via swapfilesize does not yield any changes. This is due to the module only checking for existence of the swap file, not its current size.

Directory not empty @ dir_s_rmdir

Environment:

  • puppet 4.x
  • mod 'petems/swap_file', '3.0.1'

Steps to reproduce:

puppet apply with

swap_file::files { 'default':
  ensure   => present,
}

I get:

/usr/lib/ruby/2.3.0/fileutils.rb:1445:in `rmdir': Directory not empty @ dir_s_rmdir - /tmp/vagrant-puppet/environments/default/modules/swap_file (Errno::ENOTEMPTY)

Function `validate_legacy` is deprecated

Function validate_legacy is deprecated and swap_file manifests now raise deprecation warnings.

Warning: This method is deprecated, please use the stdlib validate_legacy function, with Pattern[]. There is further documentation for validate_legacy function in the README. at ["/etc/puppetlabs/code/environments/production/modules/swap_file/manifests/files.pp", 48]:
   (location: /etc/puppetlabs/code/environments/production/modules/stdlib/lib/puppet/functions/deprecation.rb:34:in `deprecation')
Warning: This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::String. There is further documentation for validate_legacy function in the README. at ["/etc/puppetlabs/code/environments/production/modules/swap_file/manifests/files.pp", 49]:
   (location: /etc/puppetlabs/code/environments/production/modules/stdlib/lib/puppet/functions/deprecation.rb:34:in `deprecation')
Warning: This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Bool. There is further documentation for validate_legacy function in the README. at ["/etc/puppetlabs/code/environments/production/modules/swap_file/manifests/files.pp", 51]:
   (location: /etc/puppetlabs/code/environments/production/modules/stdlib/lib/puppet/functions/deprecation.rb:34:in `deprecation')

Is this module still maintained?

Hi @petems. Can you please let us know whether you intend to keep maintaining and releasing this puppet module? It seems to still have decent usage, so keeping it updated would be great.

Docker Beaker tests always fail

     Beaker::Host::CommandFailure: Host 'centos-6-x64' exited with 6 running:
  env PATH="/usr/bin:/opt/puppet-git-repos/hiera/bin:${PATH}" RUBYLIB="/opt/puppet-git-repos/hiera/lib:/opt/puppet-git-repos/hiera-puppet/lib:${RUBYLIB}" puppet apply --verbose --detailed-exitcodes /tmp/apply_manifest.pp.4f5vEF  
Last 10 lines of output were:
    Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns:         on whole disk. Use -f to force.
    Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: Setting up swapspace version 1, size = 5116 KiB
    Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: no label, UUID=d5baf075-d80d-436e-8784-6ef4c98b0240
    Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: swapon: /tmp/swapfile: swapon failed: Input/output error
    Error: /sbin/mkswap /tmp/swapfile && /sbin/swapon /tmp/swapfile returned 255 instead of one of [0]
    Error: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: change from notrun to 0 failed: /sbin/mkswap /tmp/swapfile && /sbin/swapon /tmp/swapfile returned 255 instead of one of [0]
    Notice: /Stage[main]/Swap_file/Mount[swap]: Dependency Exec[Attach swap file] has failures: true
    Warning: /Stage[main]/Swap_file/Mount[swap]: Skipping because of failed dependencies
    Info: Creating state file /var/lib/puppet/state/state.yaml
    Notice: Finished catalog run in 0.24 seconds

Puppet 8 compatibility

puppet-lint issues:

WARNING: legacy fact 'memorysize' on line 37 (check: legacy_facts)
WARNING: legacy fact 'selinux' on line 107 (check: legacy_facts)
WARNING: top scope fact instead of facts hash on line 37 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 55 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 57 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 59 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 64 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 71 (check: top_scope_facts)
WARNING: top scope fact instead of facts hash on line 107 (check: top_scope_facts)

Not issue, ask a question

Last time, when I put the merge and pull requests in your repos, github automatically redirected to a URL which build the package automatically (verify the code in different puppet versions), it refused my first pull request, then I fixed the issue, and submit again.

it is great function, May I know how to set it up?

missed "default" in fstab

Hi Peter,

I tried this module, it runs fine, I got swap space properly after reboot as well. But the setting in /etc/fstab is not perfect.

/swapfile   swap    swap        0   0

could you please fix as (6 columns)?

/swapfile   swap    swap    defaults    0   0

file fstab format for your reference

<device> <mountpoint> <filesystemtype> <options> <dump> <fsckorder>

option "defaults" meaning

defaults

Use default settings. Equivalent to rw,suid,dev,exec,auto,nouser,async.

There are numerous options for the specific filesystes supported by mount.
However these are some of the more useful, for the full list check out the man page for `mount`.

mount resource should be unique

This is similar to the issue with being able to resize a swapfile.

If mount { 'swap': ... } was changed to mount { $swapfile: ... }, then multiple swapfiles could be created or an old one could be changed to ensure=>absent, and a new one with a different size could be created.

cannot remove xxx.pack: Device or resource busy

Environment:

  • vagrant 1.8.4
  • puppet 4.5.2
  • librarian-puppet 2.2.3
  • mod 'petems/swap_file', '3.0.1'
  • host: Win 7
  • guest: ubuntu/xenial64
  • vagrant plugins (current versions): vagrant-vbguest vagrant-puppet-install vagrant-triggers

I'd like to do the following cleanup tasks with vagrant destroy:

  config.trigger.after :destroy do
    run "rm -Rf puppet/environments/default/.tmp/*"
    run "rm -Rf puppet/environments/default/.librarian/*"
    run "rm -f puppet/environments/default/Puppetfile.lock"
    run "rm -Rf puppet/environments/default/modules/*"
  end

However, rm -Rf puppet/environments/default/modules/* fails with the following message:
/usr/bin/rm: cannot remove 'puppet/environments/default/modules/swap_file/spec/fixtures/modules/stdlib/.git/objects/pack/pack-817ca3838f77e03f7d65ebde9778cba43a74a5de.pack': Device or resource busy

I need to delete the remaining files in Windows. All other modules can be removed without any problem with the commands above.

Any idea?

Resource type 'Mount' was not found

This module fails with Puppet 7 (Server: Ubuntu 22.04, Agent: Debian Bookworm).

Error: Failed to apply catalog: Resource type 'Mount' was not found

Support for Logical Volumes?

Support for logical volumes would be nice. I use LVM instead of files, device files for my swap. I tried forcing in '/dev/mapper/vg-swaplv' into the module but it doesn't like not having a mount point.

Created file size is incorrect

The current code contains:
$swapfilesize_mb = to_bytes($swapfilesize) / 1000000
and
dd if=/dev/zero of=${swapfile} bs=1M count=${swapfilesize_mb}.

However, 1MB is 1048576 bytes so the code should be instead:
$swapfilesize_mb = to_bytes($swapfilesize) / 1048576.

Add a size parameter

While defaulting to memorysize is nice, I miss an optional "size" parameter.

Is there a way around this limitation?

validate_* functions deprecated

On Puppet 5.x+ get the following:

Warning: This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Hash. There is further documentation for validate_legacy function in the README.

dd vs fallocate

In my quick test dd ran for almost 3 minutes to create a 4GB file. However fallocate ran for 0.09 seconds and worked perfect. I would like to add a parameter to swap_file allowing user choice of which tool to use to create the swap file, dd remaining default. There seems to be issues with fallocate on XFS so it would be important for the old dd command to remain default. This is an important issue for AWS EC2 users since I/O can be limited and using a file for swap is much easier and more efficient on instances.

Thoughts?

Push new module to puppet forge

Hi,
As since v4.0.0 puppet 3 support is dropped and puppet 6 support is achieved IMO there's a good time to push new version to puppet forge.

.

.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.