]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Debian.pm
76d336a12e69c3ad70c03bd4f0e37bc3105e3bbb
1 package PVE
::LXC
::Setup
::Debian
;
6 use PVE
::Tools
qw($IPV6RE);
11 use PVE
::LXC
::Setup
::Base
;
13 use base
qw(PVE::LXC::Setup::Base);
17 my ($class, $conf, $rootdir) = @_;
19 my $version = PVE
::Tools
::file_read_firstline
("$rootdir/etc/debian_version");
21 die "unable to read version info\n" if !defined($version);
23 # translate testing version and os-release incompat derivates names
33 $version = $version_map->{$version} if exists($version_map->{$version});
35 die "unable to parse version info '$version'\n"
36 if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/;
40 die "unsupported debian version '$version'\n" if !($version >= 4 && $version <= 13);
42 my $self = { conf
=> $conf, rootdir
=> $rootdir, version
=> $version };
44 $conf->{ostype
} = "debian";
46 return bless $self, $class;
49 # Debian doesn't support the /dev/lxc/ subdirectory.
55 my ($self, $conf) = @_;
57 my $systemd = $self->ct_readlink('/sbin/init');
58 if (defined($systemd) && $systemd =~ m
@/systemd$@) {
59 $self->setup_container_getty_service($conf);
62 my $filename = "/etc/inittab";
63 return if !$self->ct_file_exists($filename);
65 my $ttycount = PVE
::LXC
::Config-
>get_tty_count($conf);
66 my $inittab = $self->ct_file_get_contents($filename);
70 !/^\s*\d+:\d*:[^:]*:.*getty/ &&
73 } split(/\n/, $inittab);
75 $inittab = join("\n", @lines) . "\n";
77 $inittab .= "p0::powerfail:/sbin/init 0\n";
79 my $version = $self->{version
};
80 for (my $id = 1; $id <= $ttycount; $id++) {
81 next if $id == 7; # reserved for X11
82 my $levels = ($id == 1) ?
'2345' : '23';
84 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
86 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
90 $self->ct_file_set_contents($filename, $inittab);
93 sub remove_gateway_scripts
{
95 my $length = scalar(@$attr);
97 my $found_section = 0;
100 if ($_ eq '# --- BEGIN PVE ---') {
103 0; # remove this line
104 } elsif ($_ eq '# --- END PVE ---') {
107 0; # remove this line
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.
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))
130 splice @$attr, $i, 4;
137 sub make_gateway_scripts
{
138 my ($ifname, $gw) = @_;
141 \tpost-up ip route add $gw dev $ifname
142 \tpost-up ip route add default via $gw dev $ifname
143 \tpre-down ip route del default via $gw dev $ifname
144 \tpre-down ip route del $gw dev $ifname
149 my sub at_least
: prototype($$$) {
150 my ($str, $want_maj, $want_min) = @_;
151 return if !defined($str) || !defined($want_maj);
153 my ($maj, $min) = $str =~ /^(\d+)(?:\.(\d+))?/;
154 return if !defined($maj);
156 return $want_maj < $maj || $want_maj == $maj && (
157 (!defined($min) && $want_min == 0) || (defined($min) && $want_min <= $min)
161 # NOTE: this is re-used by Alpine Linux, please have that in mind when changing things.
163 my ($self, $conf) = @_;
166 foreach my $k (keys %$conf) {
167 next if $k !~ m/^net(\d+)$/;
169 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
173 if (defined($d->{ip
})) {
174 if ($d->{ip
} =~ /^(?:dhcp|manual)$/) {
175 $net->{address
} = $d->{ip
};
177 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
178 $net->{address
} = $ipinfo->{address
};
179 $net->{netmask
} = $ipinfo->{netmask
};
180 $net->{cidr
} = $d->{ip
};
184 if (defined($d->{'gw'})) {
185 $net->{gateway
} = $d->{'gw'};
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;
192 if (defined($d->{ip6
})) {
193 if ($d->{ip6
} =~ /^(?:auto|dhcp|manual)$/) {
194 $net->{address6
} = $d->{ip6
};
195 } elsif ($d->{ip6
} !~ /^($IPV6RE)\/(\d
+)$/) {
196 die "unable to parse ipv6 address/prefix\n";
198 $net->{address6
} = $1;
199 $net->{netmask6
} = $2;
200 $net->{cidr6
} = $d->{ip6
};
204 if (defined($d->{'gw6'})) {
205 $net->{gateway6
} = $d->{'gw6'};
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)) {
208 # gateway is not reachable, need an extra route
209 $net->{needsroute6
} = 1;
212 $networks->{$d->{name
}} = $net if keys %$net;
216 return if !scalar(keys %$networks);
218 my $filename = "/etc/network/interfaces";
224 my $done_v4_hash = {};
225 my $done_v6_hash = {};
227 my ($os, $version) = ($conf->{ostype
}, $self->{version
});
228 my $print_section = sub {
231 my $ifname = $section->{ifname
};
232 my $net = $networks->{$ifname};
234 if (!$done_auto->{$ifname}) {
235 $interfaces .= "auto $ifname\n";
236 $done_auto->{$ifname} = 1;
239 if ($section->{type
} eq 'ipv4') {
240 $done_v4_hash->{$ifname} = 1;
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";
247 $interfaces .= "iface $ifname inet static\n";
248 if ($os eq "debian" && at_least
($version, 10, 0)
249 || $os eq "alpine" && at_least
($version, 3, 13)
251 $interfaces .= "\taddress $net->{cidr}\n" if defined($net->{cidr
});
253 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address
});
254 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask
});
256 remove_gateway_scripts
($section->{attr
});
257 if (defined(my $gw = $net->{gateway
})) {
258 if ($net->{needsroute
}) {
259 $interfaces .= make_gateway_scripts
($ifname, $gw);
261 $interfaces .= "\tgateway $gw\n";
264 foreach my $attr (@{$section->{attr
}}) {
265 $interfaces .= "\t$attr\n";
269 } elsif ($section->{type
} eq 'ipv6') {
270 $done_v6_hash->{$ifname} = 1;
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";
277 $interfaces .= "iface $ifname inet6 static\n";
278 if ($os eq "debian" && at_least
($version, 10, 0)
279 || $os eq "alpine" && at_least
($version, 3, 13)
281 $interfaces .= "\taddress $net->{cidr6}\n" if defined($net->{cidr6
});
283 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6
});
284 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6
});
286 remove_gateway_scripts
($section->{attr
});
287 if (defined(my $gw = $net->{gateway6
})) {
288 if ($net->{needsroute6
}) {
289 $interfaces .= make_gateway_scripts
($ifname, $gw);
291 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6
});
294 foreach my $attr (@{$section->{attr
}}) {
295 $interfaces .= "\t$attr\n";
300 die "unknown section type '$section->{type}'";
306 if (my $fh = $self->ct_open_file_read($filename)) {
307 while (defined (my $line = <$fh>)) {
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;
315 if ($line =~ m/^#/) {
316 $interfaces .= "$line\n";
319 if ($line =~ m/^\s*$/) {
323 $interfaces .= "$line\n";
327 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
329 &$print_section(); # print previous section
330 if (!$networks->{$ifname}) {
331 $interfaces .= "$line\n";
334 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
337 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
339 &$print_section(); # print previous section
340 if (!$networks->{$ifname}) {
341 $interfaces .= "$line\n";
344 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
348 if ($line =~ m/^\s*auto\s*(.*)$/) {
349 foreach my $iface (split(/\s+/, $1)) {
350 $done_auto->{$iface} = 1;
353 $interfaces .= "$line\n";
356 # Handle other section delimiters:
357 if ($line =~ m
/^\s
*(?
:mapping\s
363 $interfaces .= "$line\n";
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') {
372 push @{$section->{attr
}}, $adata;
377 $interfaces .= "$line\n";
382 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
383 foreach my $ifname (sort keys %$networks) {
384 my $net = $networks->{$ifname};
386 if (!$done_v4_hash->{$ifname} && defined($net->{address
})) {
387 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
388 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
391 if (!$done_v6_hash->{$ifname} && defined($net->{address6
})) {
392 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
393 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
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" .
406 $self->ct_file_set_contents($filename, $interfaces);