<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
<e:e                 @   s   d dl mZ d dlT ddlZd dl mZ ddlmZ ddlj	Z	ddl
Z
ddlZddlZddlmZ ejj ZG dd	 d	ejZdS )
   )base)*    N)
exceptions)commands)sleepc                   s  e Zd ZdZ fddZdd Zdd Zedd	 Ze	d[ddZ
e	dd Zdd Zdd Zi i fddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zejfd#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&e'dAdBdC Z(e'dDdEdF Z)e'dGdHdI Z*e'dJdKdLdMdNdO Z+e'dPdKdLdMdQdR Z,e'dSdKdLdMdTdU Z-e'dVdKdLdMdWdX Z.dYdZ Z/  Z0S )\BootloaderPlugina  
	`bootloader`::
	
	Adds options to the kernel command line. This plug-in supports the
	GRUB 2 boot loader and the Boot Loader Specification (BLS).
	+
	NOTE: *TuneD* will not remove or replace kernel command line
	parameters added via other methods like *grubby*. *TuneD* will manage
	the kernel command line parameters added via *TuneD*. Please refer
	to your platform bootloader documentation about how to identify and
	manage kernel command line parameters set outside of *TuneD*.
	+
	Customized non-standard location of the GRUB 2 configuration file
	can be specified by the [option]`grub2_cfg_file` option.
	+
	The kernel options are added to the current GRUB configuration and
	its templates. Reboot the system for the kernel option to take effect.
	+
	Switching to another profile or manually stopping the `tuned`
	service removes the additional options. If you shut down or reboot
	the system, the kernel options persist in the [filename]`grub.cfg`
	file and grub environment files.
	+
	The kernel options can be specified by the following syntax:
	+
	[subs="+quotes,+macros"]
	----
	cmdline__suffix__=__arg1__ __arg2__ ... __argN__
	----
	+
	Or with an alternative, but equivalent syntax:
	+
	[subs="+quotes,+macros"]
	----
	cmdline__suffix__=+__arg1__ __arg2__ ... __argN__
	----
	+
	Where __suffix__ can be arbitrary (even empty) alphanumeric
	string which should be unique across all loaded profiles. It is
	recommended to use the profile name as the __suffix__
	(for example, [option]`cmdline_my_profile`). If there are multiple
	[option]`cmdline` options with the same suffix, during the profile
	load/merge the value which was assigned previously will be used. This
	is the same behavior as any other plug-in options. The final kernel
	command line is constructed by concatenating all the resulting
	[option]`cmdline` options.
	+
	It is also possible to remove kernel options by the following syntax:
	+
	[subs="+quotes,+macros"]
	----
	cmdline__suffix__=-__arg1__ __arg2__ ... __argN__
	----
	+
	Such kernel options will not be concatenated and thus removed during
	the final kernel command line construction.
	+
	.Modifying the kernel command line
	====
	For example, to add the [option]`quiet` kernel option to a *TuneD*
	profile, include the following lines in the [filename]`tuned.conf`
	file:
	
	----
	[bootloader]
	cmdline_my_profile=+quiet
	----
	
	An example of a custom profile `my_profile` that adds the
	[option]`isolcpus=2` option to the kernel command line:
	
	----
	[bootloader]
	cmdline_my_profile=isolcpus=2
	----
	
	An example of a custom profile `my_profile` that removes the
	[option]`rhgb quiet` options from the kernel command line (if
	previously added by *TuneD*):
	
	----
	[bootloader]
	cmdline_my_profile=-rhgb quiet
	----
	====
	+
	.Modifying the kernel command line, example with inheritance
	====
	For example, to add the [option]`rhgb quiet` kernel options to a
	*TuneD* profile `profile_1`:
	
	----
	[bootloader]
	cmdline_profile_1=+rhgb quiet
	----
	
	In the child profile `profile_2` drop the [option]`quiet` option
	from the kernel command line:
	
	----
	[main]
	include=profile_1
	
	[bootloader]
	cmdline_profile_2=-quiet
	----
	
	The final kernel command line will be [option]`rhgb`. In case the same
	[option]`cmdline` suffix as in the `profile_1` is used:
	
	----
	[main]
	include=profile_1
	
	[bootloader]
	cmdline_profile_1=-quiet
	----
	
	It will result in the empty kernel command line because the merge
	executes and the [option]`cmdline_profile_1` gets redefined to just
	[option]`-quiet`. Thus there is nothing to remove in the final kernel
	command line processing.
	====
	+
	The [option]`initrd_add_img=IMAGE` adds an initrd overlay file
	`IMAGE`. If the `IMAGE` file name begins with '/', the absolute path is
	used. Otherwise, the current profile directory is used as the base
	directory for the `IMAGE`.
	+
	The [option]`initrd_add_dir=DIR` creates an initrd image from the
	directory `DIR` and adds the resulting image as an overlay.
	If the `DIR` directory name begins with '/', the absolute path
	is used. Otherwise, the current profile directory is used as the
	base directory for the `DIR`.
	+
	The [option]`initrd_dst_img=PATHNAME` sets the name and location of
	the resulting initrd image. Typically, it is not necessary to use this
	option. By default, the location of initrd images is `/boot` and the
	name of the image is taken as the basename of `IMAGE` or `DIR`. This can
	be overridden by setting [option]`initrd_dst_img`.
	+
	The [option]`initrd_remove_dir=VALUE` removes the source directory
	from which the initrd image was built if `VALUE` is true. Only 'y',
	'yes', 't', 'true' and '1' (case insensitive) are accepted as true
	values for this option. Other values are interpreted as false.
	+
	.Adding an overlay initrd image
	====
	----
	[bootloader]
	initrd_remove_dir=True
	initrd_add_dir=/tmp/tuned-initrd.img
	----
	
	This creates an initrd image from the `/tmp/tuned-initrd.img` directory
	and and then removes the `tuned-initrd.img` directory from `/tmp`.
	====
	+
	The [option]`skip_grub_config=VALUE` does not change grub
	configuration if `VALUE` is true. However, [option]`cmdline`
	options are still processed, and the result is used to verify the current
	cmdline. Only 'y', 'yes', 't', 'true' and '1' (case insensitive) are accepted
	as true values for this option. Other values are interpreted as false.
	+
	.Do not change grub configuration
	====
	----
	[bootloader]
	skip_grub_config=True
	cmdline=+systemd.cpu_affinity=1
	----
	====
	c                s6   t jjtjstjdtt| j	|| t
 | _d S )Nz4Required GRUB2 template not found, disabling plugin.)ospathisfileconstsZGRUB2_TUNED_TEMPLATE_PATHr   ZNotSupportedPluginExceptionsuperr   __init__r   _cmd)selfargskwargs)	__class__ '/usr/lib/python3.6/plugin_bootloader.pyr      s    
