Main module

Module used to create simpler disk images, typically to boot embedded systems.

For more information see: https://github.com/zqad/simplediskimage/

class simplediskimage.DiskImage(path, partition_table='gpt', temp_fmt='{path}-{extra}.tmp', partitioner=<class 'simplediskimage.partitioners.PyParted'>, clean_temp_files='always')[source]

Bases: object

Helper class to generate disk images.

Parameters:
  • path – Path to the destination image file.
  • partition_table – Partition table (label) format, gpt or msdos, (or ‘null’ for the NullPartitioner).
  • temp_fmt – Format/path of the temp files containing format strings for path and extra. Make sure the temp files are on the same filesystem as the destination path.
  • partitioner – Partitioner class, for example PyParted or Sfdisk.
  • clean_temp_files – Whether or not to retain the temp files, accepts either “always”, “not on error” or “never”. Default is “always”. Note that an unconditional clean is usually run before image creation, and that the image is moved in place meaning that its temp file will disappear on successful runs.
check()[source]

Check this disk image for errors that will hinder us from doing a commit() later.

Will call .check() for each partition too.

commit()[source]

Commit this disk image and create the image.

get_size_bytes()[source]

Calculate and return the size of the disk image.

new_partition(filesystem, partition_label=None, partition_flags=None, filesystem_label=None, raw_filesystem_image=False)[source]

Create a new partition on this disk image.

Parameters:
  • filesystem – Filesystem, e.g. ext3 or fat32.
  • partition_label – Partition label, only supported by GPT.
  • partition_flags – Partition flags, e.g. BOOT.
  • filesystem_label – Filesystem label to be passed to mkfs.
  • raw_filesystem_image – Flag that this partition will be populated using a raw filesystem image
class simplediskimage.Partition(disk_image, path, filesystem, blocksize, metadata=None)[source]

Bases: object

Create partition instance, do not call directly, use Diskimage.new_partition().

Parameters:
  • disk_image – Disk image instance.
  • path – Path to the partition temp file.
  • filesystem – Filesystem for this partition.
  • blocksize – Block (sector) size.
  • metadata – Metadata.
check()[source]

Run a check of this partition, also called by DiskImage.

clean()[source]

Clean up all temp files of this partition.

commit()[source]

Commit this partition to it’s temp file, do not call directly.

copy(*source_paths, destination='/')[source]

Copy one or more files or directories recursively to the destination directory.

Parameters:
  • source_paths – The files to copy.
  • destination – The destination to which to copy, default /.
get_content_size_bytes()[source]

Get the size of all content copied into this image so far.

get_total_size_bytes()[source]

Get the total size of this image, using the fixed size if set, or the content + extra bytes if not.

mkdir(*dirs)[source]

Create one or many directories.

Parameters:dirs – The directories to create.
set_extra_bytes(num)[source]

Set the extra bytes to be added to the size on top of the content size.

Warning: When writing raw filesystem images to a partition, setting the partition size to something other than the size specified by the file system headers will confuse some partition parsing implementations. Notably, this has been observed with U-boot and FAT.

Parameters:num – The number of bytes, see the SI class for conversion.
set_fixed_size_bytes(num)[source]

Set a fixed size of this partition. For raw filesystem images, see the warnings under set_extra_bytes().

Parameters:num – The number of bytes, see the SI class for conversion.
set_initial_data_root(source_path)[source]

Set the initial data root directory, to be used when initializing the file system. All contents of this directory will be included in the file system. This differs from copy() in a few ways:

  • The path will be used as the file system root, rather than being copied as a file/directory under the root
  • The users and unix rights will be preserved, unlike copy() which always writes files owned by uid 0/gid 0.
  • Hard links are handled correctly, and not copied twice

Note that this feature is only supported for the ext family of file systems.

Parameters:source_paths – The directory to use as the filesystem root.
class simplediskimage.RawPartition(disk_image, temp_path, filesystem, metadata)[source]

Bases: simplediskimage.Partition

Simplified Partition class, used for partitions without filesystems. Only supports one file being copied (the raw image). Do not call directly, use Diskimage.new_partition().

Parameters:
  • disk_image – Disk image instance.
  • temp_path – Temporary partition part, only used if the partition is instructed to grow beyond the image size.
  • filesystem – Filesystem for this partition.
  • metadata – Metadata.
check()[source]

Run a check of this partition, also called by DiskImage.

clean()[source]

Usually a no-op, unless we ended up creating the temp file

commit()[source]

Usually a no-op, unless extra_bytes was set, or fixed_size_bytes does not equal the size of the image

copy(*source_paths, destination='/')[source]

Copy one or more files or directories recursively to the destination directory.

Parameters:
  • source_paths – The files to copy (only one file supported).
  • destination – The destination to which to copy, must be left out or /.
mkdir(*dirs)[source]

Not supported

set_initial_data_root(source_path)[source]

Not supported