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).
- 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
- Returns
Iterator over the processed paths
- Return type
- 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.
- 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.
- cmkinitramfs.bin._get_libdir(arch, root='/')[source]
Get the libdir corresponding to a binary class
- 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.
- cmkinitramfs.bin._get_elf_arch(elf1, elf2)[source]
Open elf2, check compatibility, and return ELF architecture
- 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
- 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
- Returns
Iterator of
(dep_src, dep_dest)
, withdep_src
the path of the dependency on the current system, anddep_dest
the path of the dependency on the initramfs- Raises
FileNotFoundError – Dependency not found
- Return type
- cmkinitramfs.bin.find_elf_deps_set(src, root='/')[source]
Find dependencies of an ELF file
Cached version of
find_elf_deps_iter()
.- Parameters
- Returns
Set of
(dep_src, dep_dest)
, seefind_elf_deps_iter()
.- Raises
FileNotFoundError – Dependency not found
- Return type
- cmkinitramfs.bin.find_lib_iter(lib, compat=None, root='/')[source]
Search a library in the system, with globbing
Same as
find_lib()
but usesglob.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)
, seefind_lib()
- Raises
FileNotFoundError – Library not found
- Return type
- cmkinitramfs.bin.find_lib(lib, compat=None, root='/')[source]
Search a library in the system, without globbing
Uses
ld.so.conf
andLD_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)
, withlib_src
the absolute path of the library on the current system, andlib_dest
the absolute path of the library on the initramfs- Raises
FileNotFoundError – Library not found
- Return type
- 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)
withsrc_path
the path of the executable onroot
, anddest_path
the default path of the executable on the initramfs.- Returns
Absolute path of the executable
- Raises
FileNotFoundError – Executable not found
- Return type
- 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
- cmkinitramfs.bin.find_kmod(module, kernel)[source]
Search a kernel module on the system
- Parameters
- Returns
Absolute path of the kernel module on the system
- Raises
FileNotFoundError – Kernel module not found
- Return type