Skip to content

Commit

Permalink
machine: Add file sharing support
Browse files Browse the repository at this point in the history
This commit adds file sharing support to `crc start`.
This uses the GetSharedDirs API from libmachine, and only supports
virtiofs for now.
File sharing is meant to be used together for podman support to support
`podman-remote -v somedir:/some-dir-in-container registry.example.com/container-image`
This is currently implemented similarly to what podman does: $HOME is
shared with the VM and mounted at the $HOME mountpoint. This way one can
use podman-remote $HOME/hostdir on the host, and the podman instance
running in the VM can also access $HOME/hostdir which is mounted in the
VM, and share it with the container.

We currently need to hardcode a selinux context on $HOME mounted in the
VM, mainly to overcome limitations in the macOS virtiofs implementation
which vfkit uses.

This fixes #3204
  • Loading branch information
cfergeau committed Jul 26, 2022
1 parent 7a7d75d commit 3a4e615
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/crc/machine/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MachineConfig struct {
ImageFormat string
SSHKeyPath string
KubeConfig string
SharedDirs []string

// macOS specific configuration
KernelCmdLine string
Expand Down
11 changes: 11 additions & 0 deletions pkg/crc/machine/config/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"fmt"

"github.com/code-ready/machine/libmachine/drivers"
)

Expand All @@ -15,4 +17,13 @@ func InitVMDriverFromMachineConfig(machineConfig MachineConfig, driver *drivers.
driver.BundleName = machineConfig.BundleName
driver.ImageSourcePath = machineConfig.ImageSourcePath
driver.ImageFormat = machineConfig.ImageFormat

for i, dir := range machineConfig.SharedDirs {
sharedDir := drivers.SharedDir{
Source: dir,
Target: dir,
Tag: fmt.Sprintf("dir%d", i),
}
driver.SharedDirs = append(driver.SharedDirs, sharedDir)
}
}
44 changes: 44 additions & 0 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ func growRootFileSystem(sshRunner *crcssh.Runner) error {

return nil
}

func configureSharedDirs(vm *virtualMachine, sshRunner *crcssh.Runner) error {
logging.Debugf("Configuring shared directories")
sharedDirs, err := vm.Driver.GetSharedDirs()
if err != nil {
return err
}
for _, mount := range sharedDirs {
// CoreOS makes / immutable, we need to handle this if we need to create a directory outside of /home and /mnt
isHomeOrMnt := strings.HasPrefix(mount.Target, "/home") || strings.HasPrefix(mount.Target, "/mnt")
if !isHomeOrMnt {
if _, _, err := sshRunner.RunPrivileged("Making / mutable", "chattr", "-i", "/"); err != nil {
return err
}
}
if _, _, err := sshRunner.RunPrivileged(fmt.Sprintf("Creating %s", mount.Target), "mkdir", "-p", mount.Target); err != nil {
return err
}
if !isHomeOrMnt {
if _, _, err := sshRunner.RunPrivileged("Making / immutable again", "chattr", "+i", "/"); err != nil {
return err
}
}
logging.Debugf("Mounting tag %s at %s", mount.Tag, mount.Target)
//FIXME: do not hardcode this
mount.Type = "virtiofs"
if _, _, err := sshRunner.RunPrivileged(fmt.Sprintf("Mounting %s", mount.Target), "mount", "-o", "context=\"system_u:object_r:container_file_t:s0\"", "-t", mount.Type, mount.Tag, mount.Target); err != nil {
return err
}
}

return nil
}

func (client *client) Start(ctx context.Context, startConfig types.StartConfig) (*types.StartResult, error) {
telemetry.SetCPUs(ctx, startConfig.CPUs)
telemetry.SetMemory(ctx, uint64(startConfig.Memory)*1024*1024)
Expand Down Expand Up @@ -174,6 +208,11 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
logging.Infof("Creating CRC VM for Podman %s...", crcBundleMetadata.GetPodmanVersion())
}

sharedDirs := []string{}
if homeDir, err := os.UserHomeDir(); err == nil {
sharedDirs = append(sharedDirs, homeDir)
}

machineConfig := config.MachineConfig{
Name: client.name,
BundleName: bundleName,
Expand All @@ -187,6 +226,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
KernelCmdLine: crcBundleMetadata.GetKernelCommandLine(),
Initramfs: crcBundleMetadata.GetInitramfsPath(),
Kernel: crcBundleMetadata.GetKernelPath(),
SharedDirs: sharedDirs,
}
if crcBundleMetadata.IsOpenShift() {
machineConfig.KubeConfig = crcBundleMetadata.GetKubeConfigPath()
Expand Down Expand Up @@ -316,6 +356,10 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
}
}

if err := configureSharedDirs(vm, sshRunner); err != nil {
return nil, err
}

if _, _, err := sshRunner.RunPrivileged("make root Podman socket accessible", "chmod 777 /run/podman/ /run/podman/podman.sock"); err != nil {
return nil, errors.Wrap(err, "Failed to change permissions to root podman socket")
}
Expand Down

0 comments on commit 3a4e615

Please sign in to comment.