zBootloaderPlugin.__init__c             C   sV   d|_ d|_d| _d| _d| _d | _d| _d| _| j | _	| j
 | _| j d k	| _d S )NFT )Z_has_dynamic_tuningZ_has_static_tuningupdate_grub2_cfg_skip_grub_config_val_initrd_remove_dir_initrd_dst_img_val_cmdline_val_initrd_val_get_grub2_cfg_files_grub2_cfg_file_names_bls_enabled_bls_rpm_ostree_status_rpm_ostree)r   instancer   r   r   _instance_init   s    

zBootloaderPlugin._instance_initc             C   s   d S )Nr   )r   r#   r   r   r   _instance_cleanup   s    z"BootloaderPlugin._instance_cleanupc             C   s   d d d d d d d dS )N)grub2_cfg_fileinitrd_dst_imginitrd_add_imginitrd_add_dirinitrd_remove_dircmdlineskip_grub_configr   )clsr   r   r   _get_config_options   s    z$BootloaderPlugin._get_config_optionsr   c             C   s`   i }|j  }xN| j  D ]B}||kr|j dd}|j|d g jt|dkrR|d nd qW |S )z
		Returns dict created from options
		e.g.: _options_to_dict("A=A A=B A B=A C=A", "A=B B=A B=B") returns {'A': ['A', None], 'C': ['A']}
		=r   r   N)split
