f2$ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z dZ dZejeZGddej$ZGd d eZegZdd Zd Zdd ZdZdZdZddZy)N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledcdeZdZdZej defdZej defdZ y)InitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfsesreturncy)zAIs this initramfs config source applicable to the current system?Nselfs 7/usr/lib/python3/dist-packages/cloudinit/net/cmdline.py is_applicablez*InitramfsNetworkConfigSource.is_applicablecy)z;Render a v1 network config from the initramfs configurationNr r s r render_configz*InitramfsNetworkConfigSource.render_config"rrN) __name__ __module__ __qualname____doc__abcabstractmethodboolrdictrr rrrrsKLPtPP JtJJrr) metaclassc0eZdZdZddZdefdZdefdZy)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.) Nc0||_||_||_|jt|_|jt j |_|j5i|_t D] }t|d}|s||j|<"yy)Naddress)_files _mac_addrs_cmdline_get_klibc_net_cfg_filesr get_cmdlinerr)r r!r"r#kmac_addrs r__init__z!KlibcNetworkConfigSource.__init__/s $   ;; 24DK ==  ,,.DM ?? " DO#% 2,Q :)1DOOA& 2 #rr c|jr[tj|jD]}|j dsyt j jtryy)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!shlexsplitr# startswithospathexists_OPEN_ISCSI_INTERFACE_FILE)r items rrz&KlibcNetworkConfigSource.is_applicableAsO ;; DMM2 ???3 ww~~89rcDt|j|jS)N)files mac_addrs)config_from_klibc_net_cfgr!r"r s rrz&KlibcNetworkConfigSource.render_configTs(++oo  r)NNN) rrrrr(rrrrr rrrr's$2$t& t rrcz|i}tj|} d|vr|dn|d}|j d|j d}|s|j drd}nd }|d vrtd |zd |gd }||vr|||d<dD]}|dz|vr |j |dz|}|d k(rd}|dd} |dk(r ||dz| d<dD]#} || z|vs ||| z| | j <%g} dD]K} |j || z} | st | jds5| j||| zM| rD| | d<|j d}|r,d|vr|jd| d<n|j| d<|dj| ||fS#t$r}td|d}~wwxYw)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). 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 NDEVICEDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataPROTO IPV6PROTOfilenamedhcpnone)r=r<dhcp6zUnexpected value for PROTO: %sphysical)typenamesubnets mac_address)IPV4IPV6ADDRstaticmanual)r@controlr )NETMASK BROADCASTGATEWAY)DNS0DNS1z:.0dns_nameservers DOMAINSEARCH, dns_searchrB) rload_shell_contentKeyError ValueErrorgetlowerlenstripappendr+)contentr4datarAeprotoifacepre cur_protosubnetkeydnsnskeynssearchs r_klibc_to_config_entryrh^s0$  " "7 +DJ!)T!1tH~tI HHWdhh{3 4E  88J EE --9EABB E  y(m '( F<( i'O'(R ;E JABIJsF F:) F55F:cXtjdtjdzS)Nz/run/net-*.confz/run/net6-*.conf)globr rrr$r$s! 99& '$))4F*G GGrc | t}g}i}|D]}ttj||\}}||vr||d}|j d|j dk7rQt dj |dj||d|j d|j d|dj|d||dj||g|d ||<|j||d d S) N)r4entryrCzedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r3)rAr3oldnewrB)r3rl)configversion) r$rhrload_text_filerVrUformatjoinextendrZ)r3r4entriesnamescfg_filerArlprevs rr5r5s% }(*G E",    )Y e 5=;w'Dxx &%))M*BB HHN!!hhuT{7';< HH]3!IIm4 IOI O " "5#3 4 $K ' ' 1%-J?E$K NN5 !)",! ,,rcltD]+}|}|js|jcSy)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_SOURCESrr)src_cls cfg_sources rread_initramfs_configrs=-*Y '') '')) * rcXtj|5}d} tjd|}|j |r|j cdddS#t $r |cY|r|j cdddSwxYw#|r|j wwxYw#1swYyxYw)Nrb)modefileobj)ioBytesIOgzipGzipFilereadcloseIOError)blobiobufgzfps r _decomp_gziprs D  U ==dE:D99;     K        s@B &AB  B'B(B BBBB  B)c tj|}t |S#ttf$rtj d|YywxYw)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 TypeErrorrULOGerrorr)r\rs r_b64dgzrsS %   z "  ;   s"%A  A c|tj}d|vrkd}|jD])}|jds|jddd}+|r+|tk(rddiStj t |Sy)Nznetwork-config==rprqr)rr%r+r,&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED load_yamlr)cmdlinedata64toks rread_kernel_cmdline_configrs""$G#==? .C~~/03*1- . ?? *-->>'&/2 2 r)N)NN)rrrjrrloggingr-r* cloudinitr cloudinit.netrrr0r getLoggerrrABCMetarrr|rhr$r5rrrrr rrrs  ;B)3&g! JS[[ J1 ;1 h66ZzH->$ &r