3 \me @sUdZddlZddlZddlmZddlmZmZddlm Z ddl m Z ddl m Z mZddlmZdd lmZd Zd d gZd ddeeeedggdZe eeZejeZddZedddZee e eddddZe edddZdS)z5Grub Dpkg: Configure grub debconf installation deviceN)dedent)subputil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCE)ProcessExecutionErroraConfigure 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. ZubuntuZdebianZ 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 )idnametitle descriptiondistrosZ frequencyZexamplesZactivate_by_schema_keyscCsld}g}d}d}trd}d}y tjdd||gdd jj}Wnptk r}z6t|jtrhtj d nd |j kr~tj d nWYd d }~Xn t k rt j tdYnX| stjj| rdSy&tjdddd|gdd jjj}Wn$t k rt j td|YnXtj ddj|dd|D}tj ddj||rXt|dn|}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_dpkgZudevadminfoz--rootz--query=symlinkz3udevadm DEVLINKS symlink query failed for disk='%s'z%considering these device symlinks: %s,cSsg|]}d|kr|qS)z disk/by-id).0Zdevrr"/usr/lib/python3.6/cc_grub_dpkg.py zszfetch_idevs..z)filtered to these disk/by-id symlinks: %srz selected %s) is_efi_bootedrstdoutstripr isinstancereasonFileNotFoundErrorLOGdebugstderr Exceptionrlogexcospathexistssplitjoinsorted)rZdevicesZ probe_targetZ probe_mounteidevsrrr fetch_idevs?sB      r+)returncCs>y tjjdStk r8}ztjd|dSd}~XnXdS)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%OSErrorrerror)r)rrrrs   r)r cfgcloudargsr,cCs|jd|jdi}|si}|jdd}tj|rDtjd||dSt|}tjd|ytjdg|dWn0tk r}ztjtd |WYdd}~XnXdS) NZ grub_dpkgz grub-dpkgenabledTz*%s disabled by config grub_dpkg/enabled=%sz-Setting grub debconf-set-selections with '%s'zdebconf-set-selections)dataz6Failed to run debconf-set-selections for grub_dpkg: %s) getrZis_falserrget_debconf_configrr!r")r r/r0r1mycfgr2Z dconf_selr)rrrhandles   r7)r6r,cCstr*tj|dd}|dkr"t}d|Stj|dd}|dkrFt}|jd}|dkr`| }nt|tsttj|}t|j }d||fSdS)zd Returns the debconf config for grub-pc or grub-efi depending on the systems boot mode. zgrub-efi/install_devicesNz+grub-pc grub-efi/install_devices string %s zgrub-pc/install_deviceszgrub-pc/install_devices_emptyz[grub-pc grub-pc/install_devices string %s grub-pc grub-pc/install_devices_empty boolean %s ) rrZget_cfg_option_strr+r4rboolZtranslate_boolstrlower)r6r*Z idevs_emptyrrrr5s"     r5) __doc__Zloggingr#textwraprZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.settingsr Zcloudinit.subpr ZMODULE_DESCRIPTIONrmetaZ getLogger__name__rr+r8rr9listr7r5rrrr s4        D