
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me              	   @   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eZ ej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titledescriptiondistrosZ	frequencyZexamplesZactivate_by_schema_keysc              C   sl  d} g }d}d}t  rd}d}y tjdd||gdd	jj } W np tk
r } z6t|jtrhtj	d
 nd|j
kr~tj	d n W Y dd}~X n  tk
r   tjtd Y nX |  stjj|  rdS y&tjdddd| gdd	jj j }W n$ tk
r   tjtd|  Y nX tj	ddj| dd |D }tj	ddj| |rXt|d n| }tj	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.6/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?   sB    
r+   )returnc              C   s>   yt jjdS  tk
r8 }  ztjd|  dS d} ~ X 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   |j d|j di }|si }|j dd}tj|rDtjd| | d S t|}tjd| ytjdg|d W n0 tk
r } ztjtd	| W Y d d }~X 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-selections)dataz6Failed 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    
r7   )r6   r,   c             C   s   t  r*tj| dd}|dkr"t }d| S tj| dd}|dkrFt }| jd}|dkr`| }nt|tsttj|}t|j	 }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+   r4   r   boolZtranslate_boolstrlower)r6   r*   Zidevs_emptyr   r   r   r5      s"    



r5   ) __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   metaZ	getLogger__name__r   r+   r8   r   r9   listr7   r5   r   r   r   r   <module>	   s4   	
D