Skip to content

Commit 88a6e4c

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 a08ce79 commit 88a6e4c

4 files changed

Lines changed: 148 additions & 23 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
"path/filepath"
22+
23+
taskAPI "github.com/containerd/containerd/api/runtime/task/v2"
24+
"github.com/urunc-dev/urunc/pkg/unikontainers"
25+
)
26+
27+
const annotRootfsParams = "com.urunc.internal.rootfs.params"
28+
29+
var errGuestRootfsChoiceSkipped = errors.New("guest rootfs choice skipped")
30+
31+
// chooseGuestRootfs runs the same ChooseRootfs logic as runtime Exec after inner
32+
// task Create (#684) and records the result in annotRootfsParams so Exec knows
33+
// selection already happened.
34+
func chooseGuestRootfs(r *taskAPI.CreateTaskRequest) error {
35+
spec, mode, err := loadSpec(r.Bundle)
36+
if err != nil {
37+
return err
38+
}
39+
40+
config, err := unikontainers.GetUnikernelConfig(filepath.Clean(r.Bundle), spec)
41+
if err != nil {
42+
return fmt.Errorf("%w: %w", errGuestRootfsChoiceSkipped, err)
43+
}
44+
45+
annotations := config.Map()
46+
uruncCfg, _ := unikontainers.LoadUruncConfig(unikontainers.UruncConfigPath)
47+
48+
rootfsParams, err := unikontainers.ChooseRootfs(
49+
filepath.Clean(r.Bundle),
50+
spec.Root.Path,
51+
annotations,
52+
uruncCfg,
53+
)
54+
if err != nil {
55+
return err
56+
}
57+
58+
encoded, err := json.Marshal(rootfsParams)
59+
if err != nil {
60+
return err
61+
}
62+
if spec.Annotations == nil {
63+
spec.Annotations = make(map[string]string)
64+
}
65+
spec.Annotations[annotRootfsParams] = string(encoded)
66+
67+
return saveSpec(r.Bundle, spec, mode)
68+
}

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"
@@ -49,7 +50,23 @@ func (s *taskService) Create(ctx context.Context, r *taskAPI.CreateTaskRequest)
4950
}
5051
}
5152

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

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

pkg/unikontainers/rootfs.go

Lines changed: 8 additions & 9 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
@@ -235,15 +239,10 @@ func (rs *rootfsSelector) tryContainerRootfs() (types.RootfsParams, bool) {
235239
return types.RootfsParams{}, false
236240
}
237241

238-
func switchMonRootfs(res types.RootfsParams, bundle string) (types.RootfsParams, error) {
239-
monRootfs := filepath.Join(bundle, monitorRootfsDirName)
240-
err := os.MkdirAll(monRootfs, 0o755)
241-
if err != nil {
242-
return types.RootfsParams{}, fmt.Errorf("failed to create monitor rootfs directory %s: %w", monRootfs, err)
243-
}
244-
res.MonRootfs = monRootfs
245-
246-
return res, nil
242+
// switchMonRootfs sets MonRootfs under the bundle; creation happens in Exec.
243+
func switchMonRootfs(res types.RootfsParams, bundle string) types.RootfsParams {
244+
res.MonRootfs = filepath.Join(bundle, monitorRootfsDirName)
245+
return res
247246
}
248247

249248
// pivotRootfs changes rootfs with pivot

pkg/unikontainers/unikontainers.go

Lines changed: 54 additions & 13 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,
@@ -285,7 +289,7 @@ func (u *Unikontainer) chooseRootfs() (types.RootfsParams, error) {
285289
// Priority 3 & 4: Container rootfs (block or shared-fs)
286290
result, ok = selector.tryContainerRootfs()
287291
if ok {
288-
return switchMonRootfs(result, bundleDir)
292+
return switchMonRootfs(result, bundleDir), nil
289293
}
290294

291295
if selector.shouldMountContainerRootfs() {
@@ -426,11 +430,39 @@ 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+
// Read annotRootfsParams: shim writes it to config.json after inner Create;
435+
// state.json may not have it yet during reexec, so fall back to spec.
436+
rootfsParamsJSON := ""
437+
if u.State.Annotations != nil {
438+
rootfsParamsJSON = u.State.Annotations[annotRootfsParams]
439+
}
440+
if rootfsParamsJSON == "" && u.Spec.Annotations != nil {
441+
rootfsParamsJSON = u.Spec.Annotations[annotRootfsParams]
442+
}
443+
if rootfsParamsJSON != "" {
444+
// Shim (or a prior run) already called ChooseRootfs; decode and use as-is.
445+
if err := json.Unmarshal([]byte(rootfsParamsJSON), &rootfsParams); err != nil {
446+
return fmt.Errorf("could not decode guest rootfs params: %w", err)
447+
}
448+
} else {
449+
// No annotRootfsParams (e.g. podman, urunc CLI): same path as legacy
450+
// u.chooseRootfs(), including no-guest-rootfs when mountRootfs is unset.
451+
specRoot := ""
452+
if u.Spec.Root != nil {
453+
specRoot = u.Spec.Root.Path
454+
}
455+
rootfsParams, err = ChooseRootfs(u.State.Bundle, specRoot, u.State.Annotations, u.UruncCfg)
456+
if err != nil {
457+
uniklog.Errorf("could not choose guest rootfs: %v", err)
458+
return err
459+
}
433460
}
461+
uniklog.WithFields(logrus.Fields{
462+
"rootfs_type": rootfsParams.Type,
463+
"rootfs_path": rootfsParams.Path,
464+
"mon_rootfs": rootfsParams.MonRootfs,
465+
}).Debug("guest rootfs params")
434466

435467
// TODO: Add support for using both an existing
436468
// block based snapshot of the container's rootfs
@@ -479,6 +511,15 @@ func (u *Unikontainer) Exec(metrics m.Writer) error {
479511
}
480512
}
481513

514+
if _, err = os.Stat(rootfsParams.MonRootfs); err != nil {
515+
if !os.IsNotExist(err) {
516+
return fmt.Errorf("failed to stat monitor rootfs directory %s: %w", rootfsParams.MonRootfs, err)
517+
}
518+
if err = os.MkdirAll(rootfsParams.MonRootfs, 0o755); err != nil {
519+
return fmt.Errorf("failed to create monitor rootfs directory %s: %w", rootfsParams.MonRootfs, err)
520+
}
521+
}
522+
482523
err = rfsBuilder.preSetup()
483524
if err != nil {
484525
return fmt.Errorf("pre setup step for rootfs failed: %w", err)

0 commit comments

Comments
 (0)