pylorax package¶
Submodules¶
pylorax.buildstamp module¶
pylorax.cmdline module¶
pylorax.discinfo module¶
pylorax.dnfhelper module¶
pylorax.executils module¶
-
pylorax.executils.
execReadlines
(command, argv, stdin=None, root='/', env_prune=None, filter_stderr=False, callback=<function <lambda>>, env_add=None, reset_handlers=True, reset_lang=True)[source]¶ Execute an external command and return the line output of the command in real-time.
This method assumes that there is a reasonably low delay between the end of output and the process exiting. If the child process closes stdout and then keeps on truckin’ there will be problems.
- NOTE/WARNING: UnicodeDecodeError will be raised if the output of the
- external command can’t be decoded as UTF-8.
Parameters: - command – The command to run
- argv – The argument list
- stdin – The file object to read stdin from.
- stdout – Optional file object to redirect stdout and stderr to.
- root – The directory to chroot to before running command.
- env_prune – environment variable to remove before execution
- filter_stderr – Whether stderr should be excluded from the returned output
- callback – method to call while waiting for process to finish, passed Popen object
- env_add – environment variables to add before execution
- reset_handlers – whether to reset to SIG_DFL any signal handlers set to SIG_IGN
- reset_lang – whether to set the locale of the child process to C
Returns: Iterator of the lines from the command
Output from the file is not logged to program.log This returns an iterator with the lines from the command until it has finished
-
pylorax.executils.
execWithCapture
(command, argv, stdin=None, root='/', log_output=True, filter_stderr=False, raise_err=False, callback=None, env_add=None, reset_handlers=True, reset_lang=True)[source]¶ Run an external program and capture standard out and err.
Parameters: - command – The command to run
- argv – The argument list
- stdin – The file object to read stdin from.
- root – The directory to chroot to before running command.
- log_output – Whether to log the output of command
- filter_stderr – Whether stderr should be excluded from the returned output
- callback – method to call while waiting for process to finish, passed Popen object
- env_add – environment variables to add before execution
- reset_handlers – whether to reset to SIG_DFL any signal handlers set to SIG_IGN
- reset_lang – whether to set the locale of the child process to C
Returns: The output of the command
-
pylorax.executils.
execWithRedirect
(command, argv, stdin=None, stdout=None, root='/', env_prune=None, log_output=True, binary_output=False, raise_err=False, callback=None, env_add=None, reset_handlers=True, reset_lang=True)[source]¶ Run an external program and redirect the output to a file.
Parameters: - command – The command to run
- argv – The argument list
- stdin – The file object to read stdin from.
- stdout – Optional file object to redirect stdout and stderr to.
- root – The directory to chroot to before running command.
- env_prune – environment variable to remove before execution
- log_output – whether to log the output of command
- binary_output – whether to treat the output of command as binary data
- raise_err – whether to raise a CalledProcessError if the returncode is non-zero
- callback – method to call while waiting for process to finish, passed Popen object
- env_add – environment variables to add before execution
- reset_handlers – whether to reset to SIG_DFL any signal handlers set to SIG_IGN
- reset_lang – whether to set the locale of the child process to C
Returns: The return code of the command
-
pylorax.executils.
setenv
(name, value)[source]¶ Set an environment variable to be used by child processes.
This method does not modify os.environ for the running process, which is not thread-safe. If setenv has already been called for a particular variable name, the old value is overwritten.
Parameters:
-
pylorax.executils.
startProgram
(argv, root='/', stdin=None, stdout=-1, stderr=-2, env_prune=None, env_add=None, reset_handlers=True, reset_lang=True, **kwargs)[source]¶ Start an external program and return the Popen object.
The root and reset_handlers arguments are handled by passing a preexec_fn argument to subprocess.Popen, but an additional preexec_fn can still be specified and will be run. The user preexec_fn will be run last.
Parameters: - argv – The command to run and argument
- root – The directory to chroot to before running command.
- stdin – The file object to read stdin from.
- stdout – The file object to write stdout to.
- stderr – The file object to write stderr to.
- env_prune – environment variables to remove before execution
- env_add – environment variables to add before execution
- reset_handlers – whether to reset to SIG_DFL any signal handlers set to SIG_IGN
- reset_lang – whether to set the locale of the child process to C
- kwargs – Additional parameters to pass to subprocess.Popen
- preexec_fn – A function to run before execution starts.
Returns: A Popen object for the running command.
pylorax.imgutils module¶
-
class
pylorax.imgutils.
PartitionMount
(disk_img, mount_ok=None, submount=None)[source]¶ Bases:
object
Mount a partitioned image file using kpartx
-
pylorax.imgutils.
compress
(command, rootdir, outfile, compression='xz', compressargs=None)[source]¶ Make a compressed archive of the given rootdir. command is a list of the archiver commands to run compression should be “xz”, “gzip”, “lzma”, “bzip2”, or None. compressargs will be used on the compression commandline.
-
pylorax.imgutils.
copytree
(src, dest, preserve=True)[source]¶ Copy a tree of files using cp -a, thus preserving modes, timestamps, links, acls, sparse files, xattrs, selinux contexts, etc. If preserve is False, uses cp -R (useful for modeless filesystems) raises CalledProcessError if copy fails.
-
pylorax.imgutils.
dm_attach
(dev, size, name=None)[source]¶ Attach a devicemapper device to the given device, with the given size. If name is None, a random name will be chosen. Returns the device name. raises CalledProcessError if dmsetup fails.
-
pylorax.imgutils.
dm_detach
(dev)[source]¶ Detach the named devicemapper device. Returns False if dmsetup fails.
-
pylorax.imgutils.
do_grafts
(grafts, dest, preserve=True)[source]¶ Copy each of the items listed in grafts into dest. If the key ends with ‘/’ it’s assumed to be a directory which should be created, otherwise just the leading directories will be created.
-
pylorax.imgutils.
estimate_size
(rootdir, graft=None, fstype=None, blocksize=4096, overhead=256)[source]¶
-
pylorax.imgutils.
get_loop_name
(path)[source]¶ Return the loop device associated with the path. Raises RuntimeError if more than one loop is associated
-
pylorax.imgutils.
loop_attach
(outfile)[source]¶ Attach a loop device to the given file. Return the loop device name. Raises CalledProcessError if losetup fails.
-
pylorax.imgutils.
loop_detach
(loopdev)[source]¶ Detach the given loop device. Return False on failure.
-
pylorax.imgutils.
mkbtrfsimg
(rootdir, outfile, size=None, label='', mountargs='', graft=None)[source]¶
-
pylorax.imgutils.
mkdosimg
(rootdir, outfile, size=None, label='', mountargs='shortname=winnt, umask=0077', graft=None)[source]¶
-
pylorax.imgutils.
mkext4img
(rootdir, outfile, size=None, label='', mountargs='', graft=None)[source]¶
-
pylorax.imgutils.
mkfsimage
(fstype, rootdir, outfile, size=None, mkfsargs=None, mountargs='', graft=None)[source]¶ Generic filesystem image creation function. fstype should be a filesystem type - “mkfs.${fstype}” must exist. graft should be a dict: {“some/path/in/image”: “local/file/or/dir”};
if the path ends with a ‘/’ it’s assumed to be a directory.Will raise CalledProcessError if something goes wrong.
-
pylorax.imgutils.
mkqcow2
(outfile, size, options=None)[source]¶ use qemu-img to create a file of the given size. options is a list of options passed to qemu-img
Default format is qcow2, override by passing “-f”, fmt in options.
-
pylorax.imgutils.
mkqemu_img
(outfile, size, options=None)[source]¶ use qemu-img to create a file of the given size. options is a list of options passed to qemu-img
Default format is qcow2, override by passing “-f”, fmt in options.
-
pylorax.imgutils.
mkrootfsimg
(rootdir, outfile, label, size=2, sysroot='')[source]¶ Make rootfs image from a directory
Parameters:
-
pylorax.imgutils.
mksparse
(outfile, size)[source]¶ use os.ftruncate to create a sparse file of the given size.
-
pylorax.imgutils.
mksquashfs
(rootdir, outfile, compression='default', compressargs=None)[source]¶ Make a squashfs image containing the given rootdir.
-
pylorax.imgutils.
mktar
(rootdir, outfile, compression='xz', compressargs=None, selinux=True)[source]¶
-
pylorax.imgutils.
mount
(dev, opts='', mnt=None)[source]¶ Mount the given device at the given mountpoint, using the given opts. opts should be a comma-separated string of mount options. if mnt is none, a temporary directory will be created and its path will be returned. raises CalledProcessError if mount fails.
pylorax.ltmpl module¶
-
class
pylorax.ltmpl.
LoraxTemplateRunner
(inroot, outroot, dbo=None, fatalerrors=True, templatedir=None, defaults=None)[source]¶ Bases:
object
This class parses and executes Lorax templates. Sample usage:
# install a bunch of packages runner = LoraxTemplateRunner(inroot=rundir, outroot=rundir, dbo=dnf_obj) runner.run(“install-packages.ltmpl”)
# modify a runtime dir runner = LoraxTemplateRunner(inroot=rundir, outroot=newrun) runner.run(“runtime-transmogrify.ltmpl”)
NOTES:
Parsing procedure is roughly: 1. Mako template expansion (on the whole file) 2. For each line of the result,
- Whitespace splitting (using shlex.split())
- Brace expansion (using brace_expand())
- If the first token is the name of a function, call that function with the rest of the line as arguments
Parsing and execution are separate passes - so you can’t use the result of a command in an %if statement (or any other control statements)!
Commands that run external programs (systemctl, gconfset) currently use the host‘s copy of that program, which may cause problems if there’s a big enough difference between the host and the image you’re modifying.
The commands are not executed under a real chroot, so absolute symlinks will point outside the inroot/outroot. Be careful with symlinks!
ADDING NEW COMMANDS:
- Each template command is just a method of the LoraxTemplateRunner object - so adding a new command is as easy as adding a new function.
- Each function gets arguments that correspond to the rest of the tokens on that line (after word splitting and brace expansion)
- Commands should raise exceptions for errors - don’t use sys.exit()
-
append
(filename, data)[source]¶ - append FILE STRING
Append STRING (followed by a newline character) to FILE. Python character escape sequences (‘n’, ‘t’, etc.) will be converted to the appropriate characters. Examples:
append /etc/depmod.d/dd.conf “search updates built-in” append /etc/resolv.conf “”
-
chmod
(fileglob, mode)[source]¶ - chmod FILEGLOB OCTALMODE
- Change the mode of all the files matching FILEGLOB to OCTALMODE.
-
copy
(src, dest)[source]¶ - copy SRC DEST
- Copy SRC to DEST. If DEST is a directory, SRC will be copied inside it. If DEST doesn’t exist, SRC will be copied to a file with that name, if the path leading to it exists.
-
createaddrsize
(addr, src, dest)[source]¶ - createaddrsize INITRD_ADDRESS INITRD ADDRSIZE
Create the initrd.addrsize file required in LPAR boot process. Examples:
createaddrsize ${INITRD_ADDRESS} ${outroot}/${BOOTDIR}/initrd.img ${outroot}/${BOOTDIR}/initrd.addrsize
-
gconfset
(path, keytype, value, outfile=None)[source]¶ - gconfset PATH KEYTYPE VALUE [OUTFILE]
Set the given gconf PATH, with type KEYTYPE, to the given value. OUTFILE defaults to /etc/gconf/gconf.xml.defaults if not given. Example:
gconfset /apps/metacity/general/num_workspaces int 1
-
install
(srcglob, dest)[source]¶ - install SRC DEST
Copy the given file (or files, if a glob is used) from the input tree to the given destination in the output tree. The path to DEST must exist in the output tree. If DEST is a directory, SRC will be copied into that directory. If DEST doesn’t exist, SRC will be copied to a file with that name, assuming the rest of the path exists. This is pretty much like how the ‘cp’ command works. Examples:
install usr/share/myconfig/grub.conf /boot install /usr/share/myconfig/grub.conf.in /boot/grub.conf
-
installimg
(*args)[source]¶ - installimg [–xz|–gzip|–bzip2|–lzma] [-ARG|–ARG=OPTION] SRCDIR DESTFILE
Create a compressed cpio archive of the contents of SRCDIR and place it in DESTFILE.
If SRCDIR doesn’t exist or is empty nothing is created.
- Examples:
- installimg ${LORAXDIR}/product/ images/product.img installimg ${LORAXDIR}/updates/ images/updates.img installimg –xz -6 ${LORAXDIR}/updates/ images/updates.img installimg –xz -9 –memlimit-compress=3700MiB ${LORAXDIR}/updates/ images/updates.img
Optionally use a different compression type and override the default args passed to it. The default is xz -9
-
installinitrd
(section, src, dest)[source]¶ - installinitrd SECTION SRC DEST
- Same as installkernel, but for “initrd”.
-
installkernel
(section, src, dest)[source]¶ - installkernel SECTION SRC DEST
Install the kernel from SRC in the input tree to DEST in the output tree, and then add an item to the treeinfo data store, in the named SECTION, where “kernel” = DEST.
- Equivalent to:
- install SRC DEST treeinfo SECTION kernel DEST
-
installpkg
(*pkgs)[source]¶ - installpkg [–required|–optional] [–except PKGGLOB [–except PKGGLOB ...]] PKGGLOB [PKGGLOB ...]
Request installation of all packages matching the given globs. Note that this is just a request - nothing is actually installed until the ‘run_pkg_transaction’ command is given.
–required is now the default. If the PKGGLOB can be missing pass –optional
-
installupgradeinitrd
(section, src, dest)[source]¶ - installupgradeinitrd SECTION SRC DEST
- Same as installkernel, but for “upgrade”.
-
log
(msg)[source]¶ - log MESSAGE
Emit the given log message. Be sure to put it in quotes! Example:
log “Reticulating splines, please wait...”
-
mkdir
(*dirs)[source]¶ - mkdir DIR [DIR ...]
Create the named DIR(s). Will create leading directories as needed. Example:
mkdir /images
-
remove
(*fileglobs)[source]¶ - remove FILEGLOB [FILEGLOB ...]
- Remove all the named files or directories. Will not raise exceptions if the file(s) are not found.
-
removefrom
(pkg, *globs)[source]¶ - removefrom PKGGLOB [–allbut] FILEGLOB [FILEGLOB...]
Remove all files matching the given file globs from the package (or packages) named. If ‘–allbut’ is used, all the files from the given package(s) will be removed except the ones which match the file globs. Examples:
removefrom usbutils /usr/bin/* removefrom xfsprogs –allbut /sbin/*
-
removekmod
(*globs)[source]¶ - removekmod GLOB [GLOB...] [–allbut] KEEPGLOB [KEEPGLOB...]
Remove all files and directories matching the given file globs from the kernel modules directory.
If ‘–allbut’ is used, all the files from the modules will be removed except the ones which match the file globs. There must be at least one initial GLOB to search and one KEEPGLOB to keep. The KEEPGLOB is expanded to be KEEPGLOB so that it will match anywhere in the path.
This only removes files from under /lib/modules/*/kernel/
- Examples:
- removekmod sound drivers/media drivers/hwmon drivers/video removekmod drivers/char –allbut virtio_console hw_random
-
removepkg
(*pkgs)[source]¶ - removepkg PKGGLOB [PKGGLOB...]
Delete the named package(s). IMPLEMENTATION NOTES:
RPM scriptlets (%preun/%postun) are not run. Files are deleted, but directories are left behind.
-
replace
(pat, repl, *fileglobs)[source]¶ - replace PATTERN REPLACEMENT FILEGLOB [FILEGLOB ...]
Find-and-replace the given PATTERN (Python-style regex) with the given REPLACEMENT string for each of the files listed. Example:
replace @VERSION@ ${product.version} /boot/grub.conf /boot/isolinux.cfg
-
run_pkg_transaction
()[source]¶ Actually install all the packages requested by previous ‘installpkg’ commands.
-
runcmd
(*cmdlist)[source]¶ - runcmd CMD [ARG ...]
Run the given command with the given arguments.
NOTE: All paths given MUST be COMPLETE, ABSOLUTE PATHS to the file or files mentioned. ${root}/${inroot}/${outroot} are good for constructing these paths.
FURTHER NOTE: Please use this command only as a last resort! Whenever possible, you should use the existing template commands. If the existing commands don’t do what you need, fix them!
- Examples:
(this should be replaced with a “find” function) runcmd find ${root} -name “.pyo” -type f -delete %for f in find(root, name=”.pyo”):
remove ${f}%endfor
pylorax.monitor module¶
-
class
pylorax.monitor.
LogMonitor
(log_path=None, host='localhost', port=0, timeout=None)[source]¶ Bases:
object
Setup a server to monitor the logs output by the installation
This needs to be running before the virt-install runs, it expects there to be a listener on the port used for the virtio log port.
-
class
pylorax.monitor.
LogRequestHandler
(request, client_address, server)[source]¶ Bases:
socketserver.BaseRequestHandler
Handle monitoring and saving the logfiles from the virtual install
Incoming data is written to self.server.log_path and each line is checked for patterns that would indicate that the installation failed. self.server.log_error is set True when this happens.
-
handle
()[source]¶ Write incoming data to a logfile and check for errors
Split incoming data into lines and check for any Tracebacks or other errors that indicate that the install failed.
Loops until self.server.kill is True
-
pylorax.mount module¶
-
class
pylorax.mount.
IsoMountpoint
(iso_path, initrd_path=None)[source]¶ Bases:
object
Mount the iso and check to make sure the vmlinuz and initrd.img files exist
Also check the iso for a a stage2 image and set a flag and extract the iso’s label.
stage2 can be either LiveOS/squashfs.img or images/install.img
pylorax.output module¶
pylorax.sysutils module¶
pylorax.treebuilder module¶
-
class
pylorax.treebuilder.
RuntimeBuilder
(product, arch, dbo, templatedir=None, installpkgs=None, excludepkgs=None, add_templates=None, add_template_vars=None)[source]¶ Bases:
object
Builds the anaconda runtime image.
-
class
pylorax.treebuilder.
TreeBuilder
(product, arch, inroot, outroot, runtime, isolabel, domacboot=True, doupgrade=True, templatedir=None, add_templates=None, add_template_vars=None, workdir=None)[source]¶ Bases:
object
Builds the arch-specific boot images. inroot should be the installtree root (the newly-built runtime dir)
-
copy_dracut_hooks
(hooks)[source]¶ Copy the hook scripts in hooks into the installroot’s /tmp/ and return a list of commands to pass to dracut when creating the initramfs
hooks is a list of tuples with the name of the hook script and the target dracut hook directory (eg. [(“99anaconda-copy-ks.sh”, “/lib/dracut/hooks/pre-pivot”)])
-
dracut_hooks_path
¶ Return the path to the lorax dracut hooks scripts
Use the configured share dir if it is setup, otherwise default to /usr/share/lorax/dracut_hooks
-
kernels
¶
-
rebuild_initrds
(add_args=None, backup='', prefix='')[source]¶ Rebuild all the initrds in the tree. If backup is specified, each initrd will be renamed with backup as a suffix before rebuilding. If backup is empty, the existing initrd files will be overwritten. If suffix is specified, the existing initrd is untouched and a new image is built with the filename “${prefix}-${kernel.version}.img”
If the initrd doesn’t exist its name will be created based on the name of the kernel.
-
pylorax.treeinfo module¶
Module contents¶
-
class
pylorax.
ArchData
(buildarch)[source]¶ Bases:
pylorax.base.DataHolder
-
bcj_arch
= {'ppc': 'powerpc', 'armhfp': 'arm', 'ppc64le': 'powerpc', 'i386': 'x86', 'ppc64': 'powerpc', 'arm': 'arm', 'x86_64': 'x86'}¶
-
lib64_arches
= ('x86_64', 'ppc64', 'ppc64le', 's390x', 'ia64', 'aarch64')¶
-
-
class
pylorax.
Lorax
[source]¶ Bases:
pylorax.base.BaseLoraxClass
-
run
(dbo, product, version, release, variant='', bugurl='', isfinal=False, workdir=None, outputdir=None, buildarch=None, volid=None, domacboot=True, doupgrade=True, remove_temp=False, installpkgs=None, excludepkgs=None, size=2, add_templates=None, add_template_vars=None, add_arch_templates=None, add_arch_template_vars=None, verify=True)[source]¶
-
templatedir
¶ Find the template directory.
Pick the first directory under sharedir/templates.d/ if it exists. Otherwise use the sharedir
-
-
pylorax.
find_templates
(templatedir='/usr/share/lorax')[source]¶ Find the templates to use.
Parameters: templatedir (str) – Top directory to search for templates Returns: Path to templates Return type: str If there is a templates.d directory under templatedir the lowest numbered directory entry is returned.
eg. /usr/share/lorax/templates.d/99-generic/
-
pylorax.
setup_logging
(logfile, theLogger)[source]¶ Setup the various logs
Parameters: - logfile (string) – filename to write the log to
- theLogger (logging.Logger) – top-level logger