<commit_before>#
# Copyright (c) 2011 rPath, Inc.
#
# All Rights Reserved
#

import logging
import os

from jobslave import lvm
from jobslave.generators import bootable_image, constants
from jobslave.geometry import FSTYPE_LINUX, FSTYPE_LINUX_LVM
from jobslave.util import logCall, divCeil

from conary.lib import util

log = logging.getLogger(__name__)


class HDDContainer(object):

    def __init__(self, image, geometry, totalSize=None):
        self.image = image
        self.geometry = geometry
        if totalSize is None:
            totalSize = os.stat(image).st_size
        self.totalSize = totalSize

    def create(self):
        # create the raw file
        # NB: blocksize is unrelated to the one in constants.py, and is
        # completely arbitrary.
        blocksize = 512
        seek = (self.totalSize - 1) / blocksize
        logCall('dd if=/dev/zero of=%s count=1 seek=%d bs=%d' % (
            self.image, max(seek, 0), blocksize))

    def partition(self, partitions):
        # Extended partitions are not supported since we're either using a
        # single partition for non-LVM or two for LVM (/boot + one PV)
        assert len(partitions) <= 4
        fObj = open(self.image, 'r+b')

        fObj.seek(440)
        fObj.write(os.urandom(4)) # disk signature
        fObj.write('\0\0')

        numParts = 0
        for start, size, fsType, bootable in partitions:
            numParts += 1
            log.info("Partition %d: start %d  size %d  flags %02x  type %02x",
                    numParts, start, size, bootable, fsType)
            fObj.write(self.geometry.makePart(start, size, bootable, fsType))

        assert numParts <= 4
        while numParts < 4:
            fObj.write('\0' * 16)
            numParts += 1

        fObj.write('\x55\xAA') # MBR signature

        assert fObj.tell() == 512
        fObj.close()


