Merge pull request #672 from Firstyear/fsresize-devel-fixed

Changes to allow FS resize in filesystem
reviewable/pr18780/r1
Brian Coca 9 years ago
commit 0e02387c32

@ -41,6 +41,12 @@ options:
description: description:
- If yes, allows to create new filesystem on devices that already has filesystem. - If yes, allows to create new filesystem on devices that already has filesystem.
required: false required: false
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
opts: opts:
description: description:
- List of options to be passed to mkfs command. - List of options to be passed to mkfs command.
@ -63,17 +69,68 @@ def main():
dev=dict(required=True, aliases=['device']), dev=dict(required=True, aliases=['device']),
opts=dict(), opts=dict(),
force=dict(type='bool', default='no'), force=dict(type='bool', default='no'),
resizefs=dict(type='bool', default='no'),
), ),
supports_check_mode=True, supports_check_mode=True,
) )
# 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',
},
'ext3' : {
'mkfs' : 'mkfs.ext3',
'grow' : 'resize2fs',
'grow_flag' : None,
'force_flag' : '-F',
},
'ext4' : {
'mkfs' : 'mkfs.ext4',
'grow' : 'resize2fs',
'grow_flag' : None,
'force_flag' : '-F',
},
'ext4dev' : {
'mkfs' : 'mkfs.ext4',
'grow' : 'resize2fs',
'grow_flag' : None,
'force_flag' : '-F',
},
'xfs' : {
'mkfs' : 'mkfs.xfs',
'grow' : 'xfs_growfs',
'grow_flag' : None,
'force_flag' : '-f',
},
'btrfs' : {
'mkfs' : 'mkfs.btrfs',
'grow' : 'btrfs',
'grow_flag' : 'filesystem resize',
'force_flag' : '-f',
}
}
dev = module.params['dev'] dev = module.params['dev']
fstype = module.params['fstype'] fstype = module.params['fstype']
opts = module.params['opts'] opts = module.params['opts']
force = module.boolean(module.params['force']) force = module.boolean(module.params['force'])
resizefs = module.boolean(module.params['resizefs'])
changed = False changed = False
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']
if not os.path.exists(dev): if not os.path.exists(dev):
module.fail_json(msg="Device %s not found."%dev) module.fail_json(msg="Device %s not found."%dev)
@ -82,9 +139,21 @@ def main():
rc,raw_fs,err = module.run_command("%s -c /dev/null -o value -s TYPE %s" % (cmd, dev)) rc,raw_fs,err = module.run_command("%s -c /dev/null -o value -s TYPE %s" % (cmd, dev))
fs = raw_fs.strip() fs = raw_fs.strip()
if fs == fstype and resizefs == False:
if fs == fstype:
module.exit_json(changed=False) module.exit_json(changed=False)
elif fs == fstype and resizefs == True:
cmd = module.get_bin_path(growcmd, required=True)
if module.check_mode:
module.exit_json(changed=True, msg="May resize filesystem")
else:
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)
elif fs and not force: elif fs and not force:
module.fail_json(msg="'%s' is already used as %s, use force=yes to overwrite"%(dev,fs), rc=rc, err=err) module.fail_json(msg="'%s' is already used as %s, use force=yes to overwrite"%(dev,fs), rc=rc, err=err)
@ -93,19 +162,13 @@ def main():
if module.check_mode: if module.check_mode:
changed = True changed = True
else: else:
mkfs = module.get_bin_path('mkfs', required=True) mkfs = module.get_bin_path(mkfscmd, required=True)
cmd = None cmd = None
if fstype in ['ext2', 'ext3', 'ext4', 'ext4dev']:
force_flag="-F"
elif fstype in ['xfs', 'btrfs']:
force_flag="-f"
else:
force_flag=""
if opts is None: if opts is None:
cmd = "%s -t %s %s '%s'" % (mkfs, fstype, force_flag, dev) cmd = "%s %s '%s'" % (mkfs, force_flag, dev)
else: else:
cmd = "%s -t %s %s %s '%s'" % (mkfs, fstype, force_flag, opts, dev) cmd = "%s %s %s '%s'" % (mkfs, force_flag, opts, dev)
rc,_,err = module.run_command(cmd) rc,_,err = module.run_command(cmd)
if rc == 0: if rc == 0:
changed = True changed = True

Loading…
Cancel
Save