summaryrefslogtreecommitdiff
path: root/cmount.py
blob: 86de18bce979d5ff35019cc77ea4b5d6bf3a05a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import os
import subprocess
import sys
import time


def main():

    if (len(sys.argv) != 2):
        sys.exit('Invalid parameter. Usage: {0} <mapping>\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]]
        if data.get('type') == 'lvm':
            if not isinstance(data.get('pv_sources'), dict):
                cstm_error = 'Invalid config for lvm-type'
                raise Exception()
            cstm_error = 'pv_sources error'
            for device, mapper_name in data['pv_sources'].items():
                subprocess.check_call(['/sbin/cryptsetup',
                                       'luksOpen',
                                       device,
                                       mapper_name])
            time.sleep(3)
            cstm_error = 'vgimport error'
            subprocess.check_call(['/sbin/vgimport',
                                   data['device'].split('/')[2]])
            cstm_error = 'lvchange error'
            subprocess.check_call(['/sbin/lvchange',
                                   '-a',
                                   'y',
                                   data['device']])
            cstm_error = 'mount error'
            arguments = ['/bin/mount', data['device']]
            if data.get('target'):
                arguments.append(data['target'])
            subprocess.check_call(arguments)
        else:
            if data.get('type') == 'efile':
                cstm_error = 'Unable to create loop device'
                data['device'] = subprocess.check_output(
                    ['/usr/sbin/losetup',
                     '--find',
                     '--show',
                     data['device']]).strip()
            cstm_error = 'system() error'
            subprocess.check_call(['/sbin/cryptsetup',
                                   'luksOpen',
                                   data['device'],
                                   data['mapper_name']])
            arguments = ['/bin/mount', '/dev/mapper/' + data['mapper_name']]
            if data.get('target'):
                arguments.append(data['target'])
            subprocess.check_call(arguments)
    except subprocess.CalledProcessError as e:
        print('Execution error: {0}\n'.format(e), file=sys.stderr, flush=True)
    except Exception:
        print('Error: {0}\n'.format(cstm_error), file=sys.stderr, flush=True)


if __name__ == '__main__':
    main()