forked from ywangd/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.py
106 lines (90 loc) · 3.79 KB
/
mount.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""mount a filesystem."""
from __future__ import print_function
import argparse
import sys
from six import string_types
from six.moves import input
from stashutils import mount_ctrl, mount_manager
from stashutils.fsi.interfaces import FILESYSTEM_TYPES
_stash = globals()["_stash"]
def list_mounts():
"""list all mounts"""
manager = mount_ctrl.get_manager()
if manager is None:
manager = mount_manager.MountManager()
mount_ctrl.set_manager(manager)
mounts = manager.get_mounts()
for p, fsi, readonly in mounts:
print("{f} on {p}".format(f=fsi.repr(), p=p))
if __name__ == "__main__":
if len(sys.argv) == 2 and ("-l" in sys.argv):
list_mounts()
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--show-labels", action="store_true", dest="list", help="show also filesystem labels")
parser.add_argument("-v", "--verbose", action="store_true", dest="v", help="be more chatty")
parser.add_argument("-y", "--yes", action="store_true", dest="yes", help="enable the monkeypatches without asking")
parser.add_argument("-f", "--fake", action="store_false", dest="do_mount", help="dry run; do not mount fs")
parser.add_argument("-r", "--read-only", action="store_true", dest="readonly", help="mount the filesystem read-only")
parser.add_argument("-t", "--type", action="store", dest="type", default=None, help="Type of the filesystem to mount")
parser.add_argument("options", action="store", nargs="*", help="additional arguments for mounting the fs", default=[])
parser.add_argument("dir", action="store", help="dir to mount to")
ns = parser.parse_args()
if ns.type is None:
print(_stash.text_color("Error: no FS-Type specified!", "red"))
sys.exit(1)
manager = mount_ctrl.get_manager()
if manager is None:
manager = mount_manager.MountManager()
mount_ctrl.set_manager(manager)
if not manager.check_patches_enabled():
if not ns.yes:
print(_stash.text_color("WARNING: ", "red"))
print(_stash.text_color("The 'mount'-command needs to enable a few monkeypatches.", "yellow"))
print(_stash.text_color("Monkeypatches may make the system unstable.", "yellow"))
y = input(_stash.text_color("Do you want to enable these patches? (y/n)", "yellow")).upper() == "Y"
if not y:
print(_stash.text_color("Error: Monkeypatches not enabled!", "red"))
sys.exit(1)
manager.enable_patches()
else:
manager.enable_patches()
if ns.type not in FILESYSTEM_TYPES:
print(_stash.text_color("Error: Unknown Filesystem-Type!", "red"))
sys.exit(1)
fsic = FILESYSTEM_TYPES[ns.type]
if ns.v:
logger = sys.stdout.write
print("Creating FSI...")
else:
logger = None
fsi = fsic(logger=logger)
if ns.v:
print("Connecting FSI...")
msg = fsi.connect(*tuple(ns.options))
if isinstance(msg, string_types):
print(_stash.text_color("Error: {m}".format(m=msg), "red"))
sys.exit(1)
if ns.do_mount:
try:
manager.mount_fsi(ns.dir, fsi, readonly=ns.readonly)
except mount_manager.MountError as e:
print(_stash.text_color("Error: {e}".format(e=e.message), "red"))
try:
if ns.v:
print("unmounting FSI...")
fsi.close()
except Exception as e:
print(_stash.text_color("Error unmounting FSI: {e}".format(e=e.message), "red"))
else:
if ns.v:
print("Finished cleanup.")
sys.exit(1)
else:
# close fs
fsi.close()
if ns.v:
print("Done.")
if ns.list:
list_mounts()