U
    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edddZeee	eddddZeed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.
ZubuntuZdebianZ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titleZdescriptiondistrosZ	frequencyZexamplesZactivate_by_schema_keysmetac               
   C   sh  d} g }d}d}t  rd}d}z tjdd||gdd	j } W np tk
r } z4t|jtrht	d
 nd|j
kr~t	d n W 5 d}~X Y n  tk
r   ttd Y nX | rtj| sdS z&tjdddd| gdd	j  }W n$ tk
r   ttd|  Y nX t	dd| dd |D }t	dd| |rTt|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/bootZdevicez	/boot/efiz
grub-probez-tT)Z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_dpkgZudevadminfoz--rootz--query=symlinkz3udevadm DEVLINKS symlink query failed for disk='%s'z%considering these device symlinks: %s,c                 S   s   g | ]}d |kr|qS )z
disk/by-id ).0Z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   ZdevicesZprobe_targetZprobe_mounteidevsr   r   r   fetch_idevs?   sP    
 
  
r+   )returnc               
   C   sH   zt jdW S  tk
rB }  ztd|  W Y dS d} ~ X Y nX dS )z4
    Check if the system is booted in EFI mode.
    z/sys/firmware/efiz7Failed to determine if system is booted in EFI mode: %sFN)r#   r$   r%   OSErrorr   error)r)   r   r   r   r      s
    r   )r   cfgcloudargsr,   c              
   C   s   | d| di }|si }| dd}t|rDtd| | d S t|}td| ztdg| W n0 tk
r } zttd| W 5 d }~X Y nX d S )	NZ	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   Zis_falser   r   get_debconf_configr   r!   r"   )r   r/   r0   r1   mycfgr2   Z	dconf_selr)   r   r   r   handle   s"    
  r6   )r5   r,   c                 C   s   t  r*t| dd}|dkr"t }d| S t| dd}|dkrFt }| d}|dkr`| }nt|tstt|}t|	 }d||f S 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 %s
grub-pc grub-pc/install_devices_empty boolean %s
)
r   r   Zget_cfg_option_strr+   r3   r   boolZtranslate_boolstrlower)r5   r*   Zidevs_emptyr   r   r   r4      s,      


r4   )!__doc__Zloggingr#   textwrapr   Z	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.settingsr	   Zcloudinit.subpr
   ZMODULE_DESCRIPTIONr   r   __annotations__Z	getLogger__name__r   r+   r7   r   r8   listr6   r4   r   r   r   r   <module>	   s:   
D