infi.parted详解

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,239评论 2 33
  • 1 概述 本文通过对讲解了磁盘的基本概念,分区,创建文件系统,挂载,卸载等步骤进行讲解。使得用户能够创建基本的磁盘...
    ghbsunny阅读 1,586评论 0 0
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,586评论 9 467
  • 目录磁盘结构磁盘分区磁盘分区管理文件系统管理挂载 一、磁盘结构 (一)设备文件 设备文件:linux系统下一切皆文...
    哈喽别样阅读 1,021评论 0 0
  • 昨天去做了一个小小的结扎手术,是因为医生在给我做了检查后动员我做的。说是手术后只需挂点盐水消消炎第二天就可出院...
    耕牛1949阅读 532评论 0 4