o
    m[h”  ã                
   @   sÐ   U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ d	d
dgeg dœZeed< e e¡Zdd„ Zdefdd„Zdede	dededdf
dd„Zde	defdd„ZdS )z5Grub Dpkg: Configure grub debconf installation deviceé    N)ÚsubpÚutil)ÚCloud)ÚConfig)Ú
MetaSchema)ÚPER_INSTANCE)ÚProcessExecutionErrorÚcc_grub_dpkgÚubuntuÚdebian)ÚidÚdistrosÚ	frequencyÚactivate_by_schema_keysÚmetac               
   C   sb  d} g }d}d}t ƒ rd}d}ztjdd||gdd	�j ¡ } W n8 tyI } zt|jtƒr3t 	d
¡ nd|j
v r>t 	d¡ n‚ W Y d}~nd}~w tyW   t td¡ Y nw | r`tj | ¡sbdS ztjdddd| gdd	�j ¡  ¡ }W n ty…   t td| ¡ Y nw t 	dd |¡¡ dd„ |D ƒ}t 	dd |¡¡ |r§t|ƒd n| }t 	d|¡ |S )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ú,c                 S   s   g | ]}d |v r|‘qS )z
disk/by-id© )Ú.0Údevr   r   ú?/usr/lib/python3/dist-packages/cloudinit/config/cc_grub_dpkg.pyÚ
<listcomp>Z   s    zfetch_idevs.<locals>.<listcomp>z)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Úidevsr   r   r   Úfetch_idevs   sX   ÿþ
€ÿþúÿÿr3   Úreturnc               
   C   sB   zt j d¡W S  ty  }  zt d| ¡ W Y d} ~ dS d} ~ w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)r1   r   r   r   r   c   s   €ýr   ÚnameÚcfgÚcloudÚargsc              
   C   s¤   |  d|  di ¡¡}|si }|  dd¡}t |¡r"t d| |¡ d S t|ƒ}t d|¡ ztjdg|d� W d S  tyQ } zt td	|¡ W Y d }~d S d }~w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'   )r7   r8   r9   r:   Úmycfgr<   Ú	dconf_selr1   r   r   r   Úhandleo   s"   
ÿ€ÿrC   rA   c                 C   sŒ   t ƒ rt | dd¡}|du rtƒ }d| S t | dd¡}|du r#tƒ }|  d¡}|du r0| }n
t|tƒs:t |¡}t|ƒ 	¡ }d||f 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 %s
grub-pc grub-pc/install_devices_empty boolean %s
)
r   r   Úget_cfg_option_strr3   r>   r    ÚboolÚtranslate_boolÚstrÚlower)rA   r2   Úidevs_emptyr   r   r   r@   „   s(   ÿ


þÿr@   )Ú__doc__Úloggingr(   Ú	cloudinitr   r   Úcloudinit.cloudr   Úcloudinit.configr   Úcloudinit.config.schemar   Úcloudinit.settingsr   Úcloudinit.subpr   r   Ú__annotations__Ú	getLoggerÚ__name__r#   r3   rE   r   rG   ÚlistrC   r@   r   r   r   r   Ú<module>   s&   ü
D