a '¡Dgƒ$ã@sÚddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z dZ dZe e¡ZGdd„dejd�ZGd d „d eƒZegZdd d „Zd d„Zddd„Zdd„Zdd„Zdd„Zddd„ZdS)éN)Úutil)Úget_devicelistÚread_sys_net_safez#/run/initramfs/open-iscsi.interfaceÚdisabledc@s8eZdZdZejedœdd„ƒZejedœdd„ƒZ dS)ÚInitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfses©ÚreturncCsdS)zAIs this initramfs config source applicable to the current system?N©©Úselfr r ú9/usr/lib/python3.9/site-packages/cloudinit/net/cmdline.pyÚ is_applicablesz*InitramfsNetworkConfigSource.is_applicablecCsdS)z;Render a v1 network config from the initramfs configurationNr r r r r Ú render_config"sz*InitramfsNetworkConfigSource.render_configN) Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚabcÚabstractmethodÚboolr Údictrr r r r rs r)Ú metaclassc@s6eZdZdZd dd„Zedœdd„Zedœdd „ZdS) ÚKlibcNetworkConfigSourcezñInitramfsNetworkConfigSource 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.) NcCsp||_||_||_|jdur$tƒ|_|jdur8t ¡|_|jdurli|_tƒD]}t|dƒ}|rN||j|<qNdS)NÚaddress)Ú_filesÚ _mac_addrsÚ_cmdlineÚ_get_klibc_net_cfg_filesrÚ get_cmdlinerr)r rrrÚkZmac_addrr r r Ú__init__/s      z!KlibcNetworkConfigSource.__init__rcCs<|jr8t |j¡D]}| d¡rdSqtj t¡r8dSdS)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 )zip=zip6=TF) rÚshlexÚsplitrÚ startswithÚosÚpathÚexistsÚ_OPEN_ISCSI_INTERFACE_FILE)r Úitemr r r r As   z&KlibcNetworkConfigSource.is_applicablecCst|j|jd�S)N)ÚfilesÚ mac_addrs)Úconfig_from_klibc_net_cfgrrr r r r rTsþz&KlibcNetworkConfigSource.render_config)NNN) rrrrr rr rrr r r r r's rc Csö|dur i}t |¡}zd|vr(|dn|d}Wn.ty`}ztdƒ|‚WYd}~n d}~00| d| d¡¡}|sŒ| d¡rˆd}nd }|d vr˜d }|d vr¬td |ƒ‚d |gdœ}||vrÌ|||d<dD�]}|d|vräqÐ| |d|¡}|d k�rd}|ddœ} |dk�r&||d| d<dD](} || |v�r*||| | |  ¡<�q*g} dD]<} | || ¡} | �r\t|  d¡ƒ�r\|  ||| ¡�q\| �rÞ| | d<| d¡}|�rÞd|v�rÒ|  d¡| d<n |  ¡| d<|d | ¡qÐ||fS)aConvert 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-.cfg. The files are shell style syntax, and examples are in the tests provided here. There is no good documentation on this unfortunately. DEVICE= 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 NZDEVICEZDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataZPROTOZ IPV6PROTOÚfilenameÚdhcpÚnone)ÚstaticZoff)r.r-Zdhcp6zUnexpected value for PROTO: %sZphysical)ÚtypeÚnameÚsubnetsÚ mac_address)ZIPV4ZIPV6ZADDRr/Zmanual)r0Zcontrolr)ZNETMASKZ BROADCASTZGATEWAY)ZDNS0ZDNS1z:.0Zdns_nameserversZ DOMAINSEARCHú,Z dns_searchr2) rZload_shell_contentÚKeyErrorÚ ValueErrorÚgetÚlowerÚlenÚstripÚappendr")Zcontentr*Údatar1ÚeÚprotoZifaceZpreZ cur_protoZsubnetÚkeyZdnsZnskeyÚnsÚsearchr r r Ú_klibc_to_config_entry^s`    ý         rBcCst d¡t d¡S)Nz/run/net-*.confz/run/net6-*.conf)Úglobr r r r r¾src CsØ|durtƒ}g}i}|D]²}tt |¡|d�\}}||vr²||d}| d¡| d¡krŒtdj|d ||d¡| d¡| d¡d�ƒ‚|d |d¡||d  |¡q|g|d œ||<|  |¡q|d d œS) N)r*Úentryr3zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}.ú r))r1r)ÚoldÚnewr2)r)rDé)ÚconfigÚversion) rrBrZload_text_filer7r6ÚformatÚjoinÚextendr;)r)r*ÚentriesÚnamesZcfg_filer1rDÚprevr r r r+Âs0 ÿ  ûÿ  r+cCs(tD]}|ƒ}| ¡sq| ¡SdS)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)Zsrc_clsZ cfg_sourcer r r Úread_initramfs_configás  rRc Csºt |¡�œ}d}zxz4tjd|d�}| ¡WW|r8| ¡WdƒStyx|YW|rh| ¡WdƒS0W|r˜| ¡n|r–| ¡0Wdƒn1s¬0YdS)NÚrb)ÚmodeZfileobj)ÚioÚBytesIOÚgzipZGzipFileÚreadÚcloseÚIOError)ÚblobZiobufZgzfpr r r Ú _decomp_gzipós"  û ý ÿr\c Cs<zt |¡}Wn$ttfy2t d|¡YdS0t|ƒS)z¦Decode 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.Ú)Úbase64Z b64decodeÚ TypeErrorr6ÚLOGÚerrorr\)r<r[r r r Ú_b64dgzsýrbcCsj|durt ¡}d|vrfd}| ¡D]}| d¡r$| dd¡d}q$|rf|tkrXddiSt t|ƒ¡SdS)Nznetwork-config=ú=rHrIr)rrr"r#Ú&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLEDZ load_yamlrb)ZcmdlineZdata64Útokr r r Úread_kernel_cmdline_configs  rf)N)NN)N)rr^rCrWrUZloggingr$r!Z cloudinitrZ cloudinit.netrrr'rdZ getLoggerrr`ÚABCMetarrrQrBrr+rRr\rbrfr r r r Ús,   4 `