]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Debian.pm
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;
40 # Debian doesn't support the /dev/lxc/ subdirectory.
46 my ($self, $conf) = @_;
48 my $systemd = $self->ct_readlink('/sbin/init');
49 if (defined($systemd) && $systemd =~ m
@/systemd$@) {
50 $self->setup_container_getty_service($conf);
53 my $filename = "/etc/inittab";
54 return if !$self->ct_file_exists($filename);
56 my $ttycount = PVE
::LXC
::Config-
>get_tty_count($conf);
57 my $inittab = $self->ct_file_get_contents($filename);
61 !/^\s*\d+:\d*:[^:]*:.*getty/ &&
64 } split(/\n/, $inittab);
66 $inittab = join("\n", @lines) . "\n";
68 $inittab .= "p0::powerfail:/sbin/init 0\n";
70 my $version = $self->{version
};
71 for (my $id = 1; $id <= $ttycount; $id++) {
72 next if $id == 7; # reserved for X11
73 my $levels = ($id == 1) ?
'2345' : '23';
75 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
77 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
81 $self->ct_file_set_contents($filename, $inittab);
84 sub remove_gateway_scripts
{
86 my $length = scalar(@$attr);
88 my $found_section = 0;
91 if ($_ eq '# --- BEGIN PVE ---') {
95 } elsif ($_ eq '# --- END PVE ---') {
104 return if $found_section;
105 # XXX: To deal with existing setups we perform two types of removal for
106 # now. Newly started containers have their routing sections marked with
107 # begin/end comments. For older containers we perform a strict matching on
108 # the routing rules we added. We can probably remove this part at some point
109 # when it is unlikely that old debian setups are still around.
111 for (my $i = 0; $i < $length-3; ++$i) {
112 next if $attr->[$i+0] !~ m
@^\s*post-up\s
+ip\s
+route\s
+add\s
+(\S
+)\s
+dev\s
+(\S
+)$@;
113 my ($ip, $dev) = ($1, $2);
114 if ($attr->[$i+1] =~ m
@^\s*post-up\s
+ip\s
+route\s
+add\s
+default\s
+via\s
+(\S
+)\s
+dev\s
+(\S
+)$@ &&
115 ($ip eq $1 && $dev eq $2) &&
116 $attr->[$i+2] =~ m
@^\s*pre-down\s
+ip\s
+route\s
+del\s
+default\s
+via\s
+(\S
+)\s
+dev\s
+(\S
+)$@ &&
117 ($ip eq $1 && $dev eq $2) &&
118 $attr->[$i+3] =~ m
@^\s*pre-down\s
+ip\s
+route\s
+del\s
+(\S
+)\s
+dev\s
+(\S
+)$@ &&
119 ($ip eq $1 && $dev eq $2))
121 splice @$attr, $i, 4;
128 sub make_gateway_scripts
{
129 my ($ifname, $gw) = @_;
132 \tpost-up ip route add $gw dev $ifname
133 \tpost-up ip route add default via $gw dev $ifname
134 \tpre-down ip route del default via $gw dev $ifname
135 \tpre-down ip route del $gw dev $ifname
141 my ($self, $conf) = @_;
144 foreach my $k (keys %$conf) {
145 next if $k !~ m/^net(\d+)$/;
147 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
151 if (defined($d->{ip
})) {
152 if ($d->{ip
} =~ /^(?:dhcp|manual)$/) {
153 $net->{address
} = $d->{ip
};
155 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
156 $net->{address
} = $ipinfo->{address
};
157 $net->{netmask
} = $ipinfo->{netmask
};
161 if (defined($d->{'gw'})) {
162 $net->{gateway
} = $d->{'gw'};
163 if (defined($cidr) && !PVE
::Network
::is_ip_in_cidr
($d->{gw
}, $cidr, 4)) {
164 # gateway is not reachable, need an extra route
165 $net->{needsroute
} = 1;
169 if (defined($d->{ip6
})) {
170 if ($d->{ip6
} =~ /^(?:auto|dhcp|manual)$/) {
171 $net->{address6
} = $d->{ip6
};
172 } elsif ($d->{ip6
} !~ /^($IPV6RE)\/(\d
+)$/) {
173 die "unable to parse ipv6 address/prefix\n";
175 $net->{address6
} = $1;
176 $net->{netmask6
} = $2;
180 if (defined($d->{'gw6'})) {
181 $net->{gateway6
} = $d->{'gw6'};
182 if (defined($cidr) && !PVE
::Network
::is_ip_in_cidr
($d->{gw6
}, $cidr, 6) &&
183 !PVE
::Network
::is_ip_in_cidr
($d->{gw6
}, 'fe80::/10', 6)) {
184 # gateway is not reachable, need an extra route
185 $net->{needsroute6
} = 1;
188 $networks->{$d->{name
}} = $net if keys %$net;
192 return if !scalar(keys %$networks);
194 my $filename = "/etc/network/interfaces";
200 my $done_v4_hash = {};
201 my $done_v6_hash = {};
203 my $print_section = sub {
206 my $ifname = $section->{ifname
};
207 my $net = $networks->{$ifname};
209 if (!$done_auto->{$ifname}) {
210 $interfaces .= "auto $ifname\n";
211 $done_auto->{$ifname} = 1;
214 if ($section->{type
} eq 'ipv4') {
215 $done_v4_hash->{$ifname} = 1;
217 if (!defined($net->{address
})) {
218 # no address => no iface line
219 } elsif ($net->{address
} =~ /^(dhcp|manual)$/) {
220 $interfaces .= "iface $ifname inet $1\n\n";
222 $interfaces .= "iface $ifname inet static\n";
223 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address
});
224 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask
});
226 remove_gateway_scripts
($section->{attr
});
227 if (defined(my $gw = $net->{gateway
})) {
228 if ($net->{needsroute
}) {
229 $interfaces .= make_gateway_scripts
($ifname, $gw);
231 $interfaces .= "\tgateway $gw\n";
234 foreach my $attr (@{$section->{attr
}}) {
235 $interfaces .= "\t$attr\n";
239 } elsif ($section->{type
} eq 'ipv6') {
240 $done_v6_hash->{$ifname} = 1;
242 if (!defined($net->{address6
})) {
243 # no address => no iface line
244 } elsif ($net->{address6
} =~ /^(auto|dhcp|manual)$/) {
245 $interfaces .= "iface $ifname inet6 $1\n\n";
247 $interfaces .= "iface $ifname inet6 static\n";
248 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6
});
249 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6
});
250 remove_gateway_scripts
($section->{attr
});
251 if (defined(my $gw = $net->{gateway6
})) {
252 if ($net->{needsroute6
}) {
253 $interfaces .= make_gateway_scripts
($ifname, $gw);
255 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6
});
258 foreach my $attr (@{$section->{attr
}}) {
259 $interfaces .= "\t$attr\n";
264 die "unknown section type '$section->{type}'";
270 if (my $fh = $self->ct_open_file_read($filename)) {
271 while (defined (my $line = <$fh>)) {
273 if ($line =~ m/^# --- (?:BEGIN|END) PVE ---/) {
274 # Include markers in the attribute section so
275 # remove_gateway_scripts() can find them.
276 push @{$section->{attr
}}, $line if $section;
279 if ($line =~ m/^#/) {
280 $interfaces .= "$line\n";
283 if ($line =~ m/^\s*$/) {
287 $interfaces .= "$line\n";
291 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
293 &$print_section(); # print previous section
294 if (!$networks->{$ifname}) {
295 $interfaces .= "$line\n";
298 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
301 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
303 &$print_section(); # print previous section
304 if (!$networks->{$ifname}) {
305 $interfaces .= "$line\n";
308 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
312 if ($line =~ m/^\s*auto\s*(.*)$/) {
313 foreach my $iface (split(/\s+/, $1)) {
314 $done_auto->{$iface} = 1;
317 $interfaces .= "$line\n";
320 # Handle other section delimiters:
321 if ($line =~ m
/^\s
*(?
:mapping\s
327 $interfaces .= "$line\n";
330 if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) {
331 my ($adata, $aname) = ($1, $2);
332 if ($aname eq 'address' || $aname eq 'netmask' ||
333 $aname eq 'gateway' || $aname eq 'broadcast') {
336 push @{$section->{attr
}}, $adata;
341 $interfaces .= "$line\n";
346 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
347 foreach my $ifname (sort keys %$networks) {
348 my $net = $networks->{$ifname};
350 if (!$done_v4_hash->{$ifname} && defined($net->{address
})) {
351 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
352 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
355 if (!$done_v6_hash->{$ifname} && defined($net->{address6
})) {
356 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
357 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
362 # older templates (< Debian 8) do not configure the loopback interface
363 # if not explicitly told to do so
364 if (!$done_auto->{lo
}) {
365 $interfaces = "auto lo\niface lo inet loopback\n" .
366 "iface lo inet6 loopback\n\n" .
370 $self->ct_file_set_contents($filename, $interfaces);