mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Merge pull request #6292 from btw616:local-timezone
PiperOrigin-RevId: 386988406
This commit is contained in:
@@ -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 {
|
||||
|
||||
+10
-4
@@ -285,16 +285,22 @@ func setupRootFS(spec *specs.Spec, conf *config.Config) error {
|
||||
|
||||
// Prepare tree structure for pivot_root(2).
|
||||
if err := os.Mkdir("/proc/proc", 0755); err != nil {
|
||||
Fatalf("%v", err)
|
||||
Fatalf("error creating /proc/proc: %v", err)
|
||||
}
|
||||
if err := os.Mkdir("/proc/root", 0755); err != nil {
|
||||
Fatalf("%v", err)
|
||||
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"
|
||||
}
|
||||
@@ -409,7 +415,7 @@ func resolveMounts(conf *config.Config, mounts []specs.Mount, root string) ([]sp
|
||||
panic(fmt.Sprintf("%q could not be made relative to %q: %v", dst, root, err))
|
||||
}
|
||||
|
||||
opts, err := adjustMountOptions(filepath.Join(root, relDst), m.Options)
|
||||
opts, err := adjustMountOptions(conf, filepath.Join(root, relDst), m.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -475,7 +481,7 @@ func resolveSymlinksImpl(root, base, rel string, followCount uint) (string, erro
|
||||
}
|
||||
|
||||
// adjustMountOptions adds 'overlayfs_stale_read' if mounting over overlayfs.
|
||||
func adjustMountOptions(path string, opts []string) ([]string, error) {
|
||||
func adjustMountOptions(conf *config.Config, path string, opts []string) ([]string, error) {
|
||||
rv := make([]string, len(opts))
|
||||
copy(rv, opts)
|
||||
|
||||
|
||||
@@ -68,13 +68,15 @@ func TestChroot(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("error listing %q: %v", chroot, err)
|
||||
}
|
||||
if want, got := 1, len(fi); want != got {
|
||||
if want, got := 2, len(fi); want != got {
|
||||
t.Fatalf("chroot dir got %d entries, want %d", got, want)
|
||||
}
|
||||
|
||||
// chroot dir is prepared by runsc and should contains only /proc.
|
||||
if fi[0].Name() != "proc" {
|
||||
t.Errorf("chroot got children %v, want %v", fi[0].Name(), "proc")
|
||||
// chroot dir is prepared by runsc and should contains only /etc and /proc.
|
||||
for i, want := range []string{"etc", "proc"} {
|
||||
if got := fi[i].Name(); got != want {
|
||||
t.Errorf("chroot got child %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
d.CleanUp(ctx)
|
||||
|
||||
Reference in New Issue
Block a user