infi.parted顧名思義杀糯,是infi.storagemodel中負(fù)責(zé)磁盤分區(qū)的部分,
位置在infi.parted-0.2.9-py2.7.egg/infi/parted/__init__.py
整個(gè)腳本基本上都圍繞著parted命令來(lái)組織俊抵。與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:
執(zhí)行的命令為parted --script --machine /dev/device ......
一、分區(qū)
對(duì)分區(qū)類的定義如下:
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
#比較重要的函數(shù),從parted的輸出中盼产,解析出一個(gè)GUIDPartition的對(duì)象
#輸出格式: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)
#比較實(shí)用的函數(shù),得到分區(qū)的設(shè)備路徑/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)
二礼华、磁盤
對(duì)磁盤類的定義如下:
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])
#將整個(gè)disk創(chuàng)建一個(gè)分區(qū)咐鹤,并且加上文件系統(tǒng)
def create_partition_for_whole_drive(self, filesystem_name, alignment_in_bytes=None):
#執(zhí)行mkfs命令,創(chuàng)建文件系統(tǒng)
def format_partition(self, partition_number, filesystem_name, mkfs_options={}): # pylint: disable=W0102