infi.parted详解

2015-08-11  本文已影响55人  Stansosleepy

infi.parted顾名思义,是infi.storagemodel中负责磁盘分区的部分,
位置在infi.parted-0.2.9-py2.7.egg/infi/parted/__init__.py
整个脚本基本上都围绕着parted命令来组织。与parted命令的接口如下:

def execute_parted(args):
    from infi.execute import execute
    commandline_arguments = ["parted", ]
    commandline_arguments.extend(PARTED_REQUIRED_ARGUMENTS)
    commandline_arguments.extend(args)
    log.debug("executing {}".format(" ".join([repr(item) for item in commandline_arguments])))
    try:
        parted = execute(commandline_arguments)
    except OSError:

执行的命令为parted --script --machine /dev/device ......

一、分区

对分区类的定义如下:

class Partition(object):
    def __init__(self, disk_block_access_path, number, start, end, size):
        super(Partition, self).__init__()
        self._disk_block_access_path = disk_block_access_path
        self._number = number
        self._start = start
        self._end = end
        self._size = size

class GUIDPartition(Partition):
    def __init__(self, disk_block_access_path, number, name, start, end, size, filesystem):
        super(GUIDPartition, self).__init__(disk_block_access_path, number, start, end, size)
        self._name = name
        self._filesystem = filesystem
    
    @classmethod
    #比较重要的函数,从parted的输出中,解析出一个GUIDPartition的对象
    #输出格式:1:1048576B:525336575B:524288000B:xfs::boot;
    def from_parted_machine_parsable_line(cls, disk_device_path, line):
        number, start, end, size, filesystem, name, flags = line.strip(';').split(':')
        return cls(disk_device_path, int(number), name, from_string(start), from_string(end), from_string(size), filesystem)
    #比较实用的函数,得到分区的设备路径/dev/sdb1
    def get_access_path(self):
        prefix = get_multipath_prefix(self._disk_block_access_path) if 'mapper' in self._disk_block_access_path else ''
        return "{}{}{}".format(self._disk_block_access_path, prefix, self._number)

二、磁盘

对磁盘类的定义如下:

class Disk(MatchingPartedMixin, Retryable, object):
    #parted mklabel gpt
    def create_a_new_partition_table(self, label_type, alignment_in_bytes=None):
        """:param label_type: one of the following: ['msdos', 'gpt']"""
        # in linux we don't create a reserved partition at the begging on the disk, so there's no alignment here
        assert(label_type in SUPPORTED_DISK_LABELS)
        self.execute_parted(["mklabel", label_type])
    #将整个disk创建一个分区,并且加上文件系统
    def create_partition_for_whole_drive(self, filesystem_name, alignment_in_bytes=None):
    #执行mkfs命令,创建文件系统
    def format_partition(self, partition_number, filesystem_name, mkfs_options={}): # pylint: disable=W0102
上一篇下一篇

猜你喜欢

热点阅读