1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package boot
import (
"errors"
"fmt"
"io"
"strconv"
time2 "time"
specs "github.com/opencontainers/runtime-spec/specs-go"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/cleanup"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/devutil"
"gvisor.dev/gvisor/pkg/fd"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sentry/control"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/host"
"gvisor.dev/gvisor/pkg/sentry/inet"
"gvisor.dev/gvisor/pkg/sentry/kernel"
"gvisor.dev/gvisor/pkg/sentry/pgalloc"
"gvisor.dev/gvisor/pkg/sentry/socket/hostinet"
"gvisor.dev/gvisor/pkg/sentry/socket/netstack"
"gvisor.dev/gvisor/pkg/sentry/state"
"gvisor.dev/gvisor/pkg/sentry/time"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sentry/watchdog"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/runsc/boot/pprof"
"gvisor.dev/gvisor/runsc/config"
)
const (
// CheckpointStateFileName is the file within the given image-path's
// directory which contains the container's saved state.
CheckpointStateFileName = "checkpoint.img"
// CheckpointPagesMetadataFileName is the file within the given image-path's
// directory containing the container's MemoryFile metadata.
CheckpointPagesMetadataFileName = "pages_meta.img"
// CheckpointPagesFileName is the file within the given image-path's
// directory containing the container's MemoryFile pages.
CheckpointPagesFileName = "pages.img"
)
// restorer manages a restore session for a sandbox. It stores information about
// all containers and triggers the full sandbox restore after the last
// container is restored.
type restorer struct {
mu sync.Mutex
// totalContainers is the number of containers expected to be restored in
// the sandbox. Sandbox restore can only happen, after all containers have
// been restored.
totalContainers int
// containers is the list of containers restored so far.
containers []*containerInfo
// Files used by restore to rehydrate the state.
stateFile io.ReadCloser
pagesMetadata *fd.FD
pagesFile *fd.FD
// deviceFile is the required to start the platform.
deviceFile *fd.FD
// restoreDone is a callback triggered when restore is successful.
restoreDone func() error
}
func (r *restorer) restoreSubcontainer(spec *specs.Spec, conf *config.Config, l *Loader, cid string, stdioFDs, goferFDs, goferFilestoreFDs []*fd.FD, devGoferFD *fd.FD, goferMountConfs []GoferMountConf) error {
containerName := l.registerContainer(spec, cid)
info := &containerInfo{
cid: cid,
containerName: containerName,
conf: conf,
spec: spec,
stdioFDs: stdioFDs,
goferFDs: goferFDs,
devGoferFD: devGoferFD,
goferFilestoreFDs: goferFilestoreFDs,
goferMountConfs: goferMountConfs,
}
return r.restoreContainerInfo(l, info)
}
func (r *restorer) restoreContainerInfo(l *Loader, info *containerInfo) error {
r.mu.Lock()
defer r.mu.Unlock()
for _, container := range r.containers {
if container.containerName == info.containerName {
return fmt.Errorf("container %q already restored", info.containerName)
}
if container.cid == info.cid {
return fmt.Errorf("container CID %q already belongs to container %q", info.cid, container.containerName)
}
}
r.containers = append(r.containers, info)
log.Infof("Restored container %d of %d", len(r.containers), r.totalContainers)
if log.IsLogging(log.Debug) {
for i, fd := range info.stdioFDs {
log.Debugf("Restore app FD: %d host FD: %d", i, fd.FD())
}
}
if len(r.containers) == r.totalContainers {
// Trigger the restore if this is the last container.
return r.restore(l)
}
return nil
}
func createNetworStackForRestore(l *Loader) (*stack.Stack, inet.Stack) {
// Save the current network stack to slap on top of the one that was restored.
curNetwork := l.k.RootNetworkNamespace().Stack()
if eps, ok := curNetwork.(*netstack.Stack); ok {
return eps.Stack, curNetwork
}
return nil, hostinet.NewStack()
}
func (r *restorer) restore(l *Loader) error {
log.Infof("Starting to restore %d containers", len(r.containers))
// Create a new root network namespace with the network stack of the
// old kernel to preserve the existing network configuration.
oldStack, oldInetStack := createNetworStackForRestore(l)
// Reset the network stack in the network namespace to nil before
// replacing the kernel. This will not free the network stack when this
// old kernel is released.
l.k.RootNetworkNamespace().ResetStack()
p, err := createPlatform(l.root.conf, r.deviceFile)
if err != nil {
return fmt.Errorf("creating platform: %v", err)
}
// Start the old watchdog before replacing it with a new one below.
l.watchdog.Start()
// Release the kernel and replace it with a new one that will be restored into.
if l.k != nil {
l.k.Release()
}
l.k = &kernel.Kernel{
Platform: p,
}
mf, err := createMemoryFile(l.root.conf.AppHugePages, l.hostShmemHuge)
if err != nil {
return fmt.Errorf("creating memory file: %v", err)
}
l.k.SetMemoryFile(mf)
if l.root.conf.ProfileEnable {
// pprof.Initialize opens /proc/self/maps, so has to be called before
// installing seccomp filters.
pprof.Initialize()
}
// Seccomp filters have to be applied before vfs restore and before parsing
// the state file.
if err := l.installSeccompFilters(); err != nil {
return err
}
// Set up the restore environment.
ctx := l.k.SupervisorContext()
if oldStack != nil {
ctx = context.WithValue(ctx, stack.CtxRestoreStack, oldStack)
}
fdmap := make(map[vfs.RestoreID]int)
mfmap := make(map[string]*pgalloc.MemoryFile)
for _, cont := range r.containers {
// TODO(b/298078576): Need to process hints here probably
mntr := newContainerMounter(cont, l.k, l.mountHints, l.sharedMounts, l.productName, cont.cid)
if err = mntr.configureRestore(fdmap, mfmap); err != nil {
return fmt.Errorf("configuring filesystem restore: %v", err)
}
for i, fd := range cont.stdioFDs {
key := host.MakeRestoreID(cont.containerName, i)
fdmap[key] = fd.Release()
}
for _, customFD := range cont.passFDs {
key := host.MakeRestoreID(cont.containerName, customFD.guest)
fdmap[key] = customFD.host.FD()
}
}
log.Debugf("Restore using fdmap: %v", fdmap)
ctx = context.WithValue(ctx, vfs.CtxRestoreFilesystemFDMap, fdmap)
log.Debugf("Restore using mfmap: %v", mfmap)
ctx = context.WithValue(ctx, pgalloc.CtxMemoryFileMap, mfmap)
ctx = context.WithValue(ctx, devutil.CtxDevGoferClientProvider, l.k)
// Load the state.
loadOpts := state.LoadOpts{Source: r.stateFile, PagesMetadata: r.pagesMetadata, PagesFile: r.pagesFile}
if err := loadOpts.Load(ctx, l.k, nil, oldInetStack, time.NewCalibratedClocks(), &vfs.CompleteRestoreOptions{}); err != nil {
return err
}
// Since we have a new kernel we also must make a new watchdog.
dogOpts := watchdog.DefaultOpts
dogOpts.TaskTimeoutAction = l.root.conf.WatchdogAction
dogOpts.StartupTimeout = 3 * time2.Minute // Give extra time for all containers to restore.
dog := watchdog.New(l.k, dogOpts)
// Change the loader fields to reflect the changes made when restoring.
l.watchdog.Stop()
l.watchdog = dog
l.root.procArgs = kernel.CreateProcessArgs{}
l.restore = true
l.sandboxID = l.root.cid
l.mu.Lock()
cu := cleanup.Make(func() {
l.mu.Unlock()
})
defer cu.Clean()
// Update all tasks in the system with their respective new container IDs.
for _, task := range l.k.TaskSet().Root.Tasks() {
oldCid := task.ContainerID()
name := l.k.ContainerName(oldCid)
newCid, ok := l.containerIDs[name]
if !ok {
return fmt.Errorf("unable to remap task with CID %q (name: %q). Available names: %v", task.ContainerID(), name, l.containerIDs)
}
task.RestoreContainerID(newCid)
}
// Rebuild `processes` map with containers' root process from the restored kernel.
for _, tg := range l.k.RootPIDNamespace().ThreadGroups() {
// Find all processes with no parent (root of execution), that were not started
// via a call to `exec`.
if tg.Leader().Parent() == nil && tg.Leader().Origin != kernel.OriginExec {
cid := tg.Leader().ContainerID()
proc := l.processes[execID{cid: cid}]
if proc == nil {
return fmt.Errorf("unable to find container root process with CID %q, processes: %v", cid, l.processes)
}
proc.tg = tg
}
}
// Kill all processes that have been exec'd since they cannot be properly
// restored -- the caller is no longer connected.
for _, tg := range l.k.RootPIDNamespace().ThreadGroups() {
if tg.Leader().Origin == kernel.OriginExec {
if err := l.k.SendExternalSignalThreadGroup(tg, &linux.SignalInfo{Signo: int32(linux.SIGKILL)}); err != nil {
log.Warningf("Failed to kill exec process after restore: %v", err)
}
}
}
l.k.RestoreContainerMapping(l.containerIDs)
if err := l.kernelInitExtra(); err != nil {
return err
}
// Refresh the control server with the newly created kernel.
l.ctrl.refreshHandlers()
// Release `l.mu` before calling into callbacks.
cu.Clean()
// r.restoreDone() signals and waits for the sandbox to start.
if err := r.restoreDone(); err != nil {
return err
}
r.stateFile.Close()
if r.pagesFile != nil {
r.pagesFile.Close()
}
if r.pagesMetadata != nil {
r.pagesMetadata.Close()
}
if err := postRestoreImpl(l.k); err != nil {
return err
}
// Restore was successful, so increment the checkpoint count manually. The
// count was saved while the previous kernel was being saved and checkpoint
// success was unknown at that time. Now we know the checkpoint succeeded.
l.k.IncCheckpointCount()
log.Infof("Restore successful")
return nil
}
func (l *Loader) save(o *control.SaveOpts) (err error) {
defer func() {
// This closure is required to capture the final value of err.
l.k.OnCheckpointAttempt(err)
}()
l.k.ResetCheckpointStatus()
// TODO(gvisor.dev/issues/6243): save/restore not supported w/ hostinet
if l.root.conf.Network == config.NetworkHost {
return errors.New("checkpoint not supported when using hostinet")
}
if o.Metadata == nil {
o.Metadata = make(map[string]string)
}
o.Metadata["container_count"] = strconv.Itoa(l.containerCount())
if err := preSaveImpl(l.k, o); err != nil {
return err
}
state := control.State{
Kernel: l.k,
Watchdog: l.watchdog,
}
if err := state.Save(o, nil); err != nil {
return err
}
if o.Resume {
if err := postResumeImpl(l.k); err != nil {
return err
}
}
return nil
}
|