Skip to content

Commit 57ce748

Browse files
author
Shreyansh Sancheti
committed
guest: use OCIBundlePath as sandbox root source of truth
Refactor the guest runtime to derive all sandbox paths from the host-provided OCIBundlePath rather than reconstructing them from hardcoded prefixes. This makes the GCS path-agnostic: it works with any OCIBundlePath the shim sends, regardless of prefix. Key changes: - runc.NewRuntime() no longer takes a base log path; runc.log is co-located with each container's bundlePath - Host.sandboxRoots map tracks the resolved sandbox root per container - CreateContainer registers sandbox roots; RemoveContainer cleans up - Sandbox, standalone, and workload container setup functions accept a resolved sandboxRoot parameter - New *FromRoot helpers in spec.go for sandbox subdirectory paths - runtime.Runtime.CreateContainer now takes sandboxID parameter Signed-off-by: Shreyansh Jain <shreyanshjain7174@gmail.com> Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
1 parent c0ec0f1 commit 57ce748

File tree

14 files changed

+736
-245
lines changed

14 files changed

+736
-245
lines changed

cmd/gcs/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2"
2828
"github.com/Microsoft/hcsshim/internal/guest/runtime/runc"
2929
"github.com/Microsoft/hcsshim/internal/guest/transport"
30-
"github.com/Microsoft/hcsshim/internal/guestpath"
3130
"github.com/Microsoft/hcsshim/internal/log"
3231
"github.com/Microsoft/hcsshim/internal/oc"
3332
"github.com/Microsoft/hcsshim/internal/version"
@@ -297,8 +296,6 @@ func main() {
297296

298297
log.SetScrubbing(*scrubLogs)
299298

300-
baseLogPath := guestpath.LCOWRootPrefixInUVM
301-
302299
logrus.WithFields(logrus.Fields{
303300
"branch": version.Branch,
304301
"commit": version.Commit,
@@ -384,7 +381,7 @@ func main() {
384381
}
385382

386383
tport := &transport.VsockTransport{}
387-
rtime, err := runc.NewRuntime(baseLogPath)
384+
rtime, err := runc.NewRuntime()
388385
if err != nil {
389386
logrus.WithError(err).Fatal("failed to initialize new runc runtime")
390387
}

internal/guest/runtime/hcsv2/container.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/Microsoft/hcsshim/internal/oc"
3030
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
3131
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
32-
"github.com/Microsoft/hcsshim/pkg/annotations"
3332
)
3433

3534
// containerStatus has been introduced to enable parallel container creation
@@ -76,6 +75,10 @@ type Container struct {
7675
// of this container is located. Usually, this is either `/run/gcs/c/<containerID>` or
7776
// `/run/gcs/c/<UVMID>/container_<containerID>` if scratch is shared with UVM scratch.
7877
scratchDirPath string
78+
79+
// sandboxRoot is the root directory of the pod within the guest.
80+
// Used during cleanup to unmount sandbox-specific paths.
81+
sandboxRoot string
7982
}
8083

8184
func (c *Container) Start(ctx context.Context, conSettings stdio.ConnectionSettings) (_ int, err error) {
@@ -228,25 +231,19 @@ func (c *Container) Kill(ctx context.Context, signal syscall.Signal) error {
228231
func (c *Container) Delete(ctx context.Context) error {
229232
entity := log.G(ctx).WithField(logfields.ContainerID, c.id)
230233
entity.Info("opengcs::Container::Delete")
231-
if c.isSandbox {
232-
// Check if this is a virtual pod
233-
virtualSandboxID := ""
234-
if c.spec != nil && c.spec.Annotations != nil {
235-
virtualSandboxID = c.spec.Annotations[annotations.VirtualPodID]
236-
}
237-
238-
// remove user mounts in sandbox container - use virtual pod aware paths
239-
if err := storage.UnmountAllInPath(ctx, specGuest.VirtualPodAwareSandboxMountsDir(c.id, virtualSandboxID), true); err != nil {
234+
if c.isSandbox && c.sandboxRoot != "" {
235+
// remove user mounts in sandbox container
236+
if err := storage.UnmountAllInPath(ctx, specGuest.SandboxMountsDirFromRoot(c.sandboxRoot), true); err != nil {
240237
entity.WithError(err).Error("failed to unmount sandbox mounts")
241238
}
242239

243-
// remove user mounts in tmpfs sandbox container - use virtual pod aware paths
244-
if err := storage.UnmountAllInPath(ctx, specGuest.VirtualPodAwareSandboxTmpfsMountsDir(c.id, virtualSandboxID), true); err != nil {
240+
// remove tmpfs mounts in sandbox container
241+
if err := storage.UnmountAllInPath(ctx, specGuest.SandboxTmpfsMountsDirFromRoot(c.sandboxRoot), true); err != nil {
245242
entity.WithError(err).Error("failed to unmount tmpfs sandbox mounts")
246243
}
247244

248-
// remove hugepages mounts in sandbox container - use virtual pod aware paths
249-
if err := storage.UnmountAllInPath(ctx, specGuest.VirtualPodAwareHugePagesMountsDir(c.id, virtualSandboxID), true); err != nil {
245+
// remove hugepages mounts in sandbox container
246+
if err := storage.UnmountAllInPath(ctx, specGuest.SandboxHugePagesMountsDirFromRoot(c.sandboxRoot), true); err != nil {
250247
entity.WithError(err).Error("failed to unmount hugepages mounts")
251248
}
252249
}

internal/guest/runtime/hcsv2/sandbox_container.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,30 @@ import (
2020
"github.com/Microsoft/hcsshim/pkg/annotations"
2121
)
2222

23-
func getSandboxHostnamePath(id, virtualSandboxID string) string {
24-
return filepath.Join(specGuest.VirtualPodAwareSandboxRootDir(id, virtualSandboxID), "hostname")
23+
func getSandboxHostnamePath(sandboxRoot string) string {
24+
return filepath.Join(sandboxRoot, "hostname")
2525
}
2626

27-
func getSandboxHostsPath(id, virtualSandboxID string) string {
28-
return filepath.Join(specGuest.VirtualPodAwareSandboxRootDir(id, virtualSandboxID), "hosts")
27+
func getSandboxHostsPath(sandboxRoot string) string {
28+
return filepath.Join(sandboxRoot, "hosts")
2929
}
3030

31-
func getSandboxResolvPath(id, virtualSandboxID string) string {
32-
return filepath.Join(specGuest.VirtualPodAwareSandboxRootDir(id, virtualSandboxID), "resolv.conf")
31+
func getSandboxResolvPath(sandboxRoot string) string {
32+
return filepath.Join(sandboxRoot, "resolv.conf")
3333
}
3434

35-
func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (err error) {
35+
func setupSandboxContainerSpec(ctx context.Context, id, sandboxRoot string, spec *oci.Spec) (err error) {
3636
ctx, span := oc.StartSpan(ctx, "hcsv2::setupSandboxContainerSpec")
3737
defer span.End()
3838
defer func() { oc.SetSpanStatus(span, err) }()
3939
span.AddAttributes(trace.StringAttribute("cid", id))
4040

41-
// Check if this is a virtual pod to use appropriate root directory
42-
virtualSandboxID := spec.Annotations[annotations.VirtualPodID]
43-
44-
// Generate the sandbox root dir - virtual pod aware
45-
rootDir := specGuest.VirtualPodAwareSandboxRootDir(id, virtualSandboxID)
46-
if err := os.MkdirAll(rootDir, 0755); err != nil {
47-
return errors.Wrapf(err, "failed to create sandbox root directory %q", rootDir)
41+
if err := os.MkdirAll(sandboxRoot, 0755); err != nil {
42+
return errors.Wrapf(err, "failed to create sandbox root directory %q", sandboxRoot)
4843
}
4944
defer func() {
5045
if err != nil {
51-
_ = os.RemoveAll(rootDir)
46+
_ = os.RemoveAll(sandboxRoot)
5247
}
5348
}()
5449

@@ -62,19 +57,20 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
6257
}
6358
}
6459

65-
sandboxHostnamePath := getSandboxHostnamePath(id, virtualSandboxID)
60+
sandboxHostnamePath := getSandboxHostnamePath(sandboxRoot)
6661
if err := os.WriteFile(sandboxHostnamePath, []byte(hostname+"\n"), 0644); err != nil {
6762
return errors.Wrapf(err, "failed to write hostname to %q", sandboxHostnamePath)
6863
}
6964

7065
// Write the hosts
7166
sandboxHostsContent := network.GenerateEtcHostsContent(ctx, hostname)
72-
sandboxHostsPath := getSandboxHostsPath(id, virtualSandboxID)
67+
sandboxHostsPath := getSandboxHostsPath(sandboxRoot)
7368
if err := os.WriteFile(sandboxHostsPath, []byte(sandboxHostsContent), 0644); err != nil {
7469
return errors.Wrapf(err, "failed to write sandbox hosts to %q", sandboxHostsPath)
7570
}
7671

7772
// Check if this is a virtual pod sandbox container by comparing container ID with virtual pod ID
73+
virtualSandboxID := spec.Annotations[annotations.VirtualPodID]
7874
isVirtualPodSandbox := virtualSandboxID != "" && id == virtualSandboxID
7975
if strings.EqualFold(spec.Annotations[annotations.SkipPodNetworking], "true") || isVirtualPodSandbox {
8076
ns := GetOrAddNetworkNamespace(specGuest.GetNetworkNamespaceID(spec))
@@ -97,7 +93,7 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
9793
if err != nil {
9894
return errors.Wrap(err, "failed to generate sandbox resolv.conf content")
9995
}
100-
sandboxResolvPath := getSandboxResolvPath(id, virtualSandboxID)
96+
sandboxResolvPath := getSandboxResolvPath(sandboxRoot)
10197
if err := os.WriteFile(sandboxResolvPath, []byte(resolvContent), 0644); err != nil {
10298
return errors.Wrap(err, "failed to write sandbox resolv.conf")
10399
}

0 commit comments

Comments
 (0)