@@ -23,7 +23,6 @@ import (
2323 "os/exec"
2424 "path/filepath"
2525 "strings"
26- "sync"
2726 "syscall"
2827
2928 "github.com/firecracker-microvm/firecracker-go-sdk"
@@ -51,13 +50,12 @@ type runcJailer struct {
5150 runcBinaryPath string
5251 uid uint32
5352 gid uint32
54- once sync.Once
53+
54+ configSpec specs.Spec
5555}
5656
5757const firecrackerFileName = "firecracker"
5858
59- var configSpec * specs.Spec
60-
6159func newRuncJailer (ctx context.Context , logger * logrus.Entry , ociBundlePath , runcBinPath string , uid , gid uint32 ) (* runcJailer , error ) {
6260 l := logger .WithField ("ociBundlePath" , ociBundlePath ).
6361 WithField ("runcBinaryPath" , runcBinPath )
@@ -71,6 +69,19 @@ func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, run
7169 gid : gid ,
7270 }
7371
72+ spec := specs.Spec {}
73+ var configBytes []byte
74+ configBytes , err := ioutil .ReadFile (runcConfigPath )
75+ if err != nil {
76+ return nil , errors .Wrapf (err , "failed to read firecracker-runc-config.json" )
77+ }
78+
79+ if err = json .Unmarshal (configBytes , & spec ); err != nil {
80+ return nil , errors .Wrapf (err , "failed to unmarshal firecracker-runc-config.json" )
81+ }
82+
83+ j .configSpec = spec
84+
7485 rootPath := j .RootPath ()
7586
7687 const mode = os .FileMode (0700 )
@@ -109,7 +120,7 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
109120 client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
110121
111122 if machineConfig .NetNS == "" {
112- if netns := getNetNS (configSpec ); netns != "" {
123+ if netns := getNetNS (j . configSpec ); netns != "" {
113124 machineConfig .NetNS = netns
114125 }
115126 }
@@ -371,46 +382,21 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
371382// overwriteConfig will set the proper default values if a field had not been set.
372383func (j * runcJailer ) overwriteConfig (cfg * Config , machineConfig * firecracker.Config , socketPath , configPath string ) error {
373384 var err error
374- j .once .Do (func () {
375- // here we attempt to cache the runc config. If the config has already been
376- // cached, we will return immediately
377- if configSpec != nil {
378- return
379- }
380-
381- spec := specs.Spec {}
382- var configBytes []byte
383- configBytes , err = ioutil .ReadFile (configPath )
384- if err != nil {
385- return
386- }
387-
388- if err = json .Unmarshal (configBytes , & spec ); err != nil {
389- return
390- }
391-
392- configSpec = & spec
393-
394- if spec .Process .User .UID != 0 ||
395- spec .Process .User .GID != 0 {
396- err = fmt .Errorf (
397- "using UID %d and GID %d, these values must not be set" ,
398- spec .Process .User .UID ,
399- spec .Process .User .GID ,
400- )
401- return
402- }
403-
404- spec = j .setDefaultConfigValues (cfg , socketPath , spec )
405- spec .Root .Path = rootfsFolder
406- spec .Root .Readonly = false
407- })
408-
409- if err != nil {
410- return err
385+ // here we attempt to cache the runc config. If the config has already been
386+ // cached, we will return immediately
387+ spec := j .configSpec
388+ if spec .Process .User .UID != 0 ||
389+ spec .Process .User .GID != 0 {
390+ return fmt .Errorf (
391+ "using UID %d and GID %d, these values must not be set" ,
392+ spec .Process .User .UID ,
393+ spec .Process .User .GID ,
394+ )
411395 }
412396
413- spec := * configSpec
397+ spec = j .setDefaultConfigValues (cfg , socketPath , spec )
398+ spec .Root .Path = rootfsFolder
399+ spec .Root .Readonly = false
414400 spec .Process .User .UID = j .uid
415401 spec .Process .User .GID = j .gid
416402
@@ -491,11 +477,7 @@ func mkdirAllWithPermissions(path string, mode os.FileMode, uid, gid uint32) err
491477 return nil
492478}
493479
494- func getNetNS (spec * specs.Spec ) string {
495- if spec == nil {
496- return ""
497- }
498-
480+ func getNetNS (spec specs.Spec ) string {
499481 for _ , ns := range spec .Linux .Namespaces {
500482 if ns .Type == networkNamespaceRuncName {
501483 return ns .Path
0 commit comments