]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - debian/cloud-tools/hv_set_ifconfig
3 # hv_set_ifconfig <config> -- take the hv_kvp_daemon generated configuration
4 # file and apply it to the Ubuntu configuration.
8 # HWADDR=11:22:33:44:55:66
13 # HWADDR=11:22:33:44:55:66
15 # IPADDR=192.168.99.10
16 # GATEWAY=192.168.99.1
18 # IPADDR2=192.168.99.11
19 # IPV6ADDR=2001:DB8:99::10
21 # IPV6_DEFAULTGW=2001:DB8:99::10
23 # set interfaces in hv_kvp_daemon style
32 if_filename
="/etc/network/interfaces"
34 # Drop our output (XXX?)
35 sys
.stdout
= open(os
.devnull
, 'w')
36 sys
.stderr
= open(os
.devnull
, 'w')
38 # Confirm we can open the network configuration.
40 if_file
=open(if_filename
,"r+")
46 # Usage: hv_set_ifconfig <config>
47 if len(sys
.argv
) != 2 :
51 # Here is the format of the ip configuration file:
54 # DEVICE=interface name
55 # BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
56 # or "none" if no boot-time protocol should be used)
60 # IPADDRx=ipaddry (where y = x + 1)
63 # NETMASKx=netmasky (where y = x + 1)
66 # GATEWAYx=ipaddry (where y = x + 1)
68 # DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
70 # IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
71 # tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
75 kvp
=dict(line
.strip().split("=") for line
in fileinput
.input())
77 # Setting the hwaddress to something azure is not expecting is fatal
79 if not "HWADDR" in kvp
:
82 # Confirm we have a device specified.
83 if not "DEVICE" in kvp
:
88 basename
=kvp
["DEVICE"]
90 # DNS entries will go with the first interface and there can be a max
91 # of three. These will be emitted with the first interface.
93 for count
in (1, 2, 3):
94 key
= "DNS" + str(count
)
99 # IPV4 may either be dhcp or static.
100 if ("DHCP" in kvp
and kvp
["DHCP"] == "yes") or \
101 ("BOOTPROTO" in kvp
and kvp
["BOOTPROTO"] == "dhcp"):
102 autolist
.append(basename
)
103 output
+= ["iface " + basename
+ " inet dhcp"]
106 # Matchup the interface specific lines
108 # No real max for the number of interface + aliases ...
109 # only required is the address (but mate everything up that comes in.
111 # IPv4 -- ensure we sort by numeric suffixes.
112 v4names
= [ int(name
[6:]) for name
in kvp
.keys() if name
.startswith("IPADDR") ]
115 for if_count
in v4names
:
117 which
= str(if_count
)
120 ifname
+= ":" + str(if_count
)
125 if not ifname
in autolist
:
128 output
+= [ "iface " + ifname
+ " inet static" ]
129 output
+= [ "\t" + "address " + kvp
["IPADDR" + which
] ]
130 if "NETMASK" + which
in kvp
:
131 output
+= [ "\tnetmask " + kvp
["NETMASK" + which
] ]
132 if "GATEWAY" + which_gw
in kvp
:
133 output
+= ["\tgateway " + kvp
["GATEWAY" + which_gw
]]
137 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
140 # IPv6 requires a netmask
141 # If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with
142 # echo 0 > /proc/sys/net/ipv6/conf/all/autoconf
143 v6names
= [ int(name
[8:]) for name
in kvp
.keys() if name
.startswith("IPV6ADDR") ]
146 for if6_count
in v6names
:
148 which
= str(if6_count
)
151 ifname
+= ":" + str(if6_count
)
156 if not ifname
in autolist
:
159 if "IPV6NETMASK" + which
in kvp
:
160 output
+= [ "iface " + ifname
+ " inet6 static"]
161 output
+= [ "\taddress " + kvp
["IPV6ADDR" + which
]]
162 output
+= [ "\tnetmask " + kvp
["IPV6NETMASK" + which
]]
163 if "IPV6_DEFAULTGW" + which_gw
in kvp
:
164 output
+= [ "\tgateway " + kvp
["IPV6_DEFAULTGW" + which_gw
] ]
167 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
170 # Mark this new interface for automatic up.
172 output
= ["auto "+" ".join(autolist
)] + output
174 print("===================================")
176 print("===================================")
179 # Time to clean out the existing interface file
182 start_mark
= "# The following stanza(s) added by hv_set_ifconfig"
183 end_mark
= "#End of hv_set_ifconfig stanzas"
185 f
=open(if_filename
,"r")
194 if line
.startswith("auto"):
197 newfile
.extend(stanza
)
201 autoline
=line
.strip().split(" ")
202 for word
in autoline
:
203 if (not word
== basename
) and (not word
.startswith(basename
+":")):
205 newline
= newline
.strip()
206 if not newline
== "auto":
207 newfile
+= [newline
.strip()]
208 elif line
.startswith(("iface","mapping","source")):
210 '''A Stanza can also start with allow- ie allow-hotplug'''
213 newfile
.extend(stanza
)
217 autoline
=line
.strip().split(" ")
218 for word
in autoline
:
219 if (word
== basename
) or (word
.startswith(basename
+":")):
222 stanza
+=[line
.strip()]
223 elif line
.strip() in (start_mark
, end_mark
):
226 newfile
.extend(stanza
)
230 # Deduplicate markers.
231 if line
!= prev_line
:
232 newfile
+= [line
.strip()]
236 stanza
+=[line
.strip()]
239 newfile
+= [line
.strip()]
242 # Include pending stanza if any.
243 if inastanza
and not pitchstanza
:
244 newfile
.extend(stanza
)
250 os
.write(fd
, output
.encode('utf-8'))
252 # Insert the new output at the end and inside the existing markers if found.
254 fd
, path
= tempfile
.mkstemp()
257 emit("\n".join(output
))
262 emit("\n".join(output
))
266 shutil
.copy(path
,if_filename
)
267 os
.chmod(if_filename
,0o644)
269 #print("TMPFILE is at: " + path)
270 #print("Copied file is at: " + if_filename)
273 retcode
= subprocess
.call("ifdown "+basename
, shell
=True)
275 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
277 print("Child returned", retcode
, file=sys
.stderr
)
279 print("Execution failed:", e
, file=sys
.stderr
)
282 retcode
= subprocess
.call("ifup "+basename
, shell
=True)
284 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
286 print("Child returned", retcode
, file=sys
.stderr
)
288 print("Execution failed:", e
, file=sys
.stderr
)