4. item

Module providing the Item class for files in the initramfs

Each file type of the initramfs has an Item subclass. Those classes provide methods used by Initramfs generation methods.

exception cmkinitramfs.item.MergeError[source]

Bases: Exception

Cannot merge an Item into another

class cmkinitramfs.item.Item[source]

Bases: ABC

An object within the initramfs

__contains__(path)[source]

Check if this item is present at the given path in the initramfs

This method should be overriden by subclasses which add files to the initramfs.

Returns

True if path is part of this Item’s destination paths, False otherwise

Parameters

path (str) –

Return type

bool

__iter__()[source]

Get the paths of this item within the initramfs

This method should be overriden by subclasses which add files to the initramfs.

Returns

Iterator over this Item’s destination paths

Return type

Iterator[str]

static build_from_cpio_list(data)[source]

Build an Item from a string

This string should respect the format of gen_init_cpio.

Parameters

data (str) – String to parse

Returns

Item corresponding to data

Raises

ValueError – Invalid string

Return type

Item

abstract build_to_cpio_list()[source]

String representing the item

The string is formatted to be compatible with the gen_init_cpio tool from the Linux kernel. This method has to be defined by subclasses.

Return type

str

abstract build_to_directory(base_dir)[source]

Add this item to a real filesystem

This will copy or create a file on a real filesystem. This method has to be defined by subclasses.

Parameters

base_dir (str) – Path to use as root directory (e.g. using /tmp/initramfs, /bin/ls will be copied to /tmp/initramfs/bin/ls)

Return type

None

is_mergeable(other)[source]

Check if two items can be merged together

By default, two items can only be merged if they are equal.

Parameters

other (Item) – Item to merge into self

Returns

True if the items can be merged, False otherwise

Return type

bool

merge(other)[source]

Merge two items together

Default merge is just a no-op. Subclasses can override this as done by File to handle hardlink of identical files.

Parameters

other (Item) – Item to merge into self

Raises

MergeError – Cannot merge the items

Return type

None

class cmkinitramfs.item.File(mode, user, group, dests, src, data_hash, chunk_size=65536)[source]

Bases: Item

Normal file within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dests (Set[str]) – Paths in the initramfs (hard-linked)

  • src (str) – Source file to copy (not unique to the file)

  • data_hash (bytes) – Hash of the file (can be obtained with hash_file())

  • chunk_size (int) – Chunk size to use when copying the file

class cmkinitramfs.item.Directory(mode, user, group, dest)[source]

Bases: Item

Directory within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dest (str) – Path in the initramfs

class cmkinitramfs.item.Node(mode, user, group, dest, nodetype, major, minor)[source]

Bases: Item

Special file within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dest (str) – Path in the initramfs

  • nodetype (NodeType) – Type of node (block, character)

  • major (int) – Major number of the node

  • minor (int) – Minor number of the node

Bases: Item

Symlinks within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dest (str) – Path in the initramfs

  • target (str) – Link target

class cmkinitramfs.item.Pipe(mode, user, group, dest)[source]

Bases: Item

Named pipe (FIFO) within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dest (str) – Path in the initramfs

class cmkinitramfs.item.Socket(mode, user, group, dest)[source]

Bases: Item

Named socket within the initramfs

Parameters
  • mode (int) – Permissions (e.g. 0o644)

  • user (int) – Owner user (UID)

  • group (int) – Owner group (GID)

  • dest (str) – Path in the initramfs