܍~c6$ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z dZ dZGddejZGd d eZegZdd Zd Zdd ZdZdZdZddZdS)N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledc^eZdZdZejdefdZejdefdZ dS)InitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfsesreturncdS)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_applicablecdS)z;Render a v1 network config from the initramfs configurationNr r s r render_configz*InitramfsNetworkConfigSource.render_config rrN) __name__ __module__ __qualname____doc__abcabstractmethodboolrdictrr rrrrszLLPtPPPP JtJJJJJJrr) metaclassc2eZdZdZddZdefdZdefdZdS)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||_||_||_|jt|_|jt j|_|j4i|_t D] }t|d}|r ||j|<dSdS)Naddress)_files _mac_addrs_cmdline_get_klibc_net_cfg_filesr get_cmdlinerr)r r!r"r#kmac_addrs r__init__z!KlibcNetworkConfigSource.__init__-s $   ; 244DK =  ,..DM ? " DO#%% 2 2,Q ::2)1DOA& # " 2 2rr c|jrotj|jD]/}|ds|drdS0t jtrdSdS)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_applicable?s} ;  DM22  ??5)) T__V-D-D 44 w~~899 turc8t|j|jS)N)files mac_addrs)config_from_klibc_net_cfgr!r"r s rrz&KlibcNetworkConfigSource.render_configRs#(+o    r)NNN) rrrrr(rrrrr rrrr%sh2222$t& t      rrc|i}tj|} d|vr|dn|d}n"#t$r}td|d}~wwxYw|d|d}|s|drd}nd }|d vrtd |zd |gd }||vr |||d<dD]?}|dz|vr ||dz|}|d krd}|dd} |dkr||dz| d<dD])} || z|vr ||| z| | <*g} dD]\} ||| z} | r@t | dr| ||| z]| rP| | d<|d}|r4d|vr| d| d<n| | d<|d| A||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). 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\s$  "7 + +DJ!)T!1!1tH~~tI JJJABBIJ HHWdhh{33 4 4E  88J   EEE ---9EABBB  E  y(m '('( >F<( i'''' ;s/ AA  AcTtjdtjdzS)Nz/run/net-*.confz/run/net6-*.conf)globr rrr$r$s# 9& ' '$)4F*G*G GGrc |t}g}i}|D]4}ttj||\}}||vr||d}|d|dkrkt d|d||d|d|d|d|d||d ||g|d ||<| |6|d d S) N)r4entryrCzedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r3)rAr3oldnewrB)r3rl)configversion) r$rhr load_filerVrUformatjoinextendrZ)r3r4entriesnamescfg_filerArlprevs rr5r5sl }(**G E"", N8 $ $    e 5==;w'Dxx &&%))M*B*BBB HHN!!hhuT{7';<< HH]33!IIm44 IOII O " "5#3 4 4 4 $K ' ' 1 1 1 1%-J??E$K NN5 ! ! ! !! , ,,rctD]6}|}|s!|cSdS)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_configrsR-**WYY ''))  ''))))) 4rctj|5}d} tjd|}||r|cdddS#t $r'|cY|r|cdddSwxYw#|r|wwxYw#1swxYwYdS)Nrb)modefileobj)ioBytesIOgzipGzipFilereadcloseIOError)blobiobufgzfps r _decomp_gziprs6 D   U =dE:::D99;;              KK                         sFB5)A%B5% B1B2B5BBB22B55B9<B9c tj|}n-#ttf$rt jd|YdSwxYwt |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 TypeErrorrUloggingerrorr)r\rs r_b64dgzrsn %% z "  ;    rr    s&AAc0|tj}d|vr|d}|D]3}|dr|ddd}4|r0|tkrddiStjt |SdS)Nznetwork-config==rprqr)rr%r+r,&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED load_yamlr)cmdlinedata64toks rread_kernel_cmdline_configrs"$$G##==?? . .C~~/00 .3**1-  3??? *-->'&//22 2 4r)N)NN)rrrjrrrr-r* cloudinitr cloudinit.netrrr0rABCMetarrr|rhr$r5rrrrr rrrs  ;;;;;;;;B)3& J J J J JS[ J J J J1 1 1 1 1 ;1 1 1 h66ZZZZzHHH---->$   &r