setdefaultappendlen)optionsZomitdoZarrr   r   r   _options_to_dict   s    .z!BootloaderPlugin._options_to_dictc             C   s   dj dd | j D S )N c             S   s2   g | ]*\}}|D ]}|d k	r(|d | n|qqS )Nr/   r   ).0kvZv1r   r   r   
<listcomp>   s    z5BootloaderPlugin._dict_to_options.<locals>.<listcomp>)joinitems)r5   r   r   r   _dict_to_options   s    z!BootloaderPlugin._dict_to_optionsc             C   sr   | j jddgdd\}}}tjd||f  |dkr8dS |j }t|dk sX|d d	krjtjd
|  dS |d S )zW
		Returns status of rpm-ostree transactions or None if not run on rpm-ostree system
		z
rpm-ostreeZstatusT)
return_errz.rpm-ostree status output stdout:
%s
stderr:
%sr   N   zState:z2Exceptional format of rpm-ostree status result:
%sr   )r   executelogdebugr0   r3   warn)r   rcouterrZsplitedr   r   r   r!      s    z#BootloaderPlugin._rpm_ostree_statusc             C   sF   d}d}x(t |D ]}| j dkr&dS t| qW | j dkrBdS dS )N
   g      ?ZidleTF)ranger!   r   )r   Zsleep_cyclesZ
sleep_secsir   r   r   _wait_till_idle  s    z BootloaderPlugin._wait_till_idlec                s  | j jddgdd\}}}tjd||f  |dkr8dS | j|}| j sXtjd dS i }| j|j }x8|j	 D ],\ }	x|	D ]}
|  j
|
 qW |	| < qtW i }| j|j }x|j	 D ]~\ }	|j r$tjd	 |  f  |j g j|   |j fd
d|  D  g | < |j g j|	 |	| < qW ||krdtjd|  |||fS tjd||f  | j jddgdd |D  dd |D  dd\}}}|dkrtjd|  | j|ddfS |||fS dS )z
		Method for appending or deleting rpm-ostree karg
		returns None if rpm-ostree not present or is run on not ostree system
		or tuple with new kargs, appended kargs and deleted kargs
		z
rpm-ostreekargsT)r@   z'rpm-ostree output stdout:
%s
stderr:
%sr   NzCannot wait for transaction endz)adding rpm-ostree kargs %s: %s for deletec                s$   g | ]}|d k	r d | n qS )Nr/   r   )r9   r;   )r:   r   r   r<   .  s    z6BootloaderPlugin._rpm_ostree_kargs.<locals>.<listcomp>z3skipping rpm-ostree kargs - append == deleting (%s)z2rpm-ostree kargs - appending: '%s'; deleting: '%s'c             S   s   g | ]}d | qS )z--append=%sr   )r9   r;   r   r   r   r<   9  s    c             S   s   g | ]}d | qS )z--delete=%sr   )r9   r;   r   r   r   r<   :  s    z-Something went wrong with rpm-ostree kargs
%s)NNN)NNN)r   rB   rC   rD   r7   rL   errorr?   r0   r>   removegetr1   extendinfo)r   r2   deleterF   rG   rH   rM   deletedZdelete_paramsvalr;   appendedZappend_params_r   )r:   r   _rpm_ostree_kargs  sF    





z"BootloaderPlugin._rpm_ostree_kargsc             C   sV  | j  j }g }xR|D ]J}t|jdr4|j| q||krJ|| ||< qtjd|| jjf  qW d}x|D ]}|| }|dksn|dkrqn|d }|dd }|dd j	 }	|dks|d	kr|dkr|	dkr|d|	 7 }qn|d
kr(|	dkr4x@|	j
 D ]&}
