def test_do_ismount_successes_ino(self):

        _os_lstat = os.lstat

        class MockStat(object):
            def __init__(self, mode, dev, ino):
                self.st_mode = mode
                self.st_dev = dev
                self.st_ino = ino

        def _mock_os_lstat(path):
            if path.endswith(".."):
                return _os_lstat(path)
            else:
                parent_path = os.path.join(path, "..")
                child = _os_lstat(path)
                parent = _os_lstat(parent_path)
                return MockStat(child.st_mode, parent.st_ino,
                                child.st_dev)

        tmpdir = mkdtemp()
        try:
            with patch("os.lstat", _mock_os_lstat):
                try:
                    fs.do_ismount(tmpdir)
                except GlusterFileSystemOSError as err:
                    self.fail("Unexpected exception")
                else:
                    pass
        finally:
            shutil.rmtree(tmpdir)
    def test_do_ismount_path_error(self):

        def _mock_os_lstat(path):
            raise OSError(13, "foo")

        tmpdir = mkdtemp()
        try:
            with patch("os.lstat", _mock_os_lstat):
                try:
                    fs.do_ismount(tmpdir)
                except GlusterFileSystemOSError as err:
                    pass
                else:
                    self.fail("Expected GlusterFileSystemOSError")
        finally:
            shutil.rmtree(tmpdir)
Example #3
0
    def _create_expiring_tracker_object(self, object_path):
        try:

            # Check if gsexpiring volume is present in ring
            if not any(d.get('device', None) == self.expiring_objects_account
                       for d in self.object_ring.devs):
                raise Exception("%s volume not in ring" %
                                self.expiring_objects_account)

            # Check if gsexpiring is mounted.
            expiring_objects_account_path = \
                os.path.join(self.devices, self.expiring_objects_account)
            mount_check = self._diskfile_router['junk'].mount_check
            if mount_check and not do_ismount(expiring_objects_account_path):
                raise Exception("Path %s doesn't exist or is not a mount "
                                "point" % expiring_objects_account_path)

            # Create object directory
            object_dir = os.path.dirname(object_path)
            try:
                mkdirs(object_dir)
            except OSError as err:
                mkdirs(object_dir)  # handle race

            # Create zero-byte file
            try:
                os.mknod(object_path)
            except OSError as err:
                if err.errno != errno.EEXIST:
                    raise
        except Exception as e:
            self.logger.error("Creation of tracker object %s failed: %s" %
                              (object_path, str(e)))
Example #4
0
    def _create_expiring_tracker_object(self, object_path):
        try:

            # Check if gsexpiring volume is present in ring
            if not any(
                    d.get('device', None) == self.expiring_objects_account
                    for d in self.object_ring.devs):
                raise Exception("%s volume not in ring" %
                                self.expiring_objects_account)

            # Check if gsexpiring is mounted.
            expiring_objects_account_path = \
                os.path.join(self.devices, self.expiring_objects_account)
            mount_check = self._diskfile_router['junk'].mount_check
            if mount_check and not do_ismount(expiring_objects_account_path):
                raise Exception("Path %s doesn't exist or is not a mount "
                                "point" % expiring_objects_account_path)

            # Create object directory
            object_dir = os.path.dirname(object_path)
            try:
                mkdirs(object_dir)
            except OSError as err:
                mkdirs(object_dir)  # handle race

            # Create zero-byte file
            try:
                os.mknod(object_path)
            except OSError as err:
                if err.errno != errno.EEXIST:
                    raise
        except Exception as e:
            self.logger.error("Creation of tracker object %s failed: %s" %
                              (object_path, str(e)))
 def test_do_ismount_path_is_symlink(self):
     tmpdir = mkdtemp()
     try:
         link = os.path.join(tmpdir, "tmp")
         os.symlink("/tmp", link)
         assert False == fs.do_ismount(link)
     finally:
         shutil.rmtree(tmpdir)
 def test_do_ismount_path_not_mount(self):
     tmpdir = mkdtemp()
     try:
         assert False == fs.do_ismount(tmpdir)
     finally:
         shutil.rmtree(tmpdir)
 def test_do_ismount_path_does_not_exist(self):
     tmpdir = mkdtemp()
     try:
         assert False == fs.do_ismount(os.path.join(tmpdir, 'bar'))
     finally:
         shutil.rmtree(tmpdir)
 def test_do_ismount_path_is_root(self):
     assert True == fs.do_ismount('/')
