cleanup start-stop-daemon without root prefix when performing a pivot-root

This commit is contained in:
Johannes Schauer Marin Rodrigues 2024-01-30 07:41:08 +01:00
parent 59c9c399c6
commit 65c27a55b3
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -1551,33 +1551,41 @@ sub setup_mounts {
if (any { $_ eq 'chroot/start-stop-daemon' } @{ $options->{skip} }) { if (any { $_ eq 'chroot/start-stop-daemon' } @{ $options->{skip} }) {
info "skipping chroot/start-stop-daemon as requested"; info "skipping chroot/start-stop-daemon as requested";
} else { } else {
# $options->{root} must not be part of $ssdloc but must instead be
# evaluated at the time the cleanup is run or otherwise, when
# performing a pivot-root, the ssd location will still be prefixed
# with the chroot path even though we changed root
my $ssdloc; my $ssdloc;
if (-f "$options->{root}/sbin/start-stop-daemon") { if (-f "$options->{root}/sbin/start-stop-daemon") {
$ssdloc = "$options->{root}/sbin/start-stop-daemon"; $ssdloc = "/sbin/start-stop-daemon";
} elsif (-f "$options->{root}/usr/sbin/start-stop-daemon") { } elsif (-f "$options->{root}/usr/sbin/start-stop-daemon") {
$ssdloc = "$options->{root}/usr/sbin/start-stop-daemon"; $ssdloc = "/usr/sbin/start-stop-daemon";
} }
push @cleanup_tasks, sub { push @cleanup_tasks, sub {
return unless length $ssdloc; return unless length $ssdloc;
if (-e "$ssdloc.REAL") { if (-e "$options->{root}/$ssdloc.REAL") {
move("$ssdloc.REAL", "$ssdloc") move(
or error "cannot move start-stop-daemon: $!"; "$options->{root}/$ssdloc.REAL",
"$options->{root}/$ssdloc"
) or error "cannot move start-stop-daemon: $!";
} }
}; };
if (length $ssdloc) { if (length $ssdloc) {
if (-e "$ssdloc.REAL") { if (-e "$options->{root}/$ssdloc.REAL") {
error "$ssdloc.REAL already exists"; error "$options->{root}/$ssdloc.REAL already exists";
} }
move("$ssdloc", "$ssdloc.REAL") move(
or error "cannot move start-stop-daemon: $!"; "$options->{root}/$ssdloc",
open my $fh, '>', $ssdloc "$options->{root}/$ssdloc.REAL"
) or error "cannot move start-stop-daemon: $!";
open my $fh, '>', "$options->{root}/$ssdloc"
or error "cannot open start-stop-daemon: $!"; or error "cannot open start-stop-daemon: $!";
print $fh "#!/bin/sh\n"; print $fh "#!/bin/sh\n";
print $fh print $fh
"echo \"Warning: Fake start-stop-daemon called, doing" "echo \"Warning: Fake start-stop-daemon called, doing"
. " nothing\">&2\n"; . " nothing\">&2\n";
close $fh; close $fh;
chmod 0755, "$ssdloc" chmod 0755, "$options->{root}/$ssdloc"
or error "cannot chmod start-stop-daemon: $!"; or error "cannot chmod start-stop-daemon: $!";
} }
} }