tj|
}tjd| d d|} qW qn|d| 7 }qnW |j	 }|dkrR||d< |S )zSMerge provided options with plugin default options and merge all cmdline.* options.r+   z$Unknown option '%s' for plugin '%s'.r   Nr   r   rA   +\-r8   z(\A|\s)z	(?=\Z|\s))rZ   rY   r[   )r.   copystr
startswithr2   rC   rE   r   __name__stripr0   reescapesub)r   r4   Z	effectiveZcmdline_keyskeyr+   rU   opZop1valspZregexr   r   r   _get_effective_optionsA  s:    





z'BootloaderPlugin._get_effective_optionsc             C   s.   g }x$t jD ]}tjj|r|j| qW |S )N)r   ZGRUB2_CFG_FILESr	   r
   existsr2   )r   Z	cfg_filesfr   r   r   r   c  s
    z%BootloaderPlugin._get_grub2_cfg_filesc             C   sH   | j jtjdd}t|dkr2tjdtj  dS tjd|tj	dd k	S )NT)no_errorr   zcannot read '%s'Fz=^\s*GRUB_ENABLE_BLSCFG\s*=\s*\"?\s*[tT][rR][uU][eE]\s*\"?\s*$)flags)
r   	read_filer   GRUB2_DEFAULT_ENV_FILEr3   rC   rR   ra   search	MULTILINE)r   grub2_default_envr   r   r   r   j  s    zBootloaderPlugin._bls_enabledc             C   s   | j jtj|S )N)r   add_modify_option_in_filer   BOOT_CMDLINE_FILE)r   r5   r   r   r   _patch_bootcmdlines  s    z#BootloaderPlugin._patch_bootcmdlinec             C   s   | j tjdtjdi | js*tjd d S x4| jD ]*}| jj|dtj	 ddtj
 didd q2W | jd k	rtjd| j  | jj| j d S )Nr   zcannot find grub.cfg to patchzset\s+F)addzremoving initrd image '%s')rt   r   BOOT_CMDLINE_TUNED_VARBOOT_CMDLINE_INITRD_ADD_VARr   rC   rR   r   rr   GRUB2_TUNED_VARGRUB2_TUNED_INITRD_VARr   unlink)r   rj   r   r   r   _remove_grub2_tuningv  s    
*
z%BootloaderPlugin._remove_grub2_tuningc             C   sf   | j jtj}tjtjd |tjd}|r2|d nd}tjtjd |tjd}|rZ|d nd}||fS )Nz	=\"(.*)\")rl   r   r   )	r   rm   r   rs   ra   ro   rv   rp   BOOT_CMDLINE_KARGS_DELETED_VAR)r   rj   rV   rT   r   r   r   _get_rpm_ostree_changes  s    z(BootloaderPlugin._get_rpm_ostree_changesc             C   s@   | j  \}}| j| j|| j|d | jtjdtjdi d S )N)r2   rS   r   )r}   rX   r7   rt   r   rv   r|   )r   rV   rT   r   r   r   _remove_rpm_ostree_tuning  s    z*BootloaderPlugin._remove_rpm_ostree_tuningc             C   sR   |t jkrN| j rN| jr,tjd | j  n"tjd | j  | jddd d S )Nz4removing rpm-ostree tuning previously added by Tunedz/removing grub2 tuning previously added by Tunedr   )tuned_paramstuned_initrd)	r   ZROLLBACK_FULLr   r"   rC   rR   r~   r{   _update_grubenv)r   r#   Zrollbackr   r   r   _instance_unapply_static  s    


