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