def open_image(image_id): global HaveAtomicException if HaveAtomicException and UseAtomic: logger.debug("atomic is either not installed or not accessable %s" % HaveAtomicException) HaveAtomicException = None if use_atomic_mount(): mount_point = tempfile.mkdtemp() logger.debug("Opening Image Id %s On %s using atomic" % (image_id, mount_point)) if runcommand(shlex.split("atomic mount") + [image_id, mount_point]) == 0: return AtomicTemporaryMountPoint(image_id, mount_point) else: logger.error('Could not mount Image Id %s On %s' % (image_id, mount_point)) shutil.rmtree(mount_point, ignore_errors=True) return None else: driver = _docker_driver() if driver is None: return None mount_point = tempfile.mkdtemp() logger.debug("Opening Image Id %s On %s using docker client" % (image_id, mount_point)) # docker mount creates a temp image # we have to use this temp image id to remove the device mount_point, cid = DockerMount(mount_point).mount(image_id) if driver == 'devicemapper': DockerMount.mount_path(os.path.join(mount_point, "rootfs"), mount_point, bind=True) if cid: return DockerTemporaryMountPoint(driver, image_id, mount_point, cid) else: logger.error('Could not mount Image Id %s On %s' % (image_id, mount_point)) shutil.rmtree(mount_point, ignore_errors=True) return None
def open_container(container_id): global HaveAtomicException if HaveAtomicException: logger.debug("atomic is either not installed or not accessable %s" % HaveAtomicException) HaveAtomicException = None if use_atomic_mount(): mount_point = tempfile.mkdtemp() logger.debug("Opening Container Id %s On %s using atomic" % (container_id, mount_point)) if runcommand(shlex.split("atomic mount") + [container_id, mount_point]) == 0: return AtomicTemporaryMountPoint(container_id, mount_point) else: logger.error('Could not mount Container Id %s On %s' % (container_id, mount_point)) shutil.rmtree(mount_point, ignore_errors=True) return None else: driver = _docker_driver() if driver is None: return None mount_point = tempfile.mkdtemp() logger.debug("Opening Container Id %s On %s using docker client" % (container_id, mount_point)) # docker mount creates a temp image # we have to use this temp image id to remove the device mount_point, cid = DockerMount(mount_point).mount(container_id) if driver == 'devicemapper': DockerMount.mount_path(os.path.join(mount_point, "rootfs"), mount_point, bind=True) if cid: return DockerTemporaryMountPoint(driver, container_id, mount_point, cid) else: logger.error('Could not mount Container Id %s On %s' % (container_id, mount_point)) shutil.rmtree(mount_point, ignore_errors=True) return None
def mount_obj(path, obj, driver): """ mounts the obj to the given path """ DockerMount(path).mount(obj) # only need to bind-mount on the devicemapper driver if driver == 'devicemapper': DockerMount.mount_path(os.path.join(path, "rootfs"), path, bind=True)
def mount_obj(path, obj, driver): """ mounts the obj to the given path """ # docker mount creates a temp image # we have to use this temp image id to remove the device path, new_cid = DockerMount(path).mount(obj) if driver == 'devicemapper': DockerMount.mount_path(os.path.join(path, "rootfs"), path, bind=True) return new_cid
def unmount_obj(path, driver): """ unmount the given path """ dev = DockerMount.get_dev_at_mountpoint(path) # If there's a bind-mount over the directory, unbind it. if dev.rsplit('[', 1)[-1].strip(']') == '/rootfs' \ and driver == 'devicemapper': Mount.unmount_path(path) DockerMount(path).unmount()
def unmount_obj(path, cid, driver): """ unmount the given path """ # If using device mapper, unmount the bind-mount over the directory if driver == 'devicemapper': Mount.unmount_path(path) DockerMount(path).unmount(cid)
def close(self): try: logger.debug("Closing Id %s On %s" % (self.image_id, self.mount_point)) # If using device mapper, unmount the bind-mount over the directory if self.driver == 'devicemapper': Mount.unmount_path(self.mount_point) DockerMount(self.mount_point).unmount(self.cid) except Exception as e: logger.debug("exception while unmounting image or container: %s" % e) shutil.rmtree(self.mount_point, ignore_errors=True)