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

MODULES-10620: Don't erase filesystem content and add force arg #253

Open
wants to merge 2 commits into
base: main
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ resources out yourself.
* fs_type (Parameter) - The file system type. eg. ext3.
* mkfs_cmd (Parameter) - Command to use to create the file system. Defaults to `mkswap` for `fs_type=swap`, otherwise `mkfs.{fs_type}`
* options (Parameter) - Params for the mkfs command. eg. `-l internal,agcount=x`
* force (Parameter) Default value: `false` - Force the creation even if the FS exists and mounted.
- `true`
- `false`

### logical_volume

Expand Down
28 changes: 22 additions & 6 deletions lib/puppet/provider/filesystem/lvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@

confine kernel: :linux

commands blkid: 'blkid'
commands blkid: 'blkid', findmnt: 'findmnt', umount: 'umount'

def create
mkfs(@resource[:fs_type], @resource[:name])
mkfs(@resource[:fs_type], @resource[:name], @resource[:force])
end

def exists?
fstype == @resource[:fs_type]
fstype(@resource[:name]) == @resource[:fs_type]
end

def destroy
# no-op
end

def fstype
%r{\bTYPE=\"(\S+)\"}.match(blkid(@resource[:name]))[1]
def fstype(name)
%r{\bTYPE=\"(\S+)\"}.match(blkid(name))[1]
rescue Puppet::ExecutionFailure
nil
end

def mkfs(fs_type, name)
def mounted(name)
findmnt('-rno', 'SOURCE', name)
rescue Puppet::ExecutionFailure
false
end

def mkfs(fs_type, name, force)
mkfs_params = { 'reiserfs' => '-q', 'xfs' => '-f' }

mkfs_cmd = !@resource[:mkfs_cmd].nil? ?
Expand All @@ -46,6 +52,16 @@ def mkfs(fs_type, name)
mkfs_cmd << mkfs_options
end

current_fs_type = fstype(name)
unless current_fs_type.nil?
if [:true, true, 'true'].include?(force)
umount(name) if mounted(name)
info("#{name} will be umount and FS will be changed to #{fs_type} (currently #{current_fs_type})")
else
raise(Puppet::Error, "Changing FS type is destructive operation and it requires manual intervention (from #{current_fs_type} to #{fs_type}) or set force argument.")
end
end

execute mkfs_cmd
if fs_type == 'swap'
swap_cmd = ['swapon']
Expand Down
6 changes: 6 additions & 0 deletions lib/puppet/type/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@
@parameters[:name].value
end
end

newparam(:force) do
desc 'Force the FS type even if the FS is mounted.'
defaultto :false
newvalues(:true, :false)
end
end
26 changes: 26 additions & 0 deletions spec/unit/puppet/provider/filesystem/lvm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('ext4')
@resource.expects(:[]).with(:options)
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkfs.ext4', '/dev/myvg/mylv'])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.create
Expand All @@ -21,6 +22,7 @@
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('ext4')
@resource.expects(:[]).with(:options).returns('-b 4096 -E stride=32,stripe-width=64').twice
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkfs.ext4', '/dev/myvg/mylv', ['-b', '4096', '-E', 'stride=32,stripe-width=64']])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.create
Expand All @@ -29,14 +31,25 @@
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('reiserfs')
@resource.expects(:[]).with(:options).returns('-b 4096 -E stride=32,stripe-width=64').twice
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkfs.reiserfs', '/dev/myvg/mylv', '-q', ['-b', '4096', '-E', 'stride=32,stripe-width=64']])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.create
end
it 'includes -f for xfs' do
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('xfs')
@resource.expects(:[]).with(:options).returns('-b 4096 -E stride=32,stripe-width=64').twice
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkfs.xfs', '/dev/myvg/mylv', '-f', ['-b', '4096', '-E', 'stride=32,stripe-width=64']])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.create
end
it 'calls mkswap for filesystem type swap' do
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('swap')
@resource.expects(:[]).with(:options)
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkswap', '/dev/myvg/mylv'])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.expects(:execute).with(['swapon', '/dev/myvg/mylv'])
Expand All @@ -46,9 +59,22 @@
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('jbd')
@resource.expects(:[]).with(:options).returns('-O journal_dev').twice
@resource.expects(:[]).with(:force).returns(:false)
@provider.expects(:execute).with(['mkfs.ext4', '/dev/myvg/mylv', ['-O', 'journal_dev']])
@resource.expects(:[]).with(:mkfs_cmd).returns('mkfs.ext4').twice
@provider.create
end
end

describe 'when creating with force' do
it "executes the 'mkfs.xfs' with force option" do
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@resource.expects(:[]).with(:fs_type).returns('xfs')
@resource.expects(:[]).with(:force).returns(:true)
@resource.expects(:[]).with(:options)
@provider.expects(:execute).with(['mkfs.xfs', '/dev/myvg/mylv', '-f'])
@resource.expects(:[]).with(:mkfs_cmd)
@provider.create
end
end
end