]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Debian.pm
debian: support containers upgraded to use systemd
[pve-container.git] / src / PVE / LXC / Setup / Debian.pm
1 package PVE::LXC::Setup::Debian;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use PVE::Tools qw($IPV6RE);
7 use PVE::LXC;
8 use PVE::Network;
9 use File::Path;
10
11 use PVE::LXC::Setup::Base;
12
13 use base qw(PVE::LXC::Setup::Base);
14
15 sub new {
16 my ($class, $conf, $rootdir) = @_;
17
18 my $version = PVE::Tools::file_read_firstline("$rootdir/etc/debian_version");
19
20 die "unable to read version info\n" if !defined($version);
21
22 # translate stretch/sid => 9.0 (used on debian testing repository)
23 $version = 9.0 if $version eq 'stretch/sid';
24
25 die "unable to parse version info '$version'\n"
26 if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/;
27
28 $version = $1;
29
30 die "unsupported debian version '$version'\n"
31 if !($version >= 4 && $version <= 9);
32
33 my $self = { conf => $conf, rootdir => $rootdir, version => $version };
34
35 $conf->{ostype} = "debian";
36
37 return bless $self, $class;
38 }
39
40 sub setup_init {
41 my ($self, $conf) = @_;
42
43 my $filename = "/etc/inittab";
44 return if !$self->ct_file_exists($filename);
45
46 my $ttycount = PVE::LXC::Config->get_tty_count($conf);
47 my $inittab = $self->ct_file_get_contents($filename);
48
49 my @lines = grep {
50 # remove getty lines
51 !/^\s*\d+:\d+:[^:]*:.*getty/ &&
52 # remove power lines
53 !/^\s*p[fno0]:/
54 } split(/\n/, $inittab);
55
56 $inittab = join("\n", @lines) . "\n";
57
58 $inittab .= "p0::powerfail:/sbin/init 0\n";
59
60 my $version = $self->{version};
61 for (my $id = 1; $id <= $ttycount; $id++) {
62 next if $id == 7; # reserved for X11
63 my $levels = ($id == 1) ? '2345' : '23';
64 if ($version < 7) {
65 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
66 } else {
67 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
68 }
69 }
70
71 $self->ct_file_set_contents($filename, $inittab);
72
73 my $systemd = $self->ct_readlink('/sbin/init');
74 if (defined($systemd) && $systemd =~ m@/systemd$@) {
75 $self->setup_container_getty_service(1);
76 }
77 }
78
79 sub remove_gateway_scripts {
80 my ($attr) = @_;
81 my $length = scalar(@$attr);
82 for (my $i = 0; $i < $length; ++$i) {
83 my $a = $attr->[$i];
84 if ($a =~ m@^\s*post-up\s+.*route.*add.*default.*(?:gw|via)\s+(\S+)@) {
85 my $gw = $1;
86 if ($i > 0 && $attr->[$i-1] =~ m@^\s*post-up\s+.*route.*add.*\Q$1\E@) {
87 --$i;
88 splice @$attr, $i, 2;
89 $length -= 2;
90 } else {
91 splice @$attr, $i, 1;
92 $length -= 1;
93 }
94 --$i;
95 next;
96 }
97 if ($a =~ m@^\s*pre-down\s+.*route.*del.*default.*(?:gw|via)\s+(\S+)@) {
98 my $gw = $1;
99 if ($attr->[$i+1] =~ m@^\s*pre-down\s+.*route.*del.*\Q$1\E@) {
100 splice @$attr, $i, 2;
101 $length -= 2;
102 } else {
103 splice @$attr, $i, 1;
104 $length -= 1;
105 }
106 --$i;
107 next;
108 }
109 }
110 }
111
112 sub make_gateway_scripts {
113 my ($ifname, $gw) = @_;
114 return <<"SCRIPTS";
115 \tpost-up ip route add $gw dev $ifname
116 \tpost-up ip route add default via $gw
117 \tpre-down ip route del default via $gw
118 \tpre-down ip route del $gw dev $ifname
119 SCRIPTS
120 }
121
122 sub setup_network {
123 my ($self, $conf) = @_;
124
125 my $networks = {};
126 foreach my $k (keys %$conf) {
127 next if $k !~ m/^net(\d+)$/;
128 my $ind = $1;
129 my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
130 if ($d->{name}) {
131 my $net = {};
132 my $cidr;
133 if (defined($d->{ip})) {
134 if ($d->{ip} =~ /^(?:dhcp|manual)$/) {
135 $net->{address} = $d->{ip};
136 } else {
137 my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip});
138 $net->{address} = $ipinfo->{address};
139 $net->{netmask} = $ipinfo->{netmask};
140 $cidr = $d->{ip};
141 }
142 }
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;
148 }
149 }
150 $cidr = undef;
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";
156 } else {
157 $net->{address6} = $1;
158 $net->{netmask6} = $2;
159 $cidr = $d->{ip6};
160 }
161 }
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 # gateway is not reachable, need an extra route
166 $net->{needsroute6} = 1;
167 }
168 }
169 $networks->{$d->{name}} = $net if keys %$net;
170 }
171 }
172
173 return if !scalar(keys %$networks);
174
175 my $filename = "/etc/network/interfaces";
176 my $interfaces = "";
177
178 my $section;
179
180 my $done_auto = {};
181 my $done_v4_hash = {};
182 my $done_v6_hash = {};
183
184 my $print_section = sub {
185 return if !$section;
186
187 my $ifname = $section->{ifname};
188 my $net = $networks->{$ifname};
189
190 if (!$done_auto->{$ifname}) {
191 $interfaces .= "auto $ifname\n";
192 $done_auto->{$ifname} = 1;
193 }
194
195 if ($section->{type} eq 'ipv4') {
196 $done_v4_hash->{$ifname} = 1;
197
198 if ($net->{address} =~ /^(dhcp|manual)$/) {
199 $interfaces .= "iface $ifname inet $1\n";
200 } else {
201 $interfaces .= "iface $ifname inet static\n";
202 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address});
203 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask});
204 if (defined(my $gw = $net->{gateway})) {
205 remove_gateway_scripts($section->{attr});
206 if ($net->{needsroute}) {
207 $interfaces .= make_gateway_scripts($ifname, $gw);
208 } else {
209 $interfaces .= "\tgateway $gw\n";
210 }
211 }
212 foreach my $attr (@{$section->{attr}}) {
213 $interfaces .= "\t$attr\n";
214 }
215 }
216
217 $interfaces .= "\n";
218
219 } elsif ($section->{type} eq 'ipv6') {
220 $done_v6_hash->{$ifname} = 1;
221
222 if ($net->{address6} =~ /^(auto|dhcp|manual)$/) {
223 $interfaces .= "iface $ifname inet6 $1\n";
224 } else {
225 $interfaces .= "iface $ifname inet6 static\n";
226 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6});
227 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6});
228 if (defined(my $gw = $net->{gateway6})) {
229 remove_gateway_scripts($section->{attr});
230 if ($net->{needsroute6}) {
231 $interfaces .= make_gateway_scripts($ifname, $gw);
232 } else {
233 $interfaces .= "\tgateway $net->{gateway6}\n" if defined($net->{gateway6});
234 }
235 }
236 foreach my $attr (@{$section->{attr}}) {
237 $interfaces .= "\t$attr\n";
238 }
239 }
240
241 $interfaces .= "\n";
242 } else {
243 die "unknown section type '$section->{type}'";
244 }
245
246 $section = undef;
247 };
248
249 if (my $fh = $self->ct_open_file_read($filename)) {
250 while (defined (my $line = <$fh>)) {
251 chomp $line;
252 if ($line =~ m/^#/) {
253 $interfaces .= "$line\n";
254 next;
255 }
256 if ($line =~ m/^\s*$/) {
257 if ($section) {
258 &$print_section();
259 } else {
260 $interfaces .= "$line\n";
261 }
262 next;
263 }
264 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
265 my $ifname = $1;
266 &$print_section(); # print previous section
267 if (!$networks->{$ifname}) {
268 $interfaces .= "$line\n";
269 next;
270 }
271 $section = { type => 'ipv4', ifname => $ifname, attr => []};
272 next;
273 }
274 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
275 my $ifname = $1;
276 &$print_section(); # print previous section
277 if (!$networks->{$ifname}) {
278 $interfaces .= "$line\n";
279 next;
280 }
281 $section = { type => 'ipv6', ifname => $ifname, attr => []};
282 next;
283 }
284 # Handle 'auto'
285 if ($line =~ m/^\s*auto\s*(.*)$/) {
286 foreach my $iface (split(/\s+/, $1)) {
287 $done_auto->{$iface} = 1;
288 }
289 &$print_section();
290 $interfaces .= "$line\n";
291 next;
292 }
293 # Handle other section delimiters:
294 if ($line =~ m/^\s*(?:mapping\s
295 |allow-
296 |source\s
297 |source-directory\s
298 )/x) {
299 &$print_section();
300 $interfaces .= "$line\n";
301 next;
302 }
303 if ($section && $line =~ m/^\s*((\S+)\s(.*))$/) {
304 my ($adata, $aname) = ($1, $2);
305 if ($aname eq 'address' || $aname eq 'netmask' ||
306 $aname eq 'gateway' || $aname eq 'broadcast') {
307 # skip
308 } else {
309 push @{$section->{attr}}, $adata;
310 }
311 next;
312 }
313
314 $interfaces .= "$line\n";
315 }
316 &$print_section();
317 }
318
319 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
320 foreach my $ifname (sort keys %$networks) {
321 my $net = $networks->{$ifname};
322
323 if (!$done_v4_hash->{$ifname} && defined($net->{address})) {
324 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
325 $section = { type => 'ipv4', ifname => $ifname, attr => []};
326 &$print_section();
327 }
328 if (!$done_v6_hash->{$ifname} && defined($net->{address6})) {
329 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
330 $section = { type => 'ipv6', ifname => $ifname, attr => []};
331 &$print_section();
332 }
333 }
334
335 $self->ct_file_set_contents($filename, $interfaces);
336 }
337
338 1;