]>
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;
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(1);
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);
82 for (my $i = 0; $i < $length; ++$i) {
84 if ($a =~ m
@^\s*post-up\s
+.*route
.*add
.*default.*(?
:gw
|via
)\s
+(\S
+)@) {
86 if ($i > 0 && $attr->[$i-1] =~ m
@^\s*post-up\s
+.*route
.*add
.*\Q
$1\E@) {
97 if ($a =~ m
@^\s*pre-down\s
+.*route
.*del
.*default.*(?
:gw
|via
)\s
+(\S
+)@) {
99 if ($attr->[$i+1] =~ m
@^\s*pre-down\s
+.*route
.*del
.*\Q
$1\E@) {
100 splice @$attr, $i, 2;
103 splice @$attr, $i, 1;
112 sub make_gateway_scripts
{
113 my ($ifname, $gw) = @_;
115 \tpost-up ip route add $gw dev $ifname
116 \tpost-up ip route add default via $gw dev $ifname
117 \tpre-down ip route del default via $gw dev $ifname
118 \tpre-down ip route del $gw dev $ifname
123 my ($self, $conf) = @_;
126 foreach my $k (keys %$conf) {
127 next if $k !~ m/^net(\d+)$/;
129 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
133 if (defined($d->{ip
})) {
134 if ($d->{ip
} =~ /^(?:dhcp|manual)$/) {
135 $net->{address
} = $d->{ip
};
137 my $ipinfo = PVE
::LXC
::parse_ipv4_cidr
($d->{ip
});
138 $net->{address
} = $ipinfo->{address
};
139 $net->{netmask
} = $ipinfo->{netmask
};
143 if (defined($d->{'gw'})) {
144 $net->{gateway
} = $d->{'gw'};
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;
151 if (defined($d->{ip6
})) {
152 if ($d->{ip6
} =~ /^(?:auto|dhcp|manual)$/) {
153 $net->{address6
} = $d->{ip6
};
154 } elsif ($d->{ip6
} !~ /^($IPV6RE)\/(\d
+)$/) {
155 die "unable to parse ipv6 address/prefix\n";
157 $net->{address6
} = $1;
158 $net->{netmask6
} = $2;
162 if (defined($d->{'gw6'})) {
163 $net->{gateway6
} = $d->{'gw6'};
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)) {
166 # gateway is not reachable, need an extra route
167 $net->{needsroute6
} = 1;
170 $networks->{$d->{name
}} = $net if keys %$net;
174 return if !scalar(keys %$networks);
176 my $filename = "/etc/network/interfaces";
182 my $done_v4_hash = {};
183 my $done_v6_hash = {};
185 my $print_section = sub {
188 my $ifname = $section->{ifname
};
189 my $net = $networks->{$ifname};
191 if (!$done_auto->{$ifname}) {
192 $interfaces .= "auto $ifname\n";
193 $done_auto->{$ifname} = 1;
196 if ($section->{type
} eq 'ipv4') {
197 $done_v4_hash->{$ifname} = 1;
199 if (defined($net->{address
}) && $net->{address
} =~ /^(dhcp|manual)$/) {
200 $interfaces .= "iface $ifname inet $1\n";
202 $interfaces .= "iface $ifname inet static\n";
203 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address
});
204 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask
});
206 remove_gateway_scripts
($section->{attr
});
207 if (defined(my $gw = $net->{gateway
})) {
208 if ($net->{needsroute
}) {
209 $interfaces .= make_gateway_scripts
($ifname, $gw);
211 $interfaces .= "\tgateway $gw\n";
214 foreach my $attr (@{$section->{attr
}}) {
215 $interfaces .= "\t$attr\n";
221 } elsif ($section->{type
} eq 'ipv6') {
222 $done_v6_hash->{$ifname} = 1;
224 if (defined($net->{address6
}) && $net->{address6
} =~ /^(auto|dhcp|manual)$/) {
225 $interfaces .= "iface $ifname inet6 $1\n";
227 $interfaces .= "iface $ifname inet6 static\n";
228 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6
});
229 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6
});
230 remove_gateway_scripts
($section->{attr
});
231 if (defined(my $gw = $net->{gateway6
})) {
232 if ($net->{needsroute6
}) {
233 $interfaces .= make_gateway_scripts
($ifname, $gw);
235 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6
});
238 foreach my $attr (@{$section->{attr
}}) {
239 $interfaces .= "\t$attr\n";
245 die "unknown section type '$section->{type}'";
251 if (my $fh = $self->ct_open_file_read($filename)) {
252 while (defined (my $line = <$fh>)) {
254 if ($line =~ m/^#/) {
255 $interfaces .= "$line\n";
258 if ($line =~ m/^\s*$/) {
262 $interfaces .= "$line\n";
266 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
268 &$print_section(); # print previous section
269 if (!$networks->{$ifname}) {
270 $interfaces .= "$line\n";
273 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
276 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
278 &$print_section(); # print previous section
279 if (!$networks->{$ifname}) {
280 $interfaces .= "$line\n";
283 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
287 if ($line =~ m/^\s*auto\s*(.*)$/) {
288 foreach my $iface (split(/\s+/, $1)) {
289 $done_auto->{$iface} = 1;
292 $interfaces .= "$line\n";
295 # Handle other section delimiters:
296 if ($line =~ m
/^\s
*(?
:mapping\s
302 $interfaces .= "$line\n";
305 if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) {
306 my ($adata, $aname) = ($1, $2);
307 if ($aname eq 'address' || $aname eq 'netmask' ||
308 $aname eq 'gateway' || $aname eq 'broadcast') {
311 push @{$section->{attr
}}, $adata;
316 $interfaces .= "$line\n";
321 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
322 foreach my $ifname (sort keys %$networks) {
323 my $net = $networks->{$ifname};
325 if (!$done_v4_hash->{$ifname} && defined($net->{address
})) {
326 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
327 $section = { type
=> 'ipv4', ifname
=> $ifname, attr
=> []};
330 if (!$done_v6_hash->{$ifname} && defined($net->{address6
})) {
331 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
332 $section = { type
=> 'ipv6', ifname
=> $ifname, attr
=> []};
337 $self->ct_file_set_contents($filename, $interfaces);