2013-04-25 11:42:07 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Alexander Bulimov <lazywolf0@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Alexander Bulimov (@abulimov)"
|
2013-05-06 16:08:38 +02:00
|
|
|
module: filesystem
|
2013-04-25 11:42:07 +02:00
|
|
|
short_description: Makes file system on block device
|
|
|
|
description:
|
|
|
|
- This module creates file system.
|
|
|
|
version_added: "1.2"
|
|
|
|
options:
|
|
|
|
fstype:
|
|
|
|
description:
|
|
|
|
- File System type to be created.
|
2016-07-28 17:52:32 +02:00
|
|
|
- reiserfs support was added in 2.2.
|
2013-04-25 11:42:07 +02:00
|
|
|
required: true
|
|
|
|
dev:
|
|
|
|
description:
|
|
|
|
- Target block device.
|
|
|
|
required: true
|
|
|
|
force:
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
description:
|
|
|
|
- If yes, allows to create new filesystem on devices that already has filesystem.
|
|
|
|
required: false
|
2015-07-02 00:26:56 +02:00
|
|
|
resizefs:
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
description:
|
|
|
|
- If yes, if the block device and filessytem size differ, grow the filesystem into the space. Note, XFS Will only grow if mounted.
|
|
|
|
required: false
|
2015-07-08 19:14:01 +02:00
|
|
|
version_added: "2.0"
|
2013-04-25 11:42:07 +02:00
|
|
|
opts:
|
|
|
|
description:
|
|
|
|
- List of options to be passed to mkfs command.
|
|
|
|
notes:
|
|
|
|
- uses mkfs command
|
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a ext2 filesystem on /dev/sdb1.
|
|
|
|
- filesystem: fstype=ext2 dev=/dev/sdb1
|
|
|
|
|
|
|
|
# Create a ext4 filesystem on /dev/sdb1 and check disk blocks.
|
|
|
|
- filesystem: fstype=ext4 dev=/dev/sdb1 opts="-cc"
|
|
|
|
'''
|
|
|
|
|
2016-01-15 18:35:45 +01:00
|
|
|
def _get_dev_size(dev, module):
|
|
|
|
""" Return size in bytes of device. Returns int """
|
|
|
|
blockdev_cmd = module.get_bin_path("blockdev", required=True)
|
|
|
|
rc, devsize_in_bytes, err = module.run_command("%s %s %s" % (blockdev_cmd, "--getsize64", dev))
|
|
|
|
return int(devsize_in_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_fs_size(fssize_cmd, dev, module):
|
|
|
|
""" Return size in bytes of filesystem on device. Returns int """
|
|
|
|
cmd = module.get_bin_path(fssize_cmd, required=True)
|
|
|
|
if 'tune2fs' == fssize_cmd:
|
|
|
|
# Get Block count and Block size
|
|
|
|
rc, size, err = module.run_command("%s %s %s" % (cmd, '-l', dev))
|
|
|
|
if rc == 0:
|
|
|
|
for line in size.splitlines():
|
|
|
|
if 'Block count:' in line:
|
|
|
|
block_count = int(line.split(':')[1].strip())
|
|
|
|
elif 'Block size:' in line:
|
|
|
|
block_size = int(line.split(':')[1].strip())
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Failed to get block count and block size of %s with %s" % (dev, cmd), rc=rc, err=err )
|
|
|
|
elif 'xfs_info' == fssize_cmd:
|
|
|
|
# Get Block count and Block size
|
|
|
|
rc, size, err = module.run_command("%s %s" % (cmd, dev))
|
|
|
|
if rc == 0:
|
|
|
|
for line in size.splitlines():
|
|
|
|
#if 'data' in line:
|
|
|
|
if 'data ' in line:
|
|
|
|
block_size = int(line.split('=')[2].split()[0])
|
|
|
|
block_count = int(line.split('=')[3].split(',')[0])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Failed to get block count and block size of %s with %s" % (dev, cmd), rc=rc, err=err )
|
|
|
|
elif 'btrfs' == fssize_cmd:
|
|
|
|
#ToDo
|
|
|
|
# There is no way to get the blocksize and blockcount for btrfs filesystems
|
|
|
|
block_size = 1
|
|
|
|
block_count = 1
|
|
|
|
|
|
|
|
|
|
|
|
return block_size*block_count
|
|
|
|
|
|
|
|
|
2013-04-25 11:42:07 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2013-06-10 16:39:19 +02:00
|
|
|
fstype=dict(required=True, aliases=['type']),
|
|
|
|
dev=dict(required=True, aliases=['device']),
|
2013-04-25 11:42:07 +02:00
|
|
|
opts=dict(),
|
|
|
|
force=dict(type='bool', default='no'),
|
2015-07-02 00:26:56 +02:00
|
|
|
resizefs=dict(type='bool', default='no'),
|
2013-04-25 11:42:07 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2015-07-02 00:26:56 +02:00
|
|
|
# There is no "single command" to manipulate filesystems, so we map them all out and their options
|
|
|
|
fs_cmd_map = {
|
|
|
|
'ext2' : {
|
|
|
|
'mkfs' : 'mkfs.ext2',
|
|
|
|
'grow' : 'resize2fs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-F',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'tune2fs',
|
2015-07-02 00:26:56 +02:00
|
|
|
},
|
|
|
|
'ext3' : {
|
|
|
|
'mkfs' : 'mkfs.ext3',
|
|
|
|
'grow' : 'resize2fs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-F',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'tune2fs',
|
2015-07-02 00:26:56 +02:00
|
|
|
},
|
|
|
|
'ext4' : {
|
|
|
|
'mkfs' : 'mkfs.ext4',
|
|
|
|
'grow' : 'resize2fs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-F',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'tune2fs',
|
2015-07-02 00:26:56 +02:00
|
|
|
},
|
2016-07-28 17:52:32 +02:00
|
|
|
'reiserfs' : {
|
|
|
|
'mkfs' : 'mkfs.reiserfs',
|
|
|
|
'grow' : 'resize_reiserfs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-f',
|
|
|
|
'fsinfo': 'reiserfstune',
|
|
|
|
},
|
2015-07-02 00:26:56 +02:00
|
|
|
'ext4dev' : {
|
|
|
|
'mkfs' : 'mkfs.ext4',
|
|
|
|
'grow' : 'resize2fs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-F',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'tune2fs',
|
2015-07-02 00:26:56 +02:00
|
|
|
},
|
|
|
|
'xfs' : {
|
|
|
|
'mkfs' : 'mkfs.xfs',
|
|
|
|
'grow' : 'xfs_growfs',
|
|
|
|
'grow_flag' : None,
|
|
|
|
'force_flag' : '-f',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'xfs_info',
|
2015-07-02 00:26:56 +02:00
|
|
|
},
|
|
|
|
'btrfs' : {
|
|
|
|
'mkfs' : 'mkfs.btrfs',
|
|
|
|
'grow' : 'btrfs',
|
|
|
|
'grow_flag' : 'filesystem resize',
|
|
|
|
'force_flag' : '-f',
|
2016-01-15 18:35:45 +01:00
|
|
|
'fsinfo': 'btrfs',
|
2015-07-02 00:26:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 11:42:07 +02:00
|
|
|
dev = module.params['dev']
|
|
|
|
fstype = module.params['fstype']
|
|
|
|
opts = module.params['opts']
|
|
|
|
force = module.boolean(module.params['force'])
|
2015-07-02 00:26:56 +02:00
|
|
|
resizefs = module.boolean(module.params['resizefs'])
|
2013-04-25 11:42:07 +02:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2015-07-02 00:26:56 +02:00
|
|
|
try:
|
|
|
|
_ = fs_cmd_map[fstype]
|
|
|
|
except KeyError:
|
|
|
|
module.exit_json(changed=False, msg="WARNING: module does not support this filesystem yet. %s" % fstype)
|
|
|
|
|
|
|
|
mkfscmd = fs_cmd_map[fstype]['mkfs']
|
|
|
|
force_flag = fs_cmd_map[fstype]['force_flag']
|
|
|
|
growcmd = fs_cmd_map[fstype]['grow']
|
2016-01-15 18:35:45 +01:00
|
|
|
fssize_cmd = fs_cmd_map[fstype]['fsinfo']
|
2015-07-02 00:26:56 +02:00
|
|
|
|
2013-04-25 11:42:07 +02:00
|
|
|
if not os.path.exists(dev):
|
|
|
|
module.fail_json(msg="Device %s not found."%dev)
|
|
|
|
|
2013-06-10 16:39:19 +02:00
|
|
|
cmd = module.get_bin_path('blkid', required=True)
|
|
|
|
|
2014-02-26 03:08:57 +01:00
|
|
|
rc,raw_fs,err = module.run_command("%s -c /dev/null -o value -s TYPE %s" % (cmd, dev))
|
2013-04-25 11:42:07 +02:00
|
|
|
fs = raw_fs.strip()
|
|
|
|
|
2016-01-13 15:39:42 +01:00
|
|
|
if fs == fstype and resizefs == False and not force:
|
2013-04-25 11:42:07 +02:00
|
|
|
module.exit_json(changed=False)
|
2015-07-02 00:26:56 +02:00
|
|
|
elif fs == fstype and resizefs == True:
|
2016-01-15 18:35:45 +01:00
|
|
|
# Get dev and fs size and compare
|
|
|
|
devsize_in_bytes = _get_dev_size(dev, module)
|
|
|
|
fssize_in_bytes = _get_fs_size(fssize_cmd, dev, module)
|
|
|
|
if fssize_in_bytes < devsize_in_bytes:
|
|
|
|
fs_smaller = True
|
2015-07-02 00:26:56 +02:00
|
|
|
else:
|
2016-01-15 18:35:45 +01:00
|
|
|
fs_smaller = False
|
|
|
|
|
|
|
|
|
|
|
|
if module.check_mode and fs_smaller:
|
|
|
|
module.exit_json(changed=True, msg="Resizing filesystem %s on device %s" % (fstype,dev))
|
|
|
|
elif module.check_mode and not fs_smaller:
|
|
|
|
module.exit_json(changed=False, msg="%s filesystem is using the whole device %s" % (fstype, dev))
|
|
|
|
elif fs_smaller:
|
|
|
|
cmd = module.get_bin_path(growcmd, required=True)
|
2015-07-02 00:26:56 +02:00
|
|
|
rc,out,err = module.run_command("%s %s" % (cmd, dev))
|
|
|
|
# Sadly there is no easy way to determine if this has changed. For now, just say "true" and move on.
|
|
|
|
# in the future, you would have to parse the output to determine this.
|
|
|
|
# thankfully, these are safe operations if no change is made.
|
|
|
|
if rc == 0:
|
|
|
|
module.exit_json(changed=True, msg=out)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Resizing filesystem %s on device '%s' failed"%(fstype,dev), rc=rc, err=err)
|
2016-01-15 18:35:45 +01:00
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, msg="%s filesystem is using the whole device %s" % (fstype, dev))
|
2013-04-25 11:42:07 +02:00
|
|
|
elif fs and not force:
|
2013-05-11 20:03:40 +02:00
|
|
|
module.fail_json(msg="'%s' is already used as %s, use force=yes to overwrite"%(dev,fs), rc=rc, err=err)
|
2013-04-25 11:42:07 +02:00
|
|
|
|
|
|
|
### create fs
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
2015-07-02 00:26:56 +02:00
|
|
|
mkfs = module.get_bin_path(mkfscmd, required=True)
|
2013-04-25 11:42:07 +02:00
|
|
|
cmd = None
|
2014-07-01 15:09:39 +02:00
|
|
|
|
2013-04-25 11:42:07 +02:00
|
|
|
if opts is None:
|
2015-07-02 00:26:56 +02:00
|
|
|
cmd = "%s %s '%s'" % (mkfs, force_flag, dev)
|
2013-04-25 11:42:07 +02:00
|
|
|
else:
|
2015-07-02 00:26:56 +02:00
|
|
|
cmd = "%s %s %s '%s'" % (mkfs, force_flag, opts, dev)
|
2013-04-25 11:42:07 +02:00
|
|
|
rc,_,err = module.run_command(cmd)
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Creating filesystem %s on device '%s' failed"%(fstype,dev), rc=rc, err=err)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2016-01-15 18:35:45 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|