1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
18 import arvados.commands._util as arv_cmd
19 from arvados_fuse import crunchstat
20 from arvados_fuse import *
21 from arvados_fuse.unmount import unmount
22 from arvados_fuse._version import __version__
24 class ArgumentParser(argparse.ArgumentParser):
26 super(ArgumentParser, self).__init__(
27 parents=[arv_cmd.retry_opt],
28 description="Interact with Arvados data through a local filesystem",
33 version=u"%s %s" % (sys.argv[0], __version__),
34 help="Print version and exit",
39 help="Directory path to mount data",
42 mode_group = self.add_argument_group("Mount contents")
43 mode = mode_group.add_mutually_exclusive_group()
50 Mount a subdirectory for each mode: `home`, `shared`, `by_id`, and `by_tag`
51 (default if no `--mount-*` options are given)
60 Mount a subdirectory for each mode specified by a `--mount-*` option
61 (default if any `--mount-*` options are given;
62 see "Mount custom layout and filtering" section)
67 metavar='UUID_OR_PDH',
68 help="Mount the specified collection",
75 help="Mount your home project",
80 help="Mount the specified project",
87 help="Mount a subdirectory for each project shared with you",
95 Mount a magic directory where collections and projects are accessible through
96 subdirectories named after their UUID or portable data hash
101 action='store_const',
105 Mount a magic directory where collections are accessible through
106 subdirectories named after their portable data hash
111 action='store_const',
114 help="Mount a subdirectory for each tag attached to a collection or project",
117 mounts = self.add_argument_group("Mount custom layout and filtering")
120 type=arv_cmd.JSONArgument(arv_cmd.validate_filters),
122 Filters to apply to all project, shared, and tag directory contents.
123 Pass filters as either a JSON string or a path to a JSON file.
124 The JSON object should be a list of filters in Arvados API list filter syntax.
132 help="Make your home project available under the mount at `PATH`",
139 help="Make projects shared with you available under the mount at `PATH`",
147 Make a new temporary writable collection available under the mount at `PATH`.
148 This collection is deleted when the mount is unmounted.
157 Make a magic directory available under the mount at `PATH` where collections and
158 projects are accessible through subdirectories named after their UUID or
168 Make a magic directory available under the mount at `PATH` where collections
169 are accessible through subdirectories named after portable data hash
178 Make a subdirectory for each tag attached to a collection or project available
179 under the mount at `PATH`
183 perms = self.add_argument_group("Mount access and permissions")
187 help="Let other users on this system read mounted data (default false)",
191 action='store_false',
194 help="Mounted data cannot be modified from the mount (default)",
201 help="Mounted data can be modified from the mount",
204 lifecycle = self.add_argument_group("Mount lifecycle management")
205 lifecycle.add_argument(
207 nargs=argparse.REMAINDER,
210 Mount data, run the specified command, then unmount and exit.
211 `--exec` reads all remaining options as the command to run,
212 so it must be the last option you specify.
213 Either end your command arguments (and other options) with a `--` argument,
214 or specify `--exec` after your mount point.
217 lifecycle.add_argument(
221 help="Run mount process in the foreground instead of daemonizing (default false)",
223 lifecycle.add_argument(
225 help="Set mounted filesystem type to `fuse.SUBTYPE` (default is just `fuse`)",
227 unmount = lifecycle.add_mutually_exclusive_group()
228 unmount.add_argument(
233 If a FUSE mount is already mounted at the given directory,
234 unmount it before mounting the requested data.
235 If `--subtype` is specified, unmount only if the mount has that subtype.
236 WARNING: This command can affect any kind of FUSE mount, not just arv-mount.
239 unmount.add_argument(
244 If a FUSE mount is already mounted at the given directory, unmount it and exit.
245 If `--subtype` is specified, unmount only if the mount has that subtype.
246 WARNING: This command can affect any kind of FUSE mount, not just arv-mount.
249 unmount.add_argument(
254 Unmount all FUSE mounts at or below the given directory, then exit.
255 If `--subtype` is specified, unmount only if the mount has that subtype.
256 WARNING: This command can affect any kind of FUSE mount, not just arv-mount.
259 lifecycle.add_argument(
265 The number of seconds to wait for a clean unmount after an `--exec` command has
266 exited (default %(default).01f).
267 After this time, the mount will be forcefully unmounted.
271 reporting = self.add_argument_group("Mount logging and statistics")
272 reporting.add_argument(
273 '--crunchstat-interval',
277 help="Write stats to stderr every N seconds (default disabled)",
279 reporting.add_argument(
282 help="Log debug information",
284 reporting.add_argument(
286 help="Write debug logs and errors to the specified file (default stderr)",
289 cache = self.add_argument_group("Mount local cache setup")
290 cachetype = cache.add_mutually_exclusive_group()
291 cachetype.add_argument(
296 help="Cache data on the local filesystem (default)",
298 cachetype.add_argument(
300 action='store_false',
303 help="Cache data in memory",
308 help="Set custom filesystem cache location",
313 default=128*1024*1024,
315 help="Size of directory data cache in bytes (default 128 MiB)",
323 Size of file data cache in bytes
324 (default 8 GiB for filesystem cache, 256 MiB for memory cache)
328 plumbing = self.add_argument_group("Mount interactions with Arvados and Linux")
329 plumbing.add_argument(
330 '--disable-event-listening',
332 dest='disable_event_listening',
334 help="Don't subscribe to events on the API server to update mount contents",
336 plumbing.add_argument(
340 Filesystem character encoding
341 (default %(default)r; specify a name from the Python codec registry)
344 plumbing.add_argument(
347 help="Comma-separated list of storage classes to request for new collections",
349 # This is a hidden argument used by tests. Normally this
350 # value will be extracted from the cluster config, but mocking
351 # the cluster config under the presence of multiple threads
352 # and processes turned out to be too complicated and brittle.
353 plumbing.add_argument(
357 help=argparse.SUPPRESS)
360 def __init__(self, args, logger=logging.getLogger('arvados.arv-mount')):
364 self.listen_for_events = False
366 self.args.mountpoint = os.path.realpath(self.args.mountpoint)
367 if self.args.logfile:
368 self.args.logfile = os.path.realpath(self.args.logfile)
371 self._setup_logging()
372 except Exception as e:
373 self.logger.exception("exception during setup: %s", e)
377 nofile_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
380 if self.args.file_cache:
381 # Adjust the file handle limit so it can meet
382 # the desired cache size. Multiply by 8 because the
383 # number of 64 MiB cache slots that keepclient
384 # allocates is RLIMIT_NOFILE / 8
385 minlimit = int((self.args.file_cache/(64*1024*1024)) * 8)
387 if nofile_limit[0] < minlimit:
388 resource.setrlimit(resource.RLIMIT_NOFILE, (min(minlimit, nofile_limit[1]), nofile_limit[1]))
390 if minlimit > nofile_limit[1]:
391 self.logger.warning("file handles required to meet --file-cache (%s) exceeds hard file handle limit (%s), cache size will be smaller than requested", minlimit, nofile_limit[1])
393 except Exception as e:
394 self.logger.warning("unable to adjust file handle limit: %s", e)
396 nofile_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
397 self.logger.info("file cache capped at %s bytes or less based on available disk (RLIMIT_NOFILE is %s)", ((nofile_limit[0]//8)*64*1024*1024), nofile_limit)
402 except Exception as e:
403 self.logger.exception("exception during setup: %s", e)
407 if self.args.replace:
408 unmount(path=self.args.mountpoint,
409 timeout=self.args.unmount_timeout)
410 llfuse.init(self.operations, str(self.args.mountpoint), self._fuse_options())
412 daemon.DaemonContext(
413 working_directory=os.path.dirname(self.args.mountpoint),
414 files_preserve=list(range(
415 3, resource.getrlimit(resource.RLIMIT_NOFILE)[1]))
417 if self.listen_for_events and not self.args.disable_event_listening:
418 self.operations.listen_for_events()
419 self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
420 self.llfuse_thread.daemon = True
421 self.llfuse_thread.start()
422 self.operations.initlock.wait()
425 def __exit__(self, exc_type, exc_value, traceback):
426 if self.operations.events:
427 self.operations.events.close(timeout=self.args.unmount_timeout)
428 subprocess.call(["fusermount", "-u", "-z", self.args.mountpoint])
429 self.llfuse_thread.join(timeout=self.args.unmount_timeout)
430 if self.llfuse_thread.is_alive():
431 self.logger.warning("Mount.__exit__:"
432 " llfuse thread still alive %fs after umount"
433 " -- abandoning and exiting anyway",
434 self.args.unmount_timeout)
437 if self.args.unmount or self.args.unmount_all:
438 unmount(path=self.args.mountpoint,
439 subtype=self.args.subtype,
440 timeout=self.args.unmount_timeout,
441 recursive=self.args.unmount_all)
442 elif self.args.exec_args:
445 self._run_standalone()
447 def _fuse_options(self):
448 """FUSE mount options; see mount.fuse(8)"""
449 opts = [optname for optname in ['allow_other', 'debug']
450 if getattr(self.args, optname)]
451 # Increase default read/write size from 4KiB to 128KiB
452 opts += ["big_writes", "max_read=131072"]
453 if self.args.subtype:
454 opts += ["subtype="+self.args.subtype]
457 def _setup_logging(self):
458 # Configure a log handler based on command-line switches.
459 if self.args.logfile:
460 log_handler = logging.FileHandler(self.args.logfile)
461 log_handler.setFormatter(logging.Formatter(
462 '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s',
463 '%Y-%m-%d %H:%M:%S'))
467 if log_handler is not None:
468 arvados.logger.removeHandler(arvados.log_handler)
469 arvados.logger.addHandler(log_handler)
472 arvados.logger.setLevel(logging.DEBUG)
473 logging.getLogger('arvados.keep').setLevel(logging.DEBUG)
474 logging.getLogger('arvados.api').setLevel(logging.DEBUG)
475 logging.getLogger('arvados.collection').setLevel(logging.DEBUG)
476 self.logger.debug("arv-mount debugging enabled")
478 self.logger.info("%s %s started", sys.argv[0], __version__)
479 self.logger.info("enable write is %s", self.args.enable_write)
481 def _setup_api(self):
483 # default value of file_cache is 0, this tells KeepBlockCache to
484 # choose a default based on whether disk_cache is enabled or not.
486 block_cache = arvados.keep.KeepBlockCache(cache_max=self.args.file_cache,
487 disk_cache=self.args.disk_cache,
488 disk_cache_dir=self.args.disk_cache_dir)
490 self.api = arvados.safeapi.ThreadSafeApiCache(
491 apiconfig=arvados.config.settings(),
493 'num_retries': self.args.retries,
496 'block_cache': block_cache,
497 'num_retries': self.args.retries,
501 except KeyError as e:
502 self.logger.error("Missing environment: %s", e)
504 # Do a sanity check that we have a working arvados host + token.
505 self.api.users().current().execute()
507 def _setup_mount(self):
508 self.operations = Operations(
512 encoding=self.args.encoding,
513 inode_cache=InodeCache(cap=self.args.directory_cache),
514 enable_write=self.args.enable_write,
517 if self.args.crunchstat_interval:
518 statsthread = threading.Thread(
519 target=crunchstat.statlogger,
520 args=(self.args.crunchstat_interval,
523 statsthread.daemon = True
526 usr = self.api.users().current().execute(num_retries=self.args.retries)
531 self.operations.inodes,
534 self.args.enable_write,
539 storage_classes = None
540 if self.args.storage_classes is not None:
541 storage_classes = self.args.storage_classes.replace(' ', '').split(',')
542 self.logger.info("Storage classes requested for new collections: {}".format(', '.join(storage_classes)))
544 if self.args.collection is not None:
545 # Set up the request handler with the collection at the root
546 # First check that the collection is readable
547 self.api.collections().get(uuid=self.args.collection).execute()
548 self.args.mode = 'collection'
549 dir_class = CollectionDirectory
550 dir_args.append(self.args.collection)
551 elif self.args.project is not None:
552 self.args.mode = 'project'
553 dir_class = ProjectDirectory
555 self.api.groups().get(uuid=self.args.project).execute(
556 num_retries=self.args.retries))
558 if (self.args.mount_by_id or
559 self.args.mount_by_pdh or
560 self.args.mount_by_tag or
561 self.args.mount_home or
562 self.args.mount_shared or
563 self.args.mount_tmp):
564 if self.args.mode is not None:
566 "Cannot combine '{}' mode with custom --mount-* options.".
567 format(self.args.mode))
568 elif self.args.mode is None:
569 # If no --mount-custom or custom mount args, --all is the default
570 self.args.mode = 'all'
572 if self.args.mode in ['by_id', 'by_pdh']:
573 # Set up the request handler with the 'magic directory' at the root
574 dir_class = MagicDirectory
575 dir_args.append(self.args.mode == 'by_pdh')
576 elif self.args.mode == 'by_tag':
577 dir_class = TagsDirectory
578 elif self.args.mode == 'shared':
579 dir_class = SharedDirectory
581 elif self.args.mode == 'home':
582 dir_class = ProjectDirectory
584 dir_args.append(True)
585 elif self.args.mode == 'all':
586 self.args.mount_by_id = ['by_id']
587 self.args.mount_by_tag = ['by_tag']
588 self.args.mount_home = ['home']
589 self.args.mount_shared = ['shared']
592 if dir_class is not None:
593 if dir_class in [TagsDirectory, CollectionDirectory]:
594 ent = dir_class(*dir_args)
596 ent = dir_class(*dir_args, storage_classes=storage_classes)
597 self.operations.inodes.add_entry(ent)
598 self.listen_for_events = ent.want_event_subscribe()
601 e = self.operations.inodes.add_entry(Directory(
603 self.operations.inodes,
604 self.args.enable_write,
607 dir_args[0] = e.inode
609 for name in self.args.mount_by_id:
610 self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=False, storage_classes=storage_classes))
611 for name in self.args.mount_by_pdh:
612 self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=True))
613 for name in self.args.mount_by_tag:
614 self._add_mount(e, name, TagsDirectory(*dir_args))
615 for name in self.args.mount_home:
616 self._add_mount(e, name, ProjectDirectory(*dir_args, project_object=usr, poll=True, storage_classes=storage_classes))
617 for name in self.args.mount_shared:
618 self._add_mount(e, name, SharedDirectory(*dir_args, exclude=usr, poll=True, storage_classes=storage_classes))
619 for name in self.args.mount_tmp:
620 self._add_mount(e, name, TmpCollectionDirectory(*dir_args, storage_classes=storage_classes))
623 text = self._readme_text(
624 arvados.config.get('ARVADOS_API_HOST'),
626 self._add_mount(e, 'README', StringFile(e.inode, text, now))
628 def _add_mount(self, tld, name, ent):
629 if name in ['', '.', '..'] or '/' in name:
630 sys.exit("Mount point '{}' is not supported.".format(name))
631 tld._entries[name] = self.operations.inodes.add_entry(ent)
632 self.listen_for_events = (self.listen_for_events or ent.want_event_subscribe())
634 def _readme_text(self, api_host, user_email):
636 Welcome to Arvados! This directory provides file system access to
637 files and objects available on the Arvados installation located at
638 '{}' using credentials for user '{}'.
640 From here, the following directories are available:
642 by_id/ Access to Keep collections by uuid or portable data hash (see by_id/README for details).
643 by_tag/ Access to Keep collections organized by tag.
644 home/ The contents of your home project.
645 shared/ Projects shared with you.
647 '''.format(api_host, user_email)
653 sp = subprocess.Popen(self.args.exec_args, shell=False)
655 # forward signals to the process.
656 signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
657 signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
658 signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
660 # wait for process to complete.
663 # restore default signal handlers.
664 signal.signal(signal.SIGINT, signal.SIG_DFL)
665 signal.signal(signal.SIGTERM, signal.SIG_DFL)
666 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
667 except Exception as e:
668 self.logger.exception(
669 'arv-mount: exception during exec %s', self.args.exec_args)
672 except AttributeError:
676 def _run_standalone(self):
678 self.daemon = not self.args.foreground
680 self.llfuse_thread.join(timeout=None)
681 except Exception as e:
682 self.logger.exception('arv-mount: exception during mount: %s', e)
683 exit(getattr(e, 'errno', 1))
686 def _llfuse_main(self):
688 llfuse.main(workers=10)
690 llfuse.close(unmount=False)
692 self.operations.begin_shutdown()