o
    w7e                  
   @   s   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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)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               
   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.
     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   
r9   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/   r0   OSErrorr)   error)r7   r   r   r!   r#      s   r#   r   cfgcloudargsc              
   C   s   | d| di }|si }| dd}t|r"td| | d S t|}td| z
tdg| W d S  tyP } 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-selectionsz6Failed to run debconf-set-selections for grub_dpkg: %s)	getr   is_falser)   r*   get_debconf_configr   r,   r-   )r   r=   r>   r?   mycfgrA   	dconf_selr7   r   r   r!   handle   s"   
rG   rE   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_strr9   rB   r&   booltranslate_boolstrlower)rE   r8   idevs_emptyr   r   r!   rD      s(   


rD   )!__doc__loggingr.   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.settingsr	   cloudinit.subpr
   MODULE_DESCRIPTIONr   r   __annotations__	getLogger__name__r)   r9   rI   r#   rK   listrG   rD   r   r   r   r!   <module>   s<   
D