]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Debian.pm
9c7ba9a325b5eea73a1102341fc8bc4b0a6884e2
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);
16 my ($class, $conf, $rootdir) = @_;
18 my $version = PVE
::Tools
::file_read_firstline
("$rootdir/etc/debian_version");
20 die "unable to read version info\n" if !defined($version);
22 # translate stretch/sid => 9.0 (used on debian testing repository)
23 $version = 9.0 if $version eq 'stretch/sid';
25 die "unable to parse version info '$version'\n"
26 if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/;
30 die "unsupported debian version '$version'\n"
31 if !($version >= 4 && $version <= 9);
33 my $self = { conf
=> $conf, rootdir
=> $rootdir, version
=> $version };
35 $conf->{ostype
} = "debian";
37 return bless $self, $class;
41 my ($self, $conf) = @_;
43 my $systemd = $self->ct_readlink('/sbin/init');
44 if (defined($systemd) && $systemd =~ m
@/systemd$@) {
45 $self->setup_container_getty_service($conf);
48 my $filename = "/etc/inittab";
49 return if !$self->ct_file_exists($filename);
51 my $ttycount = PVE
::LXC
::Config-
>get_tty_count($conf);
52 my $inittab = $self->ct_file_get_contents($filename);
56 !/^\s*\d+:\d*:[^:]*:.*getty/ &&
59 } split(/\n/, $inittab);
61 $inittab = join("\n", @lines) . "\n";
63 $inittab .= "p0::powerfail:/sbin/init 0\n";
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';
70 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
72 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
76 $self->ct_file_set_contents($filename, $inittab);
79 sub remove_gateway_scripts
{
81 my $length = scalar(@$attr);
83 my $found_section = 0;
86 if ($_ eq '# --- BEGIN PVE ---') {
90 } elsif ($_ eq '# --- END PVE ---') {
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.
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))
116 splice @$attr, $i, 4;
123 sub make_gateway_scripts
{
124 my ($ifname, $gw) = @_;
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
136 my ($self, $conf) = @_;
139 foreach my $k (keys %$conf) {
140 next if $k !~ m/^net(\d+)$/;
142 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
146 if (defined($d->{ip
})) {
147 if ($d->{ip
} =~ /^(?:dhcp|manual)$/) {
148 $net->{address
} = $d->{ip
};
150 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
151 $net->{address
} = $ipinfo->{address
};
152 $net->{netmask
} = $ipinfo->{netmask
};
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;
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";
170 $net->{address6
} = $1;
171 $net->{netmask6
} = $2;
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;
183 $networks->{$d->{name
}} = $net if keys %$net;
187 return if !scalar(keys %$networks);
189 my $filename = "/etc/network/interfaces";
195 my $done_v4_hash = {};
196 my $done_v6_hash = {};
198 my $print_section = sub {
201 my $ifname = $section->{ifname
};
202 my $net = $networks->{$ifname};
204 if (!$done_auto->{$ifname}) {
205 $interfaces .= "auto $ifname\n";
206 $done_auto->{$ifname} = 1;
209 if ($section->{type
} eq 'ipv4') {
210 $done_v4_hash->{$ifname} = 1;
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";
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
});
221 remove_gateway_scripts
($section->{attr
});
222 if (defined(my $gw = $net->{gateway
})) {
223 if ($net->{needsroute
}) {
224 $interfaces .= make_gateway_scripts
($ifname, $gw);
226 $interfaces .= "\tgateway $gw\n";
229 foreach my $attr (@{$section->{attr
}}) {
230 $interfaces .= "\t$attr\n";
234 } elsif ($section->{type
} eq 'ipv6') {
235 $done_v6_hash->{$ifname} = 1;
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";
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);
250 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6
});
253 foreach my $attr (@{$section->{attr
}}) {
254 $interfaces .= "\t$attr\n";
259 die "unknown section type '$section->{type}'";
265 if (my $fh = $self->ct_open_file_read($filename)) {
266 while (defined (my $line = <$fh>)) {
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;
274 if ($line =~ m/^#/) {
275 $interfaces .= "$line\n";
278 if ($line =~ m/^\s*$/) {
282 $interfaces .= "$line\n";
286 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
288 &$print_section(); # print previous section
289 if (!$networks->{$ifname}) {
290 $interfaces .= "$line\n";
293 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
296 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
298 &$print_section(); # print previous section
299 if (!$networks->{$ifname}) {
300 $interfaces .= "$line\n";
303 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
307 if ($line =~ m/^\s*auto\s*(.*)$/) {
308 foreach my $iface (split(/\s+/, $1)) {
309 $done_auto->{$iface} = 1;
312 $interfaces .= "$line\n";
315 # Handle other section delimiters:
316 if ($line =~ m
/^\s
*(?
:mapping\s
322 $interfaces .= "$line\n";
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') {
331 push @{$section->{attr
}}, $adata;
336 $interfaces .= "$line\n";
341 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
342 foreach my $ifname (sort keys %$networks) {
343 my $net = $networks->{$ifname};
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
=> []};
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
=> []};
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" .
365 $self->ct_file_set_contents($filename, $interfaces);