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 support for rotational #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fixtures:
forge_modules:
stdlib:
repo: "puppetlabs/stdlib"
ref: "3.2.0"
ref: "3.2.1"
symlinks:
disk: "#{source_dir}"

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ spec/fixtures/
.vagrant/
.bundle/
coverage/
junit/
log/
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ source ENV['GEM_SOURCE'] || "https://rubygems.org"

group :unit_tests do
gem 'rake', :require => false
gem 'rspec', '~> 3.1.0', :require => false
gem 'rspec', :require => false
gem 'rspec-puppet', :require => false
gem 'puppetlabs_spec_helper', :require => false
gem 'puppet-lint', '1.0.1', :require => false
gem 'puppet-lint', :require => false
gem 'puppet-syntax', :require => false
gem 'metadata-json-lint', :require => false
gem 'json', :require => false
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Configure some defaults:

```puppet
class { 'disk':
persist_file => "/etc/rc.local"
persist_file => "/etc/rc.d/rc.local"
}
```

Expand All @@ -29,6 +29,11 @@ Configure xvde1 with a readahead:
disk::readahead { 'xvde1': readahead => 2048 }
```

Configure xvde1 with a rotational:
```puppet
disk::rotational { 'xvde1': rotational => true }
```

Known Issues:
-------------

Expand Down
19 changes: 16 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# [*persist_file*]
# String. Where to write commands to persist non-persistent settings
# Default: /etc/rc.local
# Default: /etc/rc.d/rc.local
#
# [*bin_path*]
# Array|String. ???
Expand All @@ -20,7 +20,7 @@
#
# class { disk':
# fail_on_missing_device => true,
# persist_file => '/etc/rc.local',
# persist_file => '/etc/rc.d/rc.local',
# bin_path => ["/bin", "/usr/bin", "/sbin"]
# }
#
Expand All @@ -33,9 +33,22 @@
class disk (
$fail_on_missing_device = $::disk::params::fail_on_missing_device,
$persist_file = $::disk::params::persist_file,
$bin_path = $::disk::params::bin_path
$bin_path = $::disk::params::bin_path,
$hdparm_package_name = $::disk::params::hdparm_package_name,
$hdparm_package_ensure = $::disk::params::hdparm_package_ensure,
) inherits disk::params {

validate_bool($fail_on_missing_device)
validate_absolute_path($persist_file)

unless defined(Package[$hdparm_package_name]) {
package { $hdparm_package_name:
ensure => $hdparm_package_ensure,
}
}

# CentOS 7 requires this file be executable
file { $persist_file:
mode => '0755',
}
}
4 changes: 3 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class disk::params {

$fail_on_missing_device = false
$persist_file = '/etc/rc.local'
$persist_file = '/etc/rc.d/rc.local'
$bin_path = ['/bin', '/usr/bin', '/sbin']

$hdparm_package_name = 'hdparm'
$hdparm_package_ensure = 'present'
}
2 changes: 1 addition & 1 deletion manifests/persist_setting.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
# [*persist_file*]
# String. Path to a file where the command will become persisted
# Default: /etc/rc.local
# Default: /etc/rc.d/rc.local
#
#
# === Authors
Expand Down
32 changes: 14 additions & 18 deletions manifests/readahead.pp
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,20 @@
fail("Device ${name} does not exist")
}

if str2bool($has_device) == true {
$maybe_set_readahead = join([
"test -d /sys/block/${name}",
"blockdev --setra ${readahead} /dev/${name}",
], ' && ')

disk::persist_setting { "disk_readahead_for_${name}":
command => $maybe_set_readahead,
path => $::disk::bin_path,
match => "blockdev\\s--setra\\s[0-9]+\\s/dev/${name}",
}

exec { "disk_readahead_for_${name}":
command => $maybe_set_readahead,
path => $::disk::bin_path,
unless => "blockdev --getra /dev/${name} | grep -q ${readahead}",
}

$maybe_set_readahead = join([
"test -d /sys/block/${name}",
"blockdev --setra ${readahead} /dev/${name}",
], ' && ')

disk::persist_setting { "disk_readahead_for_${name}":
command => $maybe_set_readahead,
path => $::disk::bin_path,
match => "blockdev\\s--setra\\s[0-9]+\\s/dev/${name}",
}

exec { "disk_readahead_for_${name}":
command => $maybe_set_readahead,
path => $::disk::bin_path,
unless => "blockdev --getra /dev/${name} | grep -q ${readahead}",
}
}
60 changes: 60 additions & 0 deletions manifests/rotational.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# == Define: disk::rotational
#
# This definition allows the setting of a disk rotational in linux.
#
#
# === Parameters
#
# [*name*]
# String. The device to set the rotational on
# Required
#
# [*rotational*]
# String. The rotational to use
# Default: 1
#
#
# === Examples
#
# disk::rotational { 'xvde1':
# rotational => 1
# }
#
#
# === Authors
#
# * Stanley Zhang <mailto:stanley.zhang@ityin.net>
#
#
define disk::rotational (
$rotational = 1
) {

if ! defined(Class['disk']) {
fail('You must include the disk base class before using any disk defined resources')
}

$has_device = inline_template("<%= '${::blockdevices}'.split(',').include?('${name}') %>")

if str2bool($has_device) == false and $::disk::fail_on_missing_device {
fail("Device ${name} does not exist")
}

$rotational_value = bool2num($rotational)
$maybe_set_rotational = join([
"test -d /sys/block/${name}",
"echo ${rotational_value} > /sys/block/${name}/queue/rotational",
], ' && ')

disk::persist_setting { "disk_rotational_for_${name}":
command => $maybe_set_rotational,
path => $::disk::bin_path,
match => "/sys/block/${name}/queue/rotational",
}

exec { "disk_rotational_for_${name}":
command => $maybe_set_rotational,
path => $::disk::bin_path,
unless => "grep -q '${rotational_value}' /sys/block/${name}/queue/rotational",
}
}
32 changes: 14 additions & 18 deletions manifests/scheduler.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,20 @@
fail("Device ${name} does not exist")
}

if str2bool($has_device) == true {
$maybe_set_scheduler = join([
"test -d /sys/block/${name}",
"echo ${scheduler} > /sys/block/${name}/queue/scheduler",
], ' && ')

disk::persist_setting { "disk_scheduler_for_${name}":
command => $maybe_set_scheduler,
path => $::disk::bin_path,
match => "/sys/block/${name}/queue/scheduler",
}

exec { "disk_scheduler_for_${name}":
command => $maybe_set_scheduler,
path => $::disk::bin_path,
unless => "grep -q '\\[${scheduler}\\]' /sys/block/${name}/queue/scheduler",
}

$maybe_set_scheduler = join([
"test -d /sys/block/${name}",
"echo ${scheduler} > /sys/block/${name}/queue/scheduler",
], ' && ')

disk::persist_setting { "disk_scheduler_for_${name}":
command => $maybe_set_scheduler,
path => $::disk::bin_path,
match => "/sys/block/${name}/queue/scheduler",
}

exec { "disk_scheduler_for_${name}":
command => $maybe_set_scheduler,
path => $::disk::bin_path,
unless => "grep -q '\\[${scheduler}\\]' /sys/block/${name}/queue/scheduler",
}
}
60 changes: 60 additions & 0 deletions manifests/writecache.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# == Define: disk::writecache
#
# This definition allows change of write cache of a disk by using hdparm
#
#
# === Parameters
#
# [*name*]
# String. The device to set the writecache on
# Required
#
# [*writecache*]
# Boolean. Whether to turn on write cache
# Default: 1
#
#
# === Examples
#
# disk::writecache { 'xvde1':
# writecache => 1
# }
#
#
# === Authors
#
# * Stanley Zhang <mailto:stanley.zhang@ityin.net>
#
#
define disk::writecache (
$writecache = 1
) {

if ! defined(Class['disk']) {
fail('You must include the disk base class before using any disk defined resources')
}

$has_device = inline_template("<%= '${::blockdevices}'.split(',').include?('${name}') %>")

if str2bool($has_device) == false and $::disk::fail_on_missing_device {
fail("Device ${name} does not exist")
}

$writecache_value = bool2num($writecache)
$set_writecache_cmd = "hdparm -W${writecache_value} /dev/${name}"
$set_writecache_match = "hdparm -W[0,1] /dev/${name}"

disk::persist_setting { "disk_writecache_for_${name}":
command => $set_writecache_cmd,
path => $::disk::bin_path,
match => $set_writecache_match,
require => Package[$::disk::hdparm_package_name],
}

exec { "disk_writecache_for_${name}":
command => $set_writecache_cmd,
path => $::disk::bin_path,
unless => "hdparm -W /dev/${name} | grep write-caching | grep ${writecache_value}",
require => Package[$::disk::hdparm_package_name],
}
}
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
{ "operatingsystem": "Ubuntu", "operatingsystemrelease": [ "12.04", "14.04" ] }
],
"dependencies": [
{ "name": "puppetlabs/stdlib", "version_requirement": ">=3.2.0 <5.0.0" }
{ "name": "puppetlabs/stdlib", "version_requirement": ">=3.2.1 <4.16.0" }
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
HOSTS:
centos-65-x64:
centos-66-x64:
roles:
- master
platform: el-6-x86_64
box : puppetlabs/centos-6.5-64-nocm
box_url : https://vagrantcloud.com/puppetlabs/boxes/centos-6.5-64-nocm
box : puppetlabs/centos-6.6-64-nocm
box_url : https://vagrantcloud.com/puppetlabs/boxes/centos-6.6-64-nocm
hypervisor : vagrant
CONFIG:
log_level: verbose
Expand Down
30 changes: 30 additions & 0 deletions spec/acceptance/rotational_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper_acceptance'

describe 'disk tuning' do
context 'set rotational' do

it 'should work idempotently with no errors' do
pp = <<-EOS
class { 'disk': }
disk::rotational { 'sda': rotational => 1 }
EOS

# ensure rotational is changed
apply_manifest(pp, :catch_failures => true)

pp = <<-EOS
class { 'disk': }
disk::rotational { 'sda': rotational => false }
EOS

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end

describe file('/sys/block/sda/queue/rotational') do
its(:content) { should eql "0\n" }
end

end
end
Loading