|
| 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 | +// Per-container snapshot view: read-only mounts from shim-written urunc-view.json |
| 16 | +// (kernel, initrd, urunc.json bind-copied from image root). Mount descriptors are |
| 17 | +// persisted by the shim; this package does not dial containerd. |
| 18 | + |
| 19 | +package unikontainers |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + |
| 29 | + "github.com/containerd/containerd/mount" |
| 30 | + "golang.org/x/sys/unix" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + perContainerViewStateFile = "urunc-view.json" |
| 35 | + perContainerViewMountsStateFile = "urunc-view-mounts.json" |
| 36 | +) |
| 37 | + |
| 38 | +var errPerContainerViewNotPrepared = errors.New("snapshot view not prepared") |
| 39 | + |
| 40 | +type perContainerViewState struct { |
| 41 | + ViewKey string `json:"view_key"` |
| 42 | + Snapshotter string `json:"snapshotter"` |
| 43 | + Namespace string `json:"namespace"` |
| 44 | + Mounts []mount.Mount `json:"mounts,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +type perContainerViewMountsState struct { |
| 48 | + Targets []string `json:"targets,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +// tryRunOnPerContainerView loads shim-written per-container view metadata from |
| 52 | +// the bundle directory, mounts that snapshot view read-only on a temp |
| 53 | +// directory, runs fn with that mount root, then unmounts and removes the temp dir. |
| 54 | +// |
| 55 | +// If no view was prepared (missing state file), returns (false, nil) so the |
| 56 | +// caller can fall back to the legacy path. |
| 57 | +// |
| 58 | +// If state exists, returns (true, err) where err is from mounting, fn, or |
| 59 | +// unmount (unmount failure is returned only when fn succeeded). |
| 60 | +func tryRunOnPerContainerView(ctx context.Context, bundle string, fn func(mountRoot string) error) (ok bool, retErr error) { |
| 61 | + _ = ctx // API stability for callers; mount uses host paths from shim-persisted state only. |
| 62 | + state, err := loadPerContainerViewState(bundle) |
| 63 | + if err != nil { |
| 64 | + if errors.Is(err, errPerContainerViewNotPrepared) { |
| 65 | + return false, nil |
| 66 | + } |
| 67 | + return false, err |
| 68 | + } |
| 69 | + |
| 70 | + mountpoint, err := mkTempSnapshotViewDir() |
| 71 | + if err != nil { |
| 72 | + return false, err |
| 73 | + } |
| 74 | + defer os.RemoveAll(mountpoint) |
| 75 | + |
| 76 | + if err := mountPerContainerViewReadonly(state, mountpoint); err != nil { |
| 77 | + return false, err |
| 78 | + } |
| 79 | + defer func() { |
| 80 | + if uerr := unmountSnapshotViewTemp(mountpoint); uerr != nil { |
| 81 | + if retErr == nil { |
| 82 | + retErr = uerr |
| 83 | + return |
| 84 | + } |
| 85 | + uniklog.WithError(uerr).WithField("path", mountpoint).Warn("failed to unmount temporary snapshot view mount") |
| 86 | + } |
| 87 | + }() |
| 88 | + |
| 89 | + retErr = fn(mountpoint) |
| 90 | + return true, retErr |
| 91 | +} |
| 92 | + |
| 93 | +func loadPerContainerViewState(bundle string) (*perContainerViewState, error) { |
| 94 | + path := filepath.Join(bundle, perContainerViewStateFile) |
| 95 | + data, err := os.ReadFile(path) |
| 96 | + if err != nil { |
| 97 | + if os.IsNotExist(err) { |
| 98 | + return nil, errPerContainerViewNotPrepared |
| 99 | + } |
| 100 | + return nil, fmt.Errorf("read snapshot view state %s: %w", path, err) |
| 101 | + } |
| 102 | + |
| 103 | + var state perContainerViewState |
| 104 | + if err := json.Unmarshal(data, &state); err != nil { |
| 105 | + return nil, fmt.Errorf("unmarshal snapshot view state %s: %w", path, err) |
| 106 | + } |
| 107 | + return &state, nil |
| 108 | +} |
| 109 | + |
| 110 | +func mountPerContainerViewReadonly(state *perContainerViewState, target string) error { |
| 111 | + if len(state.Mounts) == 0 { |
| 112 | + return fmt.Errorf("snapshot view state %s has no mounts (recreate the container with an updated shim)", perContainerViewStateFile) |
| 113 | + } |
| 114 | + |
| 115 | + return mountReadonlySnapshotView(target, state.Mounts) |
| 116 | +} |
| 117 | + |
| 118 | +func mkTempSnapshotViewDir() (string, error) { |
| 119 | + dir, err := os.MkdirTemp("", "urunc-snapshot-view-") |
| 120 | + if err != nil { |
| 121 | + return "", fmt.Errorf("create temporary snapshot view mountpoint: %w", err) |
| 122 | + } |
| 123 | + return dir, nil |
| 124 | +} |
| 125 | + |
| 126 | +func unmountSnapshotViewTemp(path string) error { |
| 127 | + if err := mount.Unmount(path, 0); err != nil { |
| 128 | + if os.IsNotExist(err) || err == unix.EINVAL { |
| 129 | + return nil |
| 130 | + } |
| 131 | + uniklog.WithError(err).WithField("path", path).Warn("failed to unmount snapshot view") |
| 132 | + return err |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func mountReadonlySnapshotView(target string, mounts []mount.Mount) error { |
| 138 | + if err := mount.All(mounts, target); err != nil { |
| 139 | + return fmt.Errorf("mount snapshot view at %s for boot file bind: %w", target, err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func persistPerContainerViewMountsState(bundle string, targets []string) error { |
| 146 | + state := perContainerViewMountsState{Targets: targets} |
| 147 | + data, err := json.Marshal(state) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("marshal snapshot view mount state: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + path := filepath.Join(bundle, perContainerViewMountsStateFile) |
| 153 | + if err := os.WriteFile(path, data, 0o600); err != nil { |
| 154 | + return fmt.Errorf("write snapshot view mount state %s: %w", path, err) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +func loadPerContainerViewMountsState(bundle string) (*perContainerViewMountsState, error) { |
| 161 | + path := filepath.Join(bundle, perContainerViewMountsStateFile) |
| 162 | + data, err := os.ReadFile(path) |
| 163 | + if err != nil { |
| 164 | + if os.IsNotExist(err) { |
| 165 | + return nil, nil |
| 166 | + } |
| 167 | + return nil, fmt.Errorf("read snapshot view mount state %s: %w", path, err) |
| 168 | + } |
| 169 | + |
| 170 | + var state perContainerViewMountsState |
| 171 | + if err := json.Unmarshal(data, &state); err != nil { |
| 172 | + return nil, fmt.Errorf("unmarshal snapshot view mount state %s: %w", path, err) |
| 173 | + } |
| 174 | + |
| 175 | + return &state, nil |
| 176 | +} |
| 177 | + |
| 178 | +func removePerContainerViewMountsState(bundle string) error { |
| 179 | + path := filepath.Join(bundle, perContainerViewMountsStateFile) |
| 180 | + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 181 | + return fmt.Errorf("remove snapshot view mount state %s: %w", path, err) |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func cleanupPerContainerViewMounts(bundle string) error { |
| 188 | + state, err := loadPerContainerViewMountsState(bundle) |
| 189 | + if err != nil || state == nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + for i := len(state.Targets) - 1; i >= 0; i-- { |
| 194 | + target := filepath.Clean(state.Targets[i]) |
| 195 | + if err := unix.Unmount(target, unix.MNT_DETACH); err != nil { |
| 196 | + if err == unix.EINVAL || err == unix.ENOENT || os.IsNotExist(err) { |
| 197 | + continue |
| 198 | + } |
| 199 | + return fmt.Errorf("failed to unmount snapshot view target %s: %w", target, err) |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return removePerContainerViewMountsState(bundle) |
| 204 | +} |
| 205 | + |
| 206 | +func rollbackPerContainerViewTargets(targets []string) { |
| 207 | + for i := len(targets) - 1; i >= 0; i-- { |
| 208 | + target := filepath.Clean(targets[i]) |
| 209 | + if err := unix.Unmount(target, unix.MNT_DETACH); err != nil { |
| 210 | + if err == unix.EINVAL || err == unix.ENOENT || os.IsNotExist(err) { |
| 211 | + continue |
| 212 | + } |
| 213 | + uniklog.WithError(err).WithField("target", target).Warn("failed to roll back snapshot view bind mount") |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments