]>
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; |
bd1dc8d1 | 13 | use File::Basename; |
168d6b07 | 14 | |
b9cd9975 | 15 | use PVE::INotify; |
55fa4e09 | 16 | use PVE::Tools; |
4401b7d4 | 17 | use PVE::Network; |
55fa4e09 | 18 | |
633a7bd8 | 19 | sub 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 | 25 | sub 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 | 57 | sub 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 |
103 | sub template_fixup { |
104 | my ($self, $conf) = @_; | |
105 | ||
106 | # do nothing by default | |
107 | } | |
108 | ||
c325b32f | 109 | sub 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 | 126 | sub 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 | 147 | sub setup_network { |
633a7bd8 | 148 | my ($self, $conf) = @_; |
55fa4e09 DM |
149 | |
150 | die "please implement this inside subclass" | |
151 | } | |
152 | ||
d66768a2 | 153 | sub setup_init { |
633a7bd8 | 154 | my ($self, $conf) = @_; |
1c7f4f65 | 155 | |
d66768a2 DM |
156 | die "please implement this inside subclass" |
157 | } | |
158 | ||
9143dec4 DM |
159 | sub setup_systemd_console { |
160 | my ($self, $conf) = @_; | |
161 | ||
e7deac01 | 162 | my $systemd_dir_rel = $self->ct_is_executable("/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 | ||
5e84bdc8 DM |
201 | # A few distros as well as unprivileged containers cannot deal with the |
202 | # /dev/lxc/ tty subdirectory. | |
203 | sub devttydir { | |
204 | my ($self, $conf) = @_; | |
205 | return $conf->{unprivileged} ? '' : 'lxc/'; | |
206 | } | |
207 | ||
90b21cdc | 208 | sub setup_container_getty_service { |
5e84bdc8 DM |
209 | my ($self, $conf) = @_; |
210 | ||
e7deac01 | 211 | my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ? |
8f115f7c WB |
212 | "/lib/systemd/system" : "/usr/lib/systemd/system"; |
213 | my $servicefile = "$systemd_dir_rel/container-getty\@.service"; | |
90b21cdc | 214 | my $raw = $self->ct_file_get_contents($servicefile); |
5e84bdc8 | 215 | my $ttyname = $self->devttydir($conf) . 'tty%I'; |
418ec240 | 216 | if ($raw =~ s@pts/%I|lxc/tty%I@$ttyname@g) { |
90b21cdc WB |
217 | $self->ct_file_set_contents($servicefile, $raw); |
218 | } | |
219 | } | |
220 | ||
c1d32b55 WB |
221 | sub setup_systemd_networkd { |
222 | my ($self, $conf) = @_; | |
223 | ||
c1d32b55 WB |
224 | foreach my $k (keys %$conf) { |
225 | next if $k !~ m/^net(\d+)$/; | |
1b4cf758 | 226 | my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k}); |
c1d32b55 WB |
227 | next if !$d->{name}; |
228 | ||
f08b2779 | 229 | my $filename = "/etc/systemd/network/$d->{name}.network"; |
c1d32b55 WB |
230 | |
231 | my $data = <<"DATA"; | |
232 | [Match] | |
233 | Name = $d->{name} | |
234 | ||
235 | [Network] | |
236 | Description = Interface $d->{name} autoconfigured by PVE | |
237 | DATA | |
4401b7d4 WB |
238 | |
239 | my $routes = ''; | |
240 | my ($has_ipv4, $has_ipv6); | |
241 | ||
c1d32b55 WB |
242 | # DHCP bitflags: |
243 | my @DHCPMODES = ('none', 'v4', 'v6', 'both'); | |
244 | my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3); | |
245 | my $dhcp = $NONE; | |
246 | ||
247 | if (defined(my $ip = $d->{ip})) { | |
248 | if ($ip eq 'dhcp') { | |
249 | $dhcp |= $DHCP4; | |
250 | } elsif ($ip ne 'manual') { | |
4401b7d4 | 251 | $has_ipv4 = 1; |
c1d32b55 WB |
252 | $data .= "Address = $ip\n"; |
253 | } | |
254 | } | |
255 | if (defined(my $gw = $d->{gw})) { | |
256 | $data .= "Gateway = $gw\n"; | |
4401b7d4 WB |
257 | if ($has_ipv4 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip}, 4)) { |
258 | $routes .= "\n[Route]\nDestination = $gw/32\nScope = link\n"; | |
259 | } | |
c1d32b55 WB |
260 | } |
261 | ||
262 | if (defined(my $ip = $d->{ip6})) { | |
263 | if ($ip eq 'dhcp') { | |
264 | $dhcp |= $DHCP6; | |
265 | } elsif ($ip ne 'manual') { | |
4401b7d4 | 266 | $has_ipv6 = 1; |
c1d32b55 WB |
267 | $data .= "Address = $ip\n"; |
268 | } | |
269 | } | |
270 | if (defined(my $gw = $d->{gw6})) { | |
271 | $data .= "Gateway = $gw\n"; | |
d13fd23a WB |
272 | if ($has_ipv6 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip6}, 6) && |
273 | !PVE::Network::is_ip_in_cidr($gw, 'fe80::/10', 6)) { | |
4401b7d4 WB |
274 | $routes .= "\n[Route]\nDestination = $gw/128\nScope = link\n"; |
275 | } | |
c1d32b55 WB |
276 | } |
277 | ||
278 | $data .= "DHCP = $DHCPMODES[$dhcp]\n"; | |
4401b7d4 | 279 | $data .= $routes if $routes; |
c1d32b55 | 280 | |
f08b2779 | 281 | $self->ct_file_set_contents($filename, $data); |
c1d32b55 | 282 | } |
b7cd927f WB |
283 | } |
284 | ||
285 | sub setup_securetty { | |
286 | my ($self, $conf, @add) = @_; | |
c1d32b55 | 287 | |
f08b2779 | 288 | my $filename = "/etc/securetty"; |
bd3093ef TL |
289 | # root login is already allowed on every device if no securetty present |
290 | return if !$self->ct_file_exists($filename); | |
291 | ||
5e84bdc8 DM |
292 | if (!scalar(@add)) { |
293 | @add = qw(console tty1 tty2 tty3 tty4); | |
294 | if (my $dir = $self->devttydir($conf)) { | |
295 | @add = map { "${dir}$_" } @add; | |
296 | } | |
297 | } | |
298 | ||
f08b2779 | 299 | my $data = $self->ct_file_get_contents($filename); |
b7cd927f WB |
300 | chomp $data; $data .= "\n"; |
301 | foreach my $dev (@add) { | |
302 | if ($data !~ m!^\Q$dev\E\s*$!m) { | |
303 | $data .= "$dev\n"; | |
304 | } | |
305 | } | |
f08b2779 | 306 | $self->ct_file_set_contents($filename, $data); |
c1d32b55 WB |
307 | } |
308 | ||
168d6b07 | 309 | my $replacepw = sub { |
f08b2779 | 310 | my ($self, $file, $user, $epw, $shadow) = @_; |
168d6b07 DM |
311 | |
312 | my $tmpfile = "$file.$$"; | |
313 | ||
314 | eval { | |
f08b2779 | 315 | my $src = $self->ct_open_file_read($file) || |
168d6b07 DM |
316 | die "unable to open file '$file' - $!"; |
317 | ||
f08b2779 | 318 | my $st = $self->ct_stat($src) || |
168d6b07 DM |
319 | die "unable to stat file - $!"; |
320 | ||
f08b2779 | 321 | my $dst = $self->ct_open_file_write($tmpfile) || |
168d6b07 DM |
322 | die "unable to open file '$tmpfile' - $!"; |
323 | ||
324 | # copy owner and permissions | |
325 | chmod $st->mode, $dst; | |
326 | chown $st->uid, $st->gid, $dst; | |
367a7c18 DM |
327 | |
328 | my $last_change = int(time()/(60*60*24)); | |
329 | ||
330 | if ($epw =~ m/^\$TEST\$/) { # for regression tests | |
331 | $last_change = 12345; | |
332 | } | |
168d6b07 DM |
333 | |
334 | while (defined (my $line = <$src>)) { | |
367a7c18 DM |
335 | if ($shadow) { |
336 | $line =~ s/^${user}:[^:]*:[^:]*:/${user}:${epw}:${last_change}:/; | |
337 | } else { | |
338 | $line =~ s/^${user}:[^:]*:/${user}:${epw}:/; | |
339 | } | |
168d6b07 DM |
340 | print $dst $line; |
341 | } | |
342 | ||
343 | $src->close() || die "close '$file' failed - $!\n"; | |
344 | $dst->close() || die "close '$tmpfile' failed - $!\n"; | |
345 | }; | |
346 | if (my $err = $@) { | |
f08b2779 | 347 | $self->ct_unlink($tmpfile); |
168d6b07 | 348 | } else { |
f08b2779 WB |
349 | $self->ct_rename($tmpfile, $file); |
350 | $self->ct_unlink($tmpfile); # in case rename fails | |
168d6b07 DM |
351 | } |
352 | }; | |
353 | ||
354 | sub set_user_password { | |
633a7bd8 | 355 | my ($self, $conf, $user, $opt_password) = @_; |
168d6b07 | 356 | |
f08b2779 | 357 | my $pwfile = "/etc/passwd"; |
168d6b07 | 358 | |
f08b2779 | 359 | return if !$self->ct_file_exists($pwfile); |
168d6b07 | 360 | |
f08b2779 | 361 | my $shadow = "/etc/shadow"; |
168d6b07 DM |
362 | |
363 | if (defined($opt_password)) { | |
364 | if ($opt_password !~ m/^\$/) { | |
365 | my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8); | |
738076ce | 366 | $opt_password = crypt(encode("utf8", $opt_password), "\$1\$$time\$"); |
168d6b07 DM |
367 | }; |
368 | } else { | |
369 | $opt_password = '*'; | |
370 | } | |
371 | ||
f08b2779 WB |
372 | if ($self->ct_file_exists($shadow)) { |
373 | &$replacepw ($self, $shadow, $user, $opt_password, 1); | |
374 | &$replacepw ($self, $pwfile, $user, 'x'); | |
168d6b07 | 375 | } else { |
f08b2779 | 376 | &$replacepw ($self, $pwfile, $user, $opt_password); |
168d6b07 DM |
377 | } |
378 | } | |
379 | ||
f36ce482 FG |
380 | my $parse_home_dir = sub { |
381 | my ($self, $passwdfile, $user) = @_; | |
382 | ||
383 | my $fh = $self->ct_open_file_read($passwdfile); | |
384 | while (defined (my $line = <$fh>)) { | |
385 | return $2 | |
386 | if $line =~ m/^${user}:([^:]*:){4}([^:]*):/; | |
387 | } | |
388 | }; | |
389 | ||
390 | sub set_user_authorized_ssh_keys { | |
391 | my ($self, $conf, $user, $ssh_keys) = @_; | |
392 | ||
393 | my $passwd = "/etc/passwd"; | |
394 | my $home = $user eq "root" ? "/root/" : "/home/$user/"; | |
395 | ||
396 | $home = &$parse_home_dir($self, $passwd, $user) | |
397 | if $self->ct_file_exists($passwd); | |
398 | ||
399 | die "home directory '$home' of $user does not exist!" | |
400 | if ! ($self->ct_is_directory($home) || $self->ct_is_symlink($home)); | |
401 | ||
402 | $self->ct_mkdir("$home/.ssh", 0700) | |
403 | if ! $self->ct_is_directory("$home/.ssh"); | |
404 | ||
405 | $self->ct_modify_file("$home/.ssh/authorized_keys", $ssh_keys, perms => 0700); | |
406 | } | |
407 | ||
4727bd09 DM |
408 | my $randomize_crontab = sub { |
409 | my ($self, $conf) = @_; | |
410 | ||
b5e62cd0 DM |
411 | my @files; |
412 | # Note: dir_glob_foreach() untaints filenames! | |
f08b2779 | 413 | PVE::Tools::dir_glob_foreach("/etc/cron.d", qr/[A-Z\-\_a-z0-9]+/, sub { |
b5e62cd0 | 414 | my ($name) = @_; |
f08b2779 | 415 | push @files, "/etc/cron.d/$name"; |
b5e62cd0 | 416 | }); |
4727bd09 | 417 | |
f08b2779 WB |
418 | my $crontab_fn = "/etc/crontab"; |
419 | unshift @files, $crontab_fn if $self->ct_file_exists($crontab_fn); | |
4727bd09 DM |
420 | |
421 | foreach my $filename (@files) { | |
f08b2779 | 422 | my $data = $self->ct_file_get_contents($filename); |
4727bd09 DM |
423 | my $new = ''; |
424 | foreach my $line (split(/\n/, $data)) { | |
425 | # we only randomize minutes for root crontab entries | |
426 | if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) { | |
427 | my $rest = $1; | |
428 | my $min = int(rand()*59); | |
429 | $new .= "$min$rest\n"; | |
430 | } else { | |
431 | $new .= "$line\n"; | |
432 | } | |
433 | } | |
f08b2779 | 434 | $self->ct_file_set_contents($filename, $new); |
4727bd09 DM |
435 | } |
436 | }; | |
437 | ||
d66768a2 | 438 | sub pre_start_hook { |
633a7bd8 | 439 | my ($self, $conf) = @_; |
d66768a2 | 440 | |
633a7bd8 DM |
441 | $self->setup_init($conf); |
442 | $self->setup_network($conf); | |
443 | $self->set_hostname($conf); | |
444 | $self->set_dns($conf); | |
d66768a2 DM |
445 | |
446 | # fixme: what else ? | |
447 | } | |
448 | ||
449 | sub post_create_hook { | |
f36ce482 | 450 | my ($self, $conf, $root_password, $ssh_keys) = @_; |
d66768a2 | 451 | |
142444d5 | 452 | $self->template_fixup($conf); |
4727bd09 DM |
453 | |
454 | &$randomize_crontab($self, $conf); | |
455 | ||
633a7bd8 | 456 | $self->set_user_password($conf, 'root', $root_password); |
f36ce482 | 457 | $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys; |
633a7bd8 DM |
458 | $self->setup_init($conf); |
459 | $self->setup_network($conf); | |
460 | $self->set_hostname($conf); | |
461 | $self->set_dns($conf); | |
168d6b07 | 462 | |
55fa4e09 | 463 | # fixme: what else ? |
1c7f4f65 DM |
464 | } |
465 | ||
f08b2779 WB |
466 | # File access wrappers for container setup code. |
467 | # For user-namespace support these might need to take uid and gid maps into account. | |
468 | ||
bd1dc8d1 WB |
469 | sub ct_is_file_ignored { |
470 | my ($self, $file) = @_; | |
471 | my ($name, $path) = fileparse($file); | |
472 | return -f "$path/.pve-ignore.$name"; | |
473 | } | |
474 | ||
c6a605f9 WB |
475 | sub ct_reset_ownership { |
476 | my ($self, @files) = @_; | |
477 | my $conf = $self->{conf}; | |
478 | return if !$self->{id_map}; | |
bd1dc8d1 WB |
479 | |
480 | @files = grep { !$self->ct_is_file_ignored($_) } @files; | |
481 | return if !@files; | |
482 | ||
c6a605f9 WB |
483 | my $uid = $self->{rootuid}; |
484 | my $gid = $self->{rootgid}; | |
485 | chown($uid, $gid, @files); | |
486 | } | |
487 | ||
2063d380 WB |
488 | sub ct_mkdir { |
489 | my ($self, $file, $mask) = @_; | |
f08b2779 | 490 | # mkdir goes by parameter count - an `undef' mode acts like a mode of 0000 |
c6a605f9 WB |
491 | if (defined($mask)) { |
492 | return CORE::mkdir($file, $mask) && $self->ct_reset_ownership($file); | |
493 | } else { | |
494 | return CORE::mkdir($file) && $self->ct_reset_ownership($file); | |
495 | } | |
2063d380 WB |
496 | } |
497 | ||
498 | sub ct_unlink { | |
f08b2779 WB |
499 | my ($self, @files) = @_; |
500 | foreach my $file (@files) { | |
bd1dc8d1 | 501 | next if $self->ct_is_file_ignored($file); |
f08b2779 WB |
502 | CORE::unlink($file); |
503 | } | |
504 | } | |
505 | ||
506 | sub ct_rename { | |
507 | my ($self, $old, $new) = @_; | |
bd1dc8d1 | 508 | return if $self->ct_is_file_ignored($new); |
f08b2779 | 509 | CORE::rename($old, $new); |
2063d380 WB |
510 | } |
511 | ||
f08b2779 | 512 | sub ct_open_file_read { |
2063d380 | 513 | my $self = shift; |
f08b2779 WB |
514 | my $file = shift; |
515 | return IO::File->new($file, O_RDONLY, @_); | |
2063d380 WB |
516 | } |
517 | ||
f08b2779 | 518 | sub ct_open_file_write { |
2063d380 | 519 | my $self = shift; |
f08b2779 | 520 | my $file = shift; |
bd1dc8d1 | 521 | $file = '/dev/null' if $self->ct_is_file_ignored($file); |
c6a605f9 WB |
522 | my $fh = IO::File->new($file, O_WRONLY | O_CREAT, @_); |
523 | $self->ct_reset_ownership($fh); | |
524 | return $fh; | |
2063d380 WB |
525 | } |
526 | ||
f08b2779 | 527 | sub ct_make_path { |
2063d380 | 528 | my $self = shift; |
c6a605f9 WB |
529 | if ($self->{id_map}) { |
530 | my $opts = pop; | |
531 | if (ref($opts) eq 'HASH') { | |
532 | $opts->{owner} = $self->{rootuid} if !defined($self->{owner}); | |
533 | $opts->{group} = $self->{rootgid} if !defined($self->{group}); | |
534 | } | |
535 | File::Path::make_path(@_, $opts); | |
536 | } else { | |
537 | File::Path::make_path(@_); | |
538 | } | |
2063d380 WB |
539 | } |
540 | ||
541 | sub ct_symlink { | |
542 | my ($self, $old, $new) = @_; | |
bd1dc8d1 | 543 | return if $self->ct_is_file_ignored($new); |
f08b2779 | 544 | return CORE::symlink($old, $new); |
2063d380 WB |
545 | } |
546 | ||
8f115f7c WB |
547 | sub ct_readlink { |
548 | my ($self, $name) = @_; | |
549 | return CORE::readlink($name); | |
550 | } | |
551 | ||
2063d380 WB |
552 | sub ct_file_exists { |
553 | my ($self, $file) = @_; | |
f08b2779 WB |
554 | return -f $file; |
555 | } | |
556 | ||
557 | sub ct_is_directory { | |
558 | my ($self, $file) = @_; | |
559 | return -d $file; | |
560 | } | |
561 | ||
562 | sub ct_is_symlink { | |
563 | my ($self, $file) = @_; | |
564 | return -l $file; | |
565 | } | |
566 | ||
e7deac01 WB |
567 | sub ct_is_executable { |
568 | my ($self, $file) = @_; | |
569 | return -x $file | |
570 | } | |
571 | ||
f08b2779 WB |
572 | sub ct_stat { |
573 | my ($self, $file) = @_; | |
574 | return File::stat::stat($file); | |
2063d380 WB |
575 | } |
576 | ||
577 | sub ct_file_read_firstline { | |
578 | my ($self, $file) = @_; | |
f08b2779 | 579 | return PVE::Tools::file_read_firstline($file); |
2063d380 WB |
580 | } |
581 | ||
582 | sub ct_file_get_contents { | |
583 | my ($self, $file) = @_; | |
f08b2779 | 584 | return PVE::Tools::file_get_contents($file); |
2063d380 WB |
585 | } |
586 | ||
587 | sub ct_file_set_contents { | |
39243220 | 588 | my ($self, $file, $data, $perms) = @_; |
bd1dc8d1 | 589 | return if $self->ct_is_file_ignored($file); |
c6a605f9 WB |
590 | PVE::Tools::file_set_contents($file, $data, $perms); |
591 | $self->ct_reset_ownership($file); | |
2063d380 WB |
592 | } |
593 | ||
2edb50e5 WB |
594 | # Modify a marked portion of a file. |
595 | # Optionally if the file becomes empty it will be deleted. | |
596 | sub ct_modify_file { | |
597 | my ($self, $file, $data, %options) = @_; | |
bd1dc8d1 | 598 | return if $self->ct_is_file_ignored($file); |
2edb50e5 WB |
599 | |
600 | my $head = "# --- BEGIN PVE ---\n"; | |
601 | my $tail = "# --- END PVE ---\n"; | |
be7ee97a | 602 | my $perms = $options{perms}; |
a5a4b5aa | 603 | $data .= "\n" if $data && $data !~ /\n$/; |
2edb50e5 WB |
604 | |
605 | if (!$self->ct_file_exists($file)) { | |
be7ee97a | 606 | $self->ct_file_set_contents($file, $head.$data.$tail, $perms) if $data; |
2edb50e5 WB |
607 | return; |
608 | } | |
609 | ||
610 | my $old = $self->ct_file_get_contents($file); | |
611 | my @lines = split(/\n/, $old); | |
612 | ||
613 | my ($beg, $end); | |
614 | foreach my $i (0..(@lines-1)) { | |
615 | my $line = $lines[$i]; | |
616 | $beg = $i if !defined($beg) && | |
617 | $line =~ /^#\s*---\s*BEGIN\s*PVE\s*/; | |
618 | $end = $i if !defined($end) && defined($beg) && | |
619 | $line =~ /^#\s*---\s*END\s*PVE\s*/i; | |
620 | last if defined($beg) && defined($end); | |
621 | } | |
622 | ||
623 | if (defined($beg) && defined($end)) { | |
624 | # Found a section | |
625 | if ($data) { | |
626 | chomp $tail; | |
627 | splice @lines, $beg, $end-$beg+1, $head.$data.$tail; | |
fa7cb12b | 628 | } else { |
2edb50e5 WB |
629 | if ($beg == 0 && $end == (@lines-1)) { |
630 | $self->ct_unlink($file) if $options{delete}; | |
631 | return; | |
632 | } | |
633 | splice @lines, $beg, $end-$beg+1, $head.$data.$tail; | |
634 | } | |
635 | $self->ct_file_set_contents($file, join("\n", @lines) . "\n"); | |
636 | } elsif ($data) { | |
637 | # No section found | |
638 | my $content = join("\n", @lines); | |
639 | chomp $content; | |
640 | if (!$content && !$data && $options{delete}) { | |
fa7cb12b | 641 | $self->ct_unlink($file); |
2edb50e5 WB |
642 | return; |
643 | } | |
644 | $content .= "\n"; | |
645 | $data = $head.$data.$tail; | |
646 | if ($options{replace}) { | |
be7ee97a | 647 | $self->ct_file_set_contents($file, $data, $perms); |
2edb50e5 | 648 | } elsif ($options{prepend}) { |
be7ee97a | 649 | $self->ct_file_set_contents($file, $data . $content, $perms); |
2edb50e5 | 650 | } else { # append |
be7ee97a | 651 | $self->ct_file_set_contents($file, $content . $data, $perms); |
fa7cb12b | 652 | } |
fa7cb12b WB |
653 | } |
654 | } | |
655 | ||
9096a91d WB |
656 | sub remove_pve_sections { |
657 | my ($data) = @_; | |
658 | ||
659 | my $head = "# --- BEGIN PVE ---"; | |
660 | my $tail = "# --- END PVE ---"; | |
661 | ||
662 | # Remove the sections enclosed with the above headers and footers. | |
663 | # from a line (^) starting with '\h*$head' | |
664 | # to a line (the other ^) starting with '\h*$tail' up to including that | |
665 | # line's end (.*?$). | |
666 | return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms; | |
667 | } | |
668 | ||
1c7f4f65 | 669 | 1; |