Skip to content

Commit

Permalink
Only do virtfs mounts at boot time with fstab
Browse files Browse the repository at this point in the history
External filesystems can be mounted in /etc/fstab.

It is only the reverse-sshfs that needs to use ssh.

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Apr 6, 2022
1 parent f63e8bf commit 9d1d21b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 157 deletions.
26 changes: 14 additions & 12 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,21 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
if err := a.waitForRequirements(ctx, "essential", a.essentialRequirements()); err != nil {
mErr = multierror.Append(mErr, err)
}
mounts, err := a.setupMounts(ctx)
if err != nil {
mErr = multierror.Append(mErr, err)
}
a.onClose = append(a.onClose, func() error {
var unmountMErr error
for _, m := range mounts {
if unmountErr := m.close(); unmountErr != nil {
unmountMErr = multierror.Append(unmountMErr, unmountErr)
}
if *a.y.MountType == limayaml.REVSSHFS {
mounts, err := a.setupMounts(ctx)
if err != nil {
mErr = multierror.Append(mErr, err)
}
return unmountMErr
})
a.onClose = append(a.onClose, func() error {
var unmountMErr error
for _, m := range mounts {
if unmountErr := m.close(); unmountErr != nil {
unmountMErr = multierror.Append(unmountMErr, unmountErr)
}
}
return unmountMErr
})
}
go a.watchGuestAgentEvents(ctx)
if err := a.waitForRequirements(ctx, "optional", a.optionalRequirements()); err != nil {
mErr = multierror.Append(mErr, err)
Expand Down
66 changes: 3 additions & 63 deletions pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"fmt"
"os"

"github.com/docker/go-units"
"github.com/hashicorp/go-multierror"

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/remotemount"
"github.com/lima-vm/sshocker/pkg/reversesshfs"
"github.com/sirupsen/logrus"
)
Expand All @@ -24,8 +22,8 @@ func (a *HostAgent) setupMounts(ctx context.Context) ([]*mount, error) {
res []*mount
mErr error
)
for i, f := range a.y.Mounts {
m, err := a.setupMount(ctx, i, f)
for _, f := range a.y.Mounts {
m, err := a.setupMount(ctx, f)
if err != nil {
mErr = multierror.Append(mErr, err)
continue
Expand All @@ -35,17 +33,7 @@ func (a *HostAgent) setupMounts(ctx context.Context) ([]*mount, error) {
return res, mErr
}

func (a *HostAgent) setupMount(ctx context.Context, i int, m limayaml.Mount) (*mount, error) {
switch *a.y.MountType {
case limayaml.REVSSHFS:
return a.setupMountReverseSSHFS(ctx, m)
case limayaml.NINEP:
return a.setupMount9P(ctx, i, m)
}
return nil, fmt.Errorf("unknown mount type: %v", *a.y.MountType)
}

func (a *HostAgent) setupMountReverseSSHFS(ctx context.Context, m limayaml.Mount) (*mount, error) {
func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, error) {
expanded, err := localpathutil.Expand(m.Location)
if err != nil {
return nil, err
Expand Down Expand Up @@ -94,51 +82,3 @@ func (a *HostAgent) setupMountReverseSSHFS(ctx context.Context, m limayaml.Mount
}
return res, nil
}

func (a *HostAgent) setupMount9P(ctx context.Context, i int, m limayaml.Mount) (*mount, error) {
expanded, err := localpathutil.Expand(m.Location)
if err != nil {
return nil, err
}
if err := os.MkdirAll(expanded, 0755); err != nil {
return nil, err
}
mountOptions := "trans=virtio"
mountOptions += fmt.Sprintf(",version=%s", *m.NineP.ProtocolVersion)
msize, err := units.RAMInBytes(*m.NineP.Msize)
if err != nil {
return nil, fmt.Errorf("failed to parse msize for %q: %w", expanded, err)
}
mountOptions += fmt.Sprintf(",msize=%d", msize)
mountOptions += fmt.Sprintf(",cache=%s", *m.NineP.Cache)

logrus.Infof("Mounting %q", expanded)
rsf := &remotemount.RemoteMount{
SSHConfig: a.sshConfig,
LocalPath: expanded,
Host: "127.0.0.1",
Port: a.sshLocalPort,
RemotePath: expanded,
Readonly: !(*m.Writable),
MountType: "9p",
MountTag: fmt.Sprintf("mount%d", i),
MountAdditionalArgs: []string{"-o", mountOptions},
}
if err := rsf.Prepare(); err != nil {
return nil, fmt.Errorf("failed to prepare 9p for %q: %w", expanded, err)
}
if err := rsf.Start(); err != nil {
return nil, fmt.Errorf("failed to mount 9p for %q: %w", expanded, err)
}

res := &mount{
close: func() error {
logrus.Infof("Unmounting %q", expanded)
if closeErr := rsf.Close(); closeErr != nil {
return fmt.Errorf("failed to unmount 9p for %q: %w", expanded, err)
}
return nil
},
}
return res, nil
}
82 changes: 0 additions & 82 deletions pkg/remotemount/remotemount.go

This file was deleted.

0 comments on commit 9d1d21b

Please sign in to comment.