
    ܍~c6$                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZ dZdZ G d de j                  Z G d	 d
e          ZegZddZd ZddZd Zd Zd ZddZdS )    N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledc                   ^    e Zd ZdZej        defd            Zej        defd            Z	dS )InitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfsesreturnc                     dS )zAIs this initramfs config source applicable to the current system?N selfs    7/usr/lib/python3/dist-packages/cloudinit/net/cmdline.pyis_applicablez*InitramfsNetworkConfigSource.is_applicable             c                     dS )z;Render a v1 network config from the initramfs configurationNr   r   s    r   render_configz*InitramfsNetworkConfigSource.render_config    r   r   N)
__name__
__module____qualname____doc__abcabstractmethodboolr   dictr   r   r   r   r   r      sz        LLPt P P P P 	Jt J J J J J Jr   r   )	metaclassc                   2    e Zd ZdZddZdefdZdefdZdS )KlibcNetworkConfigSourcezInitramfsNetworkConfigSource for klibc initramfs (i.e. Debian/Ubuntu)

    Has three parameters, but they are intended to make testing simpler, _not_
    for use in production code.  (This is indicated by the prepended
    underscores.)
    Nc                    || _         || _        || _        | j         t                      | _         | j        t	          j                    | _        | j        4i | _        t                      D ] }t          |d          }|r
|| j        |<   d S d S )Naddress)_files
_mac_addrs_cmdline_get_klibc_net_cfg_filesr   get_cmdliner   r   )r   r!   r"   r#   kmac_addrs         r   __init__z!KlibcNetworkConfigSource.__init__-   s    $  ;244DK=  ,..DM?" DO#%% 2 2,Q	:: 2)1DOA& #"2 2r   r	   c                     | j         rot          j        | j                  D ]/}|                    d          s|                    d          r dS 0t
          j                            t                    rdS dS )aD  
        Return whether this system has klibc initramfs network config or not

        Will return True if:
            (a) klibc files exist in /run, AND
            (b) either:
                (i) ip= or ip6= are on the kernel cmdline, OR
                (ii) an open-iscsi interface file is present in the system
        zip=zip6=TF)	r!   shlexsplitr#   
startswithospathexists_OPEN_ISCSI_INTERFACE_FILE)r   items     r   r   z&KlibcNetworkConfigSource.is_applicable?   s}     ; 	DM22    ??5))  T__V-D-D  44 w~~899 tur   c                 8    t          | j        | j                  S )N)files	mac_addrs)config_from_klibc_net_cfgr!   r"   r   s    r   r   z&KlibcNetworkConfigSource.render_configR   s#    (+o
 
 
 	
r   )NNN)	r   r   r   r   r(   r   r   r   r   r   r   r   r   r   %   sh         2 2 2 2$t    &
t 
 
 
 
 
 
r   r   c                    |i }t          j        |           }	 d|v r|d         n|d         }n"# t          $ r}t          d          |d}~ww xY w|                    d|                    d                    }|s|                    d          rd}nd	}|d
vrt          d|z            d|g d}||v r||         |d<   dD ]?}|dz   |vr|                    |dz   |          }|d	k    rd}|dd}	|dk    r||dz            |	d<   dD ])}
||
z   |v r |||
z            |	|
                                <   *g }dD ]\}|                    ||z             }|r@t          |                    d                    r|                    |||z                       ]|rP||	d<   |                    d          }|r4d|v r|	                    d          |	d<   n|	                                |	d<   |d                             |	           A||fS )a  Convert a klibc written shell content file to a 'config' entry
    When ip= is seen on the kernel command line in debian initramfs
    and networking is brought up, ipconfig will populate
    /run/net-<name>.cfg.

    The files are shell style syntax, and examples are in the tests
    provided here.  There is no good documentation on this unfortunately.

    DEVICE=<name> is expected/required and PROTO should indicate if
    this is 'none' (static) or 'dhcp' or 'dhcp6' (LP: #1621507).
    note that IPV6PROTO is also written by newer code to address the
    possibility of both ipv4 and ipv6 getting addresses.

    Full syntax is documented at:
    https://git.kernel.org/pub/scm/libs/klibc/klibc.git/plain/usr/kinit/ipconfig/README.ipconfig
    NDEVICEDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataPROTO	IPV6PROTOfilenamedhcpnone)r=   r<   dhcp6zUnexpected value for PROTO: %sphysical)typenamesubnetsmac_address)IPV4IPV6ADDRstaticmanual)r@   controlr    )NETMASK	BROADCASTGATEWAY)DNS0DNS1z:.0dns_nameserversDOMAINSEARCH,
dns_searchrB   )
r   load_shell_contentKeyError
ValueErrorgetlowerlenstripappendr+   )contentr4   datarA   eprotoifacepre	cur_protosubnetkeydnsnskeynssearchs                  r   _klibc_to_config_entryrh   \   s   $ 	"7++DJ!)T!1!1tH~~tI J J JABBIJ
 HHWdhh{3344E 88J 	EEE---9EABBB  E y(m   '( '(<t## HHS7]E22	 I#99    $S6\ 2F9 7 	6 	6CSyD  &*39osyy{{#% 	. 	.E#+&&B .c"((5//** .

4e,--- 	:(+F$%XXn--F :&==+1<<+<+<F<((+1<<>>F<(i'''';s   / 
AA		Ac                  T    t          j         d          t          j         d          z   S )Nz/run/net-*.confz/run/net6-*.conf)globr   r   r   r$   r$      s#    9&''$)4F*G*GGGr   c                    | t                      } g }i }| D ]4}t          t          j        |          |          \  }}||v r||         d         }|                    d          |                    d          k    rkt          d                    |d                    ||         d                   |                    d          |                    d                              |d                             |d                    ||         d         	                    |           |g|d	||<   |	                    |           6|d
dS )N)r4   entryrC   zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r3   )rA   r3   oldnewrB   )r3   rl      )configversion)
r$   rh   r   	load_filerV   rU   formatjoinextendrZ   )r3   r4   entriesnamescfg_filerA   rl   prevs           r   r5   r5      sl   }(**GE " ",N8$$	
 
 
e 5==;w'Dxx&&%))M*B*BBB HHN!!hhuT{7';<< HH]33!IIm44	 IO I I   O""5#3444$K ''1111%-J??E$KNN5!!!!!,,,r   c                      t           D ]6}  |             }|                                s!|                                c S dS )a  
    Return v1 network config for initramfs-configured networking (or None)

    This will consider each _INITRAMFS_CONFIG_SOURCES entry in turn, and return
    v1 network configuration for the first one that is applicable.  If none are
    applicable, return None.
    N)_INITRAMFS_CONFIG_SOURCESr   r   )src_cls
cfg_sources     r   read_initramfs_configr      sR     - * *WYY
'')) 	'')))))4r   c                    t          j        |           5 }d }	 t          j        d|          }|                                |r|                                 cd d d            S # t          $ r' | cY |r|                                 cd d d            S w xY w# |r|                                 w w xY w# 1 swxY w Y   d S )Nrb)modefileobj)ioBytesIOgzipGzipFilereadcloseIOError)blobiobufgzfps      r   _decomp_gzipr      s6   	D		 	U	=dE:::D99;;  

	 	 	 	 	 	 	 	
  	 	 	KK 

	 	 	 	 	 	 	 	
	  

	 	 	 	 	 	 	 	 	 	sF   B5)A%B5%B1B2B5BBB22B55B9<B9c                     	 t          j        |           }n-# t          t          f$ r t	          j        d|            Y dS w xY wt          |          S )zDecode a string base64 encoding, if gzipped, uncompress as well

    :return: decompressed unencoded string of the data or empty string on
       unencoded data.
    z`Expected base64 encoded kernel commandline parameter network-config. Ignoring network-config=%s. )base64	b64decode	TypeErrorrU   loggingerrorr   )r\   r   s     r   _b64dgzr      sn    %%z"   ;	
 	
 	

 rr s    &A Ac                 0   | t          j                    } d| v r|d }|                                 D ]3}|                    d          r|                    dd          d         }4|r0|t          k    rddiS t          j        t          |                    S d S )Nznetwork-config==rp   rq   r   )r   r%   r+   r,   &KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED	load_yamlr   )cmdlinedata64toks      r   read_kernel_cmdline_configr     s    "$$G##==?? 	. 	.C~~/00 .3**1- 	3??? *-->'&//2224r   )N)NN)r   r   rj   r   r   r   r-   r*   	cloudinitr   cloudinit.netr   r   r0   r   ABCMetar   r   r|   rh   r$   r5   r   r   r   r   r   r   r   <module>r      s   


    				  				        ; ; ; ; ; ; ; ;B )3 &	J 	J 	J 	J 	JS[ 	J 	J 	J 	J1
 1
 1
 1
 1
; 1
 1
 1
h 66 Z Z Z ZzH H H- - - ->  $    &     r   