]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup/Base.pm
seperate serachdomain and nameserver config
[pve-container.git] / src / PVE / LXC / Setup / Base.pm
CommitLineData
7af97ad5 1package PVE::LXC::Setup::Base;
1c7f4f65
DM
2
3use strict;
4use warnings;
5
168d6b07
DM
6use File::stat;
7use Digest::SHA;
8use IO::File;
9use Encode;
f08b2779 10use Fcntl;
2063d380 11use File::Path;
f08b2779 12use File::Spec;
bd1dc8d1 13use File::Basename;
168d6b07 14
b9cd9975 15use PVE::INotify;
55fa4e09 16use PVE::Tools;
4401b7d4 17use PVE::Network;
55fa4e09 18
633a7bd8 19sub new {
153747ff 20 my ($class, $conf, $rootdir, $os_release) = @_;
633a7bd8 21
153747ff 22 return bless { conf => $conf, rootdir => $rootdir, os_release => $os_release }, $class;
633a7bd8 23}
b9cd9975 24
c0eae401 25sub lookup_dns_conf {
23d928a1 26 my ($self, $conf) = @_;
b9cd9975 27
27916659
DM
28 my $nameserver = $conf->{nameserver};
29 my $searchdomains = $conf->{searchdomain};
b9cd9975 30
ffc468ee
DC
31 if ($conf->{'testmode'}) {
32 return ('proxmox.com', '8.8.8.8 8.8.8.9');
33 }
b9cd9975 34
ffc468ee 35 my $host_resolv_conf = $self->{host_resolv_conf};
b9cd9975 36
ffc468ee
DC
37 if (!defined($nameserver)) {
38 my @list = ();
39 foreach my $k ("dns1", "dns2", "dns3") {
40 if (my $ns = $host_resolv_conf->{$k}) {
41 push @list, $ns;
b9cd9975 42 }
b9cd9975 43 }
ffc468ee
DC
44 $nameserver = join(' ', @list);
45 }
46
47 if (!defined($searchdomains)) {
48 $searchdomains = $host_resolv_conf->{search};
b9cd9975
DM
49 }
50
51 return ($searchdomains, $nameserver);
c0eae401 52}
b9cd9975 53
c0eae401 54sub update_etc_hosts {
9096a91d 55 my ($self, $hostip, $oldname, $newname, $searchdomains) = @_;
1c7f4f65 56
1c7f4f65
DM
57 my $done = 0;
58
ce289e3c
WB
59 my $namepart = ($newname =~ s/\..*$//r);
60
005f91ad 61 my $all_names = '';
ce289e3c 62 if ($newname =~ /\./) {
005f91ad 63 $all_names .= "$newname $namepart";
ce289e3c
WB
64 } else {
65 foreach my $domain (PVE::Tools::split_list($searchdomains)) {
005f91ad
WB
66 $all_names .= ' ' if $all_names;
67 $all_names .= "$newname.$domain";
ce289e3c 68 }
005f91ad
WB
69 $all_names .= ' ' if $all_names;
70 $all_names .= $newname;
e4929e97 71 }
1c7f4f65 72
9096a91d
WB
73 # Prepare section:
74 my $section = '';
75 my $hosts_fn = '/etc/hosts';
c325b32f 76
9096a91d
WB
77 my $lo4 = "127.0.0.1 localhost.localnet localhost\n";
78 my $lo6 = "::1 localhost.localnet localhost\n";
79 if ($self->ct_file_exists($hosts_fn)) {
80 my $data = $self->ct_file_get_contents($hosts_fn);
81 # don't take localhost entries within our hosts sections into account
82 $data = remove_pve_sections($data);
1c7f4f65 83
9096a91d
WB
84 # check for existing localhost entries
85 $section .= $lo4 if $data !~ /^\h*127\.0\.0\.1\h+/m;
86 $section .= $lo6 if $data !~ /^\h*::1\h+/m;
87 } else {
88 $section .= $lo4 . $lo6;
1c7f4f65
DM
89 }
90
9096a91d
WB
91 if (defined($hostip)) {
92 $section .= "$hostip $all_names\n";
93 } else {
94 $section .= "127.0.1.1 $namepart\n";
1e180f97
DM
95 }
96
9096a91d 97 $self->ct_modify_file($hosts_fn, $section);
c0eae401 98}
1c7f4f65 99
142444d5
DM
100sub template_fixup {
101 my ($self, $conf) = @_;
102
103 # do nothing by default
104}
105
c325b32f 106sub set_dns {
633a7bd8 107 my ($self, $conf) = @_;
c325b32f 108
23d928a1 109 my ($searchdomains, $nameserver) = $self->lookup_dns_conf($conf);
c325b32f 110
c325b32f
DM
111 my $data = '';
112
113 $data .= "search " . join(' ', PVE::Tools::split_list($searchdomains)) . "\n"
114 if $searchdomains;
115
116 foreach my $ns ( PVE::Tools::split_list($nameserver)) {
117 $data .= "nameserver $ns\n";
118 }
119
2edb50e5 120 $self->ct_modify_file("/etc/resolv.conf", $data, replace => 1);
c325b32f
DM
121}
122
1c7f4f65 123sub set_hostname {
633a7bd8 124 my ($self, $conf) = @_;
1c7f4f65 125
27916659 126 my $hostname = $conf->{hostname} || 'localhost';
1c7f4f65 127
ce289e3c 128 my $namepart = ($hostname =~ s/\..*$//r);
1c7f4f65 129
f08b2779 130 my $hostname_fn = "/etc/hostname";
1c7f4f65 131
f08b2779 132 my $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
1c7f4f65 133
c325b32f
DM
134 my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
135 my $hostip = $ipv4 || $ipv6;
b9cd9975 136
23d928a1 137 my ($searchdomains) = $self->lookup_dns_conf($conf);
b9cd9975 138
9096a91d 139 $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
b9cd9975 140
ce289e3c 141 $self->ct_file_set_contents($hostname_fn, "$namepart\n");
1c7f4f65
DM
142}
143
55fa4e09 144sub setup_network {
633a7bd8 145 my ($self, $conf) = @_;
55fa4e09
DM
146
147 die "please implement this inside subclass"
148}
149
d66768a2 150sub setup_init {
633a7bd8 151 my ($self, $conf) = @_;
1c7f4f65 152
d66768a2
DM
153 die "please implement this inside subclass"
154}
155
9143dec4
DM
156sub setup_systemd_console {
157 my ($self, $conf) = @_;
158
e7deac01 159 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
9143dec4
DM
160 "/lib/systemd/system" : "/usr/lib/systemd/system";
161
9143dec4
DM
162 my $systemd_getty_service_rel = "$systemd_dir_rel/getty\@.service";
163
f08b2779 164 return if !$self->ct_file_exists($systemd_getty_service_rel);
9143dec4 165
f08b2779 166 my $raw = $self->ct_file_get_contents($systemd_getty_service_rel);
9143dec4 167
c69ae0d0 168 my $systemd_container_getty_service_rel = "$systemd_dir_rel/container-getty\@.service";
c69ae0d0
DM
169
170 # systemd on CenoOS 7.1 is too old (version 205), so there is no
171 # container-getty service
f08b2779 172 if (!$self->ct_file_exists($systemd_container_getty_service_rel)) {
c69ae0d0 173 if ($raw =~ s!^ConditionPathExists=/dev/tty0$!ConditionPathExists=/dev/tty!m) {
f08b2779 174 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
c69ae0d0
DM
175 }
176 } else {
177 # undo above change (in case someone updated systemd)
178 if ($raw =~ s!^ConditionPathExists=/dev/tty$!ConditionPathExists=/dev/tty0!m) {
f08b2779 179 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
c69ae0d0 180 }
9143dec4
DM
181 }
182
1b4cf758 183 my $ttycount = PVE::LXC::Config->get_tty_count($conf);
9143dec4
DM
184
185 for (my $i = 1; $i < 7; $i++) {
f08b2779 186 my $tty_service_lnk = "/etc/systemd/system/getty.target.wants/getty\@tty$i.service";
9143dec4 187 if ($i > $ttycount) {
f08b2779 188 $self->ct_unlink($tty_service_lnk);
9143dec4 189 } else {
f08b2779
WB
190 if (!$self->ct_is_symlink($tty_service_lnk)) {
191 $self->ct_unlink($tty_service_lnk);
192 $self->ct_symlink($systemd_getty_service_rel, $tty_service_lnk);
9143dec4
DM
193 }
194 }
195 }
196}
197
5e84bdc8
DM
198# A few distros as well as unprivileged containers cannot deal with the
199# /dev/lxc/ tty subdirectory.
200sub devttydir {
201 my ($self, $conf) = @_;
202 return $conf->{unprivileged} ? '' : 'lxc/';
203}
204
90b21cdc 205sub setup_container_getty_service {
5e84bdc8
DM
206 my ($self, $conf) = @_;
207
e7deac01 208 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
8f115f7c
WB
209 "/lib/systemd/system" : "/usr/lib/systemd/system";
210 my $servicefile = "$systemd_dir_rel/container-getty\@.service";
90b21cdc 211 my $raw = $self->ct_file_get_contents($servicefile);
5e84bdc8 212 my $ttyname = $self->devttydir($conf) . 'tty%I';
418ec240 213 if ($raw =~ s@pts/%I|lxc/tty%I@$ttyname@g) {
90b21cdc
WB
214 $self->ct_file_set_contents($servicefile, $raw);
215 }
216}
217
c1d32b55
WB
218sub setup_systemd_networkd {
219 my ($self, $conf) = @_;
220
c1d32b55
WB
221 foreach my $k (keys %$conf) {
222 next if $k !~ m/^net(\d+)$/;
1b4cf758 223 my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
c1d32b55
WB
224 next if !$d->{name};
225
f08b2779 226 my $filename = "/etc/systemd/network/$d->{name}.network";
c1d32b55
WB
227
228 my $data = <<"DATA";
229[Match]
230Name = $d->{name}
231
232[Network]
233Description = Interface $d->{name} autoconfigured by PVE
234DATA
4401b7d4
WB
235
236 my $routes = '';
237 my ($has_ipv4, $has_ipv6);
238
c1d32b55
WB
239 # DHCP bitflags:
240 my @DHCPMODES = ('none', 'v4', 'v6', 'both');
241 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
242 my $dhcp = $NONE;
243
244 if (defined(my $ip = $d->{ip})) {
245 if ($ip eq 'dhcp') {
246 $dhcp |= $DHCP4;
247 } elsif ($ip ne 'manual') {
4401b7d4 248 $has_ipv4 = 1;
c1d32b55
WB
249 $data .= "Address = $ip\n";
250 }
251 }
252 if (defined(my $gw = $d->{gw})) {
253 $data .= "Gateway = $gw\n";
4401b7d4
WB
254 if ($has_ipv4 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip}, 4)) {
255 $routes .= "\n[Route]\nDestination = $gw/32\nScope = link\n";
256 }
c1d32b55
WB
257 }
258
259 if (defined(my $ip = $d->{ip6})) {
260 if ($ip eq 'dhcp') {
261 $dhcp |= $DHCP6;
262 } elsif ($ip ne 'manual') {
4401b7d4 263 $has_ipv6 = 1;
c1d32b55
WB
264 $data .= "Address = $ip\n";
265 }
266 }
267 if (defined(my $gw = $d->{gw6})) {
268 $data .= "Gateway = $gw\n";
d13fd23a
WB
269 if ($has_ipv6 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip6}, 6) &&
270 !PVE::Network::is_ip_in_cidr($gw, 'fe80::/10', 6)) {
4401b7d4
WB
271 $routes .= "\n[Route]\nDestination = $gw/128\nScope = link\n";
272 }
c1d32b55
WB
273 }
274
275 $data .= "DHCP = $DHCPMODES[$dhcp]\n";
4401b7d4 276 $data .= $routes if $routes;
c1d32b55 277
f08b2779 278 $self->ct_file_set_contents($filename, $data);
c1d32b55 279 }
b7cd927f
WB
280}
281
282sub setup_securetty {
283 my ($self, $conf, @add) = @_;
c1d32b55 284
f08b2779 285 my $filename = "/etc/securetty";
bd3093ef
TL
286 # root login is already allowed on every device if no securetty present
287 return if !$self->ct_file_exists($filename);
288
5e84bdc8
DM
289 if (!scalar(@add)) {
290 @add = qw(console tty1 tty2 tty3 tty4);
291 if (my $dir = $self->devttydir($conf)) {
292 @add = map { "${dir}$_" } @add;
293 }
294 }
295
f08b2779 296 my $data = $self->ct_file_get_contents($filename);
b7cd927f
WB
297 chomp $data; $data .= "\n";
298 foreach my $dev (@add) {
299 if ($data !~ m!^\Q$dev\E\s*$!m) {
300 $data .= "$dev\n";
301 }
302 }
f08b2779 303 $self->ct_file_set_contents($filename, $data);
c1d32b55
WB
304}
305
168d6b07 306my $replacepw = sub {
f08b2779 307 my ($self, $file, $user, $epw, $shadow) = @_;
168d6b07
DM
308
309 my $tmpfile = "$file.$$";
310
311 eval {
f08b2779 312 my $src = $self->ct_open_file_read($file) ||
168d6b07
DM
313 die "unable to open file '$file' - $!";
314
f08b2779 315 my $st = $self->ct_stat($src) ||
168d6b07
DM
316 die "unable to stat file - $!";
317
f08b2779 318 my $dst = $self->ct_open_file_write($tmpfile) ||
168d6b07
DM
319 die "unable to open file '$tmpfile' - $!";
320
321 # copy owner and permissions
322 chmod $st->mode, $dst;
323 chown $st->uid, $st->gid, $dst;
367a7c18
DM
324
325 my $last_change = int(time()/(60*60*24));
326
327 if ($epw =~ m/^\$TEST\$/) { # for regression tests
328 $last_change = 12345;
329 }
168d6b07
DM
330
331 while (defined (my $line = <$src>)) {
367a7c18
DM
332 if ($shadow) {
333 $line =~ s/^${user}:[^:]*:[^:]*:/${user}:${epw}:${last_change}:/;
334 } else {
335 $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
336 }
168d6b07
DM
337 print $dst $line;
338 }
339
340 $src->close() || die "close '$file' failed - $!\n";
341 $dst->close() || die "close '$tmpfile' failed - $!\n";
342 };
343 if (my $err = $@) {
f08b2779 344 $self->ct_unlink($tmpfile);
168d6b07 345 } else {
f08b2779
WB
346 $self->ct_rename($tmpfile, $file);
347 $self->ct_unlink($tmpfile); # in case rename fails
168d6b07
DM
348 }
349};
350
351sub set_user_password {
633a7bd8 352 my ($self, $conf, $user, $opt_password) = @_;
168d6b07 353
f08b2779 354 my $pwfile = "/etc/passwd";
168d6b07 355
f08b2779 356 return if !$self->ct_file_exists($pwfile);
168d6b07 357
f08b2779 358 my $shadow = "/etc/shadow";
168d6b07
DM
359
360 if (defined($opt_password)) {
361 if ($opt_password !~ m/^\$/) {
362 my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8);
738076ce 363 $opt_password = crypt(encode("utf8", $opt_password), "\$1\$$time\$");
168d6b07
DM
364 };
365 } else {
366 $opt_password = '*';
367 }
368
f08b2779
WB
369 if ($self->ct_file_exists($shadow)) {
370 &$replacepw ($self, $shadow, $user, $opt_password, 1);
371 &$replacepw ($self, $pwfile, $user, 'x');
168d6b07 372 } else {
f08b2779 373 &$replacepw ($self, $pwfile, $user, $opt_password);
168d6b07
DM
374 }
375}
376
f36ce482
FG
377my $parse_home_dir = sub {
378 my ($self, $passwdfile, $user) = @_;
379
380 my $fh = $self->ct_open_file_read($passwdfile);
381 while (defined (my $line = <$fh>)) {
382 return $2
383 if $line =~ m/^${user}:([^:]*:){4}([^:]*):/;
384 }
385};
386
387sub set_user_authorized_ssh_keys {
388 my ($self, $conf, $user, $ssh_keys) = @_;
389
390 my $passwd = "/etc/passwd";
391 my $home = $user eq "root" ? "/root/" : "/home/$user/";
392
393 $home = &$parse_home_dir($self, $passwd, $user)
394 if $self->ct_file_exists($passwd);
395
396 die "home directory '$home' of $user does not exist!"
397 if ! ($self->ct_is_directory($home) || $self->ct_is_symlink($home));
398
399 $self->ct_mkdir("$home/.ssh", 0700)
400 if ! $self->ct_is_directory("$home/.ssh");
401
402 $self->ct_modify_file("$home/.ssh/authorized_keys", $ssh_keys, perms => 0700);
403}
404
4727bd09
DM
405my $randomize_crontab = sub {
406 my ($self, $conf) = @_;
407
b5e62cd0
DM
408 my @files;
409 # Note: dir_glob_foreach() untaints filenames!
f08b2779 410 PVE::Tools::dir_glob_foreach("/etc/cron.d", qr/[A-Z\-\_a-z0-9]+/, sub {
b5e62cd0 411 my ($name) = @_;
f08b2779 412 push @files, "/etc/cron.d/$name";
b5e62cd0 413 });
4727bd09 414
f08b2779
WB
415 my $crontab_fn = "/etc/crontab";
416 unshift @files, $crontab_fn if $self->ct_file_exists($crontab_fn);
4727bd09
DM
417
418 foreach my $filename (@files) {
f08b2779 419 my $data = $self->ct_file_get_contents($filename);
4727bd09
DM
420 my $new = '';
421 foreach my $line (split(/\n/, $data)) {
422 # we only randomize minutes for root crontab entries
423 if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) {
424 my $rest = $1;
425 my $min = int(rand()*59);
426 $new .= "$min$rest\n";
427 } else {
428 $new .= "$line\n";
429 }
430 }
f08b2779 431 $self->ct_file_set_contents($filename, $new);
4727bd09
DM
432 }
433};
434
d66768a2 435sub pre_start_hook {
633a7bd8 436 my ($self, $conf) = @_;
d66768a2 437
633a7bd8
DM
438 $self->setup_init($conf);
439 $self->setup_network($conf);
440 $self->set_hostname($conf);
441 $self->set_dns($conf);
d66768a2
DM
442
443 # fixme: what else ?
444}
445
446sub post_create_hook {
f36ce482 447 my ($self, $conf, $root_password, $ssh_keys) = @_;
d66768a2 448
142444d5 449 $self->template_fixup($conf);
4727bd09
DM
450
451 &$randomize_crontab($self, $conf);
452
633a7bd8 453 $self->set_user_password($conf, 'root', $root_password);
f36ce482 454 $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys;
633a7bd8
DM
455 $self->setup_init($conf);
456 $self->setup_network($conf);
457 $self->set_hostname($conf);
458 $self->set_dns($conf);
168d6b07 459
55fa4e09 460 # fixme: what else ?
1c7f4f65
DM
461}
462
f08b2779
WB
463# File access wrappers for container setup code.
464# For user-namespace support these might need to take uid and gid maps into account.
465
bd1dc8d1
WB
466sub ct_is_file_ignored {
467 my ($self, $file) = @_;
468 my ($name, $path) = fileparse($file);
469 return -f "$path/.pve-ignore.$name";
470}
471
c6a605f9
WB
472sub ct_reset_ownership {
473 my ($self, @files) = @_;
474 my $conf = $self->{conf};
475 return if !$self->{id_map};
bd1dc8d1
WB
476
477 @files = grep { !$self->ct_is_file_ignored($_) } @files;
478 return if !@files;
479
c6a605f9
WB
480 my $uid = $self->{rootuid};
481 my $gid = $self->{rootgid};
482 chown($uid, $gid, @files);
483}
484
2063d380
WB
485sub ct_mkdir {
486 my ($self, $file, $mask) = @_;
f08b2779 487 # mkdir goes by parameter count - an `undef' mode acts like a mode of 0000
c6a605f9
WB
488 if (defined($mask)) {
489 return CORE::mkdir($file, $mask) && $self->ct_reset_ownership($file);
490 } else {
491 return CORE::mkdir($file) && $self->ct_reset_ownership($file);
492 }
2063d380
WB
493}
494
495sub ct_unlink {
f08b2779
WB
496 my ($self, @files) = @_;
497 foreach my $file (@files) {
bd1dc8d1 498 next if $self->ct_is_file_ignored($file);
f08b2779
WB
499 CORE::unlink($file);
500 }
501}
502
503sub ct_rename {
504 my ($self, $old, $new) = @_;
bd1dc8d1 505 return if $self->ct_is_file_ignored($new);
f08b2779 506 CORE::rename($old, $new);
2063d380
WB
507}
508
f08b2779 509sub ct_open_file_read {
2063d380 510 my $self = shift;
f08b2779
WB
511 my $file = shift;
512 return IO::File->new($file, O_RDONLY, @_);
2063d380
WB
513}
514
f08b2779 515sub ct_open_file_write {
2063d380 516 my $self = shift;
f08b2779 517 my $file = shift;
bd1dc8d1 518 $file = '/dev/null' if $self->ct_is_file_ignored($file);
c6a605f9
WB
519 my $fh = IO::File->new($file, O_WRONLY | O_CREAT, @_);
520 $self->ct_reset_ownership($fh);
521 return $fh;
2063d380
WB
522}
523
f08b2779 524sub ct_make_path {
2063d380 525 my $self = shift;
c6a605f9
WB
526 if ($self->{id_map}) {
527 my $opts = pop;
528 if (ref($opts) eq 'HASH') {
529 $opts->{owner} = $self->{rootuid} if !defined($self->{owner});
530 $opts->{group} = $self->{rootgid} if !defined($self->{group});
531 }
532 File::Path::make_path(@_, $opts);
533 } else {
534 File::Path::make_path(@_);
535 }
2063d380
WB
536}
537
538sub ct_symlink {
539 my ($self, $old, $new) = @_;
bd1dc8d1 540 return if $self->ct_is_file_ignored($new);
f08b2779 541 return CORE::symlink($old, $new);
2063d380
WB
542}
543
8f115f7c
WB
544sub ct_readlink {
545 my ($self, $name) = @_;
546 return CORE::readlink($name);
547}
548
2063d380
WB
549sub ct_file_exists {
550 my ($self, $file) = @_;
f08b2779
WB
551 return -f $file;
552}
553
554sub ct_is_directory {
555 my ($self, $file) = @_;
556 return -d $file;
557}
558
559sub ct_is_symlink {
560 my ($self, $file) = @_;
561 return -l $file;
562}
563
e7deac01
WB
564sub ct_is_executable {
565 my ($self, $file) = @_;
566 return -x $file
567}
568
f08b2779
WB
569sub ct_stat {
570 my ($self, $file) = @_;
571 return File::stat::stat($file);
2063d380
WB
572}
573
574sub ct_file_read_firstline {
575 my ($self, $file) = @_;
f08b2779 576 return PVE::Tools::file_read_firstline($file);
2063d380
WB
577}
578
579sub ct_file_get_contents {
580 my ($self, $file) = @_;
f08b2779 581 return PVE::Tools::file_get_contents($file);
2063d380
WB
582}
583
584sub ct_file_set_contents {
39243220 585 my ($self, $file, $data, $perms) = @_;
bd1dc8d1 586 return if $self->ct_is_file_ignored($file);
c6a605f9
WB
587 PVE::Tools::file_set_contents($file, $data, $perms);
588 $self->ct_reset_ownership($file);
2063d380
WB
589}
590
2edb50e5
WB
591# Modify a marked portion of a file.
592# Optionally if the file becomes empty it will be deleted.
593sub ct_modify_file {
594 my ($self, $file, $data, %options) = @_;
bd1dc8d1 595 return if $self->ct_is_file_ignored($file);
2edb50e5
WB
596
597 my $head = "# --- BEGIN PVE ---\n";
598 my $tail = "# --- END PVE ---\n";
be7ee97a 599 my $perms = $options{perms};
a5a4b5aa 600 $data .= "\n" if $data && $data !~ /\n$/;
2edb50e5
WB
601
602 if (!$self->ct_file_exists($file)) {
be7ee97a 603 $self->ct_file_set_contents($file, $head.$data.$tail, $perms) if $data;
2edb50e5
WB
604 return;
605 }
606
607 my $old = $self->ct_file_get_contents($file);
608 my @lines = split(/\n/, $old);
609
610 my ($beg, $end);
611 foreach my $i (0..(@lines-1)) {
612 my $line = $lines[$i];
613 $beg = $i if !defined($beg) &&
614 $line =~ /^#\s*---\s*BEGIN\s*PVE\s*/;
615 $end = $i if !defined($end) && defined($beg) &&
616 $line =~ /^#\s*---\s*END\s*PVE\s*/i;
617 last if defined($beg) && defined($end);
618 }
619
620 if (defined($beg) && defined($end)) {
621 # Found a section
622 if ($data) {
623 chomp $tail;
624 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
fa7cb12b 625 } else {
2edb50e5
WB
626 if ($beg == 0 && $end == (@lines-1)) {
627 $self->ct_unlink($file) if $options{delete};
628 return;
629 }
630 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
631 }
632 $self->ct_file_set_contents($file, join("\n", @lines) . "\n");
633 } elsif ($data) {
634 # No section found
635 my $content = join("\n", @lines);
636 chomp $content;
637 if (!$content && !$data && $options{delete}) {
fa7cb12b 638 $self->ct_unlink($file);
2edb50e5
WB
639 return;
640 }
641 $content .= "\n";
642 $data = $head.$data.$tail;
643 if ($options{replace}) {
be7ee97a 644 $self->ct_file_set_contents($file, $data, $perms);
2edb50e5 645 } elsif ($options{prepend}) {
be7ee97a 646 $self->ct_file_set_contents($file, $data . $content, $perms);
2edb50e5 647 } else { # append
be7ee97a 648 $self->ct_file_set_contents($file, $content . $data, $perms);
fa7cb12b 649 }
fa7cb12b
WB
650 }
651}
652
9096a91d
WB
653sub remove_pve_sections {
654 my ($data) = @_;
655
656 my $head = "# --- BEGIN PVE ---";
657 my $tail = "# --- END PVE ---";
658
659 # Remove the sections enclosed with the above headers and footers.
660 # from a line (^) starting with '\h*$head'
661 # to a line (the other ^) starting with '\h*$tail' up to including that
662 # line's end (.*?$).
663 return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;
664}
665
1c7f4f65 6661;