3 Z%d?d@Z&e'dAdBdCZ(e'dDdEdFZ)e'dGdHdIZ*e'dJdKdLdMdNdOZ+e'dPdKdLdMdQdRZ,e'dSdKdLdMdTdUZ-e'dVdKdLdMdWdXZ.dYdZZ/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 ---- ==== cs6tjjtjstjdtt|j ||t |_ dS)Nz4Required GRUB2 template not found, disabling plugin.) ospathisfileconstsZGRUB2_TUNED_TEMPLATE_PATHrZNotSupportedPluginExceptionsuperr__init__r_cmd)selfargskwargs) __class__'/usr/lib/python3.6/plugin_bootloader.pyrs zBootloaderPlugin.__init__cCsVd|_d|_d|_d|_d|_d|_d|_d|_|j|_ |j |_ |j dk |_ dS)NFT)Z_has_dynamic_tuningZ_has_static_tuningupdate_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)rinstancerrr_instance_inits  zBootloaderPlugin._instance_initcCsdS)Nr)rr#rrr_instance_cleanupsz"BootloaderPlugin._instance_cleanupcCsddddddddS)N)grub2_cfg_fileinitrd_dst_imginitrd_add_imginitrd_add_dirinitrd_remove_dircmdlineskip_grub_configr)clsrrr_get_config_optionssz$BootloaderPlugin._get_config_optionsrcCs`i}|j}xN|jD]B}||kr|jdd}|j|dgjt|dkrR|dndqW|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']} =rrN)split setdefaultappendlen)optionsZomitdoZarrrrr_options_to_dicts .z!BootloaderPlugin._options_to_dictcCsdjdd|jDS)N cSs2g|]*\}}|D]}|dk r(|d|n|qqS)Nr/r).0kvZv1rrr sz5BootloaderPlugin._dict_to_options..)joinitems)r5rrr_dict_to_optionssz!BootloaderPlugin._dict_to_optionscCsr|jjddgdd\}}}tjd||f|dkr8dS|j}t|dksX|dd krjtjd |dS|d S) zW Returns status of rpm-ostree transactions or None if not run on rpm-ostree system z rpm-ostreeZstatusT) return_errz.rpm-ostree status output stdout: %s stderr: %srNzState:z2Exceptional format of rpm-ostree status result: %sr)rexecutelogdebugr0r3warn)rrcouterrZsplitedrrrr!sz#BootloaderPlugin._rpm_ostree_statuscCsFd}d}x(t|D]}|jdkr&dSt|qW|jdkrBdSdS)N g?ZidleTF)ranger!r)rZ sleep_cyclesZ sleep_secsirrr_wait_till_idles   z BootloaderPlugin._wait_till_idlecs|jjddgdd\}}}tjd||f|dkr8dS|j|}|jsXtjddSi}|j|j}x8|j D],\} x| D]} |j | qW| |<qtWi} |j|j} x|j D]~\} |j r$tjd |f|j gj ||j fd d |Dg|<|j gj | | | <qW| |krdtjd | || |fStjd | |f|jjddgdd | Ddd |Ddd\}} }|dkrtjd||j|ddfS|| |fSdS)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-ostreekargsT)r@z'rpm-ostree output stdout: %s stderr: %srNzCannot wait for transaction endz)adding rpm-ostree kargs %s: %s for deletecs$g|]}|dk rd|nqS)Nr/r)r9r;)r:rrr<.sz6BootloaderPlugin._rpm_ostree_kargs..z3skipping rpm-ostree kargs - append == deleting (%s)z2rpm-ostree kargs - appending: '%s'; deleting: '%s'cSsg|] }d|qS)z --append=%sr)r9r;rrrr<9scSsg|] }d|qS)z --delete=%sr)r9r;rrrr<:sz-Something went wrong with rpm-ostree kargs %s)NNN)NNN)rrBrCrDr7rLerrorr?r0r>removegetr1extendinfo)rr2deleterFrGrHrMdeletedZ delete_paramsvalr;appendedZ append_params_r)r:r_rpm_ostree_kargs sF         z"BootloaderPlugin._rpm_ostree_kargsc CsV|jj}g}xR|D]J}t|jdr4|j|q||krJ||||<qtjd||jjfqWd}x|D]}||}|dksn|dkrqn|d}|dd}|ddj } |dks|d kr|dkr| dkr|d | 7}qn|d kr(| dkr4x@| j D]&} t j | } t j d | d d|}qWqn|d |7}qnW|j }|dkrR||d<|S)zSMerge provided options with plugin default options and merge all cmdline.* options.r+z$Unknown option '%s' for plugin '%s'.rNrrrA+\-r8z(\A|\s)z (?=\Z|\s))rZrYr[)r.copystr startswithr2rCrEr__name__stripr0reescapesub) rr4Z effectiveZ cmdline_keyskeyr+rUopZop1valspZregexrrr_get_effective_optionsAs:         z'BootloaderPlugin._get_effective_optionscCs.g}x$tjD]}tjj|r |j|q W|S)N)r ZGRUB2_CFG_FILESr r existsr2)rZ cfg_filesfrrrrcs   z%BootloaderPlugin._get_grub2_cfg_filescCsH|jjtjdd}t|dkr2tjdtjdStjd|tj ddk S)NT)no_errorrzcannot read '%s'Fz=^\s*GRUB_ENABLE_BLSCFG\s*=\s*\"?\s*[tT][rR][uU][eE]\s*\"?\s*$)flags) r read_filer GRUB2_DEFAULT_ENV_FILEr3rCrRrasearch MULTILINE)rgrub2_default_envrrrrjs  zBootloaderPlugin._bls_enabledcCs|jjtj|S)N)radd_modify_option_in_filer BOOT_CMDLINE_FILE)rr5rrr_patch_bootcmdlinessz#BootloaderPlugin._patch_bootcmdlinecCs|jtjdtjdi|js*tjddSx4|jD]*}|jj|dtj ddtj diddq2W|j dk rtjd|j |jj |j dS)Nrzcannot find grub.cfg to patchzset\s+F)addzremoving initrd image '%s') rtr BOOT_CMDLINE_TUNED_VARBOOT_CMDLINE_INITRD_ADD_VARrrCrRrrrGRUB2_TUNED_VARGRUB2_TUNED_INITRD_VARrunlink)rrjrrr_remove_grub2_tuningvs  * z%BootloaderPlugin._remove_grub2_tuningcCsf|jjtj}tjtjd|tjd}|r2|dnd}tjtjd|tjd}|rZ|dnd}||fS)Nz =\"(.*)\")rlrr) rrmr rsrarorvrpBOOT_CMDLINE_KARGS_DELETED_VAR)rrjrVrTrrr_get_rpm_ostree_changess z(BootloaderPlugin._get_rpm_ostree_changescCs@|j\}}|j|j||j|d|jtjdtjdidS)N)r2rSr)r}rXr7rtr rvr|)rrVrTrrr_remove_rpm_ostree_tunings z*BootloaderPlugin._remove_rpm_ostree_tuningcCsR|tjkrN|j rN|jr,tjd|jn"tjd|j|jddddS)Nz4removing rpm-ostree tuning previously added by Tunedz/removing grub2 tuning previously added by Tunedr) tuned_params tuned_initrd) r Z ROLLBACK_FULLrr"rCrRr~r{_update_grubenv)rr#Zrollbackrrr_instance_unapply_statics   z)BootloaderPlugin._instance_unapply_staticcCstjdtjdtjdd|tjd}tjdtjd|tjd}tjdtjdd|tjd}tjdtjd|tjd}tjtjdd|tjd}tjtj dd|tjdS) Nzunpatching grub.cfgz ^\s*set\s+z\s*=.* r)rlz *\$z\nz\n+) rCrDrarcr rxrpryGRUB2_TEMPLATE_HEADER_BEGINGRUB2_TEMPLATE_HEADER_END)r grub2_cfgcfgrrr_grub2_cfg_unpatchs z#BootloaderPlugin._grub2_cfg_unpatchcCstjddtjd}x8|D]0}|d|jj|d|jj||d7}qW|tjd7}tjd||tj d }tj tj d }xt|D]l}tjd |d d |||tj d }tjd |d||dd|tj d }tjd |dd|tj d }qW|S)Nzinitial patching of grub.cfgz\1\n\n zset z="z" z\nz+^(\s*###\s+END\s+[^#]+/00_header\s+### *)\n)rl)linuxZinitrdz^(\s*z(16|efi)?\s+.*)$z\1 $z(?:16|efi)?\s+\S+rescue.*)\$z *(.*)$z\1\2z(?:16|efi)?\s+\S+rescue.*) +$z\1) rCrDr rrrbrrarcrprxry)rrr5soptZd2rKrrr_grub2_cfg_patch_initials  0 $( z)BootloaderPlugin._grub2_cfg_patch_initialcCs|jjtj}t|dkr.tjdtjdStjtjd}d}xv|D]n}t j d|d||d|t j ddkrFd }|dd kr|d 7}||d |d |d||d7}qFW|rtj dtj|jj tj|d S)Nrzcannot read '%s'F)ZGRUB_CMDLINE_LINUX_DEFAULTZGRUB_INITRD_OVERLAYz^[^#]*\bz \s*=.*\\\$z\b.*$)rlTrrz="${z:+$z }\$z" z patching '%s')rrmr rnr3rCrRrxryrarorprD write_to_file)rrqr5writerKrrr_grub2_default_env_patchs   * ,z)BootloaderPlugin._grub2_default_env_patchcCs|jjtj}t|dkr.tjdtjdSd}tjdtj d|tj drd}tj dtj dd |tj d}|d d kr|d 7}|rtj d tj|jj tj|dS)Nrzcannot read '%s'Fzb^GRUB_CMDLINE_LINUX_DEFAULT=\"\$\{GRUB_CMDLINE_LINUX_DEFAULT:\+\$GRUB_CMDLINE_LINUX_DEFAULT \}\\\$z"$)rlTz"$ rrrzunpatching '%s'r)rrmr rnr3rCrRrarorxrprcrDr)rrqrrrrr_grub2_default_env_unpatchs   z+BootloaderPlugin._grub2_default_env_unpatchcCsZtjd|jstjddSx|jD]}|jj|}t|dkrVtjd|q(tjd||}d}xf|D]^}tjd|dd|jj ||d |tj d \}}|d kstj d ||tj d dkrrd}qrWttj d t j|tj d ttj d t j|tj d krd}|r*|j|j||}|jj||q(W|jrN|jn|jdS)Nzpatching grub.cfgzcannot find grub.cfg to patchFrzcannot patch %sz+adding boot command line parameters to '%s'z \b(set\s+z\s*=).*$z\1")rlrz\$Tz\1")rCrDrrRrrmr3rasubnrbrprofindallr rxryrrrr rr)rr5rjrZ grub2_cfg_newZ patch_initialrZnsubsrrr_grub2_cfg_patchs4     4"  z!BootloaderPlugin._grub2_cfg_patchcCsb|j\}}|j|j|}|s"dS|j|d\}}}|dkr@dS|jtj|jtj|j|idS)N)r2) r}r7rrXrtr rvr|r?)rrVrWZ _cmdline_dictr5rrr_rpm_ostree_updates z#BootloaderPlugin._rpm_ostree_updatecCs8|jtj|jtj|ji|jtj|jtj|jidS)N) rr rxrryrrtrvrw)rrrr _grub2_updateszBootloaderPlugin._grub2_updatecCstjjtjS)N)r r rir ZBLS_ENTRIES_PATH)rrrr_has_blsszBootloaderPlugin._has_blscCs\tjdt|dd|jD}|jjdddg|\}}|dkrXtjd|d Sd S) Nzupdating grubenv, setting %scSs$g|]\}}dt|t|fqS)z%s=%s)r])r9Zoptionvaluerrrr< sz4BootloaderPlugin._update_grubenv..z grub2-editenvr[setrzcannot update grubenv: '%s'FT)rCrDr]r>rrBrE)rr5lrFrGrrrr sz BootloaderPlugin._update_grubenvcCsb|jj}|dkrdStjdtj|jjtjdgd|id\}}|dkr^tjd|dSd S) NrFz4running kernel update hook '%s' to patch BLS entriesruZKERNEL_INSTALL_MACHINE_ID)envrzcannot patch BLS entries: '%s'T)rZget_machine_idrCrDr ZKERNEL_UPDATE_HOOK_FILErBrE)rZ machine_idrFrGrrr_bls_entries_patch_initials z+BootloaderPlugin._bls_entries_patch_initialcCs6tjd|jr2|j|j|jdr2|jr2dSdS)Nz updating BLS)rrTF)rCrDrrrrr)rrrr _bls_updates  zBootloaderPlugin._bls_updatecCs(|jdkr$tjjtjtjj||_dS)N)rr r r=r BOOT_DIRbasename)rnamerrr_init_initrd_dst_img&s z%BootloaderPlugin._init_initrd_dst_imgcCstjjtjS)N)r r isdirr ZPETITBOOT_DETECT_DIR)rrrr_check_petitboot*sz!BootloaderPlugin._check_petitbootcCs|jrtjddS|jr&tjdtjd|jtjj|j}|j j ||jsXdSd|_ |j j dj }d}t|}|rtjdd |}t||kr|}tjj|||_dS) 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.zinstalling initrd image as '%s'Tz /proc/cmdline/z)^\s*BOOT_IMAGE=\s*(?:\([^)]*\))?(\S*/).*$z\1)r"rCrErrRrr r rrr\rrmrstripr3rarcr=r)rZimgZimg_nameZ curr_cmdlineZinitrd_grubpathZlcr rrr_install_initrd-s&   z BootloaderPlugin._install_initrdr&cCs$|rdS|r |dk r t|g|_dS)N)r]r)renablingrverifyignore_missingrrr_grub2_cfg_fileBs z BootloaderPlugin._grub2_cfg_filer'cCsR|rdS|rN|dk rNt||_|jdkr,dS|jddkrNtjjtj|j|_dS)NrFrr)r]rr r r=r r)rrrrrrrr_initrd_dst_imgJs   z BootloaderPlugin._initrd_dst_imgr*cCs*|rdS|r&|dk r&|jj|dk|_dS)N1)rget_boolr)rrrrrrrrrVs z#BootloaderPlugin._initrd_remove_dirr(FrI)Z per_deviceZprioritycCsD|rdS|r@|dk r@t|}|j||dkr2dS|j|s@dSdS)NrF)r]rr)rrrrrZsrc_imgrrr_initrd_add_img^s   z BootloaderPlugin._initrd_add_imgr)c Cs|rdS|o|dk rt|}|j||dkr4dStjj|sRtjd|dStjd|tj ddd\}}tj d|tj ||j j d ||d d \}} tj d | |d krtjd|j j|d ddS|j||j j||jrtjd||j j|dS)NrFzFerror: cannot create initrd image, source directory '%s' doesn't existz+generating initrd image from directory '%s'ztuned-bootloader-z.tmp)prefixsuffixz+writing initrd image to temporary file '%s'zfind . | cpio -co > %sT)cwdshellz cpio log: %srzerror generating initrd image)rkzremoving directory '%s')r]rr r rrCrNrRtempfileZmkstemprDcloserrBrzrrZrmtree) rrrrrZsrc_dirfdZtmpfilerFrGrrr_initrd_add_dirks2      z BootloaderPlugin._initrd_add_dirr+cCsJ|jj|jj|}|r |jr8|jd}|j|}n |jjd}t|dkrTdSt |j }t |j } | |} t| dkrt j t jdt| fdSdd|D} xR| D]J} | j ddd} | | krt jt j| | fqt jt j| | | fqW| |@}t j d d j|fd S|rF|dk rFt j d d|_||_dS) Nrz /proc/cmdliner+TcSsi|]}||jdddqS)r/rr)r0)r9r;rrr sz-BootloaderPlugin._cmdline..r/rz2expected arguments that are present in cmdline: %sr8Fz;installing additional boot command line parameters to grub2)Z _variablesexpandrZunquoter"rXr?rmr3rr0rCrRr ZSTR_VERIFY_PROFILE_VALUE_OKr]rNZ'STR_VERIFY_PROFILE_CMDLINE_FAIL_MISSINGZSTR_VERIFY_PROFILE_CMDLINE_FAILr=rr)rrrrrr;Zrpm_ostree_kargsr+Z cmdline_setZ value_setZ missing_setZ cmdline_dictmargZ present_setrrr_cmdlines6         zBootloaderPlugin._cmdliner,cCs8|rdS|r4|dk r4|jj|dkr4tjdd|_dS)Nrz(skipping any modification of grub configT)rrrCrRr)rrrrrrrr_skip_grub_configs   z"BootloaderPlugin._skip_grub_configcCs|rV|jrVt|jdkr"tjdt|jdkr:tjd|jtj|jtj |jin0|r|j r|j rp|j n|j |jd|_ dS)Nrz0requested changes to initrd will not be applied!z1requested changes to cmdline will not be applied!F)rr3rrCrErrtr rvrwrr"rrr)rr#rrrr_instance_post_statics     z&BootloaderPlugin._instance_post_static)r)1r_ __module__ __qualname____doc__rr$r% classmethodr. staticmethodr7r?r!rLrXrhrrrtr{r}r~r Z ROLLBACK_SOFTrrrrrrrrrrrrrrrZcommand_customrrrrrrrr __classcell__rr)rrrsT.    4"          ! r)rrZ decoratorsZ tuned.logsZtunedrZtuned.utils.commandsrZ tuned.constsr r rarZtimerZlogsrPrCZPluginrrrrrs