Skip to content

Commit da580a7

Browse files
committed
feat(shim): choose guest rootfs after inner task create
Run ChooseRootfs in the shim after inner task Create, once the bundle rootfs is mounted (#684). Persist JSON RootfsParams in config.json as com.urunc.internal.rootfs.params so Exec reuses the choice; read state then spec annotations because reexec may not yet mirror config.json. Export ChooseRootfs from unikontainers. When the annotation is absent, Exec runs the same selection as the former u.chooseRootfs() (podman and urunc CLI). Ensure MonRootfs exists before rootfs setup in Exec. Fixes: #684 Signed-off-by: sidneychang <2190206983@qq.com>
1 parent 0773c40 commit da580a7

5 files changed

Lines changed: 152 additions & 14 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package containerdshim
16+
17+
import (
18+
"encoding/json"
19+
"errors"
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
24+
taskAPI "github.com/containerd/containerd/api/runtime/task/v2"
25+
specs "github.com/opencontainers/runtime-spec/specs-go"
26+
"github.com/urunc-dev/urunc/pkg/unikontainers"
27+
)
28+
29+
const annotRootfsParams = "com.urunc.internal.rootfs.params"
30+
31+
var errGuestRootfsChoiceSkipped = errors.New("guest rootfs choice skipped")
32+
33+
// chooseGuestRootfs runs the same ChooseRootfs logic as runtime Exec after inner
34+
// task Create (#684) and records the result in annotRootfsParams so Exec knows
35+
// selection already happened.
36+
func chooseGuestRootfs(r *taskAPI.CreateTaskRequest) error {
37+
configPath := filepath.Join(r.Bundle, "config.json")
38+
info, err := os.Stat(configPath)
39+
if err != nil {
40+
return fmt.Errorf("stat config.json: %w", err)
41+
}
42+
43+
data, err := os.ReadFile(configPath)
44+
if err != nil {
45+
return fmt.Errorf("read config.json: %w", err)
46+
}
47+
48+
var spec specs.Spec
49+
if err := json.Unmarshal(data, &spec); err != nil {
50+
return fmt.Errorf("unmarshal config.json: %w", err)
51+
}
52+
if spec.Root == nil {
53+
return fmt.Errorf("invalid OCI spec: root section is required")
54+
}
55+
56+
config, err := unikontainers.GetUnikernelConfig(filepath.Clean(r.Bundle), &spec)
57+
if err != nil {
58+
return fmt.Errorf("%w: %w", errGuestRootfsChoiceSkipped, err)
59+
}
60+
61+
annotations := config.Map()
62+
uruncCfg, err := unikontainers.LoadUruncConfig(unikontainers.UruncConfigPath)
63+
if err != nil && uruncCfg == nil {
64+
return err
65+
}
66+
67+
rootfsParams, err := unikontainers.ChooseRootfs(
68+
filepath.Clean(r.Bundle),
69+
spec.Root.Path,
70+
annotations,
71+
uruncCfg,
72+
)
73+
if err != nil {
74+
return err
75+
}
76+
77+
encoded, err := json.Marshal(rootfsParams)
78+
if err != nil {
79+
return err
80+
}
81+
if spec.Annotations == nil {
82+
spec.Annotations = make(map[string]string)
83+
}
84+
spec.Annotations[annotRootfsParams] = string(encoded)
85+
86+
patched, err := json.MarshalIndent(spec, "", " ")
87+
if err != nil {
88+
return fmt.Errorf("marshal config.json: %w", err)
89+
}
90+
91+
return os.WriteFile(configPath, patched, info.Mode())
92+
}

pkg/containerd-shim/task_service.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package containerdshim
1616

1717
import (
1818
"context"
19+
"errors"
1920

2021
taskAPI "github.com/containerd/containerd/api/runtime/task/v2"
2122
"github.com/containerd/log"
@@ -47,7 +48,23 @@ func (s *taskService) Create(ctx context.Context, r *taskAPI.CreateTaskRequest)
4748
}
4849
}
4950

50-
return s.TaskService.Create(ctx, r)
51+
resp, err := s.TaskService.Create(ctx, r)
52+
if err != nil {
53+
return resp, err
54+
}
55+
56+
// ChooseRootfs after inner task Create so bundle rootfs is mounted;
57+
// params are persisted in bundle config.json for runtime Exec.
58+
if err := chooseGuestRootfs(r); err != nil {
59+
if errors.Is(err, errGuestRootfsChoiceSkipped) {
60+
log.G(ctx).WithError(err).Debug("urunc(shim): guest rootfs choice skipped")
61+
return resp, nil
62+
}
63+
log.G(ctx).WithError(err).Warn("urunc(shim): failed to choose guest rootfs")
64+
return nil, err
65+
}
66+
67+
return resp, nil
5168
}
5269

