]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup/Debian.pm
honor acl setting with zfs
[pve-container.git] / src / PVE / LXC / Setup / Debian.pm
CommitLineData
7af97ad5 1package PVE::LXC::Setup::Debian;
1c7f4f65
DM
2
3use strict;
4use warnings;
55fa4e09 5use Data::Dumper;
a1b1a247 6use PVE::Tools qw($IPV6RE);
55fa4e09 7use PVE::LXC;
b206c519 8use PVE::Network;
55fa4e09 9use File::Path;
1c7f4f65 10
7af97ad5 11use PVE::LXC::Setup::Base;
1c7f4f65 12
7af97ad5 13use base qw(PVE::LXC::Setup::Base);
1c7f4f65 14
633a7bd8 15sub new {
5b4657d0 16 my ($class, $conf, $rootdir) = @_;
633a7bd8 17
5b4657d0 18 my $version = PVE::Tools::file_read_firstline("$rootdir/etc/debian_version");
633a7bd8
DM
19
20 die "unable to read version info\n" if !defined($version);
adc8f577 21
9a3bd509
DM
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"
adc8f577
DM
26 if $version !~ m/^(\d+(\.\d+)?)(\.\d+)?/;
27
28 $version = $1;
29
1c0a5ac7 30 die "unsupported debian version '$version'\n"
9a3bd509 31 if !($version >= 4 && $version <= 9);
633a7bd8 32
5b4657d0
DM
33 my $self = { conf => $conf, rootdir => $rootdir, version => $version };
34
27916659 35 $conf->{ostype} = "debian";
633a7bd8
DM
36
37 return bless $self, $class;
38}
39
d66768a2 40sub setup_init {
633a7bd8 41 my ($self, $conf) = @_;
d66768a2 42
b6876f11
DM
43 my $systemd = $self->ct_readlink('/sbin/init');
44 if (defined($systemd) && $systemd =~ m@/systemd$@) {
45 $self->setup_container_getty_service(1);
46 }
47
2063d380 48 my $filename = "/etc/inittab";
fe8a16b8 49 return if !$self->ct_file_exists($filename);
d66768a2 50
1b4cf758 51 my $ttycount = PVE::LXC::Config->get_tty_count($conf);
fe8a16b8
WB
52 my $inittab = $self->ct_file_get_contents($filename);
53
54 my @lines = grep {
55 # remove getty lines
56 !/^\s*\d+:\d+:[^:]*:.*getty/ &&
57 # remove power lines
58 !/^\s*p[fno0]:/
59 } split(/\n/, $inittab);
60
61 $inittab = join("\n", @lines) . "\n";
62
63 $inittab .= "p0::powerfail:/sbin/init 0\n";
64
65 my $version = $self->{version};
39630c0d
DM
66 for (my $id = 1; $id <= $ttycount; $id++) {
67 next if $id == 7; # reserved for X11
68 my $levels = ($id == 1) ? '2345' : '23';
fe8a16b8
WB
69 if ($version < 7) {
70 $inittab .= "$id:$levels:respawn:/sbin/getty -L 38400 tty$id\n";
71 } else {
72 $inittab .= "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id\n";
d66768a2 73 }
d66768a2 74 }
fe8a16b8
WB
75
76 $self->ct_file_set_contents($filename, $inittab);
d66768a2
DM
77}
78
b206c519
WB
79sub 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
112sub 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
119SCRIPTS
120}
121
55fa4e09 122sub setup_network {
633a7bd8 123 my ($self, $conf) = @_;
55fa4e09 124
55fa4e09
DM
125 my $networks = {};
126 foreach my $k (keys %$conf) {
127 next if $k !~ m/^net(\d+)$/;
93285df8 128 my $ind = $1;
1b4cf758 129 my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
55fa4e09
DM
130 if ($d->{name}) {
131 my $net = {};
b206c519 132 my $cidr;
93285df8 133 if (defined($d->{ip})) {
ecf84da0 134 if ($d->{ip} =~ /^(?:dhcp|manual)$/) {
0178cffa
WB
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};
b206c519 140 $cidr = $d->{ip};
0178cffa 141 }
55fa4e09 142 }
93285df8 143 if (defined($d->{'gw'})) {
a1b1a247 144 $net->{gateway} = $d->{'gw'};
b206c519
WB
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 }
55fa4e09 149 }
b206c519 150 $cidr = undef;
93285df8 151 if (defined($d->{ip6})) {
c6b8740b 152 if ($d->{ip6} =~ /^(?:auto|dhcp|manual)$/) {
0178cffa
WB
153 $net->{address6} = $d->{ip6};
154 } elsif ($d->{ip6} !~ /^($IPV6RE)\/(\d+)$/) {
a1b1a247 155 die "unable to parse ipv6 address/prefix\n";
0178cffa
WB
156 } else {
157 $net->{address6} = $1;
158 $net->{netmask6} = $2;
b206c519 159 $cidr = $d->{ip6};
a1b1a247 160 }
a1b1a247
WB
161 }
162 if (defined($d->{'gw6'})) {
163 $net->{gateway6} = $d->{'gw6'};
b206c519
WB
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 }
55fa4e09 168 }
0178cffa 169 $networks->{$d->{name}} = $net if keys %$net;
55fa4e09
DM
170 }
171 }
172
f44d23a5 173 return if !scalar(keys %$networks);
55fa4e09 174
2063d380 175 my $filename = "/etc/network/interfaces";
55fa4e09
DM
176 my $interfaces = "";
177
178 my $section;
179
56be201e 180 my $done_auto = {};
55fa4e09
DM
181 my $done_v4_hash = {};
182 my $done_v6_hash = {};
1c0a5ac7 183
55fa4e09 184 my $print_section = sub {
55fa4e09
DM
185 return if !$section;
186
6fd46881
WB
187 my $ifname = $section->{ifname};
188 my $net = $networks->{$ifname};
55fa4e09 189
6b0a0043 190 if (!$done_auto->{$ifname}) {
6fd46881
WB
191 $interfaces .= "auto $ifname\n";
192 $done_auto->{$ifname} = 1;
56be201e
WB
193 }
194
55fa4e09 195 if ($section->{type} eq 'ipv4') {
6fd46881 196 $done_v4_hash->{$ifname} = 1;
55fa4e09 197
ecf84da0 198 if ($net->{address} =~ /^(dhcp|manual)$/) {
6fd46881 199 $interfaces .= "iface $ifname inet $1\n";
fcaca113 200 } else {
6fd46881 201 $interfaces .= "iface $ifname inet static\n";
a1b1a247
WB
202 $interfaces .= "\taddress $net->{address}\n" if defined($net->{address});
203 $interfaces .= "\tnetmask $net->{netmask}\n" if defined($net->{netmask});
b206c519
WB
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 }
55fa4e09
DM
212 foreach my $attr (@{$section->{attr}}) {
213 $interfaces .= "\t$attr\n";
214 }
55fa4e09 215 }
1c0a5ac7 216
55fa4e09 217 $interfaces .= "\n";
1c0a5ac7 218
55fa4e09 219 } elsif ($section->{type} eq 'ipv6') {
6fd46881 220 $done_v6_hash->{$ifname} = 1;
1c0a5ac7 221
c6b8740b 222 if ($net->{address6} =~ /^(auto|dhcp|manual)$/) {
6fd46881 223 $interfaces .= "iface $ifname inet6 $1\n";
fcaca113 224 } else {
6fd46881 225 $interfaces .= "iface $ifname inet6 static\n";
a1b1a247
WB
226 $interfaces .= "\taddress $net->{address6}\n" if defined($net->{address6});
227 $interfaces .= "\tnetmask $net->{netmask6}\n" if defined($net->{netmask6});
b206c519
WB
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 }
55fa4e09
DM
236 foreach my $attr (@{$section->{attr}}) {
237 $interfaces .= "\t$attr\n";
238 }
239 }
1c0a5ac7
WB
240
241 $interfaces .= "\n";
55fa4e09
DM
242 } else {
243 die "unknown section type '$section->{type}'";
244 }
245
246 $section = undef;
247 };
1c0a5ac7 248
f08b2779 249 if (my $fh = $self->ct_open_file_read($filename)) {
55fa4e09
DM
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 }
0178cffa 264 if ($line =~ m/^\s*iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
55fa4e09 265 my $ifname = $1;
0178cffa 266 &$print_section(); # print previous section
55fa4e09
DM
267 if (!$networks->{$ifname}) {
268 $interfaces .= "$line\n";
269 next;
270 }
271 $section = { type => 'ipv4', ifname => $ifname, attr => []};
272 next;
273 }
0178cffa 274 if ($line =~ m/^\s*iface\s+(\S+)\s+inet6\s+(\S+)\s*$/) {
55fa4e09 275 my $ifname = $1;
0178cffa 276 &$print_section(); # print previous section
55fa4e09
DM
277 if (!$networks->{$ifname}) {
278 $interfaces .= "$line\n";
279 next;
280 }
281 $section = { type => 'ipv6', ifname => $ifname, attr => []};
282 next;
283 }
56be201e
WB
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 }
0178cffa
WB
293 # Handle other section delimiters:
294 if ($line =~ m/^\s*(?:mapping\s
0178cffa
WB
295 |allow-
296 |source\s
297 |source-directory\s
298 )/x) {
299 &$print_section();
300 $interfaces .= "$line\n";
301 next;
302 }
55fa4e09
DM
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 {
1c0a5ac7 309 push @{$section->{attr}}, $adata;
55fa4e09
DM
310 }
311 next;
312 }
1c0a5ac7
WB
313
314 $interfaces .= "$line\n";
55fa4e09
DM
315 }
316 &$print_section();
55fa4e09
DM
317 }
318
e90673a8 319 my $need_separator = length($interfaces) && ($interfaces !~ /\n\n$/);
55fa4e09
DM
320 foreach my $ifname (sort keys %$networks) {
321 my $net = $networks->{$ifname};
1c0a5ac7 322
fcaca113 323 if (!$done_v4_hash->{$ifname} && defined($net->{address})) {
1c0a5ac7 324 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
55fa4e09 325 $section = { type => 'ipv4', ifname => $ifname, attr => []};
6b0a0043 326 &$print_section();
55fa4e09 327 }
a1b1a247 328 if (!$done_v6_hash->{$ifname} && defined($net->{address6})) {
1c0a5ac7 329 if ($need_separator) { $interfaces .= "\n"; $need_separator = 0; };
55fa4e09 330 $section = { type => 'ipv6', ifname => $ifname, attr => []};
6b0a0043 331 &$print_section();
55fa4e09
DM
332 }
333 }
1c0a5ac7 334
2063d380 335 $self->ct_file_set_contents($filename, $interfaces);
55fa4e09 336}
1c7f4f65
DM
337
3381;