Skip to content

Commit 8e44355

Browse files
chroot before systemd switches root
chroot to the directory where systemd switches root from the initramfs. Otherwise the svg file cannot be saved.
1 parent 3f1dccd commit 8e44355

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/bootchart.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ int main(int argc, char *argv[]) {
333333
.sa_handler = signal_handler,
334334
};
335335
bool has_procfs = false;
336+
bool has_sysroot = false;
336337

337338
parse_conf();
338339

@@ -433,6 +434,25 @@ int main(int argc, char *argv[]) {
433434
if (r < 0)
434435
return EXIT_FAILURE;
435436
}
437+
if (!has_sysroot) {
438+
// sysroot is where systemd will switch root when
439+
// switching from initramfs to the root fs. We chroot
440+
// there so we can write later the svg file.
441+
//
442+
// TODO instead of detecting this way, use signal by initramfs signal.
443+
has_sysroot = access("/sysroot", F_OK) == 0;
444+
if (has_sysroot) {
445+
// chroot does not change the cwd of the process, so we do that first
446+
if (chdir("/sysroot") != 0) {
447+
log_info_errno(errno, "cannot chdir to /sysroot: %m\n");
448+
return EXIT_FAILURE;
449+
}
450+
if (chroot(".") != 0) {
451+
log_info_errno(errno, "cannot chroot: %m\n");
452+
return EXIT_FAILURE;
453+
}
454+
}
455+
}
436456

437457
sample_stop = gettime_ns();
438458

0 commit comments

Comments
 (0)