5370
func (s *taskService) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) {

pkg/unikontainers/rootfs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
// TODO: Find and set the correct size for the tmpfs in the host
2929
const tmpfsSizeForNoRootfs = "65536k"
3030

31+
// annotRootfsParams holds JSON RootfsParams after shim chooseGuestRootfs.
32+
// When present in bundle config.json, Exec reuses it; otherwise Exec runs ChooseRootfs.
33+
const annotRootfsParams = "com.urunc.internal.rootfs.params"
34+
3135
type rootfsBuilder interface {
3236
preSetup() error
3337
postSetup() error

pkg/unikontainers/unikontainers.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,33 +238,37 @@ func (u *Unikontainer) SetupNet() (types.NetDevParams, error) {
238238
// 3. Container rootfs as block device (if MountRootfs=true and supported)
239239
// 4. Container rootfs as shared-fs: virtiofs > 9pfs (if MountRootfs=true and supported)
240240
// 5. No rootfs
241-
func (u *Unikontainer) chooseRootfs() (types.RootfsParams, error) {
242-
bundleDir := filepath.Clean(u.State.Bundle)
243-
rootfsDir := filepath.Clean(u.Spec.Root.Path)
241+
func ChooseRootfs(bundle, specRoot string, annot map[string]string, cfg *UruncConfig) (types.RootfsParams, error) {
242+
bundleDir := filepath.Clean(bundle)
243+
rootfsDir := filepath.Clean(specRoot)
244244
rootfsDir, err := resolveAgainstBase(bundleDir, rootfsDir)
245245
if err != nil {
246246
uniklog.Errorf("could not resolve rootfs directory %s: %v", rootfsDir, err)
247247
return types.RootfsParams{}, err
248248
}
249249

250-
unikernelType := u.State.Annotations[annotType]
250+
if cfg == nil {
251+
return types.RootfsParams{}, fmt.Errorf("urunc config is required for guest rootfs selection")
252+
}
253+
254+
unikernelType := annot[annotType]
251255
unikernel, err := unikernels.New(unikernelType)
252256
if err != nil {
253257
return types.RootfsParams{}, err
254258
}
255259

256-
vmmType := u.State.Annotations[annotHypervisor]
257-
vmm, err := hypervisors.NewVMM(hypervisors.VmmType(vmmType), u.UruncCfg.Monitors)
260+
vmmType := annot[annotHypervisor]
261+
vmm, err := hypervisors.NewVMM(hypervisors.VmmType(vmmType), cfg.Monitors)
258262
if err != nil {
259263
return types.RootfsParams{}, err
260264
}
261265

262-
virtiofsdConfig := u.UruncCfg.ExtraBins["virtiofsd"]
266+
virtiofsdConfig := cfg.ExtraBins["virtiofsd"]
263267

264268
selector := &rootfsSelector{
265269
bundle: bundleDir,
266270
cntrRootfs: rootfsDir,
267-
annot: u.State.Annotations,
271+
annot: annot,
268272
unikernel: unikernel,
269273
vmm: vmm,
270274
vfsdPath: virtiofsdConfig.Path,
@@ -426,11 +430,28 @@ func (u *Unikontainer) Exec(metrics m.Writer) error {
426430
// if the respective annotation is set then, depending on the guest
427431
// (supports block or 9pfs), it will use the supported option. In case
428432
// both ae supported, then the block option will be used by default.
429-
rootfsParams, err := u.chooseRootfs()
430-
if err != nil {
431-
uniklog.Errorf("could not choose guest rootfs: %v", err)
432-
return err
433+
var rootfsParams types.RootfsParams
434+
435+
// Read the rootfs choice written by the shim.
436+
if rootfsParamsJSON := u.Spec.Annotations[annotRootfsParams]; rootfsParamsJSON != "" {
437+
if err := json.Unmarshal([]byte(rootfsParamsJSON), &rootfsParams); err != nil {
438+
return fmt.Errorf("could not decode guest rootfs params: %w", err)
439+
}
440+
}
441+
442+
// If there is no shim choice, the runtime chooses rootfs here.
443+
if rootfsParams.MonRootfs == "" {
444+
rootfsParams, err = ChooseRootfs(u.State.Bundle, u.Spec.Root.Path, u.State.Annotations, u.UruncCfg)
445+
if err != nil {
446+
uniklog.Errorf("could not choose guest rootfs: %v", err)
447+
return err
448+
}
433449
}
450+
uniklog.WithFields(logrus.Fields{
451+
"rootfs_type": rootfsParams.Type,
452+
"rootfs_path": rootfsParams.Path,
453+
"mon_rootfs": rootfsParams.MonRootfs,
454+
}).Debug("guest rootfs params")
434455

435456
// TODO: Add support for using both an existing
436457
// block based snapshot of the container's rootfs
@@ -479,6 +500,10 @@ func (u *Unikontainer) Exec(metrics m.Writer) error {
479500
}
480501
}
481502

503+
if err = os.MkdirAll(rootfsParams.MonRootfs, 0o755); err != nil {
504+
return fmt.Errorf("failed to create monitor rootfs directory %s: %w", rootfsParams.MonRootfs, err)
505+
}
506+
482507
err = rfsBuilder.preSetup()
483508
if err != nil {
484509
return fmt.Errorf("pre setup step for rootfs failed: %w", err)

pkg/unikontainers/urunc_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func defaultUruncConfig() *UruncConfig {
106106
// LoadUruncConfig loads the urunc configuration from the specified path.
107107
// If the file does not exist or is malformed, it returns the default configuration.
108108
func LoadUruncConfig(path string) (*UruncConfig, error) {
109-
cfg := &UruncConfig{}
109+
cfg := &UruncConfig{ExtraBins: defaultExtraBinConfig()}
110110
_, err := toml.DecodeFile(path, cfg)
111111
if err == nil {
112112
return cfg, nil

0 commit comments

Comments
 (0)