From fa4acad4aa0352d1f2ed2b7393b5648e3d175065 Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Tue, 8 Oct 2019 11:02:15 -0700 Subject: [PATCH 1/2] state: Load the state from storage early on The hypervisor.createSandbox may need to access the state. For eg, ACRN today needs to access the block index to assign it to the root image of the VM. Hence load this early on. Fixes #2026 Signed-off-by: Archana Shinde --- virtcontainers/sandbox.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 0d88e90115..b1f409868a 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -495,13 +495,11 @@ func createSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Fac } s.devManager = deviceManager.NewDeviceManager(sandboxConfig.HypervisorConfig.BlockDeviceDriver, devices) - // We first try to fetch the sandbox state from storage. - // If it exists, this means this is a re-creation, i.e. + // Sandbox state has been loaded from storage. + // If the Stae is not empty, this is a re-creation, i.e. // we don't need to talk to the guest's agent, but only // want to create the sandbox and its containers in memory. - state, err := s.store.LoadState() - if err == nil && state.State != "" { - s.state = state + if s.state.State != "" { return s, nil } } @@ -588,6 +586,12 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor return nil, err } } else { + // Load sandbox state. The hypervisor.createSandbox call, may need to access statei. + state, err := s.store.LoadState() + if err == nil { + s.state = state + } + if err = s.hypervisor.createSandbox(ctx, s.id, s.networkNS, &sandboxConfig.HypervisorConfig, s.store); err != nil { return nil, err } From f6a10bcae78c654b3c396c9ec1bd398a2b79bae2 Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Tue, 8 Oct 2019 16:25:36 -0700 Subject: [PATCH 2/2] state: Refactor code to move all the state load code Refactor so that all code to load state, devices, network takes place at one place. This is in line with the experimental api for new storage that also loads all the necessary items here all at once. Signed-off-by: Archana Shinde --- virtcontainers/sandbox.go | 43 ++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index b1f409868a..04b6f5029a 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -477,31 +477,12 @@ func createSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Fac s.Logger().WithField("features", s.config.Experimental).Infof("Enable experimental features") } - if s.supportNewStore() { - // Restored successfully from newstore before. - if s.state.State != "" { - return s, nil - } - } else { - // Fetch sandbox network to be able to access it from the sandbox structure. - var networkNS NetworkNamespace - if err := s.store.Load(store.Network, &networkNS); err == nil { - s.networkNS = networkNS - } - - devices, err := s.store.LoadDevices() - if err != nil { - s.Logger().WithError(err).WithField("sandboxid", s.id).Warning("load sandbox devices failed") - } - s.devManager = deviceManager.NewDeviceManager(sandboxConfig.HypervisorConfig.BlockDeviceDriver, devices) - - // Sandbox state has been loaded from storage. - // If the Stae is not empty, this is a re-creation, i.e. - // we don't need to talk to the guest's agent, but only - // want to create the sandbox and its containers in memory. - if s.state.State != "" { - return s, nil - } + // Sandbox state has been loaded from storage. + // If the Stae is not empty, this is a re-creation, i.e. + // we don't need to talk to the guest's agent, but only + // want to create the sandbox and its containers in memory. + if s.state.State != "" { + return s, nil } // Below code path is called only during create, because of earlier check. @@ -586,6 +567,18 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor return nil, err } } else { + // Fetch sandbox network to be able to access it from the sandbox structure. + var networkNS NetworkNamespace + if err = s.store.Load(store.Network, &networkNS); err == nil { + s.networkNS = networkNS + } + + devices, err := s.store.LoadDevices() + if err != nil { + s.Logger().WithError(err).WithField("sandboxid", s.id).Warning("load sandbox devices failed") + } + s.devManager = deviceManager.NewDeviceManager(sandboxConfig.HypervisorConfig.BlockDeviceDriver, devices) + // Load sandbox state. The hypervisor.createSandbox call, may need to access statei. state, err := s.store.LoadState() if err == nil {