Example #9
0
def mount(root, drive):
    """
    Verify that the path to the device is a mount point and mounted.  This
    allows us to fast fail on drives that have been unmounted because of
    issues, and also prevents us for accidentally filling up the root
    partition.

    This method effectively replaces the swift.common.constraints.check_mount
    method in behavior, adding the ability to auto-mount the volume, which is
    dubious (FIXME).

    :param root:  base path where the devices are mounted
    :param drive: drive name to be checked
    :returns: True if it is a valid mounted device, False otherwise
    """
    if not (urllib.quote_plus(drive) == drive):
        return False

    mount_point = _get_drive_mount_point_name(drive)
    full_mount_path = os.path.join(root, mount_point)
    if do_ismount(full_mount_path):
        # Don't bother checking volume if it is already a mount point. Allows
        # us to use local file systems for unit tests and some functional test
        # environments to isolate behaviors from GlusterFS itself.
        return True

    # FIXME: Possible thundering herd problem here

    el = _get_export_list()
    for export in el:
        if drive == export:
            break
    else:
        logging.error('No export found in %r matching drive, %s', el, drive)
        return False

    try:
        os.makedirs(full_mount_path)
    except OSError as err:
        if err.errno == errno.EEXIST:
            pass
        else:
            logging.exception('Could not create mount path hierarchy:'
                              ' %s' % full_mount_path)
            return False

    mnt_cmd = 'mount -t glusterfs %s:%s %s' % (MOUNT_IP, export,
                                               full_mount_path)

    if _allow_mount_per_server:
        if os.system(mnt_cmd):
            logging.exception('Mount failed %s' % (mnt_cmd))
        return True

    lck_file = os.path.join(RUN_DIR, '%s.lock' % mount_point)

    try:
        os.mkdir(RUN_DIR)
    except OSError as err:
        if err.errno == errno.EEXIST:
            pass
        else:
            logging.exception('Could not create RUN_DIR: %s' % full_mount_path)
            return False

    fd = os.open(lck_file, os.O_CREAT | os.O_RDWR)
    with os.fdopen(fd, 'r+b') as f:
        try:
            fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError as ex:
            if ex.errno in (errno.EACCES, errno.EAGAIN):
                # This means that some other process is mounting the
                # filesystem, so wait for the mount process to complete
                return _busy_wait(full_mount_path)
        if os.system(mnt_cmd) or not _busy_wait(full_mount_path):
            logging.error('Mount failed %s', mnt_cmd)
            return False
    return True
Example #10
0
def mount(root, drive):
    """
    Verify that the path to the device is a mount point and mounted.  This
    allows us to fast fail on drives that have been unmounted because of
    issues, and also prevents us for accidentally filling up the root
    partition.

    This method effectively replaces the swift.common.constraints.check_mount
    method in behavior, adding the ability to auto-mount the volume, which is
    dubious (FIXME).

    :param root:  base path where the devices are mounted
    :param drive: drive name to be checked
    :returns: True if it is a valid mounted device, False otherwise
    """
    if not (urllib.quote_plus(drive) == drive):
        return False

    mount_point = _get_drive_mount_point_name(drive)
    full_mount_path = os.path.join(root, mount_point)
    if do_ismount(full_mount_path):
        # Don't bother checking volume if it is already a mount point. Allows
        # us to use local file systems for unit tests and some functional test
        # environments to isolate behaviors from GlusterFS itself.
        return True

    # FIXME: Possible thundering herd problem here

    el = _get_export_list()
    for export in el:
        if drive == export:
            break
    else:
        logging.error('No export found in %r matching drive, %s', el, drive)
        return False

    try:
        os.makedirs(full_mount_path)
    except OSError as err:
        if err.errno == errno.EEXIST:
            pass
        else:
            logging.exception('Could not create mount path hierarchy:'
                              ' %s' % full_mount_path)
            return False

    mnt_cmd = 'mount -t glusterfs %s:%s %s' % (MOUNT_IP, export,
                                               full_mount_path)

    if _allow_mount_per_server:
        if os.system(mnt_cmd):
            logging.exception('Mount failed %s' % (mnt_cmd))
        return True

    lck_file = os.path.join(RUN_DIR, '%s.lock' % mount_point)

    try:
        os.mkdir(RUN_DIR)
    except OSError as err:
        if err.errno == errno.EEXIST:
            pass
        else:
            logging.exception('Could not create RUN_DIR: %s' % full_mount_path)
            return False

    fd = os.open(lck_file, os.O_CREAT | os.O_RDWR)
    with os.fdopen(fd, 'r+b') as f:
        try:
            fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError as ex:
            if ex.errno in (errno.EACCES, errno.EAGAIN):
                # This means that some other process is mounting the
                # filesystem, so wait for the mount process to complete
                return _busy_wait(full_mount_path)
        if os.system(mnt_cmd) or not _busy_wait(full_mount_path):
            logging.error('Mount failed %s', mnt_cmd)
            return False
    return True