Skip to content

Commit ae52fdb

Browse files
committed
Merge pull request #187 from dhollinger/MODULES-4753
The exec resources running swapon and swapoff in the lvm::logical_volume define type were running whenever a swap logical volume was created or updated. The logical_volume provider was also running these same commands on a swap resize. This caused a conflict where the puppet run would error out when the logical_volume provider attempted to run swapoff on a swap lvm that had already been unloaded from swap and the lvm would never be reloaded into swap. When the exec resources were removed from the lvm::logical_volume defined type, it created a gap wherein a NEW swap lvm would not be loaded into swap after being created. To solve this issue, I added the swapon command and logic that will run that command against the filesystem resource if the fs_type parameter is set to 'swap'
2 parents 31febe1 + a79c344 commit ae52fdb

File tree

7 files changed

+38
-36
lines changed

7 files changed

+38
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
.*.swp
33
Gemfile.lock
44
.bundle/
5+
.fixtures/modules
6+
.fixtures/manifests

lib/puppet/provider/filesystem/lvm.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
commands :blkid => 'blkid'
55

66
def create
7-
mkfs(@resource[:fs_type])
7+
mkfs(@resource[:fs_type], @resource[:name])
88
end
99

1010
def exists?
@@ -21,7 +21,7 @@ def fstype
2121
nil
2222
end
2323

24-
def mkfs(fs_type)
24+
def mkfs(fs_type, name)
2525
mkfs_params = { "reiserfs" => "-q" }
2626

2727
mkfs_cmd = @resource[:mkfs_cmd] != nil ?
@@ -33,7 +33,7 @@ def mkfs(fs_type)
3333
["mkfs.#{fs_type}"]
3434
end
3535

36-
mkfs_cmd << @resource[:name]
36+
mkfs_cmd << name
3737

3838
if mkfs_params[fs_type]
3939
mkfs_cmd << mkfs_params[fs_type]
@@ -45,6 +45,11 @@ def mkfs(fs_type)
4545
end
4646

4747
execute mkfs_cmd
48+
if fs_type == 'swap'
49+
swap_cmd = ["swapon"]
50+
swap_cmd << name
51+
execute swap_cmd
52+
end
4853
end
4954

5055
end

lib/puppet/provider/logical_volume/lvm.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def create
129129

130130
def destroy
131131
name_escaped = "#{@resource[:volume_group].gsub('-','--')}-#{@resource[:name].gsub('-','--')}"
132+
if blkid(path) =~ /\bTYPE=\"(swap)\"/
133+
swapoff(path)
134+
end
132135
dmsetup('remove', name_escaped)
133136
lvremove('-f', path)
134137
end

manifests/logical_volume.pp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
}
6363

6464
if $ensure == 'present' and $createfs {
65-
Logical_volume[$name] ->
66-
Filesystem[$lvm_device_path] ->
67-
Mount[$mount_title]
68-
} elsif $ensure != 'present' and $createfs {
69-
Mount[$mount_title] ->
70-
Filesystem[$lvm_device_path] ->
7165
Logical_volume[$name]
66+
-> Filesystem[$lvm_device_path]
67+
-> Mount[$mount_title]
68+
} elsif $ensure != 'present' and $createfs {
69+
Mount[$mount_title]
70+
-> Filesystem[$lvm_device_path]
71+
-> Logical_volume[$name]
7272
}
7373

7474
logical_volume { $name:
@@ -89,7 +89,7 @@
8989
mirrorlog => $mirrorlog,
9090
no_sync => $no_sync,
9191
region_size => $region_size,
92-
alloc => $alloc
92+
alloc => $alloc,
9393
}
9494

9595
if $createfs {
@@ -101,30 +101,15 @@
101101
}
102102

