]>
Commit | Line | Data |
---|---|---|
7af97ad5 | 1 | package PVE::LXC::Setup::Debian; |
1c7f4f65 DM |
2 | |
3 | use strict; | |
4 | use warnings; | |
55fa4e09 | 5 | use Data::Dumper; |
a1b1a247 | 6 | use PVE::Tools qw($IPV6RE); |
55fa4e09 | 7 | use PVE::LXC; |
b206c519 | 8 | use PVE::Network; |
55fa4e09 | 9 | use File::Path; |
1c7f4f65 | 10 | |
7af97ad5 | 11 | use PVE::LXC::Setup::Base; |
1c7f4f65 | 12 | |
7af97ad5 | 13 | use base qw(PVE::LXC::Setup::Base); |
1c7f4f65 | 14 | |
633a7bd8 | 15 | sub new { |
5b4657d0 | 16 | my ($class, $conf, $rootdir) = @_; |
633a7bd8 | 17 | |
5b4657d0 | 18 | my $version = PVE::Tools::file_read_firstline("$rootdir/etc/debian_version"); |
633a7bd8 DM |
19 | |
20 | die "unable to read version info\n" if !defined($version); | |
adc8f577 | 21 | |
9a3bd509 DM |
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" | |
adc8f577 DM |
26 | if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/; |
27 | ||
28 | $version = $1; | |
29 | ||
1c0a5ac7 | 30 | die "unsupported debian version '$version'\n" |
9a3bd509 | 31 | if !($version >= 4 && $version <= 9); |
633a7bd8 | 32 | |
5b4657d0 DM |
33 | my $self = { conf => $conf, rootdir => $rootdir, version => $version }; |
34 | ||
27916659 | 35 | $conf->{ostype} = "debian"; |
633a7bd8 DM |
36 | |
37 | return bless $self, $class; | |
38 | } | |
39 | ||
d66768a2 | 40 | sub setup_init { |
633a7bd8 | 41 | my ($self, $conf) = @_; |
d66768a2 | 42 | |
b6876f11 DM |
43 | my $systemd = $self->ct_readlink('/sbin/init'); |
44 | if (defined($systemd) && $systemd =~ m@/systemd$@) { | |
45 | $self->setup_container_getty_service(1); | |
46 | } | |
47 | ||
2063d380 | 48 | my $filename = "/etc/inittab"; |
fe8a16b8 | 49 | return if !$self->ct_file_exists($filename); |
d66768a2 | 50 | |
1b4cf758 | 51 | my $ttycount = PVE::LXC::Config->get_tty_count($conf); |
fe8a16b8 WB |
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}; | |
39630c0d DM |
66 | for (my $id = 1; $id <= $ttycount; $id++) { |
67 | next if $id == 7; # reserved for X11 | |
68 | my $levels = ($id == 1) ? '2345' : '23'; | |
fe8a16b8 WB |
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"; | |
d66768a2 | 73 | } |
d66768a2 | 74 | } |
fe8a16b8 WB |
75 | |
76 | $self->ct_file_set_contents($filename, $inittab); | |
d66768a2 DM |
77 | } |
78 | ||
b206c519 WB |
79 | sub remove_gateway_scripts { |
80 | my ($attr) = @_; | |
81 | my $length = scalar(@$attr); | |
82 | for (my $i = 0; $i < $length; ++$i) { | |
83 | my $a = $attr->[$i]; | |
84 | if ($a =~ m@^\s*post-up\s+.*route.*add.*default.*(?:gw|via)\s+(\S+)@) { | |
85 | my $gw = $1; | |
86 | if ($i > 0 && $attr->[$i-1] =~ m@^\s*post-up\s+.*route.*add.*\Q$1\E@) { | |
87 | --$i; | |
88 | splice @$attr, $i, 2; | |
89 | $length -= 2; | |
90 | } else { | |
91 | splice @$attr, $i, 1; | |
92 | $length -= 1; | |
93 | } | |
94 | --$i; | |
95 | next; | |
96 | } | |
97 | if ($a =~ m@^\s*pre-down\s+.*route.*del.*default.*(?:gw|via)\s+(\S+)@) { | |
98 | my $gw = $1; | |
99 | if ($attr->[$i+1] =~ m@^\s*pre-down\s+.*route.*del.*\Q$1\E@) { | |
100 | splice @$attr, $i, 2; | |
101 | $length -= 2; | |
102 | } else { | |
103 | splice @$attr, $i, 1; | |
104 | $length -= 1; | |
105 | } | |
106 | --$i; | |
107 | next; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | sub make_gateway_scripts { | |
113 | my ($ifname, $gw) = @_; | |
114 | return <<"SCRIPTS"; | |
115 | \tpost-up ip route add $gw dev $ifname | |
116 | \tpost-up ip route add default via $gw | |
117 | \tpre-down ip route del default via $gw | |
118 | \tpre-down ip route del $gw dev $ifname | |
119 | SCRIPTS | |
120 | } | |
121 | ||
55fa4e09 | 122 | sub setup_network { |
633a7bd8 | 123 | my ($self, $conf) = @_; |
55fa4e09 | 124 | |
55fa4e09 DM |
125 | my $networks = {}; |
126 | foreach my $k (keys %$conf) { | |
127 | next if $k !~ m/^net(\d+)$/; | |
93285df8 | 128 | my $ind = $1; |
1b4cf758 | 129 | my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k}); |
55fa4e09 DM |
130 | if ($d->{name}) { |
131 | my $net = {}; | |
b206c519 | 132 | my $cidr; |
93285df8 | 133 | if (defined($d->{ip})) { |
ecf84da0 | 134 | if ($d->{ip} =~ /^(?:dhcp|manual)$/) { |
0178cffa WB |
135 | $net->{address} = $d->{ip}; |
136 | } else { | |
137 | my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip}); | |
138 | $net->{address} = $ipinfo->{address}; | |
139 | $net->{netmask} = $ipinfo->{netmask}; | |
b206c519 | 140 | $cidr = $d->{ip}; |
0178cffa | 141 | } |
55fa4e09 | 142 | } |
93285df8 | 143 | if (defined($d->{'gw'})) { |
a1b1a247 | 144 | $net->{gateway} = $d->{'gw'}; |
b206c519 WB |
145 | if (defined($cidr) && !PVE::Network::is_ip_in_cidr($d->{gw}, $cidr, 4)) { |
146 | # gateway is not reachable, need an extra route | |
147 | $net->{needsroute} = 1; | |
148 | } | |
55fa4e09 | 149 | } |
b206c519 | 150 | $cidr = undef; |
93285df8 | 151 | if (defined($d->{ip6})) { |
c6b8740b | 152 | if ($d->{ip6} =~ /^(?:auto|dhcp|manual)$/) { |
0178cffa WB |
153 | $net->{address6} = $d->{ip6}; |
154 | } elsif ($d->{ip6} !~ /^($IPV6RE)\/(\d+)$/) { | |
a1b1a247 | 155 | die "unable to parse ipv6 address/prefix\n"; |
0178cffa WB |
156 | } else { |
157 | $net->{address6} = $1; | |
158 | $net->{netmask6} = $2; | |
b206c519 | 159 | $cidr = $d->{ip6}; |
a1b1a247 | 160 | } |
a1b1a247 WB |
161 | } |
162 | if (defined($d->{'gw6'})) { | |
163 | $net->{gateway6} = $d->{'gw6'}; | |
d13fd23a WB |
164 | if (defined($cidr) && !PVE::Network::is_ip_in_cidr($d->{gw6}, $cidr, 6) && |
165 | !PVE::Network::is_ip_in_cidr($d->{gw6}, 'fe80::/10', 6)) { | |
b206c519 WB |
166 | # gateway is not reachable, need an extra route |
167 | $net->{needsroute6} = 1; | |
168 | } | |
55fa4e09 | 169 | } |
0178cffa | 170 | $networks->{$d->{name}} = $net if keys %$net; |
55fa4e09 DM |
171 | } |
172 | } | |
173 | ||
f44d23a5 | 174 | return if !scalar(keys %$networks); |
55fa4e09 | 175 | |
2063d380 | 176 | my $filename = "/etc/network/interfaces"; |
55fa4e09 DM |
177 | my $interfaces = ""; |
178 | ||
179 | my $section; | |
180 | ||
56be201e | 181 | my $done_auto = {}; |
55fa4e09 DM |
182 | my $done_v4_hash = {}; |
183 | my $done_v6_hash = {}; | |
1c0a5ac7 | 184 | |
55fa4e09 | 185 | my $print_section = sub { |
55fa4e09 DM |
186 | return if !$section; |
187 | ||
6fd46881 WB |
188 | my $ifname = $section->{ifname}; |
189 | my $net = $networks->{$ifname}; | |
55fa4e09 | 190 | |
6b0a0043 | 191 | if (!$done_auto->{$ifname}) { |
6fd46881 WB |
192 | $interfaces .= "auto $ifname\n"; |
193 | $done_auto->{$ifname} = 1; | |
56be201e WB |
194 | } |
195 | ||
55fa4e09 | 196 | if ($section->{type} eq 'ipv4') { |
6fd46881 | 197 | $done_v4_hash->{$ifname} = 1; |
55fa4e09 | 198 | |
ecf84da0 | 199 | if ($net->{address} =~ /^(dhcp|manual)$/) { |
6fd46881 | 200 | $interfaces .= "iface $ifname inet $1\n"; |
fcaca113 | 201 | } else { |
6fd46881 | 202 | $interfaces .= "iface $ifname inet static\n"; |
a1b1a247 WB |
203 | $interfaces .= "\taddress $net->{address}\n" if defined($net->{address}); |
204 | $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask}); | |
b206c519 WB |
205 | if (defined(my $gw = $net->{gateway})) { |
206 | remove_gateway_scripts($section->{attr}); | |
207 | if ($net->{needsroute}) { | |
208 | $interfaces .= make_gateway_scripts($ifname, $gw); | |
209 | } else { | |
210 | $interfaces .= "\tgateway $gw\n"; | |
211 | } | |
212 | } | |
55fa4e09 DM |
213 | foreach my $attr (@{$section->{attr}}) { |
214 | $interfaces .= "\t$attr\n"; | |
215 | } | |
55fa4e09 | 216 | } |
1c0a5ac7 | 217 | |
55fa4e09 | 218 | $interfaces .= "\n"; |
1c0a5ac7 | 219 | |
55fa4e09 | 220 | } elsif ($section->{type} eq 'ipv6') { |
6fd46881 | 221 | $done_v6_hash->{$ifname} = 1; |
1c0a5ac7 | 222 | |
c6b8740b | 223 | if ($net->{address6} =~ /^(auto|dhcp|manual)$/) { |
6fd46881 | 224 | $interfaces .= "iface $ifname inet6 $1\n"; |
fcaca113 | 225 | } else { |
6fd46881 | 226 | $interfaces .= "iface $ifname inet6 static\n"; |
a1b1a247 WB |
227 | $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6}); |
228 | $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6}); | |
b206c519 WB |
229 | if (defined(my $gw = $net->{gateway6})) { |
230 | remove_gateway_scripts($section->{attr}); | |
231 | if ($net->{needsroute6}) { | |
232 | $interfaces .= make_gateway_scripts($ifname, $gw); | |
233 | } else { | |
234 | $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6}); | |
235 | } | |
236 | } | |
55fa4e09 DM |
237 | foreach my $attr (@{$section->{attr}}) { |
238 | $interfaces .= "\t$attr\n"; | |
239 | } | |
240 | } | |
1c0a5ac7 WB |
241 | |
242 | $interfaces .= "\n"; | |
55fa4e09 DM |
243 | } else { |
244 | die "unknown section type '$section->{type}'"; | |
245 | } | |
246 | ||
247 | $section = undef; | |
248 | }; | |
1c0a5ac7 | 249 | |
f08b2779 | 250 | if (my $fh = $self->ct_open_file_read($filename)) { |
55fa4e09 DM |
251 | while (defined (my $line = <$fh>)) { |
252 | chomp $line; | |
253 | if ($line =~ m/^#/) { | |
254 | $interfaces .= "$line\n"; | |
255 | next; | |
256 | } | |
257 | if ($line =~ m/^\s*$/) { | |
258 | if ($section) { | |
259 | &$print_section(); | |
260 | } else { | |
261 | $interfaces .= "$line\n"; | |
262 | } | |
263 | next; | |
264 | } | |
0178cffa | 265 | if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) { |
55fa4e09 | 266 | my $ifname = $1; |
0178cffa | 267 | &$print_section(); # print previous section |
55fa4e09 DM |
268 | if (!$networks->{$ifname}) { |
269 | $interfaces .= "$line\n"; | |
270 | next; | |
271 | } | |
272 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; | |
273 | next; | |
274 | } | |
0178cffa | 275 | if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) { |
55fa4e09 | 276 | my $ifname = $1; |
0178cffa | 277 | &$print_section(); # print previous section |
55fa4e09 DM |
278 | if (!$networks->{$ifname}) { |
279 | $interfaces .= "$line\n"; | |
280 | next; | |
281 | } | |
282 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; | |
283 | next; | |
284 | } | |
56be201e WB |
285 | # Handle 'auto' |
286 | if ($line =~ m/^\s*auto\s*(.*)$/) { | |
287 | foreach my $iface (split(/\s+/, $1)) { | |
288 | $done_auto->{$iface} = 1; | |
289 | } | |
290 | &$print_section(); | |
291 | $interfaces .= "$line\n"; | |
292 | next; | |
293 | } | |
0178cffa WB |
294 | # Handle other section delimiters: |
295 | if ($line =~ m/^\s*(?:mapping\s | |
0178cffa WB |
296 | |allow- |
297 | |source\s | |
298 | |source-directory\s | |
299 | )/x) { | |
300 | &$print_section(); | |
301 | $interfaces .= "$line\n"; | |
302 | next; | |
303 | } | |
55fa4e09 DM |
304 | if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) { |
305 | my ($adata, $aname) = ($1, $2); | |
306 | if ($aname eq 'address' || $aname eq 'netmask' || | |
307 | $aname eq 'gateway' || $aname eq 'broadcast') { | |
308 | # skip | |
309 | } else { | |
1c0a5ac7 | 310 | push @{$section->{attr}}, $adata; |
55fa4e09 DM |
311 | } |
312 | next; | |
313 | } | |
1c0a5ac7 WB |
314 | |
315 | $interfaces .= "$line\n"; | |
55fa4e09 DM |
316 | } |
317 | &$print_section(); | |
55fa4e09 DM |
318 | } |
319 | ||
e90673a8 | 320 | my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/); |
55fa4e09 DM |
321 | foreach my $ifname (sort keys %$networks) { |
322 | my $net = $networks->{$ifname}; | |
1c0a5ac7 | 323 | |
fcaca113 | 324 | if (!$done_v4_hash->{$ifname} && defined($net->{address})) { |
1c0a5ac7 | 325 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; |
55fa4e09 | 326 | $section = { type => 'ipv4', ifname => $ifname, attr => []}; |
6b0a0043 | 327 | &$print_section(); |
55fa4e09 | 328 | } |
a1b1a247 | 329 | if (!$done_v6_hash->{$ifname} && defined($net->{address6})) { |
1c0a5ac7 | 330 | if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; }; |
55fa4e09 | 331 | $section = { type => 'ipv6', ifname => $ifname, attr => []}; |
6b0a0043 | 332 | &$print_section(); |
55fa4e09 DM |
333 | } |
334 | } | |
1c0a5ac7 | 335 | |
2063d380 | 336 | $self->ct_file_set_contents($filename, $interfaces); |
55fa4e09 | 337 | } |
1c7f4f65 DM |
338 | |
339 | 1; |