runsc: fix the local timezone support in logs

This patch fixes the local timezone support in logs by creating
etc/localtime in the rootfs of sandbox process and gofer process
based on the current /etc/localtime on host.

Before this patch, the timestamps in sandbox and gofer logs will
fallback to UTC timezone after execving "/proc/self/exe" which
may not be very convenient for users to analyse the logs:

I0708 15:37:43.825100       1 chroot.go:69] Setting up sandbox chroot in "/tmp"
I0708 15:37:43.825189       1 chroot.go:31] Mounting "proc" at "/tmp/proc"
......
I0708 15:37:43.850926       1 cmd.go:73] Execve "/proc/self/exe" again, bye!
I0708 07:37:43.856719       1 main.go:218] ***************************
I0708 07:37:43.856751       1 main.go:219] Args: [runsc-sandbox --root=/run/...]
I0708 07:37:43.856785       1 main.go:220] Version release-20210628.0-27-g02fec8dba5a6
I0708 07:37:43.856795       1 main.go:221] GOOS: linux
I0708 07:37:43.856803       1 main.go:222] GOARCH: amd64
......

Fixes #1984

Signed-off-by: Tiwei Bie <tiwei.btw@antgroup.com>
This commit is contained in:
Tiwei Bie
2021-07-09 10:14:17 +08:00
parent c4c5f4d92a
commit c7ac581049
2 changed files with 31 additions and 0 deletions
+25
View File
@@ -59,6 +59,23 @@ func pivotRoot(root string) error {
return nil
}
func copyFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = out.ReadFrom(in)
return err
}
// setUpChroot creates an empty directory with runsc mounted at /runsc and proc
// mounted at /proc.
func setUpChroot(pidns bool) error {
@@ -78,6 +95,14 @@ func setUpChroot(pidns bool) error {
return fmt.Errorf("error mounting tmpfs in choot: %v", err)
}
if err := os.Mkdir(filepath.Join(chroot, "etc"), 0755); err != nil {
return fmt.Errorf("error creating /etc in chroot: %v", err)
}
if err := copyFile(filepath.Join(chroot, "etc/localtime"), "/etc/localtime"); err != nil {
log.Warningf("Failed to copy /etc/localtime: %v. UTC timezone will be used.", err)
}
if pidns {
flags := uint32(unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC | unix.MS_RDONLY)
if err := mountInChroot(chroot, "proc", "/proc", "proc", flags); err != nil {
+6
View File
@@ -290,11 +290,17 @@ func setupRootFS(spec *specs.Spec, conf *config.Config) error {
if err := os.Mkdir("/proc/root", 0755); err != nil {
Fatalf("error creating /proc/root: %v", err)
}
if err := os.Mkdir("/proc/etc", 0755); err != nil {
Fatalf("error creating /proc/etc: %v", err)
}
// This cannot use SafeMount because there's no available procfs. But we
// know that /proc is an empty tmpfs mount, so this is safe.
if err := unix.Mount("runsc-proc", "/proc/proc", "proc", flags|unix.MS_RDONLY, ""); err != nil {
Fatalf("error mounting proc: %v", err)
}
if err := copyFile("/proc/etc/localtime", "/etc/localtime"); err != nil {
log.Warningf("Failed to copy /etc/localtime: %v. UTC timezone will be used.", err)
}
root = "/proc/root"
procPath = "/proc/proc"
}