github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/stage0/gc.go (about)

     1  // Copyright 2015 The rkt Authors
     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  //+build linux
    16  
    17  package stage0
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  
    26  	"github.com/appc/spec/schema/types"
    27  	"github.com/hashicorp/errwrap"
    28  	"github.com/rkt/rkt/common"
    29  )
    30  
    31  // GC enters the pod by fork/exec()ing the stage1's /gc similar to /init.
    32  // /gc can expect to have its CWD set to the pod root.
    33  func GC(pdir string, uuid *types.UUID, localConfig string) error {
    34  	err := unregisterPod(pdir, uuid)
    35  	if err != nil {
    36  		// Probably not worth abandoning the rest
    37  		log.PrintE("warning: could not unregister pod with metadata service", err)
    38  	}
    39  
    40  	stage1Path := common.Stage1RootfsPath(pdir)
    41  	s1v, err := getStage1InterfaceVersion(pdir)
    42  	if err != nil {
    43  		return errwrap.Wrap(errors.New("Could not determine stage1 version"), err)
    44  	}
    45  
    46  	ep, err := getStage1Entrypoint(pdir, gcEntrypoint)
    47  	if err != nil {
    48  		return errwrap.Wrap(errors.New("error determining 'gc' entrypoint"), err)
    49  	}
    50  
    51  	args := []string{filepath.Join(stage1Path, ep)}
    52  	if debugEnabled {
    53  		args = append(args, "--debug")
    54  	}
    55  	if interfaceVersionSupportsGCLocalConfig(s1v) && localConfig != "" {
    56  		args = append(args, "--local-config", localConfig)
    57  	}
    58  	args = append(args, uuid.String())
    59  
    60  	debug("Execing %v", args)
    61  	c := exec.Cmd{
    62  		Path:   args[0],
    63  		Args:   args,
    64  		Stderr: os.Stderr,
    65  		Dir:    pdir,
    66  	}
    67  	return c.Run()
    68  }
    69  
    70  // MountGC removes mounts from pods that couldn't be GCed cleanly.
    71  func MountGC(path, uuid string) error {
    72  	err := common.ChrootPrivateUnmount(path, log, debug)
    73  	if err != nil {
    74  		return errwrap.Wrap(fmt.Errorf("error cleaning mounts for pod %s", uuid), err)
    75  	}
    76  	return nil
    77  }