Skip to content

Commit 188d210

Browse files
committed
MODULES-10620: Don't erase filesystem content and add force arg
1 parent 2d8e385 commit 188d210

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

lib/puppet/provider/filesystem/lvm.rb

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,68 @@
33

44
confine kernel: :linux
55

6-
commands blkid: 'blkid'
6+
commands blkid: 'blkid',
7+
findmnt: 'findmnt',
8+
umount: 'umount'
79

810
def create
9-
mkfs(@resource[:fs_type], @resource[:name])
10-
end
11-
12-
def exists?
13-
fstype == @resource[:fs_type]
14-
end
15-
16-
def destroy
17-
# no-op
18-
end
19-
20-
def fstype
21-
%r{\bTYPE=\"(\S+)\"}.match(blkid(@resource[:name]))[1]
22-
rescue Puppet::ExecutionFailure
23-
nil
24-
end
25-
26-
def mkfs(fs_type, name)
2711
mkfs_params = { 'reiserfs' => '-q', 'xfs' => '-f' }
2812

2913
mkfs_cmd = !@resource[:mkfs_cmd].nil? ?
3014
[@resource[:mkfs_cmd]] :
31-
case fs_type
15+
case @resource[:fs_type]
3216
when 'swap'
3317
['mkswap']
3418
else
35-
["mkfs.#{fs_type}"]
19+
["mkfs.#{@resource[:fs_type]}"]
3620
end
3721

38-
mkfs_cmd << name
22+
mkfs_cmd << @resource[:name]
3923

40-
if mkfs_params[fs_type]
41-
mkfs_cmd << mkfs_params[fs_type]
24+
if mkfs_params[@resource[:fs_type]]
25+
mkfs_cmd << mkfs_params[@resource[:fs_type]]
4226
end
4327

4428
if resource[:options]
4529
mkfs_options = Array.new(resource[:options].split)
4630
mkfs_cmd << mkfs_options
4731
end
4832

33+
current_fs_type = fstype
34+
unless current_fs_type.nil?
35+
if @resource[:force] == :true || @resource[:force] == true || @resource[:force] == 'true'
36+
umount(@resource[:name]) if mounted
37+
info("#{@resource[:name]} will be umount and FS will be changed to #{@resource[:fs_type]} (currently #{current_fs_type})")
38+
else
39+
raise(Puppet::Error, "Changing FS type is destructive operation and it requires manual intervention (from #{current_fs_type} to #{@resource[:fs_type]}) or set force argument.")
40+
end
41+
end
42+
4943
execute mkfs_cmd
50-
if fs_type == 'swap'
44+
if @resource[:fs_type] == 'swap'
5145
swap_cmd = ['swapon']
52-
swap_cmd << name
46+
swap_cmd << @resource[:name]
5347
execute swap_cmd
5448
end
5549
end
50+
51+
def destroy
52+
# no-op
53+
end
54+
55+
def exists?
56+
fstype == @resource[:fs_type]
57+
end
58+
59+
def fstype
60+
%r{\bTYPE=\"(\S+)\"}.match(blkid(@resource[:name]))[1]
61+
rescue Puppet::ExecutionFailure
62+
nil
63+
end
64+
65+
def mounted
66+
findmnt('-rno', 'SOURCE', @resource[:name])
67+
rescue Puppet::ExecutionFailure
68+
false
69+
end
5670
end

lib/puppet/type/filesystem.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@
148148
@parameters[:name].value
149149
end
150150
end
151+
152+
newparam(:force) do
153+
desc 'Force the FS type even if the FS is mounted.'
154+
defaultto :false
155+
newvalues(:true, :false)
156+
end
151157
end

0 commit comments

Comments
 (0)