def __init__(self, path, size, schema=None):
"""Initialize an image file to a given size in bytes.
:param path: Path to image file on the file system.
:type path: str
:param size: Size in bytes to set the image file to.
:type size: int
:param schema: The partitioning schema of the volume.
:type schema: VolumeSchema
Public attributes:
* path - Path to the image file.
"""
self.path = path
# Create an empty image file of a fixed size. Unlike
# truncate(1) --size 0, os.truncate(path, 0) doesn't touch the
# file; i.e. it must already exist.
with open(path, 'wb'):
pass
# Truncate to zero, so that extending the size in the next call
# will cause all the bytes to read as zero. Stevens $4.13
os.truncate(path, 0)
os.truncate(path, size)
# Prepare the device and disk objects for parted to be used for all
# future partition() calls. Only do it if we actually care about the
# partition table.
if schema is None:
self.sector_size = 512
self.device = None
self.disk = None
self.schema = None
else:
self.device = parted.Device(self.path)
label = 'msdos' if schema is VolumeSchema.mbr else 'gpt'
self.schema = schema
self.disk = parted.freshDisk(self.device, label)
self.sector_size = self.device.sectorSize
评论列表
文章目录