3 \�me� � @ s� U d Z ddlZddlZddlmZ ddlmZmZ ddlm Z ddl mZ ddlm Z mZ ddlmZ dd lmZ d ZddgZd ddeeeed�gg d�Ze ee�Z eje�Zdd� Zed�dd�Zeee edd�dd�Zeed�dd�ZdS )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. ZubuntuZdebianZcc_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 frequencyZexamplesZactivate_by_schema_keysc C sl d} g }d}d}t � rd}d}y tjdd||gdd �jj� } W np tk r� } z6t|jt�rhtj d � nd|j kr~tj d� n� W Y d d }~X n tk r� tj td� Y nX | s�tjj| � r�dS y&tjdddd| gdd �jj� j� }W n$ tk �r tj td| � Y nX tj ddj|�� dd� |D �}tj ddj|�� |�rXt|�d n| }tj 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/bootZdevicez /boot/efiz grub-probez-tT)Zcapturez'grub-probe' not found in $PATHzfailed to get canonical pathz)grub-probe 'failed to get canonical path'Nz*grub-probe failed to execute for grub_dpkgZudevadm�infoz--rootz--query=symlinkz3udevadm DEVLINKS symlink query failed for disk='%s'z%considering these device symlinks: %s�,c S s g | ]}d |kr|�qS )z disk/by-id� )�.0Zdevr r �"/usr/lib/python3.6/cc_grub_dpkg.py� <listcomp>z s zfetch_idevs.<locals>.<listcomp>z)filtered to these disk/by-id symlinks: %sr zselected %s)� is_efi_bootedr �stdout�stripr � isinstance�reason�FileNotFoundError�LOG�debug�stderr� Exceptionr �logexc�os�path�exists�split�join�sorted)r ZdevicesZprobe_targetZprobe_mount�e�idevsr r r �fetch_idevs? sB r+ )�returnc C s>