Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: copy files from host to guest
Browse files Browse the repository at this point in the history
Some hypervisors like firecracker don't support 9p, hence "static" files should
be copied from host to container rootfs (guest). File are copied using CopyFile
request.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Dec 17, 2018
1 parent 371243c commit bd2de8c
Showing 1 changed file with 78 additions and 14 deletions.
92 changes: 78 additions & 14 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -252,6 +253,11 @@ func (k *kataAgent) configure(h hypervisor, id, sharePath string, builtin bool,
k.proxyBuiltIn = true
}

caps := h.capabilities()
if !caps.is9pSupported() {
return nil
}

// Adding the shared volume.
// This volume contains all bind mounted container bundles.
sharedVolume := Volume{
Expand Down Expand Up @@ -612,22 +618,27 @@ func (k *kataAgent) startSandbox(sandbox *Sandbox) error {
return err
}

sharedDir9pOptions = append(sharedDir9pOptions, fmt.Sprintf("msize=%d", sandbox.config.HypervisorConfig.Msize9p))
storages := []*grpc.Storage{}
caps := sandbox.hypervisor.capabilities()

// We mount the shared directory in a predefined location
// in the guest.
// This is where at least some of the host config files
// (resolv.conf, etc...) and potentially all container
// rootfs will reside.
sharedVolume := &grpc.Storage{
Driver: kata9pDevType,
Source: mountGuest9pTag,
MountPoint: kataGuestSharedDir,
Fstype: type9pFs,
Options: sharedDir9pOptions,
}
if caps.is9pSupported() {
sharedDir9pOptions = append(sharedDir9pOptions, fmt.Sprintf("msize=%d", sandbox.config.HypervisorConfig.Msize9p))

storages := []*grpc.Storage{sharedVolume}
// We mount the shared directory in a predefined location
// in the guest.
// This is where at least some of the host config files
// (resolv.conf, etc...) and potentially all container
// rootfs will reside.
sharedVolume := &grpc.Storage{
Driver: kata9pDevType,
Source: mountGuest9pTag,
MountPoint: kataGuestSharedDir,
Fstype: type9pFs,
Options: sharedDir9pOptions,
}

storages = append(storages, sharedVolume)
}

if sandbox.shmSize > 0 {
path := filepath.Join(kataGuestSandboxDir, shmDir)
Expand Down Expand Up @@ -915,6 +926,48 @@ func (k *kataAgent) buildContainerRootfs(sandbox *Sandbox, c *Container, rootPat
return nil, nil
}

func (k *kataAgent) copyFiles(containerMounts, newMounts []Mount) error {
for _, nm := range newMounts {
for _, m := range containerMounts {
if nm.Destination != m.Destination {
continue
}

st, err := os.Stat(m.HostPath)
if err != nil {
k.Logger().WithError(err).WithField("path", m.HostPath).Error("Could get file information")
continue
}

b, err := ioutil.ReadFile(m.HostPath)
if err != nil {
k.Logger().WithError(err).WithField("path", m.HostPath).Error("Could not read file")
continue
}

k.Logger().WithFields(logrus.Fields{
"source": m.HostPath,
"dest": nm.Source,
}).Debugf("Copying file from host to guest")

cpReq := &grpc.CopyFileRequest{
Content: b,
Path: nm.Source,
Offset: 0,
DirMode: uint32(dirMode),
FileMode: uint32(st.Mode().Perm()),
FileSize: st.Size(),
}

if _, err = k.sendReq(cpReq); err != nil {
k.Logger().WithError(err).Error("Could not send CopyFile request")
}
}
}

return nil
}

func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process, err error) {
span, _ := k.trace("createContainer")
defer span.Finish()
Expand Down Expand Up @@ -1003,6 +1056,14 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,

k.handleShm(grpcSpec, sandbox)

caps := sandbox.hypervisor.capabilities()
if !caps.is9pSupported() {
// 9p is not supported, files must be copied
if err := k.copyFiles(c.mounts, newMounts); err != nil {
return nil, err
}
}

req := &grpc.CreateContainerRequest{
ContainerId: c.id,
ExecId: c.id,
Expand Down Expand Up @@ -1490,6 +1551,9 @@ func (k *kataAgent) installReqFunc(c *kataclient.AgentClient) {
k.reqHandlers["grpc.GuestDetailsRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) {
return k.client.GetGuestDetails(ctx, req.(*grpc.GuestDetailsRequest), opts...)
}
k.reqHandlers["grpc.CopyFileRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) {
return k.client.CopyFile(ctx, req.(*grpc.CopyFileRequest), opts...)
}
k.reqHandlers["grpc.SetGuestDateTimeRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) {
return k.client.SetGuestDateTime(ctx, req.(*grpc.SetGuestDateTimeRequest), opts...)
}
Expand Down

0 comments on commit bd2de8c

Please sign in to comment.