a 'Dg$@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z dZ dZeeZGdddejdZGd d d eZegZdd d Zd dZdddZddZddZddZdddZdS)N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledc@s8eZdZdZejedddZejedddZ dS)InitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfsesreturncCsdS)zAIs this initramfs config source applicable to the current system?Nselfr 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__abcabstractmethodboolr dictrr r r r rs r) metaclassc@s6eZdZdZd ddZedddZeddd ZdS) 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.) NcCsp||_||_||_|jdur$t|_|jdur8t|_|jdurli|_tD]}t|d}|rN||j|<qNdS)Naddress)_files _mac_addrs_cmdline_get_klibc_net_cfg_filesr get_cmdlinerr)r rrrkZmac_addrr r r __init__/s      z!KlibcNetworkConfigSource.__init__rcCs<|jr8t|jD]}|drdSqtjtr8dSdS)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) rshlexsplitr startswithospathexists_OPEN_ISCSI_INTERFACE_FILE)r itemr r r r As   z&KlibcNetworkConfigSource.is_applicablecCst|j|jdS)N)files mac_addrs)config_from_klibc_net_cfgrrr r r r rTsz&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|drd}nd }|d vrd }|d vrtd |d |gd}||vr|||d<dD]}|d|vrq||d|}|d krd}|dd} |dkr&||d| d<dD](} || |vr*||| | | <q*g} dD]<} ||| } | r\t| dr\| ||| q\| r| | d<|d}|rd|vr| 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 IPV6PROTOfilenamedhcpnone)staticZoff)r.r-Zdhcp6zUnexpected value for PROTO: %sZphysical)typenamesubnets mac_address)ZIPV4ZIPV6ZADDRr/Zmanual)r0Zcontrolr)ZNETMASKZ BROADCASTZGATEWAY)ZDNS0ZDNS1z:.0Zdns_nameserversZ DOMAINSEARCH,Z dns_searchr2) rZload_shell_contentKeyError ValueErrorgetlowerlenstripappendr")Zcontentr*datar1eprotoZifaceZpreZ cur_protoZsubnetkeyZdnsZnskeynssearchr r r _klibc_to_config_entry^s`             rBcCstdtdS)Nz/run/net-*.confz/run/net6-*.conf)globr r r r rsrc Cs|durt}g}i}|D]}tt||d\}}||vr||d}|d|dkrtdj|d||d|d|dd|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)oldnewr2)r)rD)configversion) rrBrZload_text_filer7r6formatjoinextendr;)r)r*entriesnamesZcfg_filer1rDprevr 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_configs  rRc Cst|}d}zxz4tjd|d}|WW|r8|WdStyx|YW|rh|WdS0W|r|n|r|0Wdn1s0YdS)Nrb)modeZfileobj)ioBytesIOgzipZGzipFilereadcloseIOError)blobZiobufZgzfpr r r _decomp_gzips"    r\c Cs<zt|}Wn$ttfy2td|YdS0t|S)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.)base64Z b64decode TypeErrorr6LOGerrorr\)r<r[r r r _b64dgzsrbcCsj|durt}d|vrfd}|D]}|dr$|ddd}q$|rf|tkrXddiStt|SdS)Nznetwork-config==rHrIr)rrr"r#&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLEDZ load_yamlrb)ZcmdlineZdata64tokr 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 `