z)BootloaderPlugin._instance_unapply_staticc             C   s   t jd tjdtj d d|tjd}tjdtj d|tjd}tjdtj d d|tjd}tjdtj d|tjd}tjtjd d|tjd}tjtj	d d|tjdS )	Nzunpatching grub.cfgz
^\s*set\s+z\s*=.*
r   )rl   z *\$z\nz\n+)
rC   rD   ra   rc   r   rx   rp   ry   GRUB2_TEMPLATE_HEADER_BEGINGRUB2_TEMPLATE_HEADER_END)r   	grub2_cfgcfgr   r   r   _grub2_cfg_unpatch  s    
z#BootloaderPlugin._grub2_cfg_unpatchc             C   s   t jd dtj d }x8|D ]0}|d| jj| d | jj||  d 7 }qW |tjd 7 }tjd||tj	d	}tj
tjd
}xt|D ]l}tjd| d d||  |tj	d	}tjd| d ||  d d|tj	d	}tjd| d d|tj	d	}qW |S )Nzinitial patching of grub.cfgz\1\n\n
zset z="z"
z\nz+^(\s*###\s+END\s+[^#]+/00_header\s+### *)\n)rl   )linuxZinitrdz^(\s*z(16|efi)?\s+.*)$z\1 $z(?:16|efi)?\s+\S+rescue.*)\$z *(.*)$z\1\2z(?:16|efi)?\s+\S+rescue.*) +$z\1)rC   rD   r   r   r   rb   r   ra   rc   rp   rx   ry   )r   r   r5   soptZd2rK   r   r   r   _grub2_cfg_patch_initial  s    

0
$( z)BootloaderPlugin._grub2_cfg_patch_initialc             C   s   | j jtj}t|dkr.tjdtj  dS tjtjd}d}xv|D ]n}t	j
d| d ||  d |t	jdd krFd	}|d dkr|d7 }||d | d | d ||  d 7 }qFW |rtjdtj  | j jtj| d	S )Nr   zcannot read '%s'F)ZGRUB_CMDLINE_LINUX_DEFAULTZGRUB_INITRD_OVERLAYz^[^#]*\bz
\s*=.*\\\$z\b.*$)rl   Tr   r   z="${z:+$z }\$z"
zpatching '%s')r   rm   r   rn   r3   rC   rR   rx   ry   ra   ro   rp   rD   write_to_file)r   rq   r5   writerK   r   r   r   _grub2_default_env_patch  s     
*,z)BootloaderPlugin._grub2_default_env_patchc             C   s   | j jtj}t|dkr.tjdtj  dS d}tjdtj	 d |tj
drd}tjdtj	 d d	|tj
d}|d dkr|d7 }|rtjdtj  | j jtj| dS )Nr   zcannot read '%s'Fzb^GRUB_CMDLINE_LINUX_DEFAULT=\"\$\{GRUB_CMDLINE_LINUX_DEFAULT:\+\$GRUB_CMDLINE_LINUX_DEFAULT \}\\\$z"$)rl   Tz"$
r   r   r   zunpatching '%s'r   )r   rm   r   rn   r3   rC   rR   ra   ro   rx   rp   rc   rD   r   )r   rq   r   r   r   r   r   _grub2_default_env_unpatch  s     z+BootloaderPlugin._grub2_default_env_unpatchc             C   sZ  t jd | jst jd dS x| jD ]}| jj|}t|dkrVt jd|  q(t jd|  |}d}xf|D ]^}tjd| d d| jj	||  d
 |tj
d\}}|dk stjd| |tj
dd krrd}qrW ttjdtj |tj
dttjdtj |tj
dkrd}|r*| j| j||}| jj|| q(W | jrN| j  n| j  dS )Nzpatching grub.cfgzcannot find grub.cfg to patchFr   zcannot patch %sz+adding boot command line parameters to '%s'z	\b(set\s+z\s*=).*$z\1")rl   r   z\$Tz\1")rC   rD   r   rR   r   rm   r3   ra   subnrb   rp   ro   findallr   rx   ry   r   r   r   r    r   r   )r   r5   rj   r   Zgrub2_cfg_newZpatch_initialr   Znsubsr   r   r   _grub2_cfg_patch  s4    


4" 
z!BootloaderPlugin._grub2_cfg_patchc             C   sb   | j  \}}| j| j|}|s"d S | j|d\}}}|d kr@d S | jtj| jtj| j|i d S )N)r2   )	r}   r7   r   rX   rt   r   rv   r|   r?   )r   rV   rW   Z_cmdline_dictr5   r   r   r   _rpm_ostree_update  s    z#BootloaderPlugin._rpm_ostree_updatec             C   s8   | j tj| jtj| ji | jtj| jtj| ji d S )N)	r   r   rx   r   ry   r   rt   rv   rw   )r   r   r   r   _grub2_update  s    zBootloaderPlugin._grub2_updatec             C   s   t jjtjS )N)r	   r
   ri   r   ZBLS_ENTRIES_PATH)r   r   r   r   _has_bls  s    zBootloaderPlugin._has_blsc             C   s\   t jdt|  dd |j D }| jjdddg| \}}|dkrXt jd|  d	S d
