예제 #1
0
def get_tmp_dir(default=None):
    """Check tmp directory candidates and return the one with the most
    available space.
    """
    if default is not None:
        return default

    # TODO: I need to find a better way of choosing temporary directories.
    # Maybe check all available mount points.
    TMP_CANDIDATES = [
        t for t in ('/var/tmp', os.path.expanduser('~'), '/mnt')
        if os.access(t, os.W_OK)
    ]

    space = [free_space(t) for t in TMP_CANDIDATES]

    max_idx = 0
    max_val = space[0]
    for i, val in zip(range(len(space)), space):
        if val > max_val:
            max_val = val
            max_idx = i

    # Return the candidate path with more available space
    return TMP_CANDIDATES[max_idx]
예제 #2
0
    def create_image(self, image):
        """Given an image filename, this method will create an image out of the
        running system.
        """

        size = self.disk.device.length * self.disk.device.sectorSize

        # Create sparse file to host the image
        fd = os.open(image, os.O_WRONLY | os.O_CREAT)
        try:
            os.ftruncate(fd, size)
        finally:
            os.close(fd)

        self._create_partition_table(image)
        end_sector, partitions = self._shrink_partitions(image)

        if self.disk.type == 'gpt':
            old_size = size
            size = (end_sector + 1) * self.disk.device.sectorSize
            ptable = GPTPartitionTable(image)
            size = ptable.shrink(size, old_size)
        else:
            # Align to 2048
            end_sector = ((end_sector + 2047) // 2048) * 2048
            size = (end_sector + 1) * self.disk.device.sectorSize

        # Truncate image to the new size.
        fd = os.open(image, os.O_RDWR)
        try:
            os.ftruncate(fd, size)
        finally:
            os.close(fd)

        # Check if the available space is enough to host the image
        dirname = os.path.dirname(image)
        self.out.info("Examining available space ...", False)
        if free_space(dirname) <= size:
            raise FatalError("Not enough space under %s to host the temporary "
                             "image" % dirname)
        self.out.success("sufficient")

        self._create_filesystems(image, partitions)

        return image
예제 #3
0
def get_tmp_dir(default=None):
    """Check tmp directory candidates and return the one with the most
    available space.
    """
    if default is not None:
        return default

    # TODO: I need to find a better way of choosing temporary directories.
    # Maybe check all available mount points.
    TMP_CANDIDATES = [t for t in ('/var/tmp', os.path.expanduser('~'), '/mnt')
                      if os.access(t, os.W_OK)]

    space = [free_space(t) for t in TMP_CANDIDATES]

    max_idx = 0
    max_val = space[0]
    for i, val in zip(range(len(space)), space):
        if val > max_val:
            max_val = val
            max_idx = i

    # Return the candidate path with more available space
    return TMP_CANDIDATES[max_idx]