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