S )Nzupdating grubenv, setting %sc             S   s$   g | ]\}}d t |t |f qS )z%s=%s)r]   )r9   Zoptionvaluer   r   r   r<     s    z4BootloaderPlugin._update_grubenv.<locals>.<listcomp>zgrub2-editenvr[   setr   zcannot update grubenv: '%s'FT)rC   rD   r]   r>   r   rB   rE   )r   r5   lrF   rG   r   r   r   r   
  s    z BootloaderPlugin._update_grubenvc             C   sb   | j j }|dkrdS tjdtj  | j jtjdgd|id\}}|dkr^tjd|  dS d	S )
Nr   Fz4running kernel update hook '%s' to patch BLS entriesru   ZKERNEL_INSTALL_MACHINE_ID)envr   zcannot patch BLS entries: '%s'T)r   Zget_machine_idrC   rD   r   ZKERNEL_UPDATE_HOOK_FILErB   rE   )r   Z
machine_idrF   rG   r   r   r   _bls_entries_patch_initial  s    
z+BootloaderPlugin._bls_entries_patch_initialc             C   s6   t jd | j r2| j| j| jdr2| j r2dS dS )Nzupdating BLS)r   r   TF)rC   rD   r   r   r   r   r   )r   r   r   r   _bls_update  s    
zBootloaderPlugin._bls_updatec             C   s(   | j d kr$tjjtjtjj|| _ d S )N)r   r	   r
   r=   r   BOOT_DIRbasename)r   namer   r   r   _init_initrd_dst_img&  s    
z%BootloaderPlugin._init_initrd_dst_imgc             C   s   t jjtjS )N)r	   r
   isdirr   ZPETITBOOT_DETECT_DIR)r   r   r   r   _check_petitboot*  s    z!BootloaderPlugin._check_petitbootc             C   s   | j rtjd dS | j r&tjd tjd| j  tjj| j}| j	j
|| jsXdS d| _| j	jdj }d}t|}|rtjdd	|}t||k r|}tjj||| _dS )
Nz:Detected rpm-ostree which doesn't support initrd overlays.FzkDetected Petitboot which doesn't support initrd overlays. The initrd overlay will be ignored by bootloader.zinstalling initrd image as '%s'Tz/proc/cmdline/z)^\s*BOOT_IMAGE=\s*(?:\([^)]*\))?(\S*/).*$z\1)r"   rC   rE   r   rR   r   r	   r
   r   r   r\   r   rm   rstripr3   ra   rc   r=   r   )r   ZimgZimg_nameZcurr_cmdlineZinitrd_grubpathZlcr
   r   r   r   _install_initrd-  s&    

z BootloaderPlugin._install_initrdr&   c             C   s$   |rd S |r |d k	r t |g| _d S )N)r]   r   )r   enablingr   verifyignore_missingr   r   r   _grub2_cfg_fileB  s    z BootloaderPlugin._grub2_cfg_filer'   c             C   sR   |rd S |rN|d k	rNt || _| jdkr,dS | jd dkrNtjjtj| j| _d S )Nr   Fr   r   )r]   r   r	   r
   r=   r   r   )r   r   r   r   r   r   r   r   _initrd_dst_imgJ  s    

z BootloaderPlugin._initrd_dst_imgr*   c             C   s*   |rd S |r&|d k	r&| j j|dk| _d S )N1)r   get_boolr   )r   r   r   r   r   r   r   r   r   V  s    z#BootloaderPlugin._initrd_remove_dirr(   FrI   )Z
per_deviceZpriorityc             C   sD   |rd S |r@|d k	r@t |}| j| |dkr2dS | j|s@dS d S )Nr   F)r]   r   r   )r   r   r   r   r   Zsrc_imgr   r   r   _initrd_add_img^  s    