103103
if $createfs or $ensure != 'present' {
104-
if $fs_type == 'swap' {
105-
if $ensure == 'present' {
106-
exec { "swapon for '${mount_title}'":
107-
path => [ '/bin', '/usr/bin', '/sbin' ],
108-
command => "swapon ${lvm_device_path}",
109-
unless => "grep `readlink -f ${lvm_device_path}` /proc/swaps",
110-
subscribe => Mount[$mount_title],
111-
}
112-
} else {
113-
exec { "swapoff for '${mount_title}'":
114-
path => [ '/bin', '/usr/bin', '/sbin' ],
115-
command => "swapoff ${lvm_device_path}",
116-
onlyif => "grep `readlink -f ${lvm_device_path}` /proc/swaps",
117-
notify => Mount[$mount_title],
118-
}
119-
}
120-
} else {
104+
if $fs_type != 'swap' {
121105
exec { "ensure mountpoint '${fixed_mountpath}' exists":
122106
path => [ '/bin', '/usr/bin' ],
123107
command => "mkdir -p ${fixed_mountpath}",
124108
unless => "test -d ${fixed_mountpath}",
125109
before => Mount[$mount_title],
126110
}
127111
}
112+
128113
mount { $mount_title:
129114
ensure => $mount_ensure,
130115
name => $fixed_mountpath,

spec/unit/classes/lvm_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,5 @@
111111
:pass => 0,
112112
:dump => 0
113113
}) }
114-
it { should contain_exec("swapon for '/dev/myvg/swap'") }
115-
it { should_not contain_exec("ensure mountpoint 'swap_/dev/myvg/swap' exists") }
116-
117-
it { should contain_exec("swapoff for '/dev/myvg/swap2'") }
118-
it { should_not contain_exec("ensure mountpoint 'swap_/dev/myvg/swap2' exists") }
119114
end
120115
end

spec/unit/puppet/provider/filesystem/lvm_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@resource.expects(:[]).with(:options)
4040
@provider.expects(:execute).with(['mkswap', '/dev/myvg/mylv'])
4141
@resource.expects(:[]).with(:mkfs_cmd)
42+
@provider.expects(:execute).with(['swapon', '/dev/myvg/mylv'])
4243
@provider.create
4344
end
4445
it "should create an ext4 journal correctly" do

spec/unit/puppet/provider/logical_volume/lvm_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,29 @@
228228

229229
describe 'when destroying' do
230230
it "should execute 'dmsetup' and 'lvremove'" do
231-
@resource.expects(:[]).with(:volume_group).returns('myvg').twice
232-
@resource.expects(:[]).with(:name).returns('mylv').twice
231+
@resource.expects(:[]).with(:volume_group).returns('myvg').times(3)
232+
@resource.expects(:[]).with(:name).returns('mylv').times(3)
233+
@provider.expects(:blkid).with('/dev/myvg/mylv')
233234
@provider.expects(:dmsetup).with('remove', 'myvg-mylv')
234235
@provider.expects(:lvremove).with('-f', '/dev/myvg/mylv')
235236
@provider.destroy
236237
end
237238
it "should execute 'dmsetup' and 'lvremove' and properly escape names with dashes" do
238-
@resource.expects(:[]).with(:volume_group).returns('my-vg').twice
239-
@resource.expects(:[]).with(:name).returns('my-lv').twice
239+
@resource.expects(:[]).with(:volume_group).returns('my-vg').times(3)
240+
@resource.expects(:[]).with(:name).returns('my-lv').times(3)
241+
@provider.expects(:blkid).with('/dev/my-vg/my-lv')
240242
@provider.expects(:dmsetup).with('remove', 'my--vg-my--lv')
241243
@provider.expects(:lvremove).with('-f', '/dev/my-vg/my-lv')
242244
@provider.destroy
243245
end
246+
it "should execute 'swapoff', 'dmsetup', and 'lvremove' when lvm is of type swap" do
247+
@resource.expects(:[]).with(:volume_group).returns('myvg').times(4)
248+
@resource.expects(:[]).with(:name).returns('mylv').times(4)
249+
@provider.expects(:blkid).with('/dev/myvg/mylv').returns('TYPE="swap"')
250+
@provider.expects(:swapoff).with('/dev/myvg/mylv')
251+
@provider.expects(:dmsetup).with('remove', 'myvg-mylv')
252+
@provider.expects(:lvremove).with('-f', '/dev/myvg/mylv')
253+
@provider.destroy
254+
end
244255
end
245256
end

0 commit comments

Comments
 (0)