Skip to content

Commit

Permalink
Support configuring mount locations within guests
Browse files Browse the repository at this point in the history
Closes #659

Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
  • Loading branch information
chancez committed May 24, 2022
1 parent d6aa0d4 commit 310f954
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
3 changes: 3 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ disk: null
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable
mounts:
- location: "~"
# Configure the mountPoint inside the guest.
# 🟢 Builtin default: value of location
mountPoint: null
# CAUTION: `writable` SHOULD be false for the home directory.
# Setting `writable` to true is possible, but untested and dangerous.
# 🟢 Builtin default: false
Expand Down
6 changes: 5 additions & 1 deletion pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
if err != nil {
return err
}
target, err := localpathutil.Expand(f.MountPoint)
if err != nil {
return err
}
options := "defaults"
if fstype == "9p" {
options = "ro"
Expand All @@ -151,7 +155,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
// don't fail the boot, if virtfs is not available
options += ",nofail"
}
args.Mounts = append(args.Mounts, Mount{Tag: tag, Target: expanded, Type: fstype, Options: options})
args.Mounts = append(args.Mounts, Mount{Tag: tag, Target: target, Type: fstype, Options: options})
}

switch *y.MountType {
Expand Down
18 changes: 12 additions & 6 deletions pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e
if err != nil {
return nil, err
}

target, err := localpathutil.Expand(m.MountPoint)
if err != nil {
return nil, err
}
if err := os.MkdirAll(expanded, 0755); err != nil {
return nil, err
}
Expand All @@ -49,34 +54,35 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e
if *m.SSHFS.FollowSymlinks {
sshfsOptions = sshfsOptions + ",follow_symlinks"
}
logrus.Infof("Mounting %q", expanded)
logrus.Infof("Mounting host %q to guest %q", expanded, target)

rsf := &reversesshfs.ReverseSSHFS{
Driver: *m.SSHFS.SFTPDriver,
SSHConfig: a.sshConfig,
LocalPath: expanded,
Host: "127.0.0.1",
Port: a.sshLocalPort,
RemotePath: expanded,
RemotePath: target,
Readonly: !(*m.Writable),
SSHFSAdditionalArgs: []string{"-o", sshfsOptions},
}
if err := rsf.Prepare(); err != nil {
return nil, fmt.Errorf("failed to prepare reverse sshfs for %q: %w", expanded, err)
return nil, fmt.Errorf("failed to prepare reverse sshfs for %q->%q: %w", expanded, target, err)
}
if err := rsf.Start(); err != nil {
logrus.WithError(err).Warnf("failed to mount reverse sshfs for %q, retrying with `-o nonempty`", expanded)
logrus.WithError(err).Warnf("failed to mount reverse sshfs for %q->%q, retrying with `-o nonempty`", expanded, target)
// NOTE: nonempty is not supported for libfuse3: https://github.com/canonical/multipass/issues/1381
rsf.SSHFSAdditionalArgs = []string{"-o", "nonempty"}
if err := rsf.Start(); err != nil {
return nil, fmt.Errorf("failed to mount reverse sshfs for %q: %w", expanded, err)
return nil, fmt.Errorf("failed to mount reverse sshfs for %q->%q: %w", expanded, target, err)
}
}

res := &mount{
close: func() error {
logrus.Infof("Unmounting %q", expanded)
if closeErr := rsf.Close(); closeErr != nil {
return fmt.Errorf("failed to unmount reverse sshfs for %q: %w", expanded, err)
return fmt.Errorf("failed to unmount reverse sshfs for %q->%q: %w", expanded, target, err)
}
return nil
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
if mount.Writable != nil {
mounts[i].Writable = mount.Writable
}
if mount.MountPoint != "" {
mounts[i].MountPoint = mount.MountPoint
}
} else {
location[mount.Location] = len(mounts)
mounts = append(mounts, mount)
Expand Down Expand Up @@ -439,6 +442,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
mounts[i].NineP.Cache = pointer.String(Default9pCacheForRO)
}
}
if mount.MountPoint == "" {
mounts[i].MountPoint = mount.Location
}
}

if y.MountType == nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func TestFillDefault(t *testing.T) {
}

expect.Mounts = y.Mounts
expect.Mounts[0].MountPoint = expect.Mounts[0].Location
expect.Mounts[0].Writable = pointer.Bool(false)
expect.Mounts[0].SSHFS.Cache = pointer.Bool(true)
expect.Mounts[0].SSHFS.FollowSymlinks = pointer.Bool(false)
Expand Down Expand Up @@ -299,6 +300,7 @@ func TestFillDefault(t *testing.T) {
expect = d
// Also verify that archive arch is filled in
expect.Containerd.Archives[0].Arch = *d.Arch
expect.Mounts[0].MountPoint = expect.Mounts[0].Location
expect.Mounts[0].SSHFS.Cache = pointer.Bool(true)
expect.Mounts[0].SSHFS.FollowSymlinks = pointer.Bool(false)
expect.Mounts[0].SSHFS.SFTPDriver = pointer.String("")
Expand Down
9 changes: 5 additions & 4 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ type Image struct {
}

type Mount struct {
Location string `yaml:"location" json:"location"` // REQUIRED
Writable *bool `yaml:"writable,omitempty" json:"writable,omitempty"`
SSHFS SSHFS `yaml:"sshfs,omitempty" json:"sshfs,omitempty"`
NineP NineP `yaml:"9p,omitempty" json:"9p,omitempty"`
Location string `yaml:"location" json:"location"` // REQUIRED
MountPoint string `yaml:"mountPoint,omitempty" json:"mountPoint,omitempty"`
Writable *bool `yaml:"writable,omitempty" json:"writable,omitempty"`
SSHFS SSHFS `yaml:"sshfs,omitempty" json:"sshfs,omitempty"`
NineP NineP `yaml:"9p,omitempty" json:"9p,omitempty"`
}

type SFTPDriver = string
Expand Down

0 comments on commit 310f954

Please # to comment.