
    f              
       "   U d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ d
ZddgZdddeee ed      gg dZeed<    ee      Z  ej.                  e      Zd ZdefdZdedede	deddf
dZdedefdZ y)z5Grub Dpkg: Configure grub debconf installation device    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCE)ProcessExecutionErrora  Configure which device is used as the target for grub installation. This module
can be enabled/disabled using the ``enabled`` config key in the ``grub_dpkg``
config dict. This module automatically selects a disk using ``grub-probe`` if
no installation device is specified.

The value which is placed into the debconf database is in the format which the
grub postinstall script expects. Normally, this is a /dev/disk/by-id/ value,
but we do fallback to the plain disk name if a by-id name is not present.

If this module is executed inside a container, then the debconf database is
seeded with empty values, and install_devices_empty is set to true.
ubuntudebiancc_grub_dpkgz	Grub Dpkgz*Configure grub debconf installation deviceaE              grub_dpkg:
              enabled: true
              # BIOS mode (install_devices needs disk)
              grub-pc/install_devices: /dev/sda
              grub-pc/install_devices_empty: false
              # EFI mode (install_devices needs partition)
              grub-efi/install_devices: /dev/sda
            )idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                     d} g }d}d}t               rd}d}	 t        j                  dd||gd	      j                  j                         } | rt        j                  j!                  |       sy	 t        j                  dddd| gd	      j                  j                         j#                         }t        j                  ddj%                  |             |D cg c]	  }d|v s| }}t        j                  ddj%                  |             |rt'        |      d   n| }t        j                  d|       |S # t        $ r`}t        |j                  t              rt        j                  d
       n%d|j                  v rt        j                  d       n Y d}~Qd}~wt        $ r t        j                  t        d       Y zw xY w# t        $ r t        j                  t        d|        Y @w xY wc c}w )z
    Fetches the /dev/disk/by-id device grub is installed to.
    Falls back to plain disk name if no by-id entry is present.
     diskz/bootdevicez	/boot/efiz
grub-probez-tT)capturez'grub-probe' not found in $PATHzfailed to get canonical pathz)grub-probe 'failed to get canonical path'Nz*grub-probe failed to execute for grub_dpkgudevadminfoz--rootz--query=symlinkz3udevadm DEVLINKS symlink query failed for disk='%s'z%considering these device symlinks: %s,z
disk/by-idz)filtered to these disk/by-id symlinks: %sr   zselected %s)is_efi_bootedr   stdoutstripr   
isinstancereasonFileNotFoundErrorLOGdebugstderr	Exceptionr   logexcospathexistssplitjoinsorted)r   devicesprobe_targetprobe_mountedevidevss          ?/usr/lib/python3/dist-packages/cloudinit/config/cc_grub_dpkg.pyfetch_idevsr8   ?   s   
 DG LK!Gyy4{;T

& 	( rww~~d+
 IIFH.?F VEEGUW 	 II5sxx7HI%=s)<s=G=II9388G;LM")F7OAtEIImU#LY !  ahh 12II78
 ,qxx7IIAB  GCEFG"  
F	

 >s=   3D8 .AG	 	G4#G48	GAF&GG	$G10G1returnc                      	 t         j                  j                  d      S # t        $ r } t        j                  d|        Y d} ~ yd} ~ ww xY w)z4
    Check if the system is booted in EFI mode.
    z/sys/firmware/efiz7Failed to determine if system is booted in EFI mode: %sNF)r+   r,   r-   OSErrorr&   error)r4   s    r7   r    r       s=    ww~~122 		KQOs   ! 	A
AA
r   cfgcloudargsc                    |j                  d|j                  di             }|si }|j                  dd      }t        j                  |      rt        j	                  d| |       y t        |      }t        j	                  d|       	 t        j                  dg|       y # t        $ r%}t        j                  t        d	|       Y d }~y d }~ww xY w)
N	grub_dpkgz	grub-dpkgenabledTz*%s disabled by config grub_dpkg/enabled=%sz-Setting grub debconf-set-selections with '%s'zdebconf-set-selections)dataz6Failed to run debconf-set-selections for grub_dpkg: %s)	getr   is_falser&   r'   get_debconf_configr   r)   r*   )r   r=   r>   r?   mycfgrB   	dconf_selr4   s           r7   handlerI      s    GGKb!9:Eii	4(G}}W		>gN"5)III=yI
		+,9= 
I1	
 	

s   B! !	C*C

CrG   c                 h   t               r(t        j                  | dd      }|
t               }d|z  S t        j                  | dd      }|
t               }| j	                  d      }|| }n%t        |t              st        j                  |      }t        |      j                         }d|d|dS )	zd
    Returns the debconf config for grub-pc or
    grub-efi depending on the systems boot mode.
    zgrub-efi/install_devicesNz+grub-pc grub-efi/install_devices string %s
zgrub-pc/install_deviceszgrub-pc/install_devices_emptyz'grub-pc grub-pc/install_devices string z/
grub-pc grub-pc/install_devices_empty boolean 
)
r    r   get_cfg_option_strr8   rD   r#   booltranslate_boolstrlower)rG   r6   idevs_emptys      r7   rF   rF      s    
 ''-t
 =ME=EE''/H$O=MEii ?@#)KK.--k:K+&,,. k#	
    )!__doc__loggingr+   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r	   cloudinit.settingsr
   cloudinit.subpr   MODULE_DESCRIPTIONr   r   __annotations__	getLogger__name__r&   r8   rM   r    rO   listrI   rF    rR   r7   <module>rb      s    <  	    ! # < + 0  X

9%
	
  ")j . t
g!AH	t 	
 
6 
% 
t 
 
* 
f  
  
rR   