|
3 | 3 |
|
4 | 4 | confine kernel: :linux |
5 | 5 |
|
6 | | - commands blkid: 'blkid' |
| 6 | + commands blkid: 'blkid', |
| 7 | + findmnt: 'findmnt', |
| 8 | + umount: 'umount' |
7 | 9 |
|
8 | 10 | 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) |
27 | 11 | mkfs_params = { 'reiserfs' => '-q', 'xfs' => '-f' } |
28 | 12 |
|
29 | 13 | mkfs_cmd = !@resource[:mkfs_cmd].nil? ? |
30 | 14 | [@resource[:mkfs_cmd]] : |
31 | | - case fs_type |
| 15 | + case @resource[:fs_type] |
32 | 16 | when 'swap' |
33 | 17 | ['mkswap'] |
34 | 18 | else |
35 | | - ["mkfs.#{fs_type}"] |
| 19 | + ["mkfs.#{@resource[:fs_type]}"] |
36 | 20 | end |
37 | 21 |
|
38 | | - mkfs_cmd << name |
| 22 | + mkfs_cmd << @resource[:name] |
39 | 23 |
|
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]] |
42 | 26 | end |
43 | 27 |
|
44 | 28 | if resource[:options] |
45 | 29 | mkfs_options = Array.new(resource[:options].split) |
46 | 30 | mkfs_cmd << mkfs_options |
47 | 31 | end |
48 | 32 |
|
| 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 | + |
49 | 43 | execute mkfs_cmd |
50 | | - if fs_type == 'swap' |
| 44 | + if @resource[:fs_type] == 'swap' |
51 | 45 | swap_cmd = ['swapon'] |
52 | | - swap_cmd << name |
| 46 | + swap_cmd << @resource[:name] |
53 | 47 | execute swap_cmd |
54 | 48 | end |
55 | 49 | 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 |
56 | 70 | end |
0 commit comments