github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/rump/rump-qemu.go (about)

     1  package rump
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/solo-io/unik/pkg/compilers"
    12  	unikos "github.com/solo-io/unik/pkg/os"
    13  	"github.com/solo-io/unik/pkg/types"
    14  )
    15  
    16  func CreateImageQemu(kernel string, args string, mntPoints, bakedEnv []string, noCleanup bool) (*types.RawImage, error) {
    17  	// create rump config
    18  	var c rumpConfig
    19  	if bakedEnv != nil {
    20  		c.Env = make(map[string]string)
    21  		for i, pair := range bakedEnv {
    22  			c.Env[fmt.Sprintf("env%d", i)] = pair
    23  		}
    24  	}
    25  
    26  	argv := []string{}
    27  	if args != "" {
    28  		argv = strings.Split(args, " ")
    29  	}
    30  	c = setRumpCmdLine(c, "program.bin", argv, false)
    31  
    32  	bootBlk := blk{
    33  		Source:     "dev",
    34  		Path:       "/dev/ld0e",
    35  		FSType:     "blk",
    36  		MountPoint: "/bootpart",
    37  	}
    38  	c.Blk = append(c.Blk, bootBlk)
    39  
    40  	res := &types.RawImage{}
    41  	res.RunSpec.Compiler = compilers.Rump
    42  
    43  	for i, mntPoint := range mntPoints {
    44  		deviceMapped := fmt.Sprintf("ld%ca", '1'+i)
    45  		blk := blk{
    46  			Source:     "dev",
    47  			Path:       "/dev/" + deviceMapped,
    48  			FSType:     "blk",
    49  			MountPoint: mntPoint,
    50  		}
    51  
    52  		c.Blk = append(c.Blk, blk)
    53  		logrus.Debugf("adding mount point to image: %s:%s", mntPoint, deviceMapped)
    54  		res.RunSpec.DeviceMappings = append(res.RunSpec.DeviceMappings,
    55  			types.DeviceMapping{MountPoint: mntPoint, DeviceName: deviceMapped})
    56  	}
    57  
    58  	// virtualbox network
    59  	c.Net = &net{
    60  		If:     "vioif0",
    61  		Type:   "inet",
    62  		Method: DHCP,
    63  	}
    64  
    65  	cmdline, err := toRumpJson(c)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	logrus.Debugf("writing rump json config: %s", cmdline)
    71  
    72  	imgFile, err := compilers.BuildBootableImage(kernel, cmdline, true, noCleanup)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	//copy kernel for qemu
    78  	if err := unikos.CopyFile(kernel, filepath.Join(filepath.Dir(imgFile), "program.bin")); err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	if err := ioutil.WriteFile(filepath.Join(filepath.Dir(imgFile), "cmdline"), []byte(cmdline), 0644); err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	res.LocalImagePath = imgFile
    87  	res.StageSpec.ImageFormat = types.ImageFormat_RAW
    88  	res.RunSpec.DefaultInstanceMemory = 512
    89  	return res, nil
    90  
    91  }