]>
Commit | Line | Data |
---|---|---|
1 | package PVE::LXC::Setup::Debian; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | use PVE::Tools qw($IPV6RE); | |
7 | use PVE::LXC; | |
8 | use PVE::Network; | |
9 | use File::Path; | |
10 | ||
11 | use PVE::LXC::Setup::Base; | |
12 | ||
13 | use base qw(PVE::LXC::Setup::Base); | |
14 | ||
15 | sub new { | |
16 | my ($class, $conf, $rootdir) = @_; | |
17 | ||
18 | my $version = PVE::Tools::file_read_firstline("$rootdir/etc/debian_version"); | |
19 | ||
20 | die "unable to read version info\n" if !defined($version); | |
21 | ||
22 | # translate stretch/sid => 9.0 (used on debian testing repository) | |
23 | $version = 9.0 if $version eq 'stretch/sid'; | |
24 | ||
25 | die "unable to parse version info '$version'\n" | |
26 | if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/; | |
27 | ||
28 | $version = $1; | |
29 | ||
30 | die "unsupported debian version '$version'\n" | |
31 | if !($version >= 4 && $version <= 9); | |
32 | ||
33 | my $self = { conf => $conf, rootdir => $rootdir, version => $version }; | |
34 | ||
35 | $conf->{ostype} = "debian"; | |
36 | ||
37 | return bless $self, $class; | |
38 | } | |
39 | ||
40 | sub setup_init { | |
41 | my ($self, $conf) = @_; | |
42 | ||
43 | my $systemd = $self->ct_readlink('/sbin/init'); | |
44 | if (defined($systemd) && $systemd =~ m@/systemd$@) { | |
45 | $self->setup_container_getty_service($conf); | |
46 | } | |
47 | ||
48 | my $filename = "/etc/inittab"; | |
49 | return if !$self->ct_file_exists($filename); | |
50 | ||
51 | my $ttycount = PVE::LXC::Config->get_tty_count($conf); | |
52 | my $inittab = $self->ct_file_get_contents($filename); | |
53 | ||
54 | my @lines = grep { | |
55 | # remove getty lines | |
56 | !/^\s*\d+:\d*:[^:]*:.*getty/ && | |
57 | # remove power lines | |
58 | !/^\s*p[fno0]:/ | |
59 | } split(/\n/, $inittab); | |
60 | ||
61 | $inittab = join("\n", @lines) . "\n"; | |
62 | ||
63 | $inittab .= "p0::powerfail:/sbin/init 0\n"; | |
64 | ||
65 | my $version = $self->{version}; | |
66 | for (my $id = 1; $id <= $ttycount; $id++) { | |
67 | next if $id == 7; # reserved for X11 | |
68 | my $levels = ($id == 1) ? '2345' : '23'; | |
69 | if ($version < 7) { | |
70 | $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n"; | |
71 | } else { | |
72 | $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n"; | |
73 | } | |
74 | } | |
75 | ||
76 | $self->ct_file_set_contents($filename, $inittab); | |
77 | } | |
78 | ||
79 | sub remove_gateway_scripts { | |
80 | my ($attr) = @_; | |
81 | my $length = scalar(@$attr); | |
82 | ||
83 | my $found_section = 0; | |
84 | my $keep = 1; | |
85 | @$attr = grep { | |
86 | if ($_ eq '# --- BEGIN PVE ---') { | |
87 | $found_section = 1; | |
88 | $keep = 0; | |
89 | 0; # remove this line | |
90 | } elsif ($_ eq '# --- END PVE ---') { | |
91 | $found_section = 1; | |
92 | $keep = 1; | |
93 | 0; # remove this line | |
94 | } else { | |
95 | $keep; | |
96 | } | |
97 | } @$attr; | |
98 | ||
99 | return if $found_section; | |
100 | # XXX: To deal with existing setups we perform two types of removal for | |
101 | # now. Newly started containers have their routing sections marked with | |
102 | # begin/end comments. For older containers we perform a strict matching on | |
103 | # the routing rules we added. We can probably remove this part at some point | |
104 | # when it is unlikely that old debian setups are still around. | |
105 | ||
106 | for (my $i = 0; $i < $length-3; ++$i) { | |
107 | next if $attr->[$i+0] !~ m@^\s*post-up\s+ip\s+route\s+add\s+(\S+)\s+dev\s+(\S+)$@; | |
108 | my ($ip, $dev) = ($1, $2); | |
109 | if ($attr->[$i+1] =~ m@^\s*post-up\s+ip\s+route\s+add\s+default\s+via\s+(\S+)\s+dev\s+(\S+)$@ && | |
110 | ($ip eq $1 && $dev eq $2) && | |
111 | $attr->[$i+2] =~ m@^\s*pre-down\s+ip\s+route\s+del\s+default\s+via\s+(\S+)\s+dev\s+(\S+)$@ && | |
112 | ($ip eq $1 && $dev eq $2) && | |
113 | $attr->[$i+3] =~ m@^\s*pre-down\s+ip\s+route\s+del\s+(\S+)\s+dev\s+(\S+)$@ && | |
114 | ($ip eq $1 && $dev eq $2)) | |
115 | { | |
116 | splice @$attr, $i, 4; | |
117 | $length -= 4; | |
118 | --$i; | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | sub make_gateway_scripts { | |
124 | my ($ifname, $gw) = @_; | |
125 | return <<"SCRIPTS"; | |
126 | # --- BEGIN PVE --- | |
127 | \tpost-up ip route add $gw dev $ifname | |
128 | \tpost-up ip route add default via $gw dev $ifname | |
129 | \tpre-down ip route del default via $gw dev $ifname | |
130 | \tpre-down ip route del $gw dev $ifname | |
131 | # --- END PVE --- | |
132 | SCRIPTS | |
133 | } | |
134 | ||
135 | sub setup_network { | |
136 | my ($self, $conf) = @_; | |
137 | ||
138 | my $networks = {}; | |
139 | foreach my $k (keys %$conf) { | |
140 | next if $k !~ m/^net(\d+)$/; | |
141 | my $ind = $1; | |
142 | my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k}); | |
143 | if ($d->{name}) { | |
144 | my $net = {}; | |
145 | my $cidr; | |
146 | if (defined($d->{ip})) { | |
147 | if ($d->{ip} =~ /^(?:dhcp|manual)$/) { | |
148 | $net->{address} = $d->{ip}; | |
149 | } else { | |
150 | my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip}); | |
151 | $net->{address} = $ipinfo->{address}; | |
152 | $net->{netmask} = $ipinfo->{netmask}; | |
153 | $cidr = $d->{ip}; | |
154 | } | |
155 | } | |
156 | if (defined($d->{'gw'})) { | |
157 | $net->{gateway} = $d->{'gw'}; | |
158 | if (defined($cidr) && !PVE::Network::is_ip_in_cidr($d->{gw}, $cidr, 4)) { | |
159 | # gateway is not reachable, need an extra route | |
160 | $net->{needsroute} = 1; | |
161 | } | |
162 | } | |
163 | $cidr = undef; | |
164 | if (defined($d->{ip6})) { | |
165 | if ($d->{ip6} =~ /^(?:auto|dhcp|manual)$/) { | |
166 | $net->{address6} = $d->{ip6}; | |
167 | } elsif ($d->{ip6} !~ /^($IPV6RE)\/(\d+)$/) { | |
168 | die "unable to parse ipv6 address/prefix\n"; | |
169 | } else { | |
170 | $net->{address6} = $1; | |
171 | $net->{netmask6} = $2; | |
172 | $cidr = $d->{ip6}; | |
173 | } | |
174 | } | |
175 | if (defined($d->{'gw6'})) { | |
176 | $net->{gateway6} = $d->{'gw6'}; | |
177 | if (defined($cidr) && !PVE::Network::is_ip_in_cidr($d->{gw6}, $cidr, 6) && | |
178 | !PVE::Network::is_ip_in_cidr($d->{gw6}, 'fe80::/10', 6)) { | |
179 | # gateway is not reachable, need an extra route | |
180 | $net->{needsroute6} = 1; | |
181 | } | |
182 | } | |
183 | $networks->{$d->{name}} = $net if keys %$net; | |
184 | } | |
185 | } | |
186 | ||
187 | return if !scalar(keys %$networks); | |
188 | ||
189 | my $filename = "/etc/network/interfaces"; | |
190 | my $interfaces = ""; | |
191 | ||
192 | my $section; | |
193 | ||
194 | my $done_auto = {}; | |
195 | my $done_v4_hash = {}; | |
196 | my $done_v6_hash = {}; | |
197 | ||
198 | my $print_section = sub { | |
199 | return if !$section; | |
200 | ||
201 | my $ifname = $section->{ifname}; | |
202 | my $net = $networks->{$ifname}; | |
203 | ||
204 | if (!$done_auto->{$ifname}) { | |
205 | $interfaces .= "auto $ifname\n"; | |
206 | $done_auto->{$ifname} = 1; | |
207 | } | |
208 | ||
209 | if ($section->{type} eq 'ipv4') { | |
210 | $done_v4_hash->{$ifname} = 1; | |
211 | ||
212 | if (!defined($net->{address})) { | |
213 | # no address => no iface line | |
214 | } elsif ($net->{address} =~ /^(dhcp|manual)$/) { | |
215 | $interfaces .= "iface $ifname inet $1\n\n"; | |
216 | } else { | |
217 | $interfaces .= "iface $ifname inet static\n"; | |
218 | $interfaces .= "\taddress $net->{address}\n" if defined($net->{address}); | |
219 | $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask}); | |
220 | ||
221 | remove_gateway_scripts($section->{attr}); | |
222 | if (defined(my $gw = $net->{gateway})) { | |
223 | if ($net->{needsroute}) { | |
224 | $interfaces .= make_gateway_scripts($ifname, $gw); | |
225 | } else { | |
226 | $interfaces .= "\tgateway $gw\n"; | |
227 | } | |
228 | } | |
229 | foreach my $attr (@{$section->{attr}}) { | |
230 | $interfaces .= "\t$attr\n"; | |
231 | } | |
232 | $interfaces .= "\n"; | |
233 | } | |
234 | } elsif ($section->{type} eq 'ipv6') { | |
235 | $done_v6_hash->{$ifname} = 1; | |
236 | ||
237 | if (!defined($net->{address6})) { | |
238 | # no address => no iface line | |
239 | } elsif ($net->{address6} =~ /^(auto|dhcp|manual)$/) { | |
240 | $interfaces .= "iface $ifname inet6 $1\n\n"; | |
241 | } else { | |
242 | $interfaces .= "iface $ifname inet6 static\n"; | |
243 | $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6}); | |
244 | $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6}); | |
245 | remove_gateway_scripts($section->{attr}); | |
246 | if (defined(my $gw = $net->{gateway6})) { | |
247 | if ($net->{needsroute6}) { | |
248 | $interfaces .= make_gateway_scripts($ifname, $gw); | |
249 | } else { | |
250 | $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6}); | |
251 | } | |
252 | } | |
253 | foreach my $attr (@{$section->{attr}}) { | |
254 | $interfaces .= "\t$attr\n"; | |
255 | } | |
256 | $interfaces .= "\n"; | |
257 | } | |
258 | } else { | |
259 | die "unknown section type '$section->{type}'"; | |
260 | } | |
261 | ||
262 | $section = undef; | |
263 | }; | |
264 | ||
265 | if (my $fh = $self->ct_open_file_read($filename)) { | |
266 | while (defined (my $line = <$fh>)) { | |
267 | chomp $line; | |
268 | if ($line =~ m/^# --- (?:BEGIN|END) PVE ---/) { | |
269 | # Include markers in the attribute section so | |
270 | # remove_gateway_scripts() can find them. | |
271 | push @{$section->{attr}}, $line if $section; | |
272 | next; | |
273 | } | |
274 | if ($line =~ m/^#/) { | |
275 | $interfaces .= "$line\n"; | |
276 | next; | |
277 | } | |
278 | if ($line =~ m/^\s*$/) { | |
279 | if ($section) { | |
280 | &$print_section(); | |
281 | } else { | |
282 | $interfaces .= "$line\n"; | |
283 | } | |
284 | next; | |
285 | } | |
286 | if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) { | |
287 | my $ifname = $1; | |
288 | &$print_section(); # print previous section | |
289 | if (!$networks->{$ifname}) { | |
290 | $interfaces .= "$line\n"; | |
291 | next; | |
292 | } | |
293 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; | |
294 | next; | |
295 | } | |
296 | if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) { | |
297 | my $ifname = $1; | |
298 | &$print_section(); # print previous section | |
299 | if (!$networks->{$ifname}) { | |
300 | $interfaces .= "$line\n"; | |
301 | next; | |
302 | } | |
303 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; | |
304 | next; | |
305 | } | |
306 | # Handle 'auto' | |
307 | if ($line =~ m/^\s*auto\s*(.*)$/) { | |
308 | foreach my $iface (split(/\s+/, $1)) { | |
309 | $done_auto->{$iface} = 1; | |
310 | } | |
311 | &$print_section(); | |
312 | $interfaces .= "$line\n"; | |
313 | next; | |
314 | } | |
315 | # Handle other section delimiters: | |
316 | if ($line =~ m/^\s*(?:mapping\s | |
317 | |allow- | |
318 | |source\s | |
319 | |source-directory\s | |
320 | )/x) { | |
321 | &$print_section(); | |
322 | $interfaces .= "$line\n"; | |
323 | next; | |
324 | } | |
325 | if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) { | |
326 | my ($adata, $aname) = ($1, $2); | |
327 | if ($aname eq 'address' || $aname eq 'netmask' || | |
328 | $aname eq 'gateway' || $aname eq 'broadcast') { | |
329 | # skip | |
330 | } else { | |
331 | push @{$section->{attr}}, $adata; | |
332 | } | |
333 | next; | |
334 | } | |
335 | ||
336 | $interfaces .= "$line\n"; | |
337 | } | |
338 | &$print_section(); | |
339 | } | |
340 | ||
341 | my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/); | |
342 | foreach my $ifname (sort keys %$networks) { | |
343 | my $net = $networks->{$ifname}; | |
344 | ||
345 | if (!$done_v4_hash->{$ifname} && defined($net->{address})) { | |
346 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; | |
347 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; | |
348 | &$print_section(); | |
349 | } | |
350 | if (!$done_v6_hash->{$ifname} && defined($net->{address6})) { | |
351 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; | |
352 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; | |
353 | &$print_section(); | |
354 | } | |
355 | } | |
356 | ||
357 | # older templates (< Debian 8) do not configure the loopback interface | |
358 | # if not explicitly told to do so | |
359 | if (!$done_auto->{lo}) { | |
360 | $interfaces = "auto lo\niface lo inet loopback\n" . | |
361 | "iface lo inet6 loopback\n\n" . | |
362 | $interfaces; | |
363 | } | |
364 | ||
365 | $self->ct_file_set_contents($filename, $interfaces); | |
366 | } | |
367 | ||
368 | 1; |