z BootloaderPlugin._initrd_add_imgr)   c       
      C   s  |rd S |o|d k	rt |}| j| |dkr4dS tjj|sRtjd|  dS tjd|  tj	ddd\}}tj
d|  tj| | jjd	| |d
d\}}	tj
d|	  |dkrtjd | jj|d
d dS | j| | jj| | jrtjd|  | jj| d S )Nr   FzFerror: cannot create initrd image, source directory '%s' doesn't existz+generating initrd image from directory '%s'ztuned-bootloader-z.tmp)prefixsuffixz+writing initrd image to temporary file '%s'zfind . | cpio -co > %sT)cwdshellzcpio log: %sr   zerror generating initrd image)rk   zremoving directory '%s')r]   r   r	   r
   r   rC   rN   rR   tempfileZmkstemprD   closer   rB   rz   r   r   Zrmtree)
r   r   r   r   r   Zsrc_dirfdZtmpfilerF   rG   r   r   r   _initrd_add_dirk  s2    



z BootloaderPlugin._initrd_add_dirr+   c             C   sJ  | j j| jj|}|r | jr8| j d }| j|}n| jjd}t|dkrTd S t	|j
 }t	|j
 }	|	| }
t|
dkrtjtjdt|	f  dS dd |D }xR|
D ]J}|j
ddd }||krtjtj||f  qtjtj|| |f  qW |	|@ }tjd	d
j|f  dS |rF|d k	rFtjd d| _|| _d S )Nr   z/proc/cmdliner+   Tc             S   s   i | ]}||j d dd qS )r/   r   r   )r0   )r9   r;   r   r   r   
<dictcomp>  s    z-BootloaderPlugin._cmdline.<locals>.<dictcomp>r/   r   z2expected arguments that are present in cmdline: %sr8   Fz;installing additional boot command line parameters to grub2)Z
_variablesexpandr   Zunquoter"   rX   r?   rm   r3   r   r0   rC   rR   r   ZSTR_VERIFY_PROFILE_VALUE_OKr]   rN   Z'STR_VERIFY_PROFILE_CMDLINE_FAIL_MISSINGZSTR_VERIFY_PROFILE_CMDLINE_FAILr=   r   r   )r   r   r   r   r   r;   Zrpm_ostree_kargsr+   Zcmdline_setZ	value_setZmissing_setZcmdline_dictmargZpresent_setr   r   r   _cmdline  s6    

zBootloaderPlugin._cmdliner,   c             C   s8   |rd S |r4|d k	r4| j j|dkr4tjd d| _d S )Nr   z(skipping any modification of grub configT)r   r   rC   rR   r   )r   r   r   r   r   r   r   r   _skip_grub_config  s    
z"BootloaderPlugin._skip_grub_configc             C   s   |rV| j rVt| jdkr"tjd t| jdkr:tjd | jtj| jtj	| ji n0|r| j
r| jrp| j  n| j  | j  d| _
d S )Nr   z0requested changes to initrd will not be applied!z1requested changes to cmdline will not be applied!F)r   r3   r   rC   rE   r   rt   r   rv   rw   r   r"   r   r   r   )r   r#   r   r   r   r   _instance_post_static  s    




z&BootloaderPlugin._instance_post_static)r   )1r_   
__module____qualname____doc__r   r$   r%   classmethodr.   staticmethodr7   r?   r!   rL   rX   rh   r   r   rt   r{   r}   r~   r   ZROLLBACK_SOFTr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zcommand_customr   r   r   r   r   r   r   r   __classcell__r   r   )r   r   r      sT    .4"	
	 
	!	r   )r   r   Z
decoratorsZ
tuned.logsZtunedr   Ztuned.utils.commandsr   Ztuned.constsr   r	   ra   r   Ztimer   ZlogsrP   rC   ZPluginr   r   r   r   r   <module>   s   

