3 \me-$@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z dZ dZejeZGdddejdZGd 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 initramfses)returncCsdS)zAIs this initramfs config source applicable to the current system?N)selfrr/usr/lib/python3.6/cmdline.py is_applicablesz*InitramfsNetworkConfigSource.is_applicablecCsdS)z;Render a v1 network config from the initramfs configurationNr)r rrr render_config"sz*InitramfsNetworkConfigSource.render_configN) __name__ __module__ __qualname____doc__abcabstractmethodboolr dictr rrrr 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.) NcCst||_||_||_|jdkr$t|_|jdkr8tj|_|jdkrpi|_x&tD]}t|d}|rP||j|<qPWdS)Naddress)_files _mac_addrs_cmdline_get_klibc_net_cfg_filesr get_cmdlinerr)r rrrkZmac_addrrrr __init__/s      z!KlibcNetworkConfigSource.__init__)rcCs>|jr:x"tj|jD]}|jdrdSqWtjjtr:dSdS)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 ip=ip6=TF)rr ) rshlexsplitr startswithospathexists_OPEN_ISCSI_INTERFACE_FILE)r itemrrr r As   z&KlibcNetworkConfigSource.is_applicablecCst|j|jdS)N)files mac_addrs)config_from_klibc_net_cfgrr)r rrr r Tsz&KlibcNetworkConfigSource.render_config)NNN) r rrrrrr rr rrrr r's rcCs|dkr i}tj|}yd|kr(|dn|d}Wn,tk r^}ztd|WYdd}~XnX|jd|jd}|s|jdrd}nd }|d!krtd |d |gd }||kr|||d<x,d"D]"}|d|krq|j|d|}|d krd}|dd} |dkr||d| d<x0d#D](} || |kr"||| | | j<q"Wg} xDd$D]<} |j|| } | rXt| jdrX| j||| qXW| r| | d<|jd}|rd|kr|j d| d<n |j | d<|d j| qW||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 NZDEVICEZDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataZPROTOZ IPV6PROTOfilenamedhcpnonedhcp6zUnexpected value for PROTO: %sZphysical)typenamesubnets mac_addressIPV4IPV6ZADDRZstaticZmanual)r0ZcontrolrNETMASK BROADCASTGATEWAYDNS0DNS1z:.0Zdns_nameserversZ DOMAINSEARCH,Z dns_searchr2)r.r-r/)r4r5)r6r7r8)r9r:) rZload_shell_contentKeyError ValueErrorgetlowerlenstripappendr")Zcontentr*datar1eprotoZifaceZpreZ cur_protoZsubnetkeyZdnsZnskeynssearchrrr _klibc_to_config_entry^sZ             rIcCstjdtjdS)Nz/run/net-*.confz/run/net6-*.conf)globrrrr rsrcCs|dkrt}g}i}x|D]}ttj||d\}}||kr||d}|jd|jdkrtdj|dj||d|jd|jdd|dj|d||dj |q|g|d ||<|j |qW|d d S) N)r*entryr3zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r))r1r)oldnewr2)r)rK)configversion) rrIrZ load_filer>r=formatjoinextendrB)r)r*ZentriesnamesZcfg_filer1rKprevrrr r+s*  r+cCs(x"tD]}|}|jsq|jSWdS)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_sourcerrr read_initramfs_configs   rXcCs^tj|J}d}z0ytjd|d}|jStk r<|SXWd|rN|jXWdQRXdS)Nrb)modeZfileobj)ioBytesIOgzipZGzipFilereadIOErrorclose)blobZiobufZgzfprrr _decomp_gzips  rbc Cs<ytj|}Wn$ttfk r2tjd|dSXt|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.)base64Z b64decode TypeErrorr=LOGerrorrb)rCrarrr _b64dgzsrhcCsn|dkrtj}d|krjd}x*|jD]}|jdr&|jddd}q&W|rj|tkr\ddiStjt|SdS)Nznetwork-config==rOrPr)rrr"r#&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLEDZ load_yamlrh)ZcmdlineZdata64tokrrr read_kernel_cmdline_configs rl)N)NN)N)rrdrJr]r[Zloggingr$r!Z cloudinitrZ cloudinit.netrrr'rjZ getLoggerr rfABCMetarrrWrIrr+rXrbrhrlrrrr s,   4 ]