class RawHdImage(bootable_image.BootableImage):

    def makeHDImage(self, image):
        _, realSizes = self.getImageSize()
        lvmContainer = None

        # Align to the next cylinder
        def align(size):
            alignTo = self.geometry.bytesPerCylinder
            return divCeil(size, alignTo) * alignTo

        if os.path.exists(image):
            util.rmtree(image)
        util.mkdirChain(os.path.split(image)[0])
        if '/boot' in realSizes:
            rootPart = '/boot'
        else:
            rootPart = '/'

        # Align root partition to nearest cylinder, but add in the
        # partition offset so that the *end* of the root will be on a
        # cylinder boundary.
        rootStart = self.geometry.offsetBytes
        rootEnd = align(rootStart + realSizes[rootPart])
        rootSize = rootEnd - rootStart

        # Collect sizes of all non-boot partitions, pad 10% for LVM
        # overhead, and realign to the nearest cylinder.
        lvmSize = sum(x[1] for x in realSizes.items() if x[0] != rootPart)
        lvmSize += int(lvmSize * 0.10)
        lvmSize = align(lvmSize)

        totalSize = rootEnd + lvmSize
        container = HDDContainer(image, totalSize, self.geometry)
        container.create()

        # Calculate the offsets and sizes of the root and LVM partitions.
        # Note that the Start/Blocks variables are measured in blocks.
        # NB: both of these sizes are already block-aligned.
        rootStartBlock = rootStart / self.geometry.BLOCK
        rootSizeBlock = rootSize / self.geometry.BLOCK
        partitions = [(rootStartBlock, rootSizeBlock, FSTYPE_LINUX, True)]

        if len(realSizes) > 1:
            lvmStartBlock = rootStartBlock + rootSizeBlock
            lvmSizeBlock = lvmSize / self.geometry.BLOCK
            partitions.append((lvmStartBlock, lvmSizeBlock,
                FSTYPE_LINUX_LVM, False))

            lvmContainer = lvm.LVMContainer(lvmSize, image,<commit_msg>Fix argument order<commit_after>#
# Copyright (c) 2011 rPath, Inc.
#
# All Rights Reserved
#

import logging
import os

from jobslave import lvm
from jobslave.generators import bootable_image, constants
from jobslave.geometry import FSTYPE_LINUX, FSTYPE_LINUX_LVM
from jobslave.util import logCall, divCeil

from conary.lib import util

log = logging.getLogger(__name__)


class HDDContainer(object):

    def __init__(self, image, geometry, totalSize=None):
        self.image = image
        self.geometry = geometry
        if totalSize is None:
            totalSize = os.stat(image).st_size
        self.totalSize = totalSize

    def create(self):
        # create the raw file
        # NB: blocksize is unrelated to the one in constants.py, and is
        # completely arbitrary.
        blocksize = 512
        seek = (self.totalSize - 1) / blocksize
        logCall('dd if=/dev/zero of=%s count=1 seek=%d bs=%d' % (
            self.image, max(seek, 0), blocksize))

    def partition(self, partitions):
        # Extended partitions are not supported since we're either using a
        # single partition for non-LVM or two for LVM (/boot + one PV)
        assert len(partitions) <= 4
        fObj = open(self.image, 'r+b')

        fObj.seek(440)
        fObj.write(os.urandom(4)) # disk signature
        fObj.write('\0\0')

        numParts = 0
        for start, size, fsType, bootable in partitions:
            numParts += 1
            log.info("Partition %d: start %d  size %d  flags %02x  type %02x",
                    numParts, start, size, bootable, fsType)
            fObj.write(self.geometry.makePart(start, size, bootable, fsType))

        assert numParts <= 4
        while numParts < 4:
            fObj.write('\0' * 16)
            numParts += 1

        fObj.write('\x55\xAA') # MBR signature

        assert fObj.tell() == 512
        fObj.close()


class RawHdImage(bootable_image.BootableImage):

    def makeHDImage(self, image):
        _, realSizes = self.getImageSize()
        lvmContainer = None

        # Align to the next cylinder
        def align(size):
            alignTo = self.geometry.bytesPerCylinder
            return divCeil(size, alignTo) * alignTo

        if os.path.exists(image):
            util.rmtree(image)
        util.mkdirChain(os.path.split(image)[0])
        if '/boot' in realSizes:
            rootPart = '/boot'
        else:
            rootPart = '/'

        # Align root partition to nearest cylinder, but add in the
        # partition offset so that the *end* of the root will be on a
        # cylinder boundary.
        rootStart = self.geometry.offsetBytes
        rootEnd = align(rootStart + realSizes[rootPart])
        rootSize = rootEnd - rootStart

        # Collect sizes of all non-boot partitions, pad 10% for LVM
        # overhead, and realign to the nearest cylinder.
        lvmSize = sum(x[1] for x in realSizes.items() if x[0] != rootPart)
        lvmSize += int(lvmSize * 0.10)
        lvmSize = align(lvmSize)

        totalSize = rootEnd + lvmSize
        container = HDDContainer(image, self.geometry, totalSize)
        container.create()

        # Calculate the offsets and sizes of the root and LVM partitions.
        # Note that the Start/Blocks variables are measured in blocks.
        # NB: both of these sizes are already block-aligned.
        rootStartBlock = rootStart / self.geometry.BLOCK
        rootSizeBlock = rootSize / self.geometry.BLOCK
        partitions = [(rootStartBlock, rootSizeBlock, FSTYPE_LINUX, True)]

        if len(realSizes) > 1:
            lvmStartBlock = rootStartBlock + rootSizeBlock
            lvmSizeBlock = lvmSize / self.geometry.BLOCK
            partitions.append((lvmStartBlock, lvmSizeBlock,
                FSTYPE_LINUX_LVM, False))

            lvmContainer = lvm.LVMContainer(lvmSize, image,
{'fraction_non_alphanumeric': 0.09058961343225302, 'fraction_numerical': 0.011193544188468046, 'mean_word_length': 2.249048625792812, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293080', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2216, 'n_words': 671}