From a3cb3662a8b15efb3e6a7381cb376a0ca3f095a0 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Thu, 26 Mar 2015 18:41:13 +0100 Subject: c*mount suite ready --- cmount.py | 25 ++++++++++++++++--------- cumount.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100755 cumount.py diff --git a/cmount.py b/cmount.py index 9749582..341b5a0 100755 --- a/cmount.py +++ b/cmount.py @@ -1,8 +1,9 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- import json import os +import subprocess import sys @@ -10,24 +11,30 @@ def main(): """ """ if (len(sys.argv) != 2): - sys.exit("Invalid parameter. Usage: " + sys.argv[0] + " \n") + sys.exit('Invalid parameter. Usage: {0} \n'.format( + sys.argv[0])) cstm_error = '' try: - cstm_error = 'Could not open config' + cstm_error = 'Could not open config (~/.cmount.json)' with open(os.path.expanduser('~/.cmount.json')) as fp: config_dict = json.load(fp) cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) data = config_dict[sys.argv[1]] cstm_error = 'system() error' - os.system('/sbin/cryptsetup luksOpen {device} {name}'.format( - device=data['device'], - name=sys.argv[1])) - os.system('/bin/mount /dev/mapper/{name} {target}'.format( - name=sys.argv[1], - target=data['target'])) + subprocess.check_call(['/sbin/cryptsetup', + 'luksOpen', + data['device'], + data['mapper_name']]) + subprocess.check_call(['/bin/mount', + '/dev/mapper/' + data['mapper_name'], + data['target']]) + except subprocess.CalledProcessError as e: + sys.stderr.write('Execution error: {0}\n'.format(e)) + sys.stderr.flush() except Exception as e: sys.stderr.write('Error: {0}\n'.format(cstm_error)) sys.stderr.flush() + if __name__ == '__main__': main() diff --git a/cumount.py b/cumount.py new file mode 100755 index 0000000..ad9f681 --- /dev/null +++ b/cumount.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import json +import os +import subprocess +import sys + + +def main(): + """ + """ + if (len(sys.argv) != 2): + sys.exit('Invalid parameter. Usage: {0} \n'.format( + sys.argv[0])) + cstm_error = '' + try: + cstm_error = 'Could not open config (~/.cmount.json)' + with open(os.path.expanduser('~/.cmount.json')) as fp: + config_dict = json.load(fp) + cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) + data = config_dict[sys.argv[1]] + subprocess.check_call(['/bin/umount', + '/dev/mapper/' + data['mapper_name']]) + subprocess.check_call(['/sbin/cryptsetup', + 'luksClose', + data['mapper_name']]) + except subprocess.CalledProcessError as e: + sys.stderr.write('Execution error: {0}\n'.format(e)) + sys.stderr.flush() + except Exception as e: + sys.stderr.write('Error: {0}\n'.format(cstm_error)) + sys.stderr.flush() + + +if __name__ == '__main__': + main() -- cgit v1.3