o
    f                  
   @   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	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| |rt|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.
    r	   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>\   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   e   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   handleq   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   MODULE_DESCRIPTIONr   __annotations__	getLogger__name__r#   r3   rE   r   rG   listrC   r@   r   r   r   r   <module>   s(   
D