1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
17 import arvados.commands._util as arv_cmd
18 from arvados_fuse import crunchstat
19 from arvados_fuse import *
20 from arvados_fuse.unmount import unmount
21 from arvados_fuse._version import __version__
23 class ArgumentParser(argparse.ArgumentParser):
25 super(ArgumentParser, self).__init__(
26 parents=[arv_cmd.retry_opt],
27 description='''Mount Keep data under the local filesystem. Default mode is --home''',
29 Note: When using the --exec feature, you must either specify the
30 mountpoint before --exec, or mark the end of your --exec arguments
33 self.add_argument('--version', action='version',
34 version="%s %s" % (sys.argv[0], __version__),
35 help='Print version and exit.')
36 self.add_argument('mountpoint', type=str, help="""Mount point.""")
37 self.add_argument('--allow-other', action='store_true',
38 help="""Let other users read the mount""")
39 self.add_argument('--subtype', type=str, metavar='STRING',
40 help="""Report mounted filesystem type as "fuse.STRING", instead of just "fuse".""")
42 mode = self.add_mutually_exclusive_group()
44 mode.add_argument('--all', action='store_const', const='all', dest='mode',
45 help="""Mount a subdirectory for each mode: home, shared, by_tag, by_id (default if no --mount-* arguments are given).""")
46 mode.add_argument('--custom', action='store_const', const=None, dest='mode',
47 help="""Mount a top level meta-directory with subdirectories as specified by additional --mount-* arguments (default if any --mount-* arguments are given).""")
48 mode.add_argument('--home', action='store_const', const='home', dest='mode',
49 help="""Mount only the user's home project.""")
50 mode.add_argument('--shared', action='store_const', const='shared', dest='mode',
51 help="""Mount only list of projects shared with the user.""")
52 mode.add_argument('--by-tag', action='store_const', const='by_tag', dest='mode',
53 help="""Mount subdirectories listed by tag.""")
54 mode.add_argument('--by-id', action='store_const', const='by_id', dest='mode',
55 help="""Mount subdirectories listed by portable data hash or uuid.""")
56 mode.add_argument('--by-pdh', action='store_const', const='by_pdh', dest='mode',
57 help="""Mount subdirectories listed by portable data hash.""")
58 mode.add_argument('--project', type=str, metavar='UUID',
59 help="""Mount the specified project.""")
60 mode.add_argument('--collection', type=str, metavar='UUID_or_PDH',
61 help="""Mount only the specified collection.""")
63 mounts = self.add_argument_group('Custom mount options')
64 mounts.add_argument('--mount-by-pdh',
65 type=str, metavar='PATH', action='append', default=[],
66 help="Mount each readable collection at mountpoint/PATH/P where P is the collection's portable data hash.")
67 mounts.add_argument('--mount-by-id',
68 type=str, metavar='PATH', action='append', default=[],
69 help="Mount each readable collection at mountpoint/PATH/UUID and mountpoint/PATH/PDH where PDH is the collection's portable data hash and UUID is its UUID.")
70 mounts.add_argument('--mount-by-tag',
71 type=str, metavar='PATH', action='append', default=[],
72 help="Mount all collections with tag TAG at mountpoint/PATH/TAG/UUID.")
73 mounts.add_argument('--mount-home',
74 type=str, metavar='PATH', action='append', default=[],
75 help="Mount the current user's home project at mountpoint/PATH.")
76 mounts.add_argument('--mount-shared',
77 type=str, metavar='PATH', action='append', default=[],
78 help="Mount projects shared with the current user at mountpoint/PATH.")
79 mounts.add_argument('--mount-tmp',
80 type=str, metavar='PATH', action='append', default=[],
81 help="Create a new collection, mount it in read/write mode at mountpoint/PATH, and delete it when unmounting.")
83 self.add_argument('--debug', action='store_true', help="""Debug mode""")
84 self.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
85 self.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
86 self.add_argument('--encoding', type=str, help="Character encoding to use for filesystem, default is utf-8 (see Python codec registry for list of available encodings)", default="utf-8")
88 self.add_argument('--file-cache', type=int, help="File data cache size, in bytes (default 256MiB)", default=256*1024*1024)
89 self.add_argument('--directory-cache', type=int, help="Directory data cache size, in bytes (default 128MiB)", default=128*1024*1024)
91 self.add_argument('--disable-event-listening', action='store_true', help="Don't subscribe to events on the API server", dest="disable_event_listening", default=False)
93 self.add_argument('--read-only', action='store_false', help="Mount will be read only (default)", dest="enable_write", default=False)
94 self.add_argument('--read-write', action='store_true', help="Mount will be read-write", dest="enable_write", default=False)
96 self.add_argument('--crunchstat-interval', type=float, help="Write stats to stderr every N seconds (default disabled)", default=0)
98 unmount = self.add_mutually_exclusive_group()
99 unmount.add_argument('--unmount', action='store_true', default=False,
100 help="Forcefully unmount the specified mountpoint (if it's a fuse mount) and exit. If --subtype is given, unmount only if the mount has the specified subtype. WARNING: This command can affect any kind of fuse mount, not just arv-mount.")
101 unmount.add_argument('--unmount-all', action='store_true', default=False,
102 help="Forcefully unmount every fuse mount at or below the specified path and exit. If --subtype is given, unmount only mounts that have the specified subtype. Exit non-zero if any other types of mounts are found at or below the given path. WARNING: This command can affect any kind of fuse mount, not just arv-mount.")
103 unmount.add_argument('--replace', action='store_true', default=False,
104 help="If a fuse mount is already present at mountpoint, forcefully unmount it before mounting")
105 self.add_argument('--unmount-timeout',
106 type=float, default=2.0,
107 help="Time to wait for graceful shutdown after --exec program exits and filesystem is unmounted")
109 self.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
110 dest="exec_args", metavar=('command', 'args', '...', '--'),
111 help="""Mount, run a command, then unmount and exit""")
115 def __init__(self, args, logger=logging.getLogger('arvados.arv-mount')):
119 self.listen_for_events = False
121 self.args.mountpoint = os.path.realpath(self.args.mountpoint)
122 if self.args.logfile:
123 self.args.logfile = os.path.realpath(self.args.logfile)
126 self._setup_logging()
129 except Exception as e:
130 self.logger.exception("arv-mount: exception during setup: %s", e)
134 if self.args.replace:
135 unmount(path=self.args.mountpoint,
136 timeout=self.args.unmount_timeout)
137 llfuse.init(self.operations, self.args.mountpoint, self._fuse_options())
139 daemon.DaemonContext(
140 working_directory=os.path.dirname(self.args.mountpoint),
141 files_preserve=range(
142 3, resource.getrlimit(resource.RLIMIT_NOFILE)[1])
144 if self.listen_for_events and not self.args.disable_event_listening:
145 self.operations.listen_for_events()
146 self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
147 self.llfuse_thread.daemon = True
148 self.llfuse_thread.start()
149 self.operations.initlock.wait()
152 def __exit__(self, exc_type, exc_value, traceback):
153 if self.operations.events:
154 self.operations.events.close(timeout=self.args.unmount_timeout)
155 subprocess.call(["fusermount", "-u", "-z", self.args.mountpoint])
156 self.llfuse_thread.join(timeout=self.args.unmount_timeout)
157 if self.llfuse_thread.is_alive():
158 self.logger.warning("Mount.__exit__:"
159 " llfuse thread still alive %fs after umount"
160 " -- abandoning and exiting anyway",
161 self.args.unmount_timeout)
164 if self.args.unmount or self.args.unmount_all:
165 unmount(path=self.args.mountpoint,
166 subtype=self.args.subtype,
167 timeout=self.args.unmount_timeout,
168 recursive=self.args.unmount_all)
169 elif self.args.exec_args:
172 self._run_standalone()
174 def _fuse_options(self):
175 """FUSE mount options; see mount.fuse(8)"""
176 opts = [optname for optname in ['allow_other', 'debug']
177 if getattr(self.args, optname)]
178 # Increase default read/write size from 4KiB to 128KiB
179 opts += ["big_writes", "max_read=131072"]
180 if self.args.subtype:
181 opts += ["subtype="+self.args.subtype]
184 def _setup_logging(self):
185 # Configure a log handler based on command-line switches.
186 if self.args.logfile:
187 log_handler = logging.FileHandler(self.args.logfile)
188 log_handler.setFormatter(logging.Formatter(
189 '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s',
190 '%Y-%m-%d %H:%M:%S'))
194 if log_handler is not None:
195 arvados.logger.removeHandler(arvados.log_handler)
196 arvados.logger.addHandler(log_handler)
199 arvados.logger.setLevel(logging.DEBUG)
200 logging.getLogger('arvados.keep').setLevel(logging.DEBUG)
201 logging.getLogger('arvados.api').setLevel(logging.DEBUG)
202 logging.getLogger('arvados.collection').setLevel(logging.DEBUG)
203 self.logger.debug("arv-mount debugging enabled")
205 self.logger.info("%s %s started", sys.argv[0], __version__)
206 self.logger.info("enable write is %s", self.args.enable_write)
208 def _setup_api(self):
210 self.api = arvados.safeapi.ThreadSafeApiCache(
211 apiconfig=arvados.config.settings(),
213 'block_cache': arvados.keep.KeepBlockCache(self.args.file_cache),
214 'num_retries': self.args.retries,
216 except KeyError as e:
217 self.logger.error("Missing environment: %s", e)
219 # Do a sanity check that we have a working arvados host + token.
220 self.api.users().current().execute()
222 def _setup_mount(self):
223 self.operations = Operations(
227 encoding=self.args.encoding,
228 inode_cache=InodeCache(cap=self.args.directory_cache),
229 enable_write=self.args.enable_write)
231 if self.args.crunchstat_interval:
232 statsthread = threading.Thread(
233 target=crunchstat.statlogger,
234 args=(self.args.crunchstat_interval,
237 statsthread.daemon = True
240 usr = self.api.users().current().execute(num_retries=self.args.retries)
243 dir_args = [llfuse.ROOT_INODE, self.operations.inodes, self.api, self.args.retries]
246 if self.args.collection is not None:
247 # Set up the request handler with the collection at the root
248 # First check that the collection is readable
249 self.api.collections().get(uuid=self.args.collection).execute()
250 self.args.mode = 'collection'
251 dir_class = CollectionDirectory
252 dir_args.append(self.args.collection)
253 elif self.args.project is not None:
254 self.args.mode = 'project'
255 dir_class = ProjectDirectory
257 self.api.groups().get(uuid=self.args.project).execute(
258 num_retries=self.args.retries))
260 if (self.args.mount_by_id or
261 self.args.mount_by_pdh or
262 self.args.mount_by_tag or
263 self.args.mount_home or
264 self.args.mount_shared or
265 self.args.mount_tmp):
266 if self.args.mode is not None:
268 "Cannot combine '{}' mode with custom --mount-* options.".
269 format(self.args.mode))
270 elif self.args.mode is None:
271 # If no --mount-custom or custom mount args, --all is the default
272 self.args.mode = 'all'
274 if self.args.mode in ['by_id', 'by_pdh']:
275 # Set up the request handler with the 'magic directory' at the root
276 dir_class = MagicDirectory
277 dir_args.append(self.args.mode == 'by_pdh')
278 elif self.args.mode == 'by_tag':
279 dir_class = TagsDirectory
280 elif self.args.mode == 'shared':
281 dir_class = SharedDirectory
283 elif self.args.mode == 'home':
284 dir_class = ProjectDirectory
286 dir_args.append(True)
287 elif self.args.mode == 'all':
288 self.args.mount_by_id = ['by_id']
289 self.args.mount_by_tag = ['by_tag']
290 self.args.mount_home = ['home']
291 self.args.mount_shared = ['shared']
294 if dir_class is not None:
295 ent = dir_class(*dir_args)
296 self.operations.inodes.add_entry(ent)
297 self.listen_for_events = ent.want_event_subscribe()
300 e = self.operations.inodes.add_entry(Directory(
301 llfuse.ROOT_INODE, self.operations.inodes))
302 dir_args[0] = e.inode
304 for name in self.args.mount_by_id:
305 self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=False))
306 for name in self.args.mount_by_pdh:
307 self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=True))
308 for name in self.args.mount_by_tag:
309 self._add_mount(e, name, TagsDirectory(*dir_args))
310 for name in self.args.mount_home:
311 self._add_mount(e, name, ProjectDirectory(*dir_args, project_object=usr, poll=True))
312 for name in self.args.mount_shared:
313 self._add_mount(e, name, SharedDirectory(*dir_args, exclude=usr, poll=True))
314 for name in self.args.mount_tmp:
315 self._add_mount(e, name, TmpCollectionDirectory(*dir_args))
318 text = self._readme_text(
319 arvados.config.get('ARVADOS_API_HOST'),
321 self._add_mount(e, 'README', StringFile(e.inode, text, now))
323 def _add_mount(self, tld, name, ent):
324 if name in ['', '.', '..'] or '/' in name:
325 sys.exit("Mount point '{}' is not supported.".format(name))
326 tld._entries[name] = self.operations.inodes.add_entry(ent)
327 self.listen_for_events = (self.listen_for_events or ent.want_event_subscribe())
329 def _readme_text(self, api_host, user_email):
331 Welcome to Arvados! This directory provides file system access to
332 files and objects available on the Arvados installation located at
333 '{}' using credentials for user '{}'.
335 From here, the following directories are available:
337 by_id/ Access to Keep collections by uuid or portable data hash (see by_id/README for details).
338 by_tag/ Access to Keep collections organized by tag.
339 home/ The contents of your home project.
340 shared/ Projects shared with you.
342 '''.format(api_host, user_email)
348 sp = subprocess.Popen(self.args.exec_args, shell=False)
350 # forward signals to the process.
351 signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
352 signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
353 signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
355 # wait for process to complete.
358 # restore default signal handlers.
359 signal.signal(signal.SIGINT, signal.SIG_DFL)
360 signal.signal(signal.SIGTERM, signal.SIG_DFL)
361 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
362 except Exception as e:
363 self.logger.exception(
364 'arv-mount: exception during exec %s', self.args.exec_args)
367 except AttributeError:
371 def _run_standalone(self):
373 self.daemon = not self.args.foreground
375 self.llfuse_thread.join(timeout=None)
376 except Exception as e:
377 self.logger.exception('arv-mount: exception during mount: %s', e)
378 exit(getattr(e, 'errno', 1))
381 def _llfuse_main(self):
385 llfuse.close(unmount=False)