github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/stage1/gc/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 main 18 19 import ( 20 "errors" 21 "flag" 22 "os" 23 "path/filepath" 24 "runtime" 25 26 "github.com/appc/spec/schema/types" 27 "github.com/hashicorp/errwrap" 28 29 "github.com/coreos/rkt/common" 30 "github.com/coreos/rkt/networking" 31 rktlog "github.com/coreos/rkt/pkg/log" 32 ) 33 34 var ( 35 debug bool 36 ) 37 38 func init() { 39 flag.BoolVar(&debug, "debug", false, "Run in debug mode") 40 41 // this ensures that main runs only on main thread (thread group leader). 42 // since namespace ops (unshare, setns) are done for a single thread, we 43 // must ensure that the goroutine does not jump from OS thread to thread 44 runtime.LockOSThread() 45 } 46 47 func main() { 48 flag.Parse() 49 50 log := rktlog.New(os.Stderr, "stage1 gc", debug) 51 52 podID, err := types.NewUUID(flag.Arg(0)) 53 if err != nil { 54 log.Fatal("UUID is missing or malformed") 55 } 56 57 if err := gcNetworking(podID); err != nil { 58 log.FatalE("", err) 59 } 60 } 61 62 func gcNetworking(podID *types.UUID) error { 63 var flavor string 64 // we first try to read the flavor from stage1 for backwards compatibility 65 flavor, err := os.Readlink(filepath.Join(common.Stage1RootfsPath("."), "flavor")) 66 if err != nil { 67 // if we couldn't read the flavor from stage1 it could mean the overlay 68 // filesystem is already unmounted (e.g. the system has been rebooted). 69 // In that case we try to read it from the pod's root directory 70 flavor, err = os.Readlink("flavor") 71 if err != nil { 72 return errwrap.Wrap(errors.New("failed to get stage1 flavor"), err) 73 } 74 } 75 76 n, err := networking.Load(".", podID) 77 switch { 78 case err == nil: 79 n.Teardown(flavor, debug) 80 case os.IsNotExist(err): 81 // probably ran with --net=host 82 default: 83 return errwrap.Wrap(errors.New("failed loading networking state"), err) 84 } 85 86 return nil 87 }