5. bin

Module providing functions to manage binaries and executables

This library also provides utilities like find_exec(). Multiple helper functions are also available (e.g. find_elf_deps_iter() and find_elf_deps_set() to list libraries needed by an ELF executable).

class cmkinitramfs.bin.ELFIncompatibleError[source]

Bases: ELFError

The ELF files are incompatible

cmkinitramfs.bin.parse_ld_path(ld_path=None, origin='', root='/')[source]

Parse a colon-delimited list of paths and apply ldso rules

Note the special handling as dictated by the ldso:
  • Empty paths are equivalent to $PWD

  • $ORIGIN is expanded to the path of the given file, origin

Parameters
  • ld_path (Optional[str]) – Colon-delimited string of paths, defaults to the LD_LIBRARY_PATH environment variable

  • origin (str) – Directory containing the ELF file being parsed (used for $ORIGIN), defaults to an empty string

  • root (str) – Path to prepend to all paths found

Returns

Iterator over the processed paths

Return type

Iterator[str]

cmkinitramfs.bin.parse_ld_so_conf_iter(conf_path=None, root='/')[source]

Parse a ldso config file

This should handle comments, whitespace, and “include” statements.

Parameters
  • conf_path (Optional[str]) – Path of the ldso config file to parse, defaults to {root}/etc/ld.so.conf

  • root (str) – Path to prepend to all paths found

Returns

Iterator over the processed paths

Return type

Iterator[str]

cmkinitramfs.bin.parse_ld_so_conf_tuple(conf_path=None, root='/')[source]

Parse a ldso config file

Cached version of parse_ld_so_conf_iter(), returning a tuple.

Parameters
  • conf_path (Optional[str]) – Path of the ldso config file to parse, defaults to {root}/etc/ld.so.conf

  • root (str) – Path to prepend to all paths found

Returns

Tuple with the processed paths

Return type

Tuple[str, …]

cmkinitramfs.bin._get_default_libdirs(root='/')[source]

Get the default library directories

Parameters

root (str) – Root directory to check for library directories

Returns

Libdirs in the initramfs

Return type

Tuple[str, …]

cmkinitramfs.bin._get_libdir(arch, root='/')[source]

Get the libdir corresponding to a binary class

Parameters
  • arch (int) – Binary class (e.g. 32 or 64)

  • root (str) – Directory where libdirs are searched

Returns

Libdir in the initramfs

Return type

str

cmkinitramfs.bin._is_elf_compatible(elf1, elf2)[source]

See if two ELFs are compatible

This compares the aspects of the ELF to see if they’re compatible: bit size, endianness, machine type, and operating system.

Parameters
  • elf1 (ELFFile) – First ELF object

  • elf2 (ELFFile) – Second ELF object

Returns

True if compatible, False otherwise

Return type

bool

cmkinitramfs.bin._get_elf_arch(elf1, elf2)[source]

Open elf2, check compatibility, and return ELF architecture

Parameters
  • elf1 (Union[ELFFile, str]) – First ELF

  • elf2 (Union[ELFFile, str]) – Second ELF

Returns

Architecture of the ELF files (32 or 64)

Raises
Return type

int

cmkinitramfs.bin._find_elf_deps_iter(elf, origin, root='/')[source]

Iterates over the dependencies of an ELF file

Backend of find_elf_deps_iter().

Parameters
  • elf (ELFFile) – Elf file to parse

  • origin (str) – Directory containing the ELF binary (real path as provided by os.path.realpath(), used for $ORIGIN)

  • root (str) – Path to prepend to all paths found

Returns

Same as find_elf_deps_iter()

Raises

FileNotFoundError – Dependency not found

Return type

Iterator[Tuple[str, str]]

cmkinitramfs.bin.find_elf_deps_iter(src, root='/')[source]

Iterates over the dependencies of an ELF file

Read an ELF file to search dynamic library dependencies. For each dependency, find it on the system (using RPATH, LD_LIBRARY_PATH, RUNPATH, ld.so.conf, and default library directories).

If the library is in a path encoded in the ELF binary (RPATH or RUNPATH), dep_dest = dep_src, otherwise use a default library directory according to the type of binary (/lib, /lib64, /lib32).

If the file is not an ELF file, returns an empty iterator.

Parameters
  • src (str) – File to find dependencies for

  • root (str) – Path to prepend to all paths found

Returns

Iterator of (dep_src, dep_dest), with dep_src the path of the dependency on the current system, and dep_dest the path of the dependency on the initramfs

Raises

FileNotFoundError – Dependency not found

Return type

Iterator[Tuple[str, str]]

cmkinitramfs.bin.find_elf_deps_set(src, root='/')[source]

Find dependencies of an ELF file

Cached version of find_elf_deps_iter().

Parameters
  • src (str) – File to find dependencies for

  • root (str) – Path to prepend to all paths found

Returns

Set of (dep_src, dep_dest), see find_elf_deps_iter().

Raises

FileNotFoundError – Dependency not found

Return type

FrozenSet[Tuple[str, str]]

cmkinitramfs.bin.find_lib_iter(lib, compat=None, root='/')[source]

Search a library in the system, with globbing

Same as find_lib() but uses glob.glob() to find matching libraries.

Parameters
  • lib (str) – Glob pattern for the library to search (e.g. libgcc_s.*)

  • compat (Optional[str]) – Path to a binary that the library must be compatible with (checked with _is_elf_compatible()), defaults to {root}/bin/sh

  • root (str) – Path to prepend to all paths found

Returns

Iterator over (lib_src, lib_dest), see find_lib()

Raises

FileNotFoundError – Library not found

Return type

Iterator[Tuple[str, str]]

cmkinitramfs.bin.find_lib(lib, compat=None, root='/')[source]

Search a library in the system, without globbing

Uses ld.so.conf and LD_LIBRARY_PATH.

Libraries will be installed in the default library directory in the initramfs.

Parameters
  • lib (str) – Library to search (e.g. libgcc_s.so.1)

  • compat (Optional[str]) – Path to a binary that the library must be compatible with (checked with _is_elf_compatible()), defaults to {root}/bin/sh

  • root (str) – Path to prepend to all paths found

Returns

(lib_src, lib_dest), with lib_src the absolute path of the library on the current system, and lib_dest the absolute path of the library on the initramfs

Raises

FileNotFoundError – Library not found

Return type

Tuple[str, str]

cmkinitramfs.bin.parse_path(path=None, root='/')[source]

Parse PATH variable

Parameters
  • path (Optional[str]) – PATH string to parse, default to the PATH environment variable

  • root (str) – Path to prepend to all paths found

Returns

Iterator over the processed paths

Return type

Iterator[str]

cmkinitramfs.bin.find_exec(executable, compat=None, root='/')[source]

Search an executable in the system

Uses the PATH environment variable.

Parameters
  • executable (str) – Executable to search

  • compat (Optional[str]) – Path to a binary that the executable must be compatible with (checked with _is_elf_compatible()), defaults to {root}/bin/sh

  • root (str) – Path to prepend to all paths found

Returns

(src_path, dest_path) with src_path the path of the executable on root, and dest_path the default path of the executable on the initramfs.

Returns

Absolute path of the executable

Raises

FileNotFoundError – Executable not found

Return type

Tuple[str, str]

cmkinitramfs.bin._get_all_kmods(kernel)[source]

Get all kernel modules on the system

Parameters

kernel (str) – Target kernel version

Returns

Set with the absolute path of the modules

Return type

FrozenSet[str]

cmkinitramfs.bin.KMOD_DIR = '/lib/modules'

Kernel modules will be searched in {KMOD_DIR}/{KERNEL}/**/*.ko

cmkinitramfs.bin.find_kmod_deps(path)[source]

Get kernel module dependencies

Parameters

path (str) – Path of the kernel module to parse

Returns

Set with the dependencies’ names

Raises

subprocess.CalledProcessError – Error during modinfo

Return type

FrozenSet[str]

cmkinitramfs.bin.find_kmod(module, kernel)[source]

Search a kernel module on the system

Parameters
  • module (str) – Name of the kernel module

  • kernel (str) – Target kernel version

Returns

Absolute path of the kernel module on the system

Raises

FileNotFoundError – Kernel module not found

Return type

Optional[str]