Skip to content

Commit

Permalink
Guest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevpar committed Oct 2, 2024
1 parent 4e295e0 commit 5981cd9
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 169 deletions.
5 changes: 1 addition & 4 deletions cmd/gcs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2"
"github.com/Microsoft/hcsshim/internal/guest/runtime/runc"
"github.com/Microsoft/hcsshim/internal/guest/transport"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/internal/version"
Expand Down Expand Up @@ -270,8 +269,6 @@ func main() {

log.SetScrubbing(*scrubLogs)

baseLogPath := guestpath.LCOWRootPrefixInUVM

logrus.WithFields(logrus.Fields{
"branch": version.Branch,
"commit": version.Commit,
Expand All @@ -295,7 +292,7 @@ func main() {
go kmsg.ReadForever(kmsg.LogLevel(*kmsgLogLevel))

tport := &transport.VsockTransport{}
rtime, err := runc.NewRuntime(baseLogPath)
rtime, err := runc.NewRuntime()
if err != nil {
logrus.WithError(err).Fatal("failed to initialize new runc runtime")
}
Expand Down
7 changes: 1 addition & 6 deletions internal/guest/bridge/bridge_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,9 @@ func (b *Bridge) deleteContainerStateV2(r *Request) (_ RequestResponse, err erro
return nil, errors.Wrapf(err, "failed to unmarshal JSON in message \"%s\"", r.Message)
}

c, err := b.hostState.GetCreatedContainer(request.ContainerID)
if err != nil {
return nil, err
}
// remove container state regardless of delete's success
defer b.hostState.RemoveContainer(request.ContainerID)

if err := c.Delete(ctx); err != nil {
if err := b.hostState.DeleteContainer(ctx, request.ContainerID); err != nil {
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions internal/guest/runtime/hcsv2/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/Microsoft/hcsshim/internal/guest/gcserr"
"github.com/Microsoft/hcsshim/internal/guest/prot"
"github.com/Microsoft/hcsshim/internal/guest/runtime"
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
"github.com/Microsoft/hcsshim/internal/guest/stdio"
"github.com/Microsoft/hcsshim/internal/guest/storage"
"github.com/Microsoft/hcsshim/internal/guest/transport"
Expand All @@ -46,6 +45,7 @@ const (

type Container struct {
id string
sbid string
vsock transport.Transport

spec *oci.Spec
Expand Down Expand Up @@ -189,17 +189,17 @@ func (c *Container) Kill(ctx context.Context, signal syscall.Signal) error {
return nil
}

func (c *Container) Delete(ctx context.Context) error {
func (c *Container) Delete(ctx context.Context, sbCtx *mountContext) error {
entity := log.G(ctx).WithField(logfields.ContainerID, c.id)
entity.Info("opengcs::Container::Delete")
if c.isSandbox {
// remove user mounts in sandbox container
if err := storage.UnmountAllInPath(ctx, specInternal.SandboxMountsDir(c.id), true); err != nil {
if err := storage.UnmountAllInPath(ctx, sbCtx.sandboxMountsRoot, true); err != nil {
entity.WithError(err).Error("failed to unmount sandbox mounts")
}

// remove hugepages mounts in sandbox container
if err := storage.UnmountAllInPath(ctx, specInternal.HugePagesMountsDir(c.id), true); err != nil {
if err := storage.UnmountAllInPath(ctx, sbCtx.hugePagesRoot, true); err != nil {
entity.WithError(err).Error("failed to unmount hugepages mounts")
}
}
Expand Down
23 changes: 5 additions & 18 deletions internal/guest/runtime/hcsv2/sandbox_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,18 @@ import (
"go.opencensus.io/trace"

"github.com/Microsoft/hcsshim/internal/guest/network"
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/pkg/annotations"
)

func getSandboxHostnamePath(id string) string {
return filepath.Join(specInternal.SandboxRootDir(id), "hostname")
}

func getSandboxHostsPath(id string) string {
return filepath.Join(specInternal.SandboxRootDir(id), "hosts")
}

func getSandboxResolvPath(id string) string {
return filepath.Join(specInternal.SandboxRootDir(id), "resolv.conf")
}

func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (err error) {
func setupSandboxContainerSpec(ctx context.Context, sbCtx *mountContext, id string, spec *oci.Spec) (err error) {
ctx, span := oc.StartSpan(ctx, "hcsv2::setupSandboxContainerSpec")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", id))

// Generate the sandbox root dir
rootDir := specInternal.SandboxRootDir(id)
rootDir := sbCtx.bundleRoot
if err := os.MkdirAll(rootDir, 0755); err != nil {
return errors.Wrapf(err, "failed to create sandbox root directory %q", rootDir)
}
Expand All @@ -58,14 +45,14 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
}
}

sandboxHostnamePath := getSandboxHostnamePath(id)
sandboxHostnamePath := filepath.Join(sbCtx.networkMountsRoot, "hostname")
if err := os.WriteFile(sandboxHostnamePath, []byte(hostname+"\n"), 0644); err != nil {
return errors.Wrapf(err, "failed to write hostname to %q", sandboxHostnamePath)
}

// Write the hosts
sandboxHostsContent := network.GenerateEtcHostsContent(ctx, hostname)
sandboxHostsPath := getSandboxHostsPath(id)
sandboxHostsPath := filepath.Join(sbCtx.networkMountsRoot, "hosts")
if err := os.WriteFile(sandboxHostsPath, []byte(sandboxHostsContent), 0644); err != nil {
return errors.Wrapf(err, "failed to write sandbox hosts to %q", sandboxHostsPath)
}
Expand All @@ -88,7 +75,7 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
if err != nil {
return errors.Wrap(err, "failed to generate sandbox resolv.conf content")
}
sandboxResolvPath := getSandboxResolvPath(id)
sandboxResolvPath := filepath.Join(sbCtx.networkMountsRoot, "resolv.conf")
if err := os.WriteFile(sandboxResolvPath, []byte(resolvContent), 0644); err != nil {
return errors.Wrap(err, "failed to write sandbox resolv.conf")
}
Expand Down
33 changes: 8 additions & 25 deletions internal/guest/runtime/hcsv2/standalone_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,17 @@ import (

"github.com/Microsoft/hcsshim/internal/guest/network"
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/oc"
)

func getStandaloneRootDir(id string) string {
return filepath.Join(guestpath.LCOWRootPrefixInUVM, id)
}

func getStandaloneHostnamePath(id string) string {
return filepath.Join(getStandaloneRootDir(id), "hostname")
}

func getStandaloneHostsPath(id string) string {
return filepath.Join(getStandaloneRootDir(id), "hosts")
}

func getStandaloneResolvPath(id string) string {
return filepath.Join(getStandaloneRootDir(id), "resolv.conf")
}

func setupStandaloneContainerSpec(ctx context.Context, id string, spec *oci.Spec) (err error) {
func setupStandaloneContainerSpec(ctx context.Context, sbCtx *mountContext, id string, spec *oci.Spec) (err error) {
ctx, span := oc.StartSpan(ctx, "hcsv2::setupStandaloneContainerSpec")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", id))

// Generate the standalone root dir
rootDir := getStandaloneRootDir(id)
rootDir := sbCtx.bundleRoot
if err := os.MkdirAll(rootDir, 0755); err != nil {
return errors.Wrapf(err, "failed to create container root directory %q", rootDir)
}
Expand All @@ -63,15 +46,15 @@ func setupStandaloneContainerSpec(ctx context.Context, id string, spec *oci.Spec

// Write the hostname
if !specInternal.MountPresent("/etc/hostname", spec.Mounts) {
standaloneHostnamePath := getStandaloneHostnamePath(id)
standaloneHostnamePath := filepath.Join(sbCtx.networkMountsRoot, "hostname")
if err := os.WriteFile(standaloneHostnamePath, []byte(hostname+"\n"), 0644); err != nil {
return errors.Wrapf(err, "failed to write hostname to %q", standaloneHostnamePath)
}

mt := oci.Mount{
Destination: "/etc/hostname",
Type: "bind",
Source: getStandaloneHostnamePath(id),
Source: standaloneHostnamePath,
Options: []string{"bind"},
}
if isRootReadonly(spec) {
Expand All @@ -83,15 +66,15 @@ func setupStandaloneContainerSpec(ctx context.Context, id string, spec *oci.Spec
// Write the hosts
if !specInternal.MountPresent("/etc/hosts", spec.Mounts) {
standaloneHostsContent := network.GenerateEtcHostsContent(ctx, hostname)
standaloneHostsPath := getStandaloneHostsPath(id)
standaloneHostsPath := filepath.Join(sbCtx.networkMountsRoot, "hosts")
if err := os.WriteFile(standaloneHostsPath, []byte(standaloneHostsContent), 0644); err != nil {
return errors.Wrapf(err, "failed to write standalone hosts to %q", standaloneHostsPath)
}

mt := oci.Mount{
Destination: "/etc/hosts",
Type: "bind",
Source: getStandaloneHostsPath(id),
Source: standaloneHostsPath,
Options: []string{"bind"},
}
if isRootReadonly(spec) {
Expand All @@ -116,15 +99,15 @@ func setupStandaloneContainerSpec(ctx context.Context, id string, spec *oci.Spec
if err != nil {
return errors.Wrap(err, "failed to generate standalone resolv.conf content")
}
standaloneResolvPath := getStandaloneResolvPath(id)
standaloneResolvPath := filepath.Join(sbCtx.networkMountsRoot, "resolv.conf")
if err := os.WriteFile(standaloneResolvPath, []byte(resolvContent), 0644); err != nil {
return errors.Wrap(err, "failed to write standalone resolv.conf")
}

mt := oci.Mount{
Destination: "/etc/resolv.conf",
Type: "bind",
Source: getStandaloneResolvPath(id),
Source: standaloneResolvPath,
Options: []string{"bind"},
}
if isRootReadonly(spec) {
Expand Down
Loading

0 comments on commit 5981cd9

Please sign in to comment.