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