Exemplo n.º 1
0
    def mount_index_device(self):
        """Mount our index device in the correct location."""

        if self.index_uuid is None:
            return self.NOT_OK, "No known index device."

        index_dev = disk_man.Device.find_device_by_uuid(self.index_uuid)

        if index_dev is None:
            return self.NOT_OK, "Index device {} has disappeared.".format(
                self.index_uuid)

        mount_point = settings.INDEX_PATH
        if index_dev.mountpoint is not None and index_dev.mountpoint != mount_point:
            # The disk is mounted in the wrong place somehow. Try to unmount it.
            try:
                Disk.umount_uuid(self.index_uuid)
            except RuntimeError as err:
                return self.NOT_OK, 'Index device {} mounted incorrectly, ' \
                                    'but could not unmount: {}'.format(self.index_uuid, err)

        # Try to mount the index disk.
        try:
            Disk.mount_uuid(self.index_uuid, is_index=True)
        except RuntimeError as err:
            return self.NOT_OK, 'Index device {} could not ' \
                                'be mounted: {}'.format(self.index_uuid, err)

        return self.OK, 'Mounted successfully'
Exemplo n.º 2
0
def destroy_raid(dev_name, trial_run=False):
    """Destroy the RAID identified by the """

    bd = Device.get_devices()

    dev_ob = bd.get(dev_name)
    if dev_ob is None:
        raise RuntimeError("No such device: {}.".format(dev_name))

    from apps.capture_node_api.models.capture import Disk
    if dev_ob.mountpoint is not None:
        Disk.umount_uuid(dev_ob.uuid)

    raid_devs = [bd[dev].dev_path for dev in dev_ob.disks.keys()]

    if not trial_run:
        if subprocess.call([settings.SUDO_PATH, STOP_CMD, dev_ob.dev_path]) != 0:
            raise RuntimeError("Could not stop raid {}.".format(dev_name))

        if subprocess.call([settings.SUDO_PATH, DESTROY_CMD] + raid_devs) != 0:
            raise RuntimeError("Could not destroy devices {}.".format(raid_devs))
Exemplo n.º 3
0
def init_capture_device(devices, status_file=None, trial_run=False, task=None):
    """Initialize the given device for capture. This involves:
    - Formatting the disk with XFS.
    - Registering the disk with the database.
    - Mounting the disk.
    - Preallocating BUCKET_SIZE files to fill the disk.
    - Registering those buckets with the database.
    :param str dev: The device to initialize as a capture disk.
    :param file status_file: A file like object where status updates should be written. Turned
    off by default.
    :param bool trial_run: Prepare the action, but don't do anything.
    :param task: A celery task for giving progress updates.
    """

    dev = devices[0]

    from apps.capture_node_api.models.capture import Disk

    with Lockfile('init_lock'):
        if status_file:
            print("Initializing {}. This may take a while.".format(dev), file=status_file)

        bd = Device.get_devices()

        if dev not in bd.keys():
            raise RuntimeError("Unknown device: {}".format(dev))

        dev_ob = bd[dev]

        if dev_ob.state:
            # If this device has a state, then it's not eligble to be a capture device.
            raise RuntimeError("Device is not eligible to be a capture because its state is {}"
                               .format(dev_ob.state))

        if task is not None:
            task.update_state(state='WORKING', meta={'msg': 'Formatting device. This may take a '
                                                            'while.',
                                                     'progress': 5})

        if status_file:
            print("Formatting {} as xfs.".format(dev_ob.dev_path), file=status_file)
        if not trial_run:
            dev_ob.format_xfs()

        if task is not None:
            task.update_state(state='WORKING', meta={'msg': 'Finished formating.',
                                                     'progress': 25})

        # Make sure this disk has a UUID now.
        if not dev_ob.uuid:
            raise RuntimeError("Could not get uuid of recently formatted disk.")

        if status_file:
            print("Adding {} to database.".format(dev_ob.dev_path), file=status_file)

        # Add the disk to the capture database.
        # It's added as disabled so we don't try to use it right away.
        disk = Disk(uuid=dev_ob.uuid, mode='DISABLED', size=dev_ob.size)

        # Figure out which disk is the biggest so we can reset the usage_inc for each disk.
        # When trying to figure out which disk to use next, capture chooses the disk with
        # the lowest 'usage'. After using it, the disk's usage is increased by the usage_inc.
        # If all disks are the same size, their usage_inc's should all be about 1.
        # If they're of different sizes, the increment will be larger for the smaller disks,
        # so that those disks will be used less frequently.
        max_size = max([d.size for d in Disk.objects.all()] + [dev_ob.size])
        disk.usage_inc = max_size/disk.size

        if not trial_run:
            for d in Disk.objects.all():
                # Reset the usage_inc and usage for all disks. If we don't do this, the new
                # disk will be used exclusively until it catches up in usage to the old disks.
                d.usage_inc = max_size/disk.size
                d.usage = 0
                d.save()

            disk.save()

        if task is not None:
            task.update_state(state='WORKING', meta={'msg': 'Device mounted and dded device to '
                                                            'the system.',
                                                     'progress': 30})

        if status_file:
            print("Mounting {} and filling it with capture slots.".format(dev_ob.dev_path),
                  file=status_file)

        # Fill our new capture disk with capture slots. This also mounts it.
        if not trial_run:
            try:
                disk.populate(task)
            except RuntimeError:
                disk.umount()
                disk.delete()
                raise

        if status_file:
            print("Finished initializing {}.".format(dev_ob.dev_path), file=status_file)