
    x[h$                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
JrJr  SrSr\R                  " \5      r " S S\ R$                  S9r " S	 S
\5      r\/rSS jrS rSS jrS rS rS rSS jrg)    N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledc                   p    \ rS rSrSr\R                  S\4S j5       r\R                  S\	4S j5       r
Srg)InitramfsNetworkConfigSource   zBABC for net config sources that read config written by initramfsesreturnc                     g)zAIs this initramfs config source applicable to the current system?N selfs    7/usr/lib/python3/dist-packages/cloudinit/net/cmdline.pyis_applicable*InitramfsNetworkConfigSource.is_applicable           c                     g)z;Render a v1 network config from the initramfs configurationNr   r   s    r   render_config*InitramfsNetworkConfigSource.render_config"   r   r   r   N)__name__
__module____qualname____firstlineno____doc__abcabstractmethodboolr   dictr   __static_attributes__r   r   r   r   r      sK    LPt P P 	Jt J Jr   r   )	metaclassc                   >    \ rS rSrSrS	S jrS\4S jrS\4S jr	Sr
g)
KlibcNetworkConfigSource'   zInitramfsNetworkConfigSource 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                 @   Xl         X l        X0l        U R                   c  [        5       U l         U R                  c  [        R
                  " 5       U l        U R                  c<  0 U l        [        5        H&  n[        US5      nU(       d  M  XPR                  U'   M(     g g )Naddress)_files
_mac_addrs_cmdline_get_klibc_net_cfg_filesr   get_cmdliner   r   )r   r'   r(   r)   kmac_addrs         r   __init__!KlibcNetworkConfigSource.__init__/   s    $  ;;24DK==  ,,.DM??" DO#%,Q	:8)1OOA& & #r   r
   c                     U R                   (       ah  [        R                  " U R                  5       H  nUR	                  S5      (       d  M    g   [
        R                  R                  [        5      (       a  gg)a  
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   &KlibcNetworkConfigSource.is_applicableA   sP     ;;DMM2???33 3 ww~~899r   c                 >    [        U R                  U R                  S9$ )N)files	mac_addrs)config_from_klibc_net_cfgr'   r(   r   s    r   r   &KlibcNetworkConfigSource.render_configT   s    (++oo
 	
r   )r)   r'   r(   )NNN)r   r   r   r   r   r.   r   r   r   r   r    r   r   r   r#   r#   '   s$    2$t &
t 
r   r#   c                    Uc  0 n[         R                  " U 5      n SU;   a  US   OUS   nUR	                  SUR	                  S5      5      nU(       d  UR	                  S5      (       a  SnOS	nUS
;   a  S	nUS;  a  [        SU-  5      eSU/ S.nX1;   a  X   US'   S GH.  nUS-   U;  a  M  UR	                  US-   U5      nUS	:X  a  SnUSS.n	US:X  a
  X'S-      U	S'   S H$  n
Xz-   U;   d  M  X'U
-      XR                  5       '   M&     / nS HV  nUR	                  X|-   5      nU(       d  M  [        UR                  S5      5      (       d  M@  UR                  X'U-      5        MX     U(       aJ  XS'   UR	                  S5      nU(       a.  SU;   a  UR                  S5      U	S'   OUR                  5       U	S'   US   R                  U	5        GM1     X64$ ! [         a  n[        S5      UeSnAff = f)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) or 'static'
or 'off' (LP: 2065787). Note that IPV6PROTO is also written 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)staticoff)rF   rE   dhcp6zUnexpected value for PROTO: %sphysical)typenamesubnetsmac_address)IPV4IPV6ADDRrG   manual)rK   controlr&   )NETMASK	BROADCASTGATEWAY)DNS0DNS1z:.0dns_nameserversDOMAINSEARCH,
dns_searchrM   )
r   load_shell_contentKeyError
ValueErrorgetlowerlenstripappendr2   )contentr<   datarL   eprotoifacepre	cur_protosubnetkeydnsnskeynssearchs                  r   _klibc_to_config_entryrr   ^   s#   $ 	""7+DJ!)T!1tH~tI HHWdhh{34E88JEE!!--9EABB E (m  <t# HHS7]E2	 I#9   $6\ 2F9 7CyD &*9oyy{# 7 %E#+&Brc"((5/**

4e,-	 &
 (+$%XXn-F&=+1<<+<F<(+1<<>F<(i'O  R ;K  JABIJs   G 
G1 G,,G1c                  \    [         R                   " S5      [         R                   " S5      -   $ )Nz/run/net-*.confz/run/net6-*.conf)globr   r   r   r*   r*      s!    99&'$))4F*GGGr   c                 
   U c
  [        5       n / n0 nU  H  n[        [        R                  " U5      US9u  pVXS;   a  X5   S   nUR	                  S5      UR	                  S5      :w  aM  [        SR                  USR                  X5   S   5      UR	                  S5      UR	                  S5      S95      eUS   R                  US   5        X5   S   R                  U5        M  U/US	.X5'   UR                  U5        M     US
S.$ )N)r<   entryrN   zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r;   )rL   r;   oldnewrM   )r;   rv      )configversion)
r*   rr   r   load_text_filer`   r_   formatjoinextendrd   )r;   r<   entriesnamescfg_filerL   rv   prevs           r   r=   r=      s   }(*GE,)Y
 =;w'Dxx&%))M*BB HHN!!hhu{7';< HH]3!IIm4	 IO I  O""5#34K ''1%-J?EKNN5!) , !,,r   c                  z    [          H1  n U " 5       nUR                  5       (       d  M!  UR                  5       s  $    g)z
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      s;     -Y
''))'')) - r   c                    [         R                  " U 5       nS n [        R                  " SUS9nUR	                  5       U(       a  UR                  5         sS S S 5        $ ! [         a%    U s U(       a  UR                  5         sS S S 5        $ f = f! U(       a  UR                  5         f f = f! , (       d  f       g = f)Nrb)modefileobj)ioBytesIOgzipGzipFilereadcloseIOError)blobiobufgzfps      r   _decomp_gzipr      s    	D	U	==dE:D99; 

 
	
  	K

 
	
	 

  
	s?   B/$A B/ B,B-B/BBB,,B//
B=c                      [         R                  " U 5      n[        U5      $ ! [        [        4 a    [        R                  SU 5         gf = f)zDecode a string base64 encoding, if gzipped, uncompress as well

:return: decompressed unencoded string of the data or empty string on
   unencoded data.
zaExpected base64 encoded kernel command line parameter network-config. Ignoring network-config=%s. )base64	b64decode	TypeErrorr_   LOGerrorr   )rf   r   s     r   _b64dgzr     sS    %  z" 		;	

 s   # &AAc                 4   U c  [         R                  " 5       n SU ;   az  S nU R                  5        H0  nUR                  S5      (       d  M  UR                  SS5      S   nM2     U(       a-  U[        :X  a  SS0$ [         R
                  " [        U5      5      $ g )Nznetwork-config==rz   r{   r   )r   r+   r2   r3   &KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED	load_yamlr   )cmdlinedata64toks      r   read_kernel_cmdline_configr     s    ""$G#==?C~~/003*1- # ?? *-->>'&/22r   )N)NN)r   r   rt   r   r   loggingr4   r1   	cloudinitr   cloudinit.netr   r   r7   r   	getLoggerr   r   ABCMetar   r#   r   rr   r*   r=   r   r   r   r   r   r   r   <module>r      s        	  	   ;B )3 &!	JS[[ 	J1
; 1
h 66 